Take the 2-minute tour ×

Or are truly iterative algorithms like this not vectorizable?

s += usage can be vectorized with cumsum, but the floor on the sum is problematic.

Is there some fancy way to use lags or shifting?

s = 0
for (time, usage) in timeseries:
    s += usage
    s = max(s-rate, 0)
    new_timeseries[time] = s

I pryed away at it for a while but couldn't come up with anything.

share|improve this question

1 Answer 1

Put timeseries into an array first. Let's assume the values of timeseries are my_array. Then,

import numpy as np
s = np.cumsum(my_array) - rate
s[s < 0] = 0
new_timeseries = s 

UPDATE: this is not right. It doesn't account for zeroing the cumsum when s the increment is below the rate. You can find the points where the cumsum is below rate with the derivative:

In [1]: dd = np.diff(np.cumsum(my_array))
In [2]: dd < rate
Out[3]: array([ True, False, True, False, False, True, True,  

However, this doesn't 'reset' the cumsum. One could hunt along those indices and do a cumsum in blocks of 'Trues', but I'm not sure if it would be more efficient than your loop.

share|improve this answer
it seems to be incorrect. It overestimates sums that follow s < rate sums. –  J.F. Sebastian Dec 13 '12 at 4:33
@J.F.Sebastian, you are right, I missed that part. Even my latest edit is not right. –  tiago Dec 13 '12 at 4:50
usage is always > 0, i should have mentioned. np.diff(np.cumsum(a)) = a no? this doesn't quite seem complete (which you acknowledge in your answer). i hadn't seen np.diff before though thanks. –  corsair Dec 13 '12 at 21:14

Your Answer


