Skip to content

Instantly share code, notes, and snippets.

@anyuzx
Created September 28, 2016 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anyuzx/0888680ea61090102d706f35bd9b97ea to your computer and use it in GitHub Desktop.
Save anyuzx/0888680ea61090102d706f35bd9b97ea to your computer and use it in GitHub Desktop.
autocorrelation in python
# Since there is no real autocorrelation compute function in python and numpy
# I write my own one
import numpy as np
# Suppose data array represent a time series data
# each element represent data at given time
# Assume time are equally spaced
def acf(data):
mean = np.mean(data)
var = data.var()
length = len(data)
acf_array = []
for t in np.arange(0,length):
temp = np.mean((data[:(length-t)] - mean)*(data[t:] - mean))/var
acf_array.append(temp)
acf_array = np.array(acf_array)
return acf_array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment