【问题标题】:No confidence interval are shown when using plot_acf使用 plot_acf 时不显示置信区间
【发布时间】:2020-07-31 09:48:28
【问题描述】:

我有以下代码:

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)

此函数产生以下图:

但是,我在 95% (alpha=0.5) 处询问的置信区间未绘制。 如果我将该行从函数中取出,它会起作用并显示间隔。

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

【问题讨论】:

    标签: python matplotlib confidence-interval autocorrelation statmodels


    【解决方案1】:

    首先,对于 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)),它再次与样本大小的平方根成反比。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-06
      • 2021-04-08
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      相关资源
      最近更新 更多