For a given time series of floats, it is pretty easy to calculate an unconstrained maximum drawdown in O(n)
time, where max drawdown is defined as
\min_{i,j, i<j}(x_j - x_i)
In python, we the calculation is min(x - numpy.expanding_max(x))
, but to get a O(n)
algo write it explicitly:
def max_drawdown(s):
drawdn = 0
m,M = s[0],s[0]
t1,t2 = 0,0
T1,T2 = 0,0
for i,v in enumerate(s):
if (v < m):
if (v-m) < drawdn:
drawdn = v-m
t2 = i
T1,T2 = t1,t2
else:
m = v
t1 = i
return T1,T2,drawdn
Is there an O(n)
algorithm for a max_drawdown that is constrained to have window duration > min_length? In this case I want \min_{i,j, (j-i) > min_length}(x_j - x_i).
Note that this is not a "rolling drawdown" calculation as in Compute *rolling* maximum drawdown of pandas Series.