Luigi87
Luigi87

Reputation: 275

No confidence interval are shown when using plot_acf

I have the following code:

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)

this function produces the following plot: enter image description here

however the confidence interval that I am asking at 95% (alpha=0.5) is not plotted. If I take that line out of the function it works and interval are shown.

I am stuck, can you please help? thanks

Upvotes: 2

Views: 2702

Answers (1)

thesofakillers
thesofakillers

Reputation: 450

Well, first of all for a 95% confidence interval you would want to set alpha=0.05, rather than what you currently have.

But more importantly from what I notice, you have a rather large, high resolution time series. Due to the nature of how the ACF and PACF confidence limits are calculated in statsmodels1,2, such a large time series will result in incredibly small values, resulting in the confidence intervals being basically invisible.

As such my guess is that the confidence intervals are indeed shown, just very small and you have to zoom in to see them. Perhaps you may consider performing ACF/PACF on a smaller chunk of your series, or alternatively you could downsample the series.

Finally, for the sake of your plot, it may be worth passing the argument zero=False to plot_acf and plot_pacf to get rid of the 0th lag, which will obviously be 1 and may mess up your axis scaling. Doing so may aid in seeing the confidence intervals.


1 For ACF, statsmodels uses Barlett's formula, in which the bounds are computed to be inversely proportional to the square root of the sample size.

2 For PACF, statsmodels uses 1/sqrt(len(x)), which is once again inversely proportional to the square root of the sample size.

Upvotes: 2

Related Questions