How this calculation works
Residual autocorrelation diagnostics check serial structure left after fitting a model. This page combines centered residual ACF values, the Durbin–Watson ratio and a Ljung–Box portmanteau statistic. The model degrees-of-freedom adjustment is explicit, and only the Ljung–Box approximation receives a chi-square p-value.
DW=Σ(e_t−e_(t−1))²/Σe_t²; Q=n(n+2)Σ_(k=1)^h r_k²/(n−k).A worked example
Alternating residuals 1,−1,1,−1,1,−1 have five adjacent differences of magnitude two. Their Durbin–Watson statistic is 20/6=10/3. The lag-one centered autocorrelation is −5/6, making the alternating negative dependence visible without reading a p-value.
Good to know
Ordered residuals, 5–500 observations, positive residual spread and valid lag adjustment. Ljung–Box p-values are asymptotic. No generic Durbin–Watson p-value, independent-test guarantee, automatic model repair or permission to reorder residuals.
Reference: Statsmodels residual diagnostic functions (opens in a new tab) ↗
Keep residuals in their original order
Residuals must be listed in the sequence in which their observations occurred. Sorting by size would create artificial adjacent dependence and invalidate both the autocorrelation and portmanteau calculations. The calculator cannot infer the correct temporal order from the numbers themselves, so it preserves the supplied order without modification.
The residual ACF subtracts the residual mean and uses a common full-sample denominator at every lag. Other software can use lag-adjusted denominators, which produce different finite-sample values. The declared convention matches the direct diagnostic calculation rather than presenting several incompatible ACF normalizations under the same output label.
The Discrete cross-correlation calculator with lag covers the nearby task. Diagnostic test statistics and adjusted p-values differ from a generic correlation sum; combine diagnostics on one page.
The two statistics use different centering rules
Durbin–Watson compares adjacent differences with the uncentered residual sum of squares. Values near zero suggest positive serial association and values near four suggest alternating association, but those descriptions are not a universal decision rule. The test distribution depends on the fitted regression design and assumptions not supplied on this page.
Ljung–Box pools squared ACF contributions over the selected lag range, with finite-sample weights in its statistic. Its approximate chi-square degrees of freedom subtract the supplied model adjustment. A lag not exceeding that adjustment is rejected, because displaying a p-value with zero or negative degrees of freedom would be meaningless. The adjustment is entered explicitly rather than guessed from residual values, which do not identify the model that produced them.
Approximate rejection does not select a repaired model
A small approximate p-value is evidence against the diagnostic’s zero-autocorrelation null under its conditions. It does not identify which autoregressive model to fit next, establish causation or prove that all remaining error assumptions hold. Searching many lag choices after seeing results also changes the interpretation of a nominal threshold.
The alternating default independently checks the adjacent-difference numerator and the first ACF value. Broader tests compare a separate diagnostic implementation under matched centering and degree adjustments. The page combines these related outputs to support inspection without creating multiple near-identical residual-diagnostic pages with inconsistent assumptions.
Read the two diagnostics as different summaries
Durbin–Watson is Σ(et−et−1)²/Σet² using the entered residuals. It is sensitive to successive differences and is often interpreted approximately around 2 for uncorrelated errors, below 2 for positive serial association and above 2 for negative association. That informal guide is not a universal significance test, especially with lagged dependent variables or unusual designs.
Ljung–Box combines centered residual autocorrelations through the chosen lag: Q=n(n+2)Σrk²/(n−k). It asks whether a collection of correlations is collectively larger than expected under a specified white-noise null. A positive or negative correlation contributes positively after squaring, so the Q statistic alone does not reveal the direction or lag pattern.
Work out the alternating-residual example
For 1,−1,1,−1,1,−1, the residual mean is zero and Σe²=6. There are five successive squared differences, each equal to 4, giving Durbin–Watson 20/6=10/3. At lag one, the centered cross-product is −5, so r1=−5/6; at lag two it is 4, giving r2=2/3.
With lag 2, Q=6×8[(25/36)/5+(4/9)/4]=12. If fitted-model adjustment is zero, the reference degrees of freedom are 2 and the chi-square tail is exp(−6)≈0.002479. This tiny series is useful for checking arithmetic, but the asymptotic p-value is not a reliable small-sample certification of a real forecasting model.
Choose the model degrees-of-freedom adjustment consciously
The adjustment reduces the Ljung–Box reference degrees of freedom from the selected lag h to h−model_df. It does not subtract observations from each autocorrelation denominator. For an ordinary fitted ARMA model, p+q is a common adjustment; other models require their own justification. The form cannot infer how residuals were produced.
The lag must exceed that adjustment. Choosing an invalid or tiny remaining reference degree of freedom is not repaired by entering zero merely to obtain a p-value. Select lags that correspond to the time scale you care about and that remain sensible for the available sample size. Testing many lags and reporting only the smallest p-value changes the interpretation.
Investigate residual problems rather than chasing a p-value
Keep residuals in their original time order. A mean offset, outlier, missed seasonality or structural break can affect the pattern, and neither diagnostic tells you which mechanism caused it. Inspect the individual ACF rows and a residual plot alongside the test. Reordering by fitted value or residual size invalidates the serial-correlation question.
A nonsignificant result does not prove independent, normally distributed or constant-variance errors. Squared residuals can remain dependent even when ordinary residual autocorrelations are small. Conversely, a very large sample may detect small correlations with little practical effect. Decide whether the remaining dependence matters for forecast intervals or inference, then revise the actual model rather than deleting inconvenient residuals.
A few questions, answered
Why is there no Durbin–Watson p-value?
Its null distribution depends on regression design information not supplied here.
What is the Ljung–Box adjustment?
The entered fitted-model degrees of freedom are subtracted from the selected lag count.
Should I sort residuals before entering them?
No. Their original observation order defines the serial dependence. Sorting creates an artificial sequence and invalidates the intended diagnostic.
Does a high Ljung–Box p-value prove independent errors?
No. It concerns selected autocorrelations under an approximate null. Nonlinear dependence, changing variance and other departures can remain.