Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何绘制stattools ccf函数的置信区间?_Python_Matplotlib_Statistics_Statsmodels - Fatal编程技术网

Python 如何绘制stattools ccf函数的置信区间?

Python 如何绘制stattools ccf函数的置信区间?,python,matplotlib,statistics,statsmodels,Python,Matplotlib,Statistics,Statsmodels,我正在使用statsmodels计算互相关函数。它工作得很好,只是我不知道如何绘制置信区间。我注意到它似乎有更多的功能。下面是一个玩具示例,只是想看看: 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.

我正在使用statsmodels计算互相关函数。它工作得很好,只是我不知道如何绘制置信区间。我注意到它似乎有更多的功能。下面是一个玩具示例,只是想看看:

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])
将numpy导入为np
将matplotlib.pyplot作为plt导入
将statsmodels.tsa.stattools导入为stattools
def创建(n):
x=np.零(n)
对于范围(1,n)内的i:
如果np.random.rand()小于0.9:
如果np.random.rand()小于0.5:
x[i]=x[i-1]+1
其他:
x[i]=np.random.randint(0100)
返回x
x=创建(4000)
y=创建(4000)
plt.plot(stattools.ccf(x,y)[:100])
这使得:


不幸的是,置信区间不是由statsmodels互相关函数()提供的。在R中,ccf()还将打印置信区间

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

  • Rob Hyndman的回答:
将numpy导入为np
将matplotlib.pyplot作为plt导入
将statsmodels.tsa.stattools导入为stattools
def创建(n):
x=np.零(n)
对于范围(1,n)内的i:
如果np.random.rand()小于0.9:
如果np.random.rand()小于0.5:
x[i]=x[i-1]+1
其他:
x[i]=np.random.randint(0100)
返回x
x=创建(4000)
y=创建(4000)
滞后=4000
sl=2/np.sqrt(滞后)
plt.plot(x,list(np.one(滞后)*sl),color='r')
plt.plot(x,list(np.one(滞后)*-sl),color='r')
plt.plot(stattools.ccf(x,y)[:100])
这将导致以下带有附加红线的绘图:

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