I wrote a simple function that calculates and returns the maximum drawdown of a set of returns. I am trying to squeeze as much efficiency out of the code as possible for speed. I've got it down to about as fast as I can go. Does anyone have suggestions on how to write this function more efficiently perhaps through list comprehensions etc.?
import numpy as np
def max_drawdown(returns):
draw_series = np.array(np.ones(np.size(returns)))
max_return = 0; max_draw = 1; draw = 1
returns = returns + 1
for r in range(returns.count()-1, 0, -1):
if returns[r] > max_return:
max_return = returns[r]
else:
draw = returns[r] / max_return
if draw < max_draw:
max_draw = draw
draw_series[r-1] = -(1 - max_draw)
return draw_series
returns
?returns = returns + 1
suggests it might be an integer, or a numpy array, but neither of those has acount
method. – unutbu Jul 15 '11 at 21:54numpy.ones
returns an array, so passing it tonumpy.array
is redundant. – senderle Jul 15 '11 at 22:11