DPredict · Guide test 215
Diebold–Mariano test
Tests equal predictive accuracy through the mean difference between forecast losses.
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
1: both models are equally accurate.
Alternative
Your data
Simulated with known truthThe test asks whether the mean loss differential (gray line, d̄ = −0.832) is far from zero.
If H₀ were true
- The reference: how DM varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .013).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- loss differential
- forecast horizon
- 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 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
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.
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()
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
- pkg.robjhyndman.com/forecast/reference/dm.test.htmlpkg.robjhyndman.com
- dm.testpkg.robjhyndman.com
- dm.testsearch.r-project.org
- 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.