Initializing playground…
← Back to roadmap

ACF / PACF & Portmanteau Tests

Autocorrelation (acf), partial autocorrelation (pacf), cross-correlation (ccf), Durbin-Watson, Ljung-Box, and Box-Pierce tests — mirrors statsmodels.tsa.stattools and pd.Series.autocorr.

1 — Single-lag autocorrelation (pandas-style)

autocorr(x, lag) computes the Pearson correlation between x[0..n−lag−1] and x[lag..n−1], exactly like pd.Series.autocorr(lag).

JavaScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 — Full ACF with Bartlett confidence intervals

acf(x, { nlags, alpha }) returns all autocorrelations at lags 0…nlags. With alpha=0.05, Bartlett confidence intervals are returned: lags whose CI excludes zero are statistically significant.

JavaScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 — Partial ACF (Levinson-Durbin)

pacf(x, { nlags, alpha }) uses the Levinson-Durbin recursion to compute partial autocorrelations. For a true AR(p) process, only the first p PACF values are significantly non-zero — this is how you identify the AR order.

JavaScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 — Cross-Correlation Function (CCF)

ccf(x, y, { nlags, alpha }) measures the linear relationship between x[t] and y[t+k] at each lag k. Peaks in the CCF reveal lead/lag relationships between two series.

JavaScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 — Durbin-Watson statistic

durbinWatson(residuals) tests for first-order autocorrelation in OLS residuals. Values near 2 indicate no autocorrelation; values near 0 indicate positive autocorrelation; values near 4 indicate negative autocorrelation.

JavaScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

6 — Ljung-Box & Box-Pierce portmanteau tests

ljungBox(x, { lags }) and boxPierce(x, { lags }) test the null hypothesis that no autocorrelation exists up to a given lag. Small p-values reject the white-noise hypothesis. Ljung-Box has better finite-sample properties.

JavaScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent