[SciPy-user] moving average, moving standard deviation, etc...
Matt Knox
mattknox_ca at hotmail.com
Sun Dec 24 10:23:08 CST 2006
>
> Hi,
>
> The technique I've seen involves keeping some intermediate variables,
> and subtracting off the oldest value while adding on the newest. At
> each timestep then, you just update the variables and recalculate the
> result.
>
> xsum += (x[i] - x[i-window_size])
> x2sum += (x[i]*x[i] - x[i-window_size]*x[i-window_size])
> average = xsum/window_size
> stdev_squared = (x2sum - xsum*xsum)/window_size
> stdev = sqrt(stdev_squared)
>
> If you want the "sample" stdev, you can compute it directly, or use:
>
> stdev_sample_squared = window_size*stdev_squared/(window_size-1)
> stdev_sample = sqrt(stdev_squared)
>
> Don't get me started on this though... IMHO it's usually a waste of time.
>
> -Frank
>
Ok, thanks. So it sounds like there is no easy way to do this generally without
doing some looping in python (for the standard deviation anyway), or writing
some C code. I'm more interested in the general strategy for doing these kinds
of calculations than these specific examples themselves necessarily. Although
they are useful for making pretty charts sometimes.
- Matt
More information about the SciPy-user
mailing list