# Scoring forecasts directly

#### 2023-01-30

A variety of metrics and scoring rules can also be accessed directly through the scoringutils package.

The following gives an overview of (most of) the implemented metrics.

# Bias

The function bias determines bias from predictive Monte-Carlo samples, automatically recognising whether forecasts are continuous or integer valued.

For continuous forecasts, Bias is measured as $B_t (P_t, x_t) = 1 - 2 \cdot (P_t (x_t))$

where $$P_t$$ is the empirical cumulative distribution function of the prediction for the true value $$x_t$$. Computationally, $$P_t (x_t)$$ is just calculated as the fraction of predictive samples for $$x_t$$ that are smaller than $$x_t$$.

For integer valued forecasts, Bias is measured as

$B_t (P_t, x_t) = 1 - (P_t (x_t) + P_t (x_t + 1))$

to adjust for the integer nature of the forecasts. In both cases, Bias can assume values between -1 and 1 and is 0 ideally.

## integer valued forecasts
true_values <- rpois(30, lambda = 1:30)
predictions <- replicate(200, rpois(n = 30, lambda = 1:30))
bias_sample(true_values, predictions)
#>   -0.58 -0.39 -0.28  0.07  0.20 -0.16 -0.70 -0.57 -0.20  0.99  0.75 -0.25
#>  -0.99 -0.58 -0.96  0.30  0.75 -0.92  0.13 -0.58  0.85  0.71  0.25 -0.02
#>  -0.88  0.46  0.21  0.70  0.75 -0.57

## continuous forecasts
true_values <- rnorm(30, mean = 1:30)
predictions <- replicate(200, rnorm(30, mean = 1:30))
bias_sample(true_values, predictions)
#>    0.24  0.33 -0.50  0.13 -0.98 -0.87 -0.39  0.50  0.81 -0.64  0.94  0.81
#>  -0.67 -0.10  0.60  0.93  0.27  0.50 -0.58 -0.80  0.33  0.13  0.90 -0.76
#>   0.01 -0.94 -0.34  0.22 -0.13 -0.79

# Sharpness

Sharpness is the ability of the model to generate predictions within a narrow range. It is a data-independent measure, and is purely a feature of the forecasts themselves.

Sharpness / dispersion of predictive samples corresponding to one single true value is measured as the normalised median of the absolute deviation from the median of the predictive samples. For details, see ?stats::mad

predictions <- replicate(200, rpois(n = 30, lambda = 1:30))
#>   1.4826 1.4826 1.4826 1.4826 2.9652 2.9652 2.9652 2.2239 2.9652 4.4478
#>  2.9652 2.9652 4.4478 2.9652 2.9652 4.4478 4.4478 4.4478 4.4478 4.4478
#>  4.4478 4.4478 4.4478 4.4478 4.4478 5.9304 4.4478 4.4478 5.9304 4.4478

# Calibration

Calibration or reliability of forecasts is the ability of a model to correctly identify its own uncertainty in making predictions. In a model with perfect calibration, the observed data at each time point look as if they came from the predictive probability distribution at that time.

Equivalently, one can inspect the probability integral transform of the predictive distribution at time t,

$u_t = F_t (x_t)$

where $$x_t$$ is the observed data point at time $$t \text{ in } t_1, …, t_n$$, n being the number of forecasts, and $$F_t$$ is the (continuous) predictive cumulative probability distribution at time t. If the true probability distribution of outcomes at time t is $$G_t$$ then the forecasts $$F_t$$ are said to be ideal if $$F_t = G_t$$ at all times $$t$$. In that case, the probabilities ut are distributed uniformly.

In the case of discrete outcomes such as incidence counts, the PIT is no longer uniform even when forecasts are ideal. In that case a randomised PIT can be used instead:

$u_t = P_t(k_t) + v \cdot (P_t(k_t) - P_t(k_t - 1) )$

where $$k_t$$ is the observed count, $$P_t(x)$$ is the predictive cumulative probability of observing incidence $$k$$ at time $$t$$, $$P_t (-1) = 0$$ by definition and $$v$$ is standard uniform and independent of $$k$$. If $$P_t$$ is the true cumulative probability distribution, then $$u_t$$ is standard uniform.

The function checks whether integer or continuous forecasts were provided. It then applies the (randomised) probability integral and tests the values $$u_t$$ for uniformity using the Anderson-Darling test.

As a rule of thumb, there is no evidence to suggest a forecasting model is miscalibrated if the p-value found was greater than a threshold of $$p >= 0.1$$, some evidence that it was miscalibrated if $$0.01 < p < 0.1$$, and good evidence that it was miscalibrated if $$p <= 0.01$$. In this context it should be noted, though, that uniformity of the PIT is a necessary but not sufficient condition of calibration. It should also be noted that the test only works given sufficient samples, otherwise the Null hypothesis will often be rejected outright.

# Continuous Ranked Probability Score (CRPS)

Wrapper around the crps_sample() function from the scoringRules package. For more information look at the manuals from the scoringRules package. The function can be used for continuous as well as integer valued forecasts. Smaller values are better.

true_values <- rpois(30, lambda = 1:30)
predictions <- replicate(200, rpois(n = 30, lambda = 1:30))
crps_sample(true_values, predictions)
#>   0.562000 0.333725 0.599450 1.031925 0.777950 0.783875 0.671975 2.420550
#>   1.053375 2.344550 1.183575 3.659250 4.455125 2.485650 1.297900 1.259700
#>  0.991875 4.179400 4.703500 1.168125 3.188200 4.559400 1.225575 1.532450
#>  1.748800 1.447125 3.458375 2.105425 1.899000 2.938050

# Dawid-Sebastiani Score (DSS)

Wrapper around the dss_sample() function from the scoringRules package. For more information look at the manuals from the scoringRules package. The function can be used for continuous as well as integer valued forecasts. Smaller values are better.

true_values <- rpois(30, lambda = 1:30)
predictions <- replicate(200, rpois(n = 30, lambda = 1:30))
dss_sample(true_values, predictions)
#>   1.337772 2.746928 1.263989 5.206980 1.551809 2.010888 1.818360 5.591931
#>   2.530657 2.794045 4.686407 3.375068 2.726063 2.637986 3.441049 3.653023
#>  3.182899 3.993585 3.137148 3.521687 3.639165 3.372666 3.318063 3.305571
#>  3.371805 5.668469 4.869611 4.783353 4.926390 3.736051

# Log Score

Wrapper around the logs_sample() function from the scoringRules package. For more information look at the manuals from the scoringRules package. The function should not be used for integer valued forecasts. While Log Scores are in principle possible for integer valued forecasts they require a kernel density estimate which is not well defined for discrete values. Smaller values are better.

true_values <- rnorm(30, mean = 1:30)
predictions <- replicate(200, rnorm(n = 30, mean = 1:30))
logs_sample(true_values, predictions)
#>   0.9906834 0.9294375 1.0734542 0.9975393 2.1856369 0.9371875 2.0732780
#>   1.2022622 2.5648357 1.2353973 1.5259518 1.0412646 1.1298885 1.8240796
#>  1.2184082 1.1613428 1.2530820 0.9496398 1.2371132 1.1132590 1.0491634
#>  1.2198807 0.9498077 2.4421814 0.9606593 1.4859348 1.1814060 0.8166935
#>  0.9385670 1.2747719

# Brier Score

The Brier score is a proper score rule that assesses the accuracy of probabilistic binary predictions. The outcomes can be either 0 or 1, the predictions must be a probability that the true outcome will be 1.

The Brier Score is then computed as the mean squared error between the probabilistic prediction and the true outcome.

$\text{Brier_Score} = \frac{1}{N} \sum_{t = 1}^{n} (\text{prediction}_t - \text{outcome}_t)^2$

true_values <- sample(c(0, 1), size = 30, replace = TRUE)
predictions <- runif(n = 30, min = 0, max = 1)

brier_score(true_values, predictions)
#>   0.1279277004 0.4702689944 0.4843226247 0.2944442476 0.3430024449
#>   0.0638939309 0.4801719402 0.3989647112 0.9036381527 0.9401694631
#>  0.0007886997 0.5437564604 0.0361858575 0.2752880462 0.0742479058
#>  0.2037761371 0.0044424151 0.0006483796 0.1811697345 0.4869030261
#>  0.4050974865 0.4368005448 0.0602962610 0.9670389758 0.0344264367
#>  0.6835302995 0.0361517643 0.2160881336 0.0155668568 0.1577043712

## Interval Score

The Interval Score is a Proper Scoring Rule to score quantile predictions, following Gneiting and Raftery (2007). Smaller values are better.

The score is computed as

$\text{score} = (\text{upper} - \text{lower}) + \\ \frac{2}{\alpha} \cdot (\text{lower} - \text{true_value}) \cdot 1(\text{true_values} < \text{lower}) + \\ \frac{2}{\alpha} \cdot (\text{true_value} - \text{upper}) \cdot 1(\text{true_value} > \text{upper})$

where $$1()$$ is the indicator function and $$\alpha$$ is the decimal value that indicates how much is outside the prediction interval. To improve usability, the user is asked to provide an interval range in percentage terms, i.e. interval_range = 90 (percent) for a 90 percent prediction interval. Correspondingly, the user would have to provide the 5% and 95% quantiles (the corresponding alpha would then be 0.1). No specific distribution is assumed, but the range has to be symmetric (i.e you can’t use the 0.1 quantile as the lower bound and the 0.7 quantile as the upper). Setting weigh = TRUE will weigh the score by $$\frac{\alpha}{2}$$ such that the Interval Score converges to the CRPS for increasing number of quantiles.

true_values <- rnorm(30, mean = 1:30)
interval_range <- 90
alpha <- (100 - interval_range) / 100
lower <- qnorm(alpha / 2, rnorm(30, mean = 1:30))
upper <- qnorm((1 - alpha / 2), rnorm(30, mean = 1:30))

interval_score(
true_values = true_values,
lower = lower,
upper = upper,
interval_range = interval_range
)
#>   0.19410498 0.20711537 0.65669592 0.21154197 0.15923509 0.14804556
#>   0.09704569 0.17902605 0.39733259 0.19372045 0.11760496 0.41122129
#>  0.49963935 0.73144897 0.31879613 0.15302427 0.14344829 0.91285186
#>  0.09777623 0.27964760 0.15934355 0.10608986 0.23475748 0.28002224
#>  0.43628444 0.23780754 0.16002560 2.54933872 0.21973767 0.23847923