Skip to content

DPredict · Guide test 194

Durbin–Watson test

Tests first order serial correlation in regression errors under its applicable model assumptions.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with one of these methods:

In Linear regression (OLS), choose Residual diagnostics = Full battery.

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 · Durbin–Watson test

40
0.040
0.50

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

0.0
0.050

Your data

Simulated with a known ρ

If H₀ were true

d = 0.81
  1. The reference: how d varies across samples when H₀ is true (Null Atlas: quantiles simulated with 200,000 replicates).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = < .001).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

d=∑t=2n(et−et−1)2∑t=1net2≈2(1−ρ^)d = \dfrac{\sum_{t=2}^{n}(e_t - e_{t-1})^2}{\sum_{t=1}^{n} e_t^2} \approx 2(1 - \hat\rho)

With your numbers

d=0.81,ρ^≈0.59,n=40d = 0.81,\qquad \hat\rho \approx 0.59,\qquad n = 40
ete_t
the residual at step t from y on a constant and t
dd
near 2 with no autocorrelation, near 0 with strong positive autocorrelation
ρ^\hat\rho
the implied lag-1 autocorrelation of the residuals

The verdict

d = 0.81, p < .001

p ≤ α = .050: reject H₀

The null of d depends on the regressors, which is why old tables gave only bounds. This design regresses on a constant and time, and the Null Atlas holds that exact null. Small d is compatible with positive autocorrelation, which makes standard errors too small.

Statistic
0.812
p-value
0.0000
Durbin–Watson test on 40 (x, y) pairs: d = 0.81, p < .001. The p-value is below α = .050, so the data are hard to reconcile with H0.

Durbin–Watson test on 40 (x, y) pairs: d = 0.81, p < .001. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: no first-order serial correlation in linear-model errors.

Assumptions

Correct regression with an intercept and suitable exogenous predictors; classical tables assume normal errors and exclude lagged dependent variables.

Formula

d=∑t=2n(et−et−1)2∑t=1net2d=\frac{\sum_{t=2}^{n}(e_t-e_{t-1})^2}{\sum_{t=1}^{n}e_t^2}

Parameters and notation

d is roughly 2(1−r̂1): values near zero suggest positive correlation and near four negative correlation.

Structure of the method

Fit the regression, retain time-ordered residuals, and divide the sum of squared consecutive residual differences by total residual sum of squares. The statistic is bounded between zero and four, but its exact null law depends on the regression design. The rough 2(1−r1) relationship is a diagnostic approximation.

Estimated and nuisance parameters

Regression coefficients and residuals are fitted; the overall error scale cancels. The design matrix influences the relevant eigenvalues or tabulated bounds used for calibration. A statistic-only calculation, such as the displayed Python function, does not by itself return a p-value.

Settings and design choices

Specify ordering, positive/negative/two-sided alternative, exact versus approximate algorithm, numerical tolerance, and whether the model satisfies the usual exogenous-regressor restrictions. A lagged dependent variable changes the null behavior and generally calls for another appropriate serial-correlation procedure.

Fitting and adaptation

Refit the regression whenever predictors or samples change and preserve chronological ordering. Use design-specific exact computation or justified bounds/approximation; arbitrary fixed cutoffs around two are not a calibrated test. If null simulation is used, reproduce regression fitting and the same time design.

Sensitivity and robustness

Irregular observation spacing, missing intervals, influential residuals, heteroskedasticity, and structural change can complicate interpretation. The test focuses on first-order structure and may miss other dependence. A bounds result can be inconclusive; report that honestly. Selecting a model until d is near two is not sufficient evidence of white-noise innovations or a sound forecasting specification.

How to use and calibrate

Use design-specific exact calculations or Durbin–Watson bounds; a result can be inconclusive between bounds.

Example application

Check residual serial correlation in an ordered regression.

Interpretation and pitfalls

Watch out

Do not apply its usual tables to models containing lagged outcomes; Breusch–Godfrey is more flexible.

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 reports the statistic only; the R routine also calibrates a p-value for this regression design. Do not interpret distance from 2 using a generic universal p-value.

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)
import statsmodels.api as sm
n=100
x=rng.normal(size=n); z=rng.normal(size=n); y=1+.5*x+.3*z+rng.normal(size=n)
X=sm.add_constant(np.column_stack([x,z])); fit=sm.OLS(y,X).fit(); reduced=sm.OLS(y,X[:,:2]).fit()
from statsmodels.stats.stattools import durbin_watson
print("Durbin-Watson statistic:",durbin_watson(fit.resid))
Output
Durbin-Watson statistic: 1.5291692866118347
R

Validated — Executed successfully in the validation environment · needs lmtest

set.seed(2026)
n <- 100; x <- rnorm(n); z <- rnorm(n); y <- 1+.5*x+.3*z+rnorm(n)
d <- data.frame(y,x,z); fit <- lm(y~x+z,data=d); reduced <- lm(y~x,data=d)
print(lmtest::dwtest(fit,alternative="two.sided"))
Output
	Durbin-Watson test

data:  fit
DW = 2.1102, p-value = 0.5648
alternative hypothesis: true autocorrelation is not 0

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