Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

1 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

statsmodels

1 import pandas as pd
2 import numpy as np
3 from sklearn.linear_model import LinearRegression
4 from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
5 from statsmodels.tsa.stattools import acf, pacf
6 from statsmodels.tsa.tsatools import lagmat
7
8 import matplotlib.pyplot as plt
9
10 # settings
11 plt.style.use("seaborn")

2 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

seaborn sns.load_dataset("flights.csv")

pandas y

1 df = pd.read_csv("../data/air_passengers.csv", index_col=0)
2 df.index = pd.to_datetime(df.index)
3 y = df["#Passengers"]

acf_pacf_2.py hosted with by GitHub view raw

3 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

1 fig, ax = plt.subplots(2, 1)
2
3 plot_acf(df, ax=ax[0])
4 plot_pacf(df, ax=ax[1], method="ols")

statsmodels

4 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

acf(df, nlags=10)

array([1. , 0.94804734, 0.87557484, 0.80668116,


0.75262542, 0.71376997, 0.6817336 , 0.66290439,
0.65561048, 0.67094833, 0.70271992])

corr

1 acf_df = pd.DataFrame()
2 for lag in range(0, 11):
3 acf_df[f"y_lag_{lag}"] = y.shift(lag)
4
5 acf_df

5 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

lag_0

acf_df.corr()["y_lag_0"].values

array([1. , 0.96019465, 0.89567531, 0.83739477,


0.7977347 , 0.78594315, 0.7839188 , 0.78459213,
0.79221505, 0.8278519 , 0.8827128 ])

6 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

corr

7 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

1 acf_list = []
2 mu = y.mean()
3
4 for lag in range(0, 11):
5 acf_list.append(sum((y - mu).iloc[lag:] * (y.shift(lag) - mu).iloc[lag:])
6
7 np.array(acf_list)

array([1. , 0.94804734, 0.87557484, 0.80668116,


0.75262542, 0.71376997, 0.6817336 , 0.66290439,
0.65561048, 0.67094833, 0.70271992])

acf

statsmodels

8 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

statsmodels

9 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

statsmodels

statsmodels

pacf

statsmodels

pacf(df, nlags=10, method="ols")

10 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

array([ 1. , 0.95893198, -0.32983096,


0.2018249 , 0.14500798, 0.25848232,
-0.02690283, 0.20433019, 0.15607896, 0.56860841,
0.29256358])

1 N_LAGS = 10
2
3 # the first partial autocorrelation is always equal to 1
4 pacf_list = [1]
5
6 X = pd.DataFrame(lagmat(y, N_LAGS))
7 X.columns = [f"lag_{lag+1}" for lag in range(10)]
8
9 for k in range(1, N_LAGS + 1):
10 fitted_model = LinearRegression().fit(X.iloc[k:, :k],
11 y.iloc[k:])
12 pacf_list.append(fitted_model.coef_[-1])
13

lagmat statsmodels

pacf

11 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

array([ 1. , 0.95893198, -0.32983096,


0.2018249 , 0.14500798, 0.25848232,
-0.02690283, 0.20433019, 0.15607896, 0.56860841,
0.29256358])

12 of 13 3/1/2022, 11:46 AM
A Step-by-Step Guide to Calculating Autocorrelation and Part... https://towardsdatascience.com/a-step-by-step-guide-to-calcul...

13 of 13 3/1/2022, 11:46 AM

You might also like