Statsmodels ACF 置信区间不匹配 - Python

问题描述 投票:0回答:1

我正在尝试使用 ACF 图查找显着输出的数量,但是 statsmodels.tsa.acf() 置信区间的结果与 statsmodels.graphics.tsa.acf() 图不匹配。

示例代码:

import statsmodels.api as sm
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf

acf,confidence_interval=sm.tsa.acf(df_data,nlags=df_data.shape[0]-1,alpha=0.05,fft=False)

plot_acf(df_data,lags=df_data.shape[0]-1)

print(confidence_interval)

这是剧情

但是,从 sm.tsa.acf() 返回的置信区间值与图中的值有很大不同。

返回值;

[[ 1.          1.        ]
 [-0.27174973  0.37268246]
 [-0.3286431   0.31742828]
 [ 0.0203798   0.66647139]
 [-0.61221928  0.10569058]
 [-0.61407253  0.14003004]
 [-0.42569193  0.35873921]
 [-0.58610165  0.19892257]
 [-0.64565391  0.15895208]
 [-0.34123344  0.49337893]
 [-0.53223297  0.30525403]
 [-0.56775509  0.2760946 ]
 [-0.02246426  0.83178741]
 [-0.55237867  0.37808097]
 [-0.53964256  0.39420078]
 [-0.19144858  0.74474359]
 [-0.63752942  0.33201877]
 [-0.66170085  0.31779123]
 [-0.5026759   0.48927364]
 [-0.63266561  0.35930273]
 [-0.60042286  0.39933612]
 [-0.50945575  0.49449365]
 [-0.47942564  0.52454691]
 [-0.48578234  0.51840072]
 [-0.32312106  0.68117201]
 [-0.40066389  0.61679615]
 [-0.3917795   0.63043611]
 [-0.35304025  0.67494402]
 [-0.52974159  0.50865544]
 [-0.57667548  0.46176601]
 [-0.5657842   0.47397661]
 [-0.61493365  0.42566845]
 [-0.57909456  0.46507539]
 [-0.54230719  0.50315461]
 [-0.51974363  0.52587038]
 [-0.53350424  0.5121135 ]
 [-0.52597853  0.51968465]]

看起来第一个值与图表匹配,然后它变得完全不相关。我在 Statsmodels PACF 图置信区间与 PACF 函数不匹配中发现类似的问题,但没有解决方案。我阅读了文档,搜索了类似的问题,但找不到解决方案。

如何获得图表上反映的置信区间值?

python statsmodels arima
1个回答
0
投票

解决方案

要获得反映在

plot_acf
返回的数字上的置信区间,您需要从
acf_values
边界中减去
confint

示例

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.graphics.tsaplots import acf

# Generate some sample time series data
np.random.seed(0)
t = np.linspace(0, 10, n)
time_series = np.sin(t)

# Calculate the ACF with confidence intervals using statsmodels
acf_values, confint = acf(time_series, alpha=0.05)

lower_bound = confint[1:, 0] - acf_values[1:]
upper_bound = confint[1:, 1] - acf_values[1:]

# Create lag values for the x-axis
lags = np.arange(0, len(acf_values))

# Plot the ACF with confidence intervals using Matplotlib
plt.figure(figsize=(10, 6))
plt.bar(lags, acf_values, width=0.2, align='center', label='ACF')

# Fill the area between upper and lower bounds
plt.fill_between(lags[1:], lower_bound, upper_bound, color='grey', alpha=0.2, label='95% Confidence Interval')

# Plot the upper and lower bounds
plt.plot(lags[1:], upper_bound, color='grey')
plt.plot(lags[1:], lower_bound, color='grey')

plt.axhline(y=0, color='black', linewidth=0.8, linestyle='dotted')
plt.xlabel('Lag')
plt.ylabel('Autocorrelation')
plt.title('Autocorrelation Function (ACF) with Confidence Interval')
plt.legend()
plt.grid(True)
plt.show()

说明

plot_acf
acf
的用途不同:
plot_acf
提供自相关的快速视觉表示,重点是视觉解释,而
acf
更适合在没有图形上下文的情况下提供自相关值本身。

  • statsmodels.graphics.tsaplots.plot_acf
    (带绘图)主要设计用于绘制时间序列的自相关函数(ACF)并提供可视化见解。使用
    plot_acf
    计算和绘制 ACF 时,置信区间以零为中心。这对于可视化自相关值如何偏离原假设(无自相关)非常有用。请参阅plot_acf参数
    barlett_confint
    文档

  • statsmodels.graphics.tsaplots.acf
    (不绘图):
    acf
    函数专注于计算自相关函数,而不创建图形图。当您使用
    acf
    计算 ACF 时,该函数不会将置信区间以零为中心,而是以自相关值本身为中心。这对于涉及自相关的统计计算和假设检验非常有用。

参考

© www.soinside.com 2019 - 2024. All rights reserved.