【问题标题】:How to plot confidence intervals for stattools ccf function?如何绘制 stattools ccf 函数的置信区间?
【发布时间】:2019-01-04 22:02:14
【问题描述】:

我正在使用来自 statsmodels 的ccf 计算互相关函数。它工作正常,除了我看不到如何绘制置信区间。我注意到acf 似乎有更多的功能。这是一个玩具示例,仅供参考:

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.tsa.stattools as stattools

def create(n):
    x = np.zeros(n)
    for i in range(1, n):
        if np.random.rand() < 0.9:
            if np.random.rand() < 0.5:
                x[i] = x[i-1] + 1
        else:
            x[i] = np.random.randint(0,100)
    return x
x = create(4000)
y = create(4000)
plt.plot(stattools.ccf(x, y)[:100])

这给出了:

【问题讨论】:

    标签: python matplotlib statistics statsmodels


    【解决方案1】:

    很遗憾,statsmodels 互相关函数 (ccf) 不提供置信区间。在 R 中, ccf() 也会打印置信区间。

    这里,我们需要自己计算置信区间,然后绘制出来。置信区间在这里计算为2 / np.sqrt(lags)。有关互相关置信区间的基本信息,请参阅:

    import numpy as np
    import matplotlib.pyplot as plt
    import statsmodels.tsa.stattools as stattools
    
    def create(n):
        x = np.zeros(n)
        for i in range(1, n):
            if np.random.rand() < 0.9:
                if np.random.rand() < 0.5:
                    x[i] = x[i-1] + 1
            else:
                x[i] = np.random.randint(0,100)
        return x
    x = create(4000)
    y = create(4000)
    
    lags= 4000
    sl = 2 / np.sqrt(lags)
    
    plt.plot(x, list(np.ones(lags) * sl), color='r')
    plt.plot(x, list(np.ones(lags) * -sl), color='r')
    
    plt.plot(stattools.ccf(x, y)[:100])
    

    这导致以下带有额外红线的图:

    【讨论】:

      猜你喜欢
      • 2013-02-05
      • 2012-12-13
      • 2023-04-07
      • 2020-05-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2012-12-20
      相关资源
      最近更新 更多