Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Time Forecasting using Python/2.2 Autocorrelation and Partial Correlation.ipynb
3074 views
Kernel: Python 3 (ipykernel)
  1. Autocorrelation Definition:

image.png image-2.png

It seems the dataset size [1, 2, 3, 4, 5, 6, 7, 8] is too small to compute the Partial Autocorrelation Function (PACF) for 7 lags because PACF can only be computed for lags up to 50% of the sample size.

import numpy as np import matplotlib.pyplot as plt import pandas as pd from statsmodels.graphics.tsaplots import plot_acf # Generate a sample time series data np.random.seed(0) n = 100 time_series_data = pd.Series(np.random.randn(n)) # Plot Autocorrelation plt.figure(figsize=(15, 0)) plot_acf(time_series_data, lags=20) plt.show()
<Figure size 1080x0 with 0 Axes>
Image in a Jupyter notebook

2. Partial Autocorrelation

image.png image-2.png

  • ACF Plot shows how each value in the series is correlated with its lagged values.

  • PACF Plot shows the direct correlation between the values and their lagged counterparts after removing the effects of intermediate lags.

from statsmodels.graphics.tsaplots import plot_pacf # Plot Partial Autocorrelation plt.figure(figsize=(10, 5)) plot_pacf(time_series_data, lags=30) plt.show()
<Figure size 720x360 with 0 Axes>
Image in a Jupyter notebook

Difference Between Autocorrelation and Partial Autocorrelation:

1- Autocorrelation

  • Measure the correlation between a time series and its lagged Values

  • ACF plots the Correlation values values against different lasgs

2- Partial

  • Measure the corrlation between a time series and its lagged values controlling the effects of intermediate lags.

  • PACF curve

Visualizing and Interpreting the Results

Visualizing and Interpreting the Results: The autocorrelation plot (ACF) usually starts at 1 (since a series is perfectly correlated with itself at lag 0) and then decreases as the lag increases. The partial autocorrelation plot (PACF) typically drops sharply after a certain lag, especially in autoregressive processes

Xt Xt-6