Skip to content

DPredict · Guide test 211

Box–Pierce portmanteau test

Tests whether autocorrelations through a chosen lag are jointly zero.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

  • 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 = Box–Pierce 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 · Box–Pierce portmanteau test

40
0.000
0.40

Each error carries φ × the previous one; 1 is a random walk.

0.0
3
0.050

Your data

Simulated with a known ρ

If H₀ were true

Q = 5.31
  1. The reference: how Q varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .150).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

Q=n∑k=1hrk2∼χh2Q = n\sum_{k=1}^{h} r_k^2 \sim \chi^2_h

With your numbers

Q=5.31,r1=0.35Q = 5.31,\quad r_1 = 0.35
rkr_k
lag-k sample autocorrelation
hh
lags tested

The verdict

Q(3) = 5.31, p = .150

p > α = .050: compatible with H₀

Classic white-noise check; the Ljung–Box correction is better in small samples.

Statistic
5.310
p-value
0.1505
Box–Pierce portmanteau test on 40 (x, y) pairs: Q(3) = 5.31, p = .150. The p-value is above α = .050, so the data are compatible with H0.

Box–Pierce portmanteau test on 40 (x, y) pairs: Q(3) = 5.31, p = .150. 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

Q=T∑k=1hr^k2Q=T\sum_{k=1}^{h}\widehat r_k^2

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

Estimate sample or residual autocorrelations through a specified lag horizon and sum their squares, scaled by sample size. The Box–Pierce statistic aggregates linear serial-dependence evidence. Its chi-square approximation depends on the input process and on whether model parameters were estimated before residuals were computed.

Estimated and nuisance parameters

Sample mean/autocovariances and, for residual diagnostics, the underlying ARMA or other model coefficients are fitted. Degrees-of-freedom adjustment reflects the relevant fitted dynamic parameters under a justified model, not every coefficient count borrowed from an unrelated regression.

Settings and design choices

Prespecify maximum lag, seasonal considerations, model degrees of freedom, input centering, and single versus multiple horizons. The displayed raw-normal example uses model_df/fitdf zero because no ARMA dynamics were fitted. The adjusted lag degrees of freedom must remain positive.

Fitting and adaptation

Fit the intended time-series model first, retain correctly aligned residuals, and use the appropriate parameter adjustment or model-based bootstrap. Refit the generating model in each simulation where its estimation matters. If model order is selected repeatedly until a test passes, report that exploration and validate forecasts independently.

Sensitivity and robustness

Box–Pierce can be poorly calibrated in smaller samples, especially with a large lag horizon. Heteroskedastic innovations, nonlinear dependence, seasonal effects beyond the chosen horizon, and structural breaks can escape or distort the test. Zero autocorrelation is weaker than independence; squared-residual or other diagnostics answer additional questions. Report the chosen horizon and residual plots with the p-value.

How to use and calibrate

Use an upper χ² tail with valid positive adjusted df. Box–Pierce is the uncorrected portmanteau statistic.

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.

Python · native

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
R

Validated — Executed successfully in the validation environment · needs Base/standard library

set.seed(2026)
x<-rnorm(160);print(Box.test(x,lag=10,type="Box-Pierce",fitdf=0))
Output
	Box-Pierce test

data:  x
X-squared = 11.376, df = 10, p-value = 0.329

Sources

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 211, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT211.