Loading...
Loading...
Compute ACF and PACF for time series analysis. Identify lag patterns, seasonality, and determine ARIMA model orders with interactive correlograms.
import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import acf, pacf
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.stats.diagnostic import acorr_ljungbox
import matplotlib.pyplot as plt
# Data
data = np.array([200, 220, 250, 280, 210, 230, 260, 290, 220, 240, 270, 300, 230, 250, 280, 310, 240, 260, 290, 320])
# Compute ACF and PACF
acf_values = acf(data, nlags=10)
pacf_values = pacf(data, nlags=10, method='ywm')
print("ACF values:")
for lag, val in enumerate(acf_values):
sig = "*" if abs(val) > 1.96 / np.sqrt(len(data)) else ""
print(f" Lag {lag}: {val:.4f} {sig}")
print("\nPACF values:")
for lag, val in enumerate(pacf_values):
sig = "*" if abs(val) > 1.96 / np.sqrt(len(data)) else ""
print(f" Lag {lag}: {val:.4f} {sig}")
# Ljung-Box test for serial correlation
lb_test = acorr_ljungbox(data, lags=10, return_df=True)
print("\nLjung-Box Test:")
print(lb_test)
# Plot ACF and PACF
fig, axes = plt.subplots(2, 1, figsize=(12, 8))
plot_acf(data, lags=10, ax=axes[0], title='Autocorrelation Function (ACF)')
plot_pacf(data, lags=10, ax=axes[1], title='Partial Autocorrelation Function (PACF)')
plt.tight_layout()
plt.show()ACF (Autocorrelation Function) shows the total correlation between a time series and its lagged values, including indirect correlations through intermediate lags. PACF (Partial ACF) shows only the direct correlation at each lag, removing the effect of shorter lags. PACF is essential for identifying the order of AR models.
For ARIMA(p,d,q): If ACF cuts off after lag q and PACF tails off → MA(q) model. If PACF cuts off after lag p and ACF tails off → AR(p) model. If both tail off → ARMA(p,q). Differencing d times should make the series stationary before checking ACF/PACF.
The dashed blue lines are 95% confidence bands at ±1.96/√n. Any bar extending beyond these bands is statistically significant - the autocorrelation at that lag is unlikely to be zero. Bars within the bands are consistent with white noise.
A common rule of thumb is n/4 or 10·log₁₀(n) lags, where n is the series length. For seasonal data, examine at least 2 full seasonal cycles (e.g., 24 lags for monthly data with yearly seasonality).
Slowly decaying ACF (many significant lags) indicates a non-stationary series - it needs differencing. After differencing (computing the first differences), re-check the ACF. A stationary series should show ACF that drops off quickly.