使用plot_acf时不显示置信区间

2024-10-17 12:28:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from pandas import ExcelWriter
import numpy as np
import pandas as pd
import seaborn as sns
import statsmodels.formula.api as smf
import statsmodels.tsa.api as smt
import statsmodels.api as sm
import scipy.stats as scs
from arch import arch_model
import sys
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline

df1 = df[['EURUSD Curncy']]
df1 = df1['EURUSD Curncy']
def tsplot(y, lags=None, figsize=(15, 12), style='bmh'):
    if not isinstance(y, pd.Series):
        y = pd.Series(y)
    with plt.style.context(style):    
        fig = plt.figure(figsize=figsize)
        #mpl.rcParams['font.family'] = 'Ubuntu Mono'
        layout = (3, 2)
        ts_ax = plt.subplot2grid(layout, (0, 0), colspan=2)
        acf_ax = plt.subplot2grid(layout, (1, 0))
        pacf_ax = plt.subplot2grid(layout, (1, 1))
        qq_ax = plt.subplot2grid(layout, (2, 0))
        pp_ax = plt.subplot2grid(layout, (2, 1))
        
        y.plot(ax=ts_ax)
        ts_ax.set_title('Time Series Analysis Plots')
        smt.graphics.plot_acf(y, lags=lags, ax=acf_ax, alpha=0.5)#<-- this line here the issue
        smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax, alpha=0.5)#<-- this line here the issue
        sm.qqplot(y, line='s', ax=qq_ax)
        qq_ax.set_title('QQ Plot')        
        scs.probplot(y, sparams=(y.mean(), y.std()), plot=pp_ax)

        plt.tight_layout()
    return
tsplot(df1.pct_change().dropna(), lags=30)
tsplot(df1.pct_change().dropna()**2, lags=30)

此函数生成以下绘图: enter image description here

但是,我所要求的95%(α=0.5)的置信区间没有绘制出来。 如果我将该行从函数中取出,它将工作并显示间隔

我卡住了,你能帮忙吗?谢谢


Tags: fromimportplotaspltaxpddf1
1条回答
网友
1楼 · 发布于 2024-10-17 12:28:58

首先,对于95%的置信区间,您需要设置alpha=0.05,而不是您当前拥有的

但更重要的是,我注意到,你们有一个相当大的,高分辨率的时间序列。由于在statsmodels1、2中如何计算ACF和PACF置信限的性质,如此大的时间序列将导致难以置信的小值,从而导致置信区间基本不可见

因此,我的猜测是,置信区间确实显示出来了,只是非常小,你必须放大才能看到它们。也许你可以考虑在你的系列中的一小部分上执行ACF/PACF,或者你可以对这个系列进行下采样。

最后,为了您的绘图,可能值得将参数zero=False传递给plot_acfplot_pacf以消除第0个滞后,这显然是1,可能会扰乱轴的缩放。这样做可能有助于查看置信区间


1对于ACF,statsmodels使用Barlett's formula,其中边界的计算与样本大小的平方根成反比。

2对于PACF,statsmodels使用1/sqrt(len(x)),这再次与样本量的平方根成反比。

相关问题 更多 >