We describe how to plot a correlogram with a graph of autocorrelation coefficient and partial autocorrelation coefficient in python.
The function sm.graphics.tsa.plot_acf of statsmodels.api is used.
The function sm.graphics.tsa.plot_pacf of statsmodels.api is used.
As an example, the autocorrelation coefficient of the following AR (1) model is plotted.
y_t = 1 + 0.5 y_{t-1} + \epsilon_t
However, $ \ epsilon_t $ is the normal white noise with variance 1. Also, let $ y_0 = 2 $.
#A magical spell that makes module capture and graphs look good
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
%matplotlib inline
import numpy as np
import statsmodels.api as sm
#Creating a column of data to plot
#This time, capture the data at 100 times
y = np.zeros(100)
np.random.seed(42)
epsilon = np.random.standard_normal(100)
y[0] = 2
for t in range(1,100):
y[t] = 1 + 0.5 * y[t-1] + epsilon[t]
#Take a look at the time series data to plot
plt.plot(y)
plt.xlabel('time')
plt.ylabel('value')
plt.title('time-value plot');
The following graph is plotted.
#Plot of autocorrelation coefficient
sm.graphics.tsa.plot_acf(y, lags=20)
plt.xlabel('lags')
plt.ylabel('corr')
#Partial autocorrelation coefficient plot
sm.graphics.tsa.plot_pacf(y, lags=20)
plt.xlabel('lags')
plt.ylabel('corr')
The correlogram of the autocorrelation coefficient and the correlogram of the partial autocorrelation coefficient are plotted.
Recommended Posts