【问题标题】:Durbin–Watson statistic for one dimensional time series data一维时间序列数据的 Durbin-Watson 统计量
【发布时间】:2017-09-05 10:23:15
【问题描述】:

我正在尝试确定时间序列(例如,一个浮点数列表)是否与自身相关。我已经玩过 statsmodels (http://statsmodels.sourceforge.net/devel/generated/statsmodels.tsa.stattools.acf.html) 中的 acf 函数,现在我正在研究 Durbin-Watson 统计数据是否有价值。

看来这种事情应该可行:

from statsmodels.regression.linear_model import OLS
import numpy as np

data = np.arange(100)  # this should be highly correlated
ols_res = OLS(data)
dw_res = np.sum(np.diff(ols_res.resid.values))

如果你运行这个,你会得到:

Traceback (most recent call last):
...
  File "/usr/lib/pymodules/python2.7/statsmodels/regression/linear_model.py", line 165, in initialize
    self.nobs = float(self.wexog.shape[0])
AttributeError: 'NoneType' object has no attribute 'shape'

似乎 D/W 通常用于比较两个时间序列(例如http://connor-johnson.com/2014/02/18/linear-regression-with-python/)的相关性,所以我认为问题在于我没有通过另一个时间序列进行比较。也许这应该在exog参数中传递给OLS

exog : array-like

A nobs x k array where nobs is the number of observations and k is
the number of regressors.

(来自http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.linear_model.OLS.html

旁注:我不确定“nobs x k”数组的含义。也许一个数组是x by k

那么我应该在这里做什么?我是否希望通过data 两次, 还是自己手动滞后,或者?

谢谢!

【问题讨论】:

    标签: python numpy statistics correlation statsmodels


    【解决方案1】:

    我已接受 user333700 的回答,但我想发布代码 sn-p 跟进。

    这个小程序计算线性范围(应该高度相关,因此给出接近 0 的值)的 durbin-watson 相关性,然后计算随机值(不应该相关,因此给出接近 2 的值) ):

    from statsmodels.regression.linear_model import OLS
    import numpy as np
    from statsmodels.stats.stattools import durbin_watson
    
    
    
    def dw(data):
        ols_res = OLS(data, np.ones(len(data))).fit()
        return durbin_watson(ols_res.resid)
    
    
    print("dw of range=%f" % dw(np.arange(2000)))
    print("dw of rand=%f" % dw(np.random.randn(2000)))
    

    运行时:

    dw of range=0.000003
    dw of rand=2.036162
    

    所以我认为这看起来不错:)

    【讨论】:

      【解决方案2】:

      OLS 是需要 y 和 x(或 endog 和 exog)的回归。 x 在您的情况下至少需要是一个常数,即。 np.ones(len(endog),1)。

      另外,您需要拟合模型,即ols_res = OLS(y, x).fit()

      nobs x k 表示二维,行中为 nobs,列中为 k 个变量,即 exog.shape 为 (nobs, k)

      Durbin Watson 是序列相关性的检验统计量。它包含在 OLS 摘要输出中。 statsmodels 中还包含其他没有自相关的测试。

      (我建议您阅读一些示例或教程笔记本。)

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 2021-07-16
      • 2018-02-04
      • 2021-10-11
      • 2018-08-27
      • 1970-01-01
      相关资源
      最近更新 更多