Skip to content Skip to sidebar Skip to footer

Calculating The Drawdown Within A Numpy Array Python

I am trying to write a function that calculates how much the biggest dip was in each array. the function below calculates between the max and the min but it does not get Expected O

Solution 1:

The biggest dip does not necessarily happen at the global maximum or global minimum. We need an exhaustive approach to find the largest dip:

  1. check the maximum value so far, for which we can use numpy.maximum.accumulate;
  2. calculate the biggest dip for each position.
  3. And take the largest dip among all the dips.

def calc(a):
    acc_max = np.maximum.accumulate(a)
    return (a - acc_max).min()

calc(A)
# -56
calc(B)
# -120
calc(C)
# -62
calc(D)
# 0
calc(E)
# -5

Post a Comment for "Calculating The Drawdown Within A Numpy Array Python"