大熊猫ACF和statsmodel ACF有什么区别?

BML*_*L91 24 python pandas statsmodels

我正在计算股票收益的自相关函数.为此,我测试了两个函数,autocorrPandas内置的函数,以及由... acf提供的函数statsmodels.tsa.这在以下MWE中完成:

import pandas as pd
from pandas_datareader import data
import matplotlib.pyplot as plt
import datetime
from dateutil.relativedelta import relativedelta
from statsmodels.tsa.stattools import acf, pacf

ticker = 'AAPL'
time_ago = datetime.datetime.today().date() - relativedelta(months = 6)

ticker_data = data.get_data_yahoo(ticker, time_ago)['Adj Close'].pct_change().dropna()
ticker_data_len = len(ticker_data)

ticker_data_acf_1 =  acf(ticker_data)[1:32]
ticker_data_acf_2 = [ticker_data.autocorr(i) for i in range(1,32)]

test_df = pd.DataFrame([ticker_data_acf_1, ticker_data_acf_2]).T
test_df.columns = ['Pandas Autocorr', 'Statsmodels Autocorr']
test_df.index += 1
test_df.plot(kind='bar')
Run Code Online (Sandbox Code Playgroud)

我注意到他们预测的价值不相同:

在此输入图像描述

是什么导致了这种差异,应该使用哪些值?

nik*_*ase 24

Pandas和Statsmodels版本之间的区别在于平均减法和归一化/方差除法:

  • autocorr只是将原始系列的子系列传递给np.corrcoef.在该方法中,这些子系列的样本均值和样本方差用于确定相关系数
  • acf相反,使用整个系列样本均值和样本方差来确定相关系数.

对于较长的时间序列,差异可能会变小,但对于较短的时间序列则差异很大.

与Matlab相比,Pandas autocorr函数可能对应于xcorr使用(滞后)序列本身进行Matlabs (交叉corr),而不是Matlab autocorr,它计算样本自相关(从文档中猜测;我无法验证这一点,因为我无法访问Matlab的).

请参阅此MWE以获得澄清:

import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import acf
import matplotlib.pyplot as plt
plt.style.use("seaborn-colorblind")

def autocorr_by_hand(x, lag):
    # Slice the relevant subseries based on the lag
    y1 = x[:(len(x)-lag)]
    y2 = x[lag:]
    # Subtract the subseries means
    sum_product = np.sum((y1-np.mean(y1))*(y2-np.mean(y2)))
    # Normalize with the subseries stds
    return sum_product / ((len(x) - lag) * np.std(y1) * np.std(y2))

def acf_by_hand(x, lag):
    # Slice the relevant subseries based on the lag
    y1 = x[:(len(x)-lag)]
    y2 = x[lag:]
    # Subtract the mean of the whole series x to calculate Cov
    sum_product = np.sum((y1-np.mean(x))*(y2-np.mean(x)))
    # Normalize with var of whole series
    return sum_product / ((len(x) - lag) * np.var(x))

x = np.linspace(0,100,101)

results = {}
nlags=10
results["acf_by_hand"] = [acf_by_hand(x, lag) for lag in range(nlags)]
results["autocorr_by_hand"] = [autocorr_by_hand(x, lag) for lag in range(nlags)]
results["autocorr"] = [pd.Series(x).autocorr(lag) for lag in range(nlags)]
results["acf"] = acf(x, unbiased=True, nlags=nlags-1)

pd.DataFrame(results).plot(kind="bar", figsize=(10,5), grid=True)
plt.xlabel("lag")
plt.ylim([-1.2, 1.2])
plt.ylabel("value")
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Statsmodels用于np.correlate优化它,但这基本上是它的工作原理.

  • 我认为 `statsmodels` 方式是显而易见的。作为参考,这也是[维基百科](https://en.wikipedia.org/wiki/Autocorrelation#Estimation)中指出的方式。要检查使用像 Pandas 那样的互相关是否也是一个有效的估计,需要检查文献。仅供参考:虽然 statsmodels 的这个估计被认为是“无偏的”,因为我们使用了 `nk` 而不是 `n`,但根据维基百科,它仍然有偏差,因为我们使用样本均值和样本协方差进行计算。 (2认同)