3

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.

1 Answer 1

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,  
                True, True, False, True, False, True, False,
                True, True, True, False, False], dtype=bool)

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.

Sign up to request clarification or add additional context in comments.

3 Comments

it seems to be incorrect. It overestimates sums that follow s < rate sums.
@J.F.Sebastian, you are right, I missed that part. Even my latest edit is not right.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.