【问题标题】:Calculate autocorrelation as a function of lag in Python在 Python 中计算自相关作为滞后的函数
【发布时间】:2019-09-25 11:05:59
【问题描述】:

在 python (+ pandas/numpy/scipy/statsmodels) 中是否有返回自相关 wrt lag 的函数?这样的东西是否存在现成的库函数?

为避免混淆,我想要以下内容,只是我不想绘制它,但我希望它作为一个系列(pd.Series 或 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)

实际上,我想要 pd.Series.autocorr() 返回的内容,但我想要一个系列而不是一个标量,其中系列包含各种滞后的自相关。

编辑:

实现上述目的的一种方法是:

pd.Series([s.autocorr(i) for i in range(0,s.shape[0]-1)], index=range(0,s.shape[0]-1))

【问题讨论】:

  • @Wen-Ben 它只显示如何绘制 acf/pacf。看不到任何返回 acf/pacf 系列的代码。
  • stackoverflow.com/questions/14297012/… sry 只是复制错误的页面
  • @Wen-Ben 我的意思是我可以使用 pd.Series.autocorr() tbh 的列表理解,只是认为有更复杂的东西。好的,我自己写函数。
  • 我提供了一种方法,希望它比你的旧线路更快

标签: python pandas statistics signal-processing statsmodels


【解决方案1】:

Statsmodels acf 函数怎么样?

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))

产量

0    1.000000
1    0.033136
2   -0.124275
3   -0.396403
4   -0.248519
5    0.078170
dtype: float64

【讨论】:

    【解决方案2】:

    我只能想到自己加速的方法vectorize

    np.vectorize(s.autocorr)(np.arange(0,len(s)-1))
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 2023-01-30
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2017-11-01
      • 1970-01-01
      相关资源
      最近更新 更多