Created
September 28, 2016 04:15
-
-
Save anyuzx/0888680ea61090102d706f35bd9b97ea to your computer and use it in GitHub Desktop.
autocorrelation in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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