python - 如何绘制 stattools ccf 函数的置信区间?
问题描述
我正在使用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])
这给出了:
解决方案
不幸的是,statsmodels 互相关函数 ( ccf )不提供置信区间。在 R 中, ccf() 也会打印置信区间。
在这里,我们需要自己计算置信区间,然后绘制出来。置信区间在这里计算为2 / np.sqrt(lags)
。有关互相关置信区间的基本信息,请参阅:
- Rob Hyndman 的 Stats StackExchange 回答:https ://stats.stackexchange.com/a/3128/43304
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])
推荐阅读
- python - 基于 n 个 json 文件创建动态 Flask-SocketIO 页面
- python - 使用 tkinter 在 python 中单击时单选按钮不更新值
- unity3d - NavMeshAgent 在 Unity3D 中停止后仍在移动
- c++ - 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项的源代码控制的最佳方法是什么?
- javascript - 在 Zeit Now 无服务器函数中加载目录
- clojure - 如何为列表中的每个项目添加括号?
- android - 如何动态自动调整editText?
- python - 将字符串列表转换为具有单独整数的列表时出错?
- mysql - 为什么“NULL 不等于任何东西”是错误的陈述?
- php - 代码执行时if和else都执行