5

I'm trying find number of significant output using ACF graph, however results of statsmodels.tsa.acf() confidence intervals don't match with statsmodels.graphics.tsa.acf() graph.

Sample code:

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)

Here is the plot,

ACF Graph

However, confidence interval values returned from sm.tsa.acf() is way different comparing to the values in the graph.

Returned Values;

[[ 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]]

It seems like first value is matching the graph then, it becomes quite unrelated. I find similar question at Statsmodels PACF plot confidence interval does not match PACF function , yet there was no solution. I read the documents, search through similar questions but I cannot find a solution.

How can I get the confidence interval values that are reflected on the graph?

  • 1
    duplicate, The answer in the linked question explains it. Just subtract the acf from the confidence interval to have it centered at zero. – Josef Jun 05 '20 at 21:08

1 Answers1

0

Solution

To get the confidence intervals that are reflected on the figure returned by plot_acf, you need to subtract the acf_values from the confint boundaries.

Example

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()

Examplary ACF plot

Explanation

plot_acf and acf differ in their intended use: plot_acf provides a quick visual representation of autocorrelation with a focus on visual interpretation, while acf is more geared towards providing the autocorrelation values themselves without the graphical context.

  • statsmodels.graphics.tsaplots.plot_acf (with plotting) is primarily designed to plot the autocorrelation function (ACF) of a time series and provide visual insights. When calculating and plotting the ACF using plot_acf, the confidence interval is centered around zero. This is useful to visualize how the autocorrelation values deviate from the null hypothesis (no autocorrelation). See the documentation of plot_acf's parameter barlett_confint.

  • statsmodels.graphics.tsaplots.acf (without plotting): The acf function is focused on calculating the autocorrelation function without creating a graphical plot. When you calculate the ACF using acf, the function doesn't center the confidence interval around zero but around the autocorrelation value itself. This can be useful for statistical calculations and hypothesis testing involving autocorrelation.

Reference

Geronimo
  • 51
  • 6