5

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))
s5s
  • 11,159
  • 21
  • 74
  • 121
  • https://machinelearningmastery.com/gentle-introduction-autocorrelation-partial-autocorrelation/ – BENY May 07 '19 at 18:24
  • @Wen-Ben It only shows how to plot acf/pacf. Can't see any code to return the acf/pacf series. – s5s May 07 '19 at 18:27
  • https://stackoverflow.com/questions/14297012/estimate-autocorrelation-using-python sry just copy the wrong page – BENY May 07 '19 at 18:31
  • @Wen-Ben I mean I can just use a list comprehension with pd.Series.autocorr() tbh, just thought there is something more sophisticated. OK, I'll write my own functions. – s5s May 07 '19 at 18:33
  • I have provided one method hope it is faster than your old lines – BENY May 07 '19 at 18:51

2 Answers2

5

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
cfulton
  • 2,855
  • 2
  • 14
  • 13
0

I can only think of speed up your own method vectorize

np.vectorize(s.autocorr)(np.arange(0,len(s)-1))
BENY
  • 317,841
  • 20
  • 164
  • 234