Skip to content

DPredict · Guide test 215

Diebold–Mariano test

Tests equal predictive accuracy through the mean difference between forecast losses.

Runs in CodeSplashPython (calls R) example not runR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

  • Forecast accuracy comparison — Is one set of forecasts really more accurate than another, or do they predict the direction of change better than chance?

In Forecast accuracy comparison, choose Test = Diebold–Mariano (HLN-corrected).

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 · Diebold–Mariano test

90
1.25

1: both models are equally accurate.

0.60
0.050

Alternative

Your data

Simulated with known truth
—— model 1 error– – model 2 error—— loss differential d = e₁² − e₂²

The test asks whether the mean loss differential (gray line, d̄ = −0.832) is far from zero.

If H₀ were true

DM = −2.52
  1. The reference: how DM varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .013).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

dt=e1t2−e2t2,DM=dˉVar⁡^(dˉ)n+1−2h+h(h−1)/nnd_t = e_{1t}^2 - e_{2t}^2,\qquad DM = \dfrac{\bar d}{\sqrt{\widehat{\operatorname{Var}}(\bar d)}}\sqrt{\tfrac{n+1-2h+h(h-1)/n}{n}}

With your numbers

dˉ=(−0.83),DM=(−2.52), n=90\bar d = (-0.83),\qquad DM = (-2.52),\ n = 90
dtd_t
loss differential
h=1h = 1
forecast horizon
⋅\sqrt{\cdot}
Harvey–Leybourne–Newbold small-sample correction, with t(n − 1)

The verdict

t(89) = −2.52, p = .013

p ≤ α = .050: reject H₀

Compares forecasts, not models: with nested models estimated on the same data the null distribution is not standard.

Statistic
−2.523
p-value
0.0134
Diebold–Mariano test on 90 paired forecast errors: t(89) = −2.52, p = .013. The p-value is below α = .050, so the data are hard to reconcile with H0.

Diebold–Mariano test on 90 paired forecast errors: t(89) = −2.52, p = .013. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: two forecast methods have equal expected loss.

Assumptions

Comparable genuine out-of-sample forecasts; a stationary loss differential with a valid long-run variance estimate.

Formula

dt=L(e1t)−L(e2t)DM=dˉω^d2/T\begin{gathered}d_t=L(e_{1t})-L(e_{2t})\\[5pt] DM=\frac{\bar d}{\sqrt{\widehat\omega_d^2/T}}\end{gathered}

Parameters and notation

Choose loss L, forecast horizon h, and HAC estimator. Positive d favors method 2. A common small sample correction multiplies DM by √[(T+1−2h+h(h−1)/T)/T].

Structure of the method

Align forecast errors at common origins and horizons, convert them into a prespecified loss differential, estimate its mean and long-run variance, and standardize. The forecast models may be complex, but the comparison operates on paired loss observations. Overlapping horizons induce dependence even if primitive innovations are independent.

Estimated and nuisance parameters

Estimate the mean loss difference and its autocovariance/long-run variance; forecast-model parameters were fitted upstream on training windows. Report mean losses and the difference as effect sizes. The test does not estimate a universal ranking independent of the chosen outcome, horizon, or economic loss.

Settings and design choices

Choose loss power/function, horizon, evaluation origins, rolling versus expanding training, variance estimator, and alternative. The forecast implementation supplies autocorrelation and Bartlett choices; the latter avoids negative variance estimates. The small-sample modification and its t reference should be named rather than described as an unmodified normal test.

Fitting and adaptation

Generate honest out-of-sample forecasts using the same information set and refit schedule for both methods. If a model or tuning parameter was selected using evaluation losses, rerun the whole selection/forecasting procedure within a justified resampling design or use a fresh test period. Nested models need specialized null reasoning.

Sensitivity and robustness

Inspect autocorrelation, outlying losses, horizon overlap, regime changes, and forecast-failure handling. Zero or nearly zero loss-difference variance makes the statistic unstable. A statistically detectable difference can be practically tiny. The supplied normal-error example illustrates calculation rather than an actual rolling forecast experiment.

How to use and calibrate

Use asymptotic normal inference or the stated Harvey–Leybourne–Newbold t correction.

Example application

Compare squared prediction errors from two forecasting procedures.

Interpretation and pitfalls

Watch out

Nested forecast models can have nonstandard null behavior; data reuse, overlapping horizons and model selection need attention.

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

Forecast errors from the same target dates; squared-error loss, horizon one, and the package’s finite-sample correction. Higher horizons require the corresponding long-run variance estimate.

Python · invokes R

Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: forecast

# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
    raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
e1<-rnorm(100);e2<-rnorm(100,sd=1.2);print(forecast::dm.test(e1,e2,h=1,power=2,alternative="two.sided"))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
                        text=True, capture_output=True)
print(result.stdout)
if result.stderr:
    print(result.stderr)
result.check_returncode()
R

Validated — Executed successfully in the validation environment · needs forecast

set.seed(2026)
e1<-rnorm(100);e2<-rnorm(100,sd=1.2);print(forecast::dm.test(e1,e2,h=1,power=2,alternative="two.sided"))
Output
	Diebold-Mariano Test

data:  e1e2
DM = -1.3518, Forecast horizon = 1, Loss function power = 2, p-value =
0.1795
alternative hypothesis: two.sided

Sources

Statistics guide v4 (historical):
Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 215, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT215.