Reputation: 12134
In python (+ pandas/numpy/scipy/statsmodels) Is there a function to return the autocorrelations wrt lag? Does anything like this exist ready made as a library function?
To avoid confusion, I want the following, just I don't want to plot it but I want it returned as a series (pd.Series or pd.DataFrame):
import numpy as np
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf
from matplotlib import pyplot as plt
plt.ion()
s = pd.Series(np.sin(range(1,100))) + pd.Series(np.random.randn(99))
plot_acf(s)
Effectively, I want what pd.Series.autocorr()
returns but I want a series rather than a scalar returned where the series contains the autocorrelation for various lags.
Edit:
One way to achieve the above is to do:
pd.Series([s.autocorr(i) for i in range(0,s.shape[0]-1)], index=range(0,s.shape[0]-1))
Upvotes: 5
Views: 8728
Reputation: 3195
How about the Statsmodels acf
function?
import statsmodels.api as sm
np.random.seed(1234)
s = pd.Series(np.sin(range(1,100))) + pd.Series(np.random.randn(99))
pd.Series(sm.tsa.acf(s, nlags=5))
yields
0 1.000000
1 0.033136
2 -0.124275
3 -0.396403
4 -0.248519
5 0.078170
dtype: float64
Upvotes: 5
Reputation: 323226
I can only think of speed up your own method vectorize
np.vectorize(s.autocorr)(np.arange(0,len(s)-1))
Upvotes: 0