Ljung-Box Test
Introduction
The Ljung-Box Test checks whether a time series, or the residuals left over from a fitted model, are autocorrelated across several lags at once-rather than checking just one lag, the way the Durbin-Watson Test does. If you have just fit an ARIMA model and want a single, definitive check that the residuals resemble white noise-no leftover predictable pattern at any of the lags you care about-this is the test almost every time series workflow reaches for.
By the end of this article you will be able to state exactly when a Ljung-Box Test applies, compute the underlying statistic completely by hand on two different worked examples, understand how its degrees of freedom are chosen (and why they shrink after fitting a model), interpret the result correctly, know what to do if autocorrelation is detected, and run the same test in one line of Python with statsmodels.
What Is the Ljung-Box Test?
A well-fitted time series model should leave behind residuals that look like white noise- no systematic relationship between a residual and any of its own past values. Checking that one lag at a time is slow and doesn't answer the question analysts actually care about: is there any leftover structure across the lags that matter? The Ljung-Box Test, sometimes called a portmanteau test (a single test that bundles many smaller checks into one), answers exactly this by combining the sample autocorrelations at several lags into one joint statistic.
It exists because individually testing lag 1, then lag 2, then lag 3, and so on both inflates the false positive rate from repeated testing and misses patterns that only show up when several lags are considered together-a series can pass every single-lag test yet still carry meaningful joint structure. The Ljung-Box statistic sidesteps this by squaring and summing the autocorrelations across all tested lags into a single number, then comparing that number against a chi-square distribution.
When to Use It
Use a Ljung-Box Test whenever you want to check a single time series, or a set of model residuals, for autocorrelation across a group of lags jointly rather than just lag 1. It is most often the very last diagnostic run after fitting an ARIMA/SARIMA model, confirming the model has extracted all the structure it can before the residuals are treated as unpredictable noise.
| Scenario | Series | What's Being Checked |
|---|---|---|
| ARIMA residual diagnostics | Residuals from a fitted ARIMA/SARIMA model | Whether the model has captured all the serial structure |
| Pre-modeling screening | Raw monthly sales, demand, or traffic data | Whether the series has any autocorrelation worth modeling at all |
| Financial return series | Daily or intraday asset returns | Whether returns show detectable serial dependence (often expected to be near zero) |
| Regression residual diagnostics | Residuals from a time-ordered regression | Joint autocorrelation across several lags, beyond lag 1 |
| Model comparison | Residuals from two or more candidate models | Which model leaves the least leftover structure behind |
Key Assumptions
- The series (or residuals) is a single, time-ordered sequence: the test is built for one series at a time, observed in its correct chronological order-shuffled or unordered data makes the test meaningless.
- The sample autocorrelations are approximately normally distributed under \( H_0 \): this asymptotic result underlies the chi-square approximation, and holds reasonably well once \( n \) is moderately large.
- The number of lags \( h \) is chosen sensibly relative to \( n \): too few lags can miss structure at longer lags; too many relative to the sample size reduces the accuracy of the chi-square approximation and wastes degrees of freedom.
- Degrees of freedom are correctly adjusted for fitted parameters: when testing residuals from a fitted ARMA(\( p, q \)) model, the degrees of freedom should be \( h - p - q \), not \( h \)-see Degrees of Freedom and Critical Values.
- No missing observations within the sequence: gaps in an otherwise time-ordered series distort the sample autocorrelation calculation the test relies on.
Hypotheses
The Ljung-Box Test formally tests whether the population autocorrelations at lags 1 through \( h \) are all jointly zero:
- Null Hypothesis (\( H_0 \)): \( \rho_1 = \rho_2 = \cdots = \rho_h = 0 \)-the series behaves like white noise over the tested lags, with no autocorrelation.
- Alternative Hypothesis (\( H_1 \)): at least one \( \rho_k \neq 0 \) for \( k \in \{1, \ldots, h\} \)-some detectable autocorrelation exists among the tested lags.
(This is a joint test across every lag from 1 to \( h \) at once-rejecting \( H_0 \) tells you that somewhere in that range a real pattern exists, but not automatically which lag is responsible; inspecting the autocorrelation function directly, as covered in Checking Assumptions in Practice, usually answers that follow-up question.)
The Formula, Explained
Let \( y_1, y_2, \ldots, y_n \) be the observed time series (or a set of model residuals), and let \( \hat{\rho}_k \) denote the sample autocorrelation at lag \( k \):
\[ \hat{\rho}_k = \frac{\displaystyle\sum_{t=k+1}^{n} (y_t - \bar{y})(y_{t-k} - \bar{y})}{\displaystyle\sum_{t=1}^{n} (y_t - \bar{y})^2} \]The Ljung-Box statistic combines the squared autocorrelations across lags 1 through \( h \) into a single number, with a small-sample correction weighting each term:
\[ Q = n(n+2) \sum_{k=1}^{h} \frac{\hat{\rho}_k^2}{n-k} \]The \( n(n+2) \) factor out front and the \( \frac{1}{n-k} \) weighting inside the sum are what distinguish this from the simpler, older Box-Pierce statistic-they correct for the fact that sample autocorrelations at higher lags are estimated less precisely, giving the Ljung-Box version a chi-square approximation that holds up much better in small and moderate samples.
Under the null hypothesis of no autocorrelation, \( Q \) approximately follows a chi-square distribution:
\[ Q \;\sim\; \chi^2_{h} \](or \( \chi^2_{h-p-q} \) when testing residuals from a fitted ARMA(\( p, q \)) model, since fitting the model already used up \( p + q \) degrees of freedom-see the next section).
Degrees of Freedom and Critical Values
Unlike the Durbin-Watson Test's bounded 0-to-4 scale, the Ljung-Box statistic is compared directly against a standard chi-square distribution-no inconclusive zone, no published bounds tables, just an exact p-value once the degrees of freedom are correctly specified.
| Context | Degrees of Freedom | Why |
|---|---|---|
| Testing a raw series before modeling | \( h \) | No parameters have been estimated yet |
| Testing residuals from a fitted ARMA(\( p, q \)) model | \( h - p - q \) | Fitting the AR and MA terms already consumed \( p + q \) degrees of freedom |
Representative chi-square critical values at common degrees of freedom:
| Degrees of Freedom | 5% Critical Value | 1% Critical Value |
|---|---|---|
| 5 | \( 11.07 \) | \( 15.09 \) |
| 10 | \( 18.31 \) | \( 23.21 \) |
| 15 | \( 25.00 \) | \( 30.58 \) |
| 20 | \( 31.41 \) | \( 37.57 \) |
Worked Example 1: A White-Noise-Like Series
Suppose we observe a series over 12 periods that bounces around its mean with no obvious repeating pattern:
| Time \( t \) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| \( y_t \) | 2.1 | -1.4 | 0.8 | -0.6 | 1.3 | -2.0 | 0.5 | 1.1 | -1.7 | -0.3 | 1.9 | -0.7 |
We test \( h = 3 \) lags jointly. First, \( \bar{y} \approx 0.0833 \).
Step 1: Compute the sample autocorrelations at lags 1, 2, and 3
\[ \sum_{t=1}^{12}(y_t-\bar{y})^2 \approx 21.517 \] \[ \hat{\rho}_1 \approx \frac{-11.604}{21.517} \approx -0.539 \qquad \hat{\rho}_2 \approx \frac{-0.929}{21.517} \approx -0.043 \qquad \hat{\rho}_3 \approx \frac{3.076}{21.517} \approx 0.143 \]The lag-1 autocorrelation is fairly large in magnitude on its own (\( \approx -0.539 \))-a reminder that a single noisy autocorrelation at one lag doesn't necessarily mean the series fails a joint test across several lags at once, which is exactly what the next steps check.
Step 2: Square each autocorrelation and weight by \( 1/(n-k) \)
\[ \frac{\hat{\rho}_1^2}{n-1} = \frac{0.2908}{11} \approx 0.02644 \qquad \frac{\hat{\rho}_2^2}{n-2} = \frac{0.0019}{10} \approx 0.00019 \] \[ \frac{\hat{\rho}_3^2}{n-3} = \frac{0.0204}{9} \approx 0.00227 \] \[ \sum_{k=1}^{3} \frac{\hat{\rho}_k^2}{n-k} \approx 0.02644+0.00019+0.00227 \approx 0.02890 \]Step 3: Compute the Ljung-Box statistic
\[ Q = n(n+2)\sum_{k=1}^{3}\frac{\hat{\rho}_k^2}{n-k} = 12(14)(0.02890) \approx 4.855 \]Step 4: Interpret
With \( h = 3 \) lags and no fitted model, degrees of freedom \( = 3 \), and the 5% critical value for \( \chi^2_3 \) is \( 7.81 \). Since \( Q \approx 4.855 \) is below \( 7.81 \) (p-value \( \approx 0.183 \)), we fail to reject \( H_0 \). This series is consistent with white noise across the first three lags jointly-even though the lag-1 autocorrelation alone looked moderately large, it wasn't enough, combined with the much smaller lag-2 and lag-3 values, to clear the joint significance threshold.
Worked Example 2: A Series with Leftover Autocorrelation
Now suppose a second series over 12 periods alternates in a smoother, more repeating pattern-values tend to echo what happened one and two periods earlier:
| Time \( t \) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| \( y_t \) | 3.0 | 2.6 | 2.9 | 2.4 | 2.7 | 2.2 | 2.5 | 2.0 | 2.3 | 1.8 | 2.1 | 1.6 |
We again test \( h = 3 \) lags jointly. First, \( \bar{y} \approx 2.342 \).
Step 1: Compute the sample autocorrelations at lags 1, 2, and 3
\[ \sum_{t=1}^{12}(y_t-\bar{y})^2 \approx 2.009 \] \[ \hat{\rho}_1 \approx \frac{0.587}{2.009} \approx 0.292 \qquad \hat{\rho}_2 \approx \frac{1.270}{2.009} \approx 0.632 \qquad \hat{\rho}_3 \approx \frac{-0.028}{2.009} \approx -0.014 \]The lag-2 autocorrelation stands out here-large and positive at \( \approx 0.632 \)-consistent with the gently oscillating, roughly two-period rhythm visible in the raw data, where each value more strongly echoes the value from two periods back than the value immediately before it.
Step 2: Square each autocorrelation and weight by \( 1/(n-k) \)
\[ \frac{\hat{\rho}_1^2}{n-1} = \frac{0.0855}{11} \approx 0.00777 \qquad \frac{\hat{\rho}_2^2}{n-2} = \frac{0.3995}{10} \approx 0.03995 \] \[ \frac{\hat{\rho}_3^2}{n-3} = \frac{0.0002}{9} \approx 0.00002 \] \[ \sum_{k=1}^{3} \frac{\hat{\rho}_k^2}{n-k} \approx 0.00777+0.03995+0.00002 \approx 0.04774 \]Step 3: Compute the Ljung-Box statistic
\[ Q = n(n+2)\sum_{k=1}^{3}\frac{\hat{\rho}_k^2}{n-k} = 12(14)(0.04774) \approx 8.020 \]Step 4: Interpret
With \( h = 3 \) lags and no fitted model, degrees of freedom \( = 3 \), and \( Q \approx 8.020 \) is just past the 5% critical value (\( 7.81 \)) for \( \chi^2_3 \), though still below the 1% critical value (\( 11.34 \)). We reject \( H_0 \) at the 5% level (p-value \( \approx 0.046 \)): this series shows statistically significant autocorrelation across the first three lags and is not consistent with white noise, driven mainly by the strong lag-2 relationship. Before modeling this series, an ARIMA specification that can capture that two-period rhythm is worth investigating.
Python Example
You can compute the Ljung-Box statistic across any set of lags in one line using statsmodels.stats.diagnostic.acorr_ljungbox:
import numpy as np
from statsmodels.stats.diagnostic import acorr_ljungbox
# Worked Example 2 data: a series with leftover autocorrelation
y = np.array([3.0, 2.6, 2.9, 2.4, 2.7, 2.2, 2.5, 2.0, 2.3, 1.8, 2.1, 1.6])
result = acorr_ljungbox(y, lags=[3], return_df=True)
print(result)
Output (matching the hand-worked case exactly):
lb_stat lb_pvalue
3 8.020152 0.045597
This matches Worked Example 2 exactly-a p-value just under \( 0.05 \) confirms the result is significant at the conventional 5% level, but only just, which is a useful reminder that "reject \( H_0 \)" and "overwhelming evidence" are not the same thing; a borderline p-value like this one is often worth confirming with a slightly different lag count before treating it as conclusive.
Checking ARIMA Residuals with the Correct Degrees of Freedom
import statsmodels.api as sm
from statsmodels.tsa.arima.model import ARIMA
# Fit a simple ARMA(1,1) model to Worked Example 2's data
model = ARIMA(y, order=(1, 0, 1)).fit()
# Test the residuals across several lag counts, adjusting degrees of freedom
lb_test = acorr_ljungbox(model.resid, lags=[5, 10], model_df=2, return_df=True)
print(lb_test)
# model_df=2 subtracts the 1 AR + 1 MA parameter from the usual h degrees of freedom
The model_df argument is the practical way to apply the degrees-of-freedom correction from Degrees of Freedom and Critical Values-forgetting it makes the test slightly too conservative (too hard to reject \( H_0 \)) when checking residuals from an already-fitted ARMA model.
How to Interpret Results
Because the Ljung-Box statistic follows a standard chi-square distribution, interpretation is straightforward compared to the bounded, sometimes-inconclusive Durbin-Watson statistic-there is always a definite p-value to read.
| Result | Interpretation |
|---|---|
| \( Q \) below the chi-square critical value (large p-value) | Fail to reject \( H_0 \)-consistent with white noise, no significant autocorrelation |
| \( Q \) above the chi-square critical value (small p-value) | Reject \( H_0 \)-significant autocorrelation exists among the tested lags |
| Significant on raw data, before modeling | Series has structure worth modeling-consider ARIMA or similar |
| Significant on residuals, after modeling | Model hasn't captured all the structure-see What to Do If Autocorrelation Is Detected |
Checking Assumptions in Practice
- Plot the autocorrelation function (ACF) alongside the test: the Ljung-Box Test gives a single joint verdict, but an ACF plot shows exactly which lags are driving a rejection-essential context the \( Q \) statistic alone doesn't provide.
- Adjust degrees of freedom for fitted models: when testing residuals from an ARMA(\( p, q \)) model, subtract \( p + q \) from \( h \)-most software supports this directly (e.g.,
model_dfinstatsmodels), and skipping it makes the test too conservative. - Test multiple values of \( h \): checking \( h = 10 \), \( 15 \), and \( 20 \) together, rather than relying on a single lag count, guards against a result that only holds for one arbitrary choice of \( h \).
- Include seasonal lags where relevant: for monthly data with suspected annual seasonality, make sure \( h \) is large enough to include lag 12 (or the appropriate seasonal period), or the test can miss seasonal autocorrelation entirely.
- Cross-check with a residual plot over time: a simple line plot often shows visually what the statistic suggests-irregular bouncing (as in Worked Example 1) or a smooth, repeating pattern (as in Worked Example 2).
What to Do If Autocorrelation Is Detected
- On raw data before modeling: a significant Ljung-Box result is expected and useful-it confirms the series has structure worth capturing, and the ACF/PACF plots can help choose reasonable starting AR and MA orders.
- On residuals after fitting ARIMA: increase the AR order \( p \), the MA order \( q \), or both, and re-check; the goal is to keep adjusting the model order until residual Ljung-Box tests consistently fail to reject \( H_0 \) across several values of \( h \).
- Check for a missed seasonal component: if significant autocorrelation clusters around a specific periodic lag (e.g., lag 12 for monthly data), a SARIMA model with a seasonal term is usually the fix rather than simply raising the non-seasonal AR/MA order.
- Verify the series was properly differenced: a residual unit root, which the ADF Test or KPSS Test can help confirm, often manifests as persistent Ljung-Box rejections that no amount of extra AR/MA terms fully resolves.
- Consider a structural break or omitted variable: for regression residuals specifically, leftover autocorrelation across many lags can signal a missing predictor or a shift in the underlying relationship, not just an autocorrelation problem to patch mechanically.
Advantages
- Tests multiple lags jointly in a single statistic, rather than requiring separate tests (and separate false-positive risk) at each lag individually.
- Always produces a definite chi-square-based p-value-no inconclusive zone, unlike the Durbin-Watson Test.
- Correctly adjustable for fitted model parameters, making it the standard, statistically sound choice for validating ARIMA and similar model residuals.
- The small-sample correction factor gives it a notably better chi-square approximation than the older Box-Pierce statistic, especially for shorter series.
- Widely implemented and reported automatically by standard software, including one-line support in
statsmodelsandR'sstatspackage.
Limitations
- A significant result tells you that autocorrelation exists among the tested lags, but not automatically which lag is responsible-an ACF plot is needed to localize the source.
- Choice of \( h \) matters and is somewhat subjective-too few lags can miss longer-range structure, too many relative to \( n \) can weaken the chi-square approximation.
- Assumes the degrees of freedom are correctly adjusted for any fitted model parameters; forgetting this adjustment on residual diagnostics produces an overly conservative test.
- Like most joint tests, has relatively modest power to detect a single, isolated autocorrelation spike buried among many near-zero lags, since the effect gets diluted across the sum.
- Assumes a linear notion of dependence through autocorrelation-nonlinear serial dependence (e.g., volatility clustering) can pass a Ljung-Box Test on the raw series while still being present in squared or absolute values.
When NOT to Use It
- Durbin-Watson Test: use instead when you specifically want a quick, single-lag check reported automatically alongside an OLS regression summary, and don't need to test multiple lags jointly.
- Breusch-Godfrey Test: use instead when testing regression residuals that include a lagged dependent variable as a predictor, since it remains valid in that case in the same way Ljung-Box generally does, but is more standard in that specific regression context.
- ARCH/Lagrange Multiplier Test: use instead when checking for autocorrelation in squared residuals (volatility clustering) rather than in the residuals themselves-the plain Ljung-Box Test on raw residuals can miss this entirely.
- Cross-sectional data with no time ordering: don't use the Ljung-Box Test at all if your observations have no meaningful sequence-there is no "lag" for the test to work with.
Ljung-Box vs Box-Pierce vs Durbin-Watson vs Breusch-Godfrey
17.1 Ljung-Box Test vs Box-Pierce Test
| Aspect | Box-Pierce Test | Ljung-Box Test |
|---|---|---|
| Formula | \( Q = n\sum_{k=1}^{h}\hat{\rho}_k^2 \) | \( Q = n(n+2)\sum_{k=1}^{h}\hat{\rho}_k^2/(n-k) \) |
| Small-sample accuracy | Poor-chi-square approximation is weak for small \( n \) | Good-the correction factor markedly improves the approximation |
| Null hypothesis | Same joint white-noise null | Same joint white-noise null |
| Practical use today | Rare-mostly of historical interest | Standard choice in essentially all modern software |
17.2 Ljung-Box Test vs Durbin-Watson Test
| Aspect | Ljung-Box Test | Durbin-Watson Test |
|---|---|---|
| Lags tested | Jointly tests multiple lags at once | Lag 1 only |
| Distribution | Chi-square with an exact p-value | Bounded 0-4 statistic with published bounds and an inconclusive zone |
| Typical use case | Time series or ARIMA model residual diagnostics | Diagnostic on regression residuals |
| Handles fitted-model degrees of freedom | Yes, via \( h - p - q \) | Not applicable-tests only lag 1 regardless of model |
17.3 Ljung-Box Test vs Breusch-Godfrey Test
| Aspect | Ljung-Box Test | Breusch-Godfrey Test |
|---|---|---|
| Underlying framework | Direct combination of sample autocorrelations | Lagrange Multiplier (Score) Test on an auxiliary regression |
| Lagged dependent variable as regressor | Not typically the context-used on series/residuals directly | Remains valid, a key advantage in that regression setting |
| Most common context | ARIMA/time-series residual diagnostics | Regression residual diagnostics |
| Output | Chi-square statistic and p-value | Chi-square or F statistic and p-value |
Common Misconceptions
- "A large Ljung-Box statistic tells you exactly which lag is the problem." Not directly- it is a joint test across all tested lags; use an ACF plot alongside the test to localize which specific lags are driving the rejection, as discussed in Checking Assumptions in Practice.
- "Failing to reject \( H_0 \) proves the residuals are exactly white noise." Failing to reject is evidence consistent with white noise over the tested lags, not proof-untested lags, or nonlinear dependence like volatility clustering, could still be present; see Limitations.
- "You don't need to adjust degrees of freedom when testing model residuals." You do- using \( h \) instead of \( h - p - q \) after fitting an ARMA(\( p, q \)) model makes the test too conservative, as covered in Degrees of Freedom and Critical Values.
- "The Ljung-Box Test and Box-Pierce Test always give the same answer." They test the same null hypothesis but use different formulas-in small samples they can disagree, which is exactly why Ljung-Box replaced Box-Pierce as the standard in modern software.
- "More lags tested is always more rigorous." Not necessarily-choosing \( h \) far larger than needed relative to \( n \) dilutes the test's power against isolated autocorrelation spikes and can weaken the chi-square approximation itself.
Interview Questions
- Explain what a "portmanteau test" is, and why the Ljung-Box Test is described that way.
- Derive, in words, why the Ljung-Box statistic uses \( \frac{1}{n-k} \) weighting on each squared autocorrelation term rather than a constant weight.
- Why does the degrees of freedom for the Ljung-Box Test change from \( h \) to \( h - p - q \) when testing residuals from a fitted ARMA(\( p, q \)) model?
- What is the difference between the Box-Pierce statistic and the Ljung-Box statistic, and why did Ljung-Box become the standard?
- If a Ljung-Box Test on ARIMA residuals rejects \( H_0 \), what concrete modeling steps would you take next?
- Explain why the Ljung-Box Test is generally preferred over running a separate Durbin-Watson Test at each individual lag.
- How would you choose the number of lags \( h \) to test for a monthly series with suspected annual seasonality?
- Why can a series pass a Ljung-Box Test on its raw residuals but still show volatility clustering, and what test would you reach for instead to detect that?
- What does it mean, practically, that the Ljung-Box statistic follows a chi-square distribution rather than the bounded 0-to-4 scale of the Durbin-Watson statistic?
- Describe how you would use the Ljung-Box Test as part of a complete ARIMA model-building workflow, from initial series screening to final residual validation.
Frequently Asked Questions
- The Ljung-Box Test checks whether a time series, or the residuals left over from a fitted model such as ARIMA, are autocorrelated across a group of lags jointly, rather than checking just one lag at a time. It is the standard final diagnostic after fitting a time series model, confirming that the residuals behave like white noise with no leftover predictable structure.
- The statistic is Q = n(n+2) * sum from k=1 to h of [rho_k^2 / (n-k)], where n is the number of observations, h is the number of lags tested, and rho_k is the sample autocorrelation coefficient at lag k. Larger values of rho_k, especially at small k, inflate Q the most because of the 1/(n-k) weighting.
- Compare Q against the chi-square distribution with h degrees of freedom (or h minus the number of fitted AR and MA parameters, if testing model residuals). If Q exceeds the critical value, or equivalently the p-value is small, reject the null hypothesis and conclude significant autocorrelation exists somewhere among the tested lags. If Q is below the critical value, fail to reject the null-the data is consistent with white noise.
- The null hypothesis (H0) is that the autocorrelations at lags 1 through h are all simultaneously zero-the series behaves like white noise over that range of lags. The alternative hypothesis (H1) is that at least one of those h autocorrelations is nonzero, meaning some detectable serial structure remains.
- Both test the same joint null hypothesis using the same sample autocorrelations, but the Box-Pierce statistic uses a simpler asymptotic approximation, Q = n * sum of rho_k^2, while the Ljung-Box statistic applies a small-sample correction factor, n(n+2)/(n-k), to each term. The Ljung-Box version has noticeably better performance in small samples and is used almost universally in modern software as a result.
- A common rule of thumb for raw (non-seasonal) series is to test around ln(n) lags, or a small set like h = 10 or h = 20 for moderate sample sizes; for ARIMA residual diagnostics, testing multiple values of h (e.g., 10, 15, 20) and confirming the result is stable across them is standard practice. For seasonal data, include at least one full seasonal cycle among the lags tested.
- A significant result on model residuals means the fitted ARIMA order has not fully captured the series' structure-try increasing the autoregressive (p) or moving-average (q) order, check for a missed seasonal component, or verify the series was properly differenced for stationarity before fitting, since a residual unit root can also produce this symptom.
Key Takeaways
- The Ljung-Box Test checks whether a time series or a set of model residuals is autocorrelated across several lags jointly, rather than checking one lag at a time.
- The statistic \( Q = n(n+2)\sum_{k=1}^{h}\hat{\rho}_k^2/(n-k) \) squares and weights the sample autocorrelations at each tested lag, then sums them into one number.
- Under \( H_0 \), \( Q \) follows a chi-square distribution with \( h \) degrees of freedom (or \( h - p - q \) when testing residuals from a fitted ARMA(\( p, q \)) model)-always producing a definite p-value, with no inconclusive zone.
- Failing to reject \( H_0 \) is the reassuring outcome for ARIMA residual diagnostics-it means the model has captured the series' predictable structure, leaving behind something close to white noise.
- In Python,
statsmodels.stats.diagnostic.acorr_ljungboxruns the test in one line, including support for correctly adjusting degrees of freedom viamodel_df. - If autocorrelation is detected, the next step depends on context: add AR/MA terms or seasonal components for model residuals, or treat it as a modeling signal for raw data, as outlined in What to Do If Autocorrelation Is Detected.
- Because a significant result only tells you that autocorrelation exists, not where, pairing the test with an ACF plot (and cross-checking against Durbin-Watson or Breusch-Godfrey where relevant) gives a complete and actionable diagnosis.
The Ljung-Box Test earns its place as the standard closing diagnostic in time series modeling because the question it answers-"is there any leftover structure my model missed?"-is exactly the question every analyst needs answered before trusting a forecast. Checking lags one at a time is slow, inflates the false positive rate, and can miss patterns that only emerge when several lags are considered together; bundling them into a single chi-square statistic solves both problems at once, which is why the test appears in nearly every ARIMA workflow, textbook, and software package's default output.
The two worked examples above show both directions this can go-a genuinely irregular series whose squared autocorrelations stay small across every tested lag, and a smoothly repeating series whose autocorrelations are large enough to reject white noise decisively. Reporting the Ljung-Box statistic alongside an ACF plot, correctly adjusting degrees of freedom for any fitted model parameters, and testing more than one value of \( h \) gives a complete and reliable picture of residual quality that the statistic alone, at a single lag count, cannot always provide.