Definition
For each test period, compute the loss of forecast A and of forecast B, for instance their squared errors, and take the difference d. The test asks whether the mean of d is zero.
DM = mean(d) ÷ √( estimated variance of mean(d) )
The variance of the mean has to account for the autocorrelation of d, which is present by construction for forecasts several steps ahead. Harvey, Leybourne and Newbold proposed a small-sample correction and a comparison with a Student distribution instead of a normal one, which is the version to use on short test windows.
Worked example
Forecast B is slightly better than forecast A by construction. The same comparison is run on 30 periods and on 400 periods.
import numpy as np
from scipy import stats
def dm_test(e_a, e_b, h=1):
d = e_a ** 2 - e_b ** 2
n = len(d)
dc = d - d.mean()
gamma = [np.sum(dc[k:] * dc[:n - k]) / n for k in range(h)]
var = (gamma[0] + 2 * sum(gamma[1:])) / n
dm = d.mean() / np.sqrt(var)
dm *= np.sqrt((n + 1 - 2 * h + h * (h - 1) / n) / n) # Harvey, Leybourne, Newbold
return dm, 2 * stats.t.sf(abs(dm), df=n - 1)
rng = np.random.default_rng(4)
for n in (30, 400):
noise = rng.normal(0, 1, n)
e_a = noise + rng.normal(0, 0.6, n) # errors of A: common part + its own
e_b = noise + rng.normal(0, 0.4, n) # errors of B: smaller own part
dm, p = dm_test(e_a, e_b)
print(f"n = {n:3d} MSE A = {np.mean(e_a**2):.3f} MSE B = {np.mean(e_b**2):.3f} DM = {dm:+.2f} p = {p:.3f}")Output, produced by running the code above when this page was built:
n = 30 MSE A = 1.338 MSE B = 1.729 DM = -1.62 p = 0.115 n = 400 MSE A = 1.469 MSE B = 1.208 DM = +3.12 p = 0.002
On 30 periods the ranking is reversed: A, the worse forecast by construction, shows the lower error, and the test finds nothing. On 400 periods B is ahead and the p-value is small. A real difference of this size cannot be told from chance on the short window.
Where it misleads
- A large p-value on a short window is an empty result, not a proof that the methods are equal. State the difference the test could have detected.
- The test compares forecasts, not models. When one model nests the other and both are re-estimated, the statistic does not follow its usual distribution and tests designed for nested models apply.
- Run on many pairs of methods, some differences will pass by chance. The comparison that matters should be named before the test window is read.
- For intermittent series the loss differential is zero in most periods and far from normal. A paired bootstrap over series is more reliable than the test on one series.
In our work
We compare methods on paired differences, with an interval, and we name the comparison before reading the test window. A paired comparison resolves much smaller gaps than two separate scores: in our own tests, about ±0.065 against ±0.20 for a level.
Service: forecasting and statistical studies Research note: our gates
References
- Diebold, F. X., Mariano, R. S. (1995). Comparing predictive accuracy. Journal of Business and Economic Statistics, 13(3), 253-263. doi:10.1080/07350015.1995.10524599
- Harvey, D., Leybourne, S., Newbold, P. (1997). Testing the equality of prediction mean squared errors. International Journal of Forecasting, 13(2), 281-291. doi:10.1016/S0169-2070(96)00719-4
- Diebold, F. X. (2015). Comparing predictive accuracy, twenty years later: a personal perspective on the use and abuse of Diebold-Mariano tests. Journal of Business and Economic Statistics, 33(1), 1-9. doi:10.1080/07350015.2014.983236