DPredict · Guide test 212
Ljung–Box portmanteau test
Uses a finite sample adjustment to test a collection of autocorrelations jointly.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- Interrupted time series (segmented regression) — Did the level or the trend of a series change when an intervention started?
- Randomness and serial-dependence tests — Is the series (or a model's residuals) just random noise, or does it carry runs, trends, autocorrelation, volatility clustering or nonlinear structure?
In Randomness and serial-dependence tests, choose Test = Ljung–Box Q.
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Ljung–Box portmanteau test
Each error carries φ × the previous one; 1 is a random walk.
Your data
Simulated with a known ρIf H₀ were true
- The reference: how Q* varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .125).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- lag-k autocorrelation
- lags tested (subtract fitted ARMA parameters from the df)
The verdict
Q*(3) = 5.74, p = .125
p > α = .050: compatible with H₀
The standard model check in forecasting. Choose h before looking; for residuals of an ARMA(p, q) fit use h − p − q degrees of freedom.
- Statistic
- 5.738
- p-value
- 0.1251
Ljung–Box portmanteau test on 40 (x, y) pairs: Q*(3) = 5.74, p = .125. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: autocorrelations at lags 1,…,h are all zero.
Assumptions
A stationary series or correctly fitted stationary model residuals; large sample regularity and appropriate parameter df adjustment.
Formula
Parameters and notation
T sample size,h chosen maximum lag,r̂k residual/sample autocorrelation. For common fitted ARMA(p,q) models df is approximately h−p−q; other models need their own correction.
Structure of the method
Sum squared autocorrelations using the Ljung–Box lag-specific finite-sample weights T(T+2)/(T−k). This adjusts the Box–Pierce scaling while retaining an asymptotic chi-square calibration under the relevant assumptions. It is an omnibus test across the selected lag range, not a separate test of each individual lag.
Estimated and nuisance parameters
Mean/autocorrelation estimates and any previously fitted dynamic coefficients are estimated quantities. The number of fitted ARMA parameters enters a common degrees-of-freedom adjustment, but more complex models require their own justification rather than mechanically subtracting every trained parameter.
Settings and design choices
State maximum lag, seasonal period when relevant, model_df/fitdf, missing-data handling, and whether several horizons were examined. The same function may return both Ljung–Box and Box–Pierce columns; report the intended statistic rather than using whichever p-value is more convenient.
Fitting and adaptation
After fitting or refitting a model, recompute residuals and apply a matching diagnostic calibration. For bootstrap assessment, repeat model estimation and any relevant order selection. A separate forecasting validation set evaluates predictive adequacy; passing an in-sample residual test is not a substitute for that assessment.
Sensitivity and robustness
The correction improves some finite-sample behavior but does not make small, dependent, heteroskedastic, or badly specified samples universally chi-square. Excessive lag horizons reduce power or leave nonpositive adjusted degrees of freedom. Volatility dependence can remain despite zero linear autocorrelation. Report the lag horizon, sample size, fitted-dynamic adjustment, and interpretable residual diagnostics alongside the test result.
How to use and calibrate
Use an upper χ² tail with valid positive adjusted df. Ljung–Box applies a finite sample correction to Box–Pierce.
Example application
Check whether a fitted time-series model leaves linear serial dependence.
Interpretation and pitfalls
Watch out
Zero autocorrelation is weaker than independence; volatility clustering may remain. Searching many h values requires caution.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Python prints both bp and lb columns; select the named test. When applied to fitted ARMA residuals, account for estimated dynamic parameters with fitdf/model_df.
Validated — Executed successfully in the validation environment · needs numpy, scipy, statsmodels
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
from statsmodels.stats.diagnostic import acorr_ljungbox
x=rng.normal(size=160)
print(acorr_ljungbox(x,lags=[10],boxpierce=True,model_df=0,return_df=True))
Output
lb_stat lb_pvalue bp_stat bp_pvalue 10 5.525394 0.853439 5.31305 0.869309
Validated — Executed successfully in the validation environment · needs Base/standard library
set.seed(2026)
x<-rnorm(160);print(Box.test(x,lag=10,type="Ljung-Box",fitdf=0))
Output
Box-Ljung test data: x X-squared = 11.808, df = 10, p-value = 0.2981
Sources
- statsmodels — statsmodels · stats.diagnostic.acorr_ljungboxstatsmodels.org
- statsmodels.stats.diagnostic.acorr_ljungboxstatsmodels.org
- Statistics guide v4 (historical):
- Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 212, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT212.