Ljung-Box Autocorrelation Test Calculator

Is the series remembering its own past?

  • ACF and PACF bands
  • Ljung-Box p-value
  • Residual df fix
A small rounded clay table holding a row of soft clay bars of different heights with a flat clay band lying across them and a few loose rounded beads.

The guide

A correlogram shows ten verdicts; Ljung-Box gives one, and fitted models change its degrees of freedom

Autocorrelation asks whether a series remembers its own past; one summary statistic over many lags beats reading single bars, but a pass proves nothing about independence.

  • 52.838 Ljung-Box Q, 10 lags, simulated AR(1) series of 100 values
  • 0.1648 p value for that series' AR(1) residuals, 9 degrees of freedom
  • 0.9721 lag 1 autocorrelation if the mean is not removed
  • 6.7% against 3.3% white noise rejected by Ljung-Box and Box-Pierce, n = 50, 10 lags
Read the full guide Fold the guide 7 min · 16 sources

Take a hundred numbers recorded one after another. The first few are negative, the next stretch drifts up, and a run near the end sags again. Does each value lean on the one before it, or is the wandering just what chance looks like? That question has a name, serial correlation, and a short answer: compute how strongly the series agrees with a copy of itself shifted by a few steps.

What an autocorrelation measures

Subtract the mean from every value. Multiply each deviation by the deviation k steps later, add the products, and divide by the sum of the squared deviations. The result, written r_k, lies between -1 and +1. A value near +1 at lag 1 says that a high reading tends to be followed by another high one; near -1 says the readings alternate; near zero says the past tells you nothing about the next step in a straight-line sense.

Two details decide whether your number matches someone else’s. The first is the mean. Skip the subtraction and a series that sits near 5 looks almost perfectly self-similar, because every product is large and positive. The worked example in the tool is a simulated series of 100 values whose true lag 1 autocorrelation is 0.5266 once the mean is removed. Shift it up by 5, forget the subtraction, and r_1 jumps to 0.9721. Nothing about the dynamics changed.

The second detail is the divisor. The usual definition divides every autocovariance by N, the whole sample size. An alternative divides the lag k sum by N - k, which has less bias but loses the tidy properties of the first form. The NIST handbook names both and says the 1/N form is the one most used in the statistics literature, and the tool lets you switch. For the same series the tenth autocorrelation moves from -0.1125 to -0.1250. Mixing the forms, say a 1/(N - k) correlogram under bands built for 1/N, quietly breaks the comparison.

Reading a correlogram, and why one bar is not a verdict

Plot r_k against k and you have a correlogram. If the series is random, the bars should hover near zero; if not, one or more will be significantly different from zero. For white noise, a series of uncorrelated values with zero mean, the handbook’s rule of thumb is a band of plus or minus z over the square root of N, where z is 1.96 for 95 percent. With 100 values that band is plus or minus 0.1960.

Here is the catch. A 95 percent band means that a perfectly random series still puts about one bar in twenty outside it. Test ten lags and you should expect half a bar to poke out by luck alone. A single bar at 0.21 against a band of 0.196 is not a discovery; it is the kind of thing chance hands out regularly.

The second band in the handbook is for judging moving-average models. It plots plus or minus z times the square root of (1 + 2 times the sum of the squared r_i up to lag k) divided by N, so it widens as the lag grows. At lag 5 of the example it reads 0.2740 against the flat 0.1960. Which band you use depends on the question: the flat one asks whether the series looks random, the widening one asks whether a moving-average model is plausible.

The partial autocorrelation chart answers a different question. The partial autocorrelation at lag k is what remains of the link between a value and the one k steps back after lags 1 to k - 1 are accounted for. For an autoregressive process of order p it is zero beyond lag p. In the example the partial values at lags 1 to 3 are 0.5266, -0.0554 and 0.2019 against a band of about 0.200, so only lag 1 stands out, which is exactly the signature of an AR(1) series. The tool computes it with the Durbin-Levinson recursion, a procedure built to solve the underlying equations quickly.

One statistic over many lags

Looking at ten bars invites cherry picking. A portmanteau test replaces the ten judgments with one. Ljung and Box’s statistic is Q = n(n + 2) times the sum, over lags 1 to h, of r_k squared divided by (n - k). Under the hypothesis that the data are uncorrelated, Q follows a chi-squared distribution with h degrees of freedom, the distribution of a sum of h squared independent standard normals. Reject when Q is above the 95 percent point of that distribution, which for 10 degrees of freedom is 18.307.

For the 100-value example Q at 10 lags is 52.838, a p value of 7.98e-08, so the series is clearly not white. For a second simulated series of 100 uncorrelated normals, Q is 14.642 and the p value 0.1457: not rejected, and its largest single lag, 0.2128, sat just outside the band without meaning anything. Ten lags, one decision.

The weights n(n + 2) / (n - k) are what separate this statistic from its older sibling. Box and Pierce proposed Q = n times the sum of r_k squared, without the weights. On the example it gives 50.569, with p = 2.10e-07.

Both reject here, so the distinction is invisible. It matters at modest sample sizes. Simulating 400,000 white-noise series of length 50 with 10 lags, at a nominal 5 percent, the Ljung-Box statistic rejected about 6.7% against 3.3% for Box-Pierce. At length 20, Ljung-Box rejected about 8.0 percent and Box-Pierce about 1.3 percent. Those rates are approximate figures from this tool’s own checks, and the tool reruns a smaller simulation with a seed you can set.

The encyclopedia entry says the Ljung-Box distribution is closer to chi-squared at all sample sizes, but flags the sentence as lacking a citation. The measurement agrees at length 20, where Ljung-Box is off by three points and Box-Pierce by nearly four. At length 50 it is a tie: Ljung-Box over-rejects slightly where Box-Pierce under-rejects. Report the numbers, not the slogan.

Residuals lose degrees of freedom

Most real uses of the test are not on raw data. After fitting a model, say an autoregression, you test its residuals, the leftovers after the model has taken what it can. A residual is an observable estimate of an unobservable error, and a good model should leave residuals that behave like white noise. Testing the raw series instead is a category error: a series full of structure will fail, and the failure tells you nothing about the model.

When the residuals come from a model with p autoregressive and q moving-average parameters, the sources agree that the degrees of freedom must reflect the parameter estimation. For an ARIMA(p,0,q) model the reference becomes chi-squared with h - p - q degrees. The test statistic is unchanged; only the comparison curve moves. Forget the adjustment and the p value comes out too large, which makes a poor model look better than it is.

The tool works one case end to end. A one-parameter autoregression with phi taken as r_1 leaves 99 residuals. Their Q at 10 lags is 12.951. With the right 9 degrees of freedom the p value is 0.1648; with the mistaken 10 it is 0.2264. Either way the answer is that the model left white noise behind, but at the margin a 0.05 threshold can sit between the two values. The estimate here is a moment estimate for illustration, not a fitted model with proper likelihood, and the page says so.

One more wrong turn is cutting the degrees of freedom to a number that matches nothing. Using 5 for the plain 10-lag test gives p = 3.63e-10 in place of 7.98e-08. Both reject, but the quoted evidence is off by two orders of magnitude, which matters when someone reads the number out of context.

What a pass does not prove

A non-rejection is the absence of evidence at lags 1 to h, not proof of independence. The test looks at autocorrelations only, and the NIST handbook is plain that uncorrelated does not necessarily mean random: data without significant autocorrelation can still be non-random in other ways. A dependence at a lag beyond h is also invisible to it. Power is modest at short lengths too. For an AR(1) series with coefficient 0.3 and 100 values, the Ljung-Box test at 10 lags catches the dependence only about half the time, roughly 0.49 in the checks behind this page.

Choice of h matters too, because Q adds h terms and one large lag is averaged in with the rest. The tool allows up to 40 lags or n - 1, whichever is smaller, and shows every lag’s contribution so you can see where Q comes from.

The Durbin-Watson statistic, aimed at lag 1 of regression residuals, is a cousin with a narrower target. The Ljung-Box test spreads its attention over several lags and so suits a model check, where the interesting failure may sit anywhere in a short span.

Finally, the test says nothing about trends, seasonal patterns, unit roots or changing variance, and the page does not fit models, difference a series or forecast. Treat a rejected test as a prompt to look at the plots, and an accepted one as one input among several. Used that way, a single statistic is a better summary of a correlogram than a glance at the tallest bar.

Sources

Every source consulted for this guide. Links checked November 22, 2026.

  1. Ljung-Box test Wikipedia
  2. 1.3.3.1. Autocorrelation Plot NIST/SEMATECH e-Handbook of Statistical Methods
  3. 6.4.4.6.3. Partial Autocorrelation Plot NIST/SEMATECH e-Handbook of Statistical Methods
  4. 6.4.4.8.1. Box-Ljung Test NIST/SEMATECH e-Handbook of Statistical Methods
  5. 6.4.4. Univariate Time Series Models NIST/SEMATECH e-Handbook of Statistical Methods
  6. Autocorrelation Wikipedia
  7. Partial autocorrelation function Wikipedia
  8. Box-Jenkins method Wikipedia
  9. Correlogram Wikipedia
  10. White noise Wikipedia
  11. Autoregressive model Wikipedia
  12. Chi-squared distribution Wikipedia
  13. Levinson recursion Wikipedia
  14. Durbin-Watson statistic Wikipedia
  15. Errors and residuals Wikipedia
  16. scipy.stats.chi2 SciPy documentation