Search code examples
pythonmeanmoving-average

Running Mean of Circular Data: Average and Standard Deviation?


Given such a list:

mylist = [1, 2, 3, 4, 5, 6, 7]

with N = 3 as the size of running mean at each step.

What is the fastest way to calculate the average and standard deviation of this list?

If it was only average np.convolve could do the job, but what about the standard deviation? or standard error?


Solution

  • Try:

    import numpy as np
    N=3
    mylist = [1, 2, 3, 4, 5, 6, 7]
    
    res=np.vstack([mylist[i:]+mylist[:i] for i in range(N)])
    
    ma=res.mean(axis=0)
    std=res.std(axis=0)
    

    Just for moving average you can do: https://stackoverflow.com/a/14314054/11610186