Ljung-Box Test Calculator
Inputs
- Inputs: sample size 100 and the first 5 residual autocorrelations: 0.18, 0.12, 0.05, -0.03, 0.02.
- Each squared autocorrelation is weighted by n(n + 2)/(n - k): the weighted sum is 0.000514.
- Ljung-Box Q = 100 x 102 x 0.000514 = 5.24 on 5 degrees of freedom.
- The chi-square survival probability at Q = 5.24 is 0.3875: a small p-value says the residuals still carry autocorrelation the model failed to capture.
A time-series model is finished only when its residuals are indistinguishable from white noise. The Ljung-Box test, published by Greta Ljung and George Box in 1978, is the standard final check: it gathers the first several residual autocorrelations into a single portmanteau statistic and asks whether, jointly, they are larger than chance allows. Enter the sample size and the residual autocorrelations at the lags you want to test, and the calculator returns Q, its degrees of freedom and the p-value.
The test refines the older Box-Pierce statistic with a small-sample correction - each squared autocorrelation is weighted by n(n + 2)/(n - k) - which makes the chi-square approximation noticeably more accurate at the sample sizes practitioners actually have.
Worked examples
Clean residuals, n = 100
(100 · 0.18 · 0.12 · 0.05 · -0.03 · 0.02 · 5)
| Ljung-Box Q statistic | 5.24 |
| P-value | 0.3875 |
| Degrees of freedom | 5 |
| Sum of squared autocorrelations | 0.0506 |
Leftover autocorrelation
(60 · 0.31 · 0.22 · 0.15 · 0.08 · 0.05 · 5)
| Ljung-Box Q statistic | 11.23 |
| P-value | 0.0471 |
| Degrees of freedom | 5 |
| Sum of squared autocorrelations | 0.1759 |
Long quiet series, 3 lags
(250 · 0.06 · 0.04 · 0.02 · 0.01 · -0.02 · 3)
| Ljung-Box Q statistic | 1.42 |
| P-value | 0.7010 |
| Degrees of freedom | 3 |
| Sum of squared autocorrelations | 0.0056 |
Why residuals must be white noise
Every ARMA-style model makes the same promise: the systematic, predictable part of the series has been extracted into the model, and what remains is unforecastable noise. Autocorrelation left in the residuals is a broken promise - structure the model failed to capture, which usually means missing lags, a missing seasonal term, or the wrong model family altogether.
Checking the autocorrelations one at a time invites false alarms, because with many lags some will look large by chance. The portmanteau construction tests them jointly, with one statistic and one p-value, which is the honest way to ask the question.
- The null hypothesis: residuals are independently distributed (no autocorrelation up to the chosen lag).
- A small p-value is evidence the model is incomplete, not that any particular lag is at fault.
- The test applies to residuals from a fitted model or to a raw series being screened for structure.
The statistic and its weighting
For each lag k, the squared autocorrelation is divided by n - k and the results are summed and multiplied by n(n + 2). The division by n - k corrects for the shrinking number of pairs available at longer lags, and it is this weighting that separates Ljung-Box from the cruder Box-Pierce sum of squared autocorrelations.
In the first worked example - 100 observations with autocorrelations 0.18, 0.12, 0.05, -0.03 and 0.02 over 5 lags - the plain sum of squared autocorrelations is 0.0506, the weighted sum is 0.000514 before scaling, and Q comes out at 5.24 on 5 degrees of freedom, with a p-value of 0.3875. Nothing in those residuals rises above noise.
| Example | n | Lags | Q | P-value |
|---|---|---|---|---|
| First | 100 | 5 | 5.24 | 0.3875 |
| Second | 60 | 5 | 11.23 | 0.0471 |
| Third | 250 | 3 | 1.42 | 0.7010 |
Worked examples
Example 1: Sample size n 100; Lag 1 autocorrelation 0.18; Lag 2 autocorrelation 0.12; Lag 3 autocorrelation 0.05; Lag 4 autocorrelation -0.03; Lag 5 autocorrelation 0.02; Lags used in the test: 5. Ljung-Box Q statistic = 5.24. Supporting values: P-value 0.3875; Degrees of freedom 5; Sum of squared autocorrelations 0.0506. Inputs: sample size 100 and the first 5 residual autocorrelations: 0.18, 0.12, 0.05, -0.03, 0.02. Each squared autocorrelation is weighted by n(n + 2)/(n - k): the weighted sum is 0.000514. Ljung-Box Q = 100 x 102 x 0.000514 = 5.24 on 5 degrees of freedom. The chi-square survival probability at Q = 5.24 is 0.3875: a small p-value says the residuals still carry autocorrelation the model failed to capture.
Example 2: Sample size n 60; Lag 1 autocorrelation 0.31; Lag 2 autocorrelation 0.22; Lag 3 autocorrelation 0.15; Lag 4 autocorrelation 0.08; Lag 5 autocorrelation 0.05; Lags used in the test: 5. Ljung-Box Q statistic = 11.23. Supporting values: P-value 0.0471; Degrees of freedom 5; Sum of squared autocorrelations 0.1759. Inputs: sample size 60 and the first 5 residual autocorrelations: 0.31, 0.22, 0.15, 0.08, 0.05. Each squared autocorrelation is weighted by n(n + 2)/(n - k): the weighted sum is 0.003018. Ljung-Box Q = 60 x 62 x 0.003018 = 11.23 on 5 degrees of freedom. The chi-square survival probability at Q = 11.23 is 0.0471: a small p-value says the residuals still carry autocorrelation the model failed to capture.
Example 3: Sample size n 250; Lag 1 autocorrelation 0.06; Lag 2 autocorrelation 0.04; Lag 3 autocorrelation 0.02; Lag 4 autocorrelation 0.01; Lag 5 autocorrelation -0.02; Lags used in the test: 3. Ljung-Box Q statistic = 1.42. Supporting values: P-value 0.7010; Degrees of freedom 3; Sum of squared autocorrelations 0.0056. Inputs: sample size 250 and the first 3 residual autocorrelations: 0.06, 0.04, 0.02. Each squared autocorrelation is weighted by n(n + 2)/(n - k): the weighted sum is 0.000023. Ljung-Box Q = 250 x 252 x 0.000023 = 1.42 on 3 degrees of freedom. The chi-square survival probability at Q = 1.42 is 0.7010: a small p-value says the residuals still carry autocorrelation the model failed to capture.
Reading the p-value
The second worked example shows the test earning its keep. Sixty observations with autocorrelations of 0.31, 0.22, 0.15, 0.08 and 0.05 - a decaying pattern typical of a missed autoregressive term - give Q = 11.23 and a p-value of 0.0471, just significant at the 5% level. The verdict is not that the model is useless, but that it has left predictable structure on the table.
The third example is the comfortable case: 250 observations tested over 3 lags give Q = 1.42 and a p-value of 0.7010. Failing to reject is never proof of white noise, but a large p-value on a long series is the strongest reassurance this test can give.
Choosing the number of lags
The lag count is the one judgement call in the test. Too few lags and slow-moving leftover structure escapes; too many and the statistic dilutes a real signal at the early lags across dozens of noise terms. Common practice tests around 10 lags for non-seasonal data, or two to three full seasonal cycles for seasonal data, and this calculator accepts up to 5 so the early-lag behaviour - where model failure usually shows first - can be isolated.
It is also legitimate to run the test at several lag counts and read the pattern: a model whose p-value collapses only when seasonal lags are included is telling you exactly which term it is missing.
Limits of the portmanteau approach
The Ljung-Box test detects linear autocorrelation only. Residuals can pass it cleanly while still carrying structure it cannot see - volatility clustering, for instance, where the residuals are uncorrelated but their squares are strongly predictable. That is the territory of the ARCH LM test, and the two are natural companions in a residual diagnostic routine.
The chi-square approximation also leans on the sample size: at very small n the weighted statistic helps but does not fully cure the approximation, so borderline p-values on short series deserve a sceptical read and, where possible, a simulation check.
The test's portmanteau character is both its strength and its known weakness: by pooling lags it gains power against diffuse alternatives - many small autocorrelations, none decisive alone - and loses power against a single sharp spike at one lag, which a targeted look at that lag would catch. The second example's decaying pattern is the diffuse case the test was built for. A series whose only leftover structure is a seasonal spike would be better served by testing that lag directly, and a careful diagnostic routine does both: the joint test for the general verdict, the individual correlogram for the specific one.
One more degree-of-freedom subtlety is worth stating plainly. The adjustment for fitted ARMA parameters matters most exactly when the sample is short and the model is large - the second example's territory of 60 observations - because each estimated parameter consumes information the chi-square reference distribution must be charged for. Software differs in whether it applies the correction automatically, so a reported p-value of 0.0471 is only interpretable alongside a statement of which degrees of freedom produced it.
Reporting practice for the test has settled, and it is worth following: state the lag count, the statistic, the degrees of freedom actually used and the p-value together, every time. A bare 'the Ljung-Box test was passed' conceals every judgement the test contains - the first example's comfortable 0.3875 at 5 lags and a hypothetical pass at 2 lags are different amounts of evidence, and only the full report lets a reader weigh them. The four numbers are the finding; the verdict word is a summary, not a substitute.
FAQs
What Q statistic does the first example give?
The first example gives Q = 5.24, from 100 observations and 5 lags of residual autocorrelations beginning at 0.18. On 5 degrees of freedom that is unremarkable - the p-value is 0.3875.
What p-value does the first example give?
The first example gives a p-value of 0.3875. There is no evidence that the residuals carry leftover autocorrelation, so the model passes the white-noise check at this lag count.
What sum of squared autocorrelations does example one use?
Example one uses a sum of squared autocorrelations of 0.0506 - the squares of 0.18, 0.12, 0.05, -0.03 and 0.02 added together, before the Ljung-Box weighting turns them into Q.
What Q does the autocorrelated second example give?
The autocorrelated second example gives Q = 11.23. Its decaying autocorrelations, starting at 0.31, are exactly the pattern of a missed autoregressive term, and the statistic accumulates them into a rejection.
What p-value does the second example give?
The second example gives a p-value of 0.0471 - just below the conventional 5% line. The model has left predictable structure in its residuals and should be respecified.
What Q does the long third example give?
The long third example gives Q = 1.42 on 3 degrees of freedom, with a p-value of 0.7010. Across 250 observations the residuals show no meaningful autocorrelation at the tested lags.