I'm accustomed to writing vectorized statements and list comprehensions in Python, but I've got a problem that appears with both a "running" computation that depends on the previous value in the loop, as well as an if statement. Schematically it looks like this:
def my_loop(x, a=0.5, b=0.9):
out = np.copy(x)
prev_val = 0
for i in np.arange(x.shape[0]):
if x[i] < prev_val:
new_val = (1-a)*x[i] + a*prev_val
else:
new_val = (1-b)*x[i] + b*prev_val
out[i] = new_val
prev_val = new_val
return out
I haven't been able to figure out how one could vectorize this (e.g. via using some kind of accumulator), so I'll ask: Is there a way to make this more Pythonic/faster?
I've seen previous posts about vectorizing when there's an if statement -- usually solved via np.where() -- but not one where there's a "running" value that depends on its previous state...so I haven't found any duplicate questions yet (and this one isn't about vectorization in the usual sense, this one is about 'previous value' but referring to list indices).
So far, I have tried np.vectorize
and numba's @jit
, and they do run somewhat faster, but neither gives me the speed I'm hoping for. Is there something I'm missing? (Maybe something with map()
?) Thanks.
(Yes, in the case of a=b this becomes easy!)