Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocorrelation to estimate periodicity with numpy

Tags:

I have a large set of time series (> 500), I'd like to select only the ones that are periodic. I did a bit of literature research and I found out that I should look for autocorrelation. Using numpy I calculate the autocorrelation as:

def autocorr(x):
    norm = x - np.mean(x)
    result = np.correlate(norm, norm, mode='full')
    acorr = result[result.size/2:]
    acorr /= ( x.var() * np.arange(x.size, 0, -1) )
    return acorr

This returns a set of coefficients (r?) that when plot should tell me if the time series is periodic or not.

I generated two toy examples:

#random signal
s1 = np.random.randint(5, size=80)
#periodic signal
s2 = np.array([5,2,3,1] * 20)

s1 s2

When I generate the autocorrelation plots I obtain:

Autocorr1 Autocorr2

The second autocorrelation vector clearly indicates some periodicity:

Autocorr1 =  [1, 0.28, -0.06,  0.19, -0.22, -0.13,  0.07 ..]
Autocorr2 =  [1, -0.50, -0.49,  1, -0.50, -0.49,  1 ..]

My question is, how can I automatically determine, from the autocorrelation vector, if a time series is periodic? Is there a way to summarise the values into a single coefficient, e.g. if = 1 perfect periodicity, if = 0 no periodicity at all. I tried to calculate the mean but it is not meaningful. Should I look at the number of 1?