Professional Documents
Culture Documents
A Step-by-Step Guide To Calculating Autocorrelation and Partial Autocorrelation
A Step-by-Step Guide To Calculating Autocorrelation and Partial Autocorrelation
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"]
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)
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
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)
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
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...
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...
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