# Scoring forecasts directly

#### 2022-05-13

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)
#>  [1]  0.20  0.23 -0.90  0.01 -0.58  0.15 -0.98  0.75 -0.63  0.39 -0.16  0.23
#> [13] -0.81  0.42  0.13  0.93  0.14 -0.66 -0.58 -0.86  0.63 -0.23  0.43  0.50
#> [25]  0.40 -0.36 -0.15 -0.78 -0.69  0.47

## continuous forecasts
true_values <- rnorm(30, mean = 1:30)
predictions <- replicate(200, rnorm(30, mean = 1:30))
bias_sample(true_values, predictions)
#>  [1]  0.84  0.56 -0.89  0.65  0.39  0.52 -0.67 -0.58  0.82 -0.41 -0.92 -0.76
#> [13] -0.50 -0.09  0.86 -0.15  0.47 -0.58 -0.52 -0.50 -0.05 -0.20  0.48  0.33
#> [25] -0.27 -0.05  0.18 -0.93 -0.80  0.39

# 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] 1.4826 1.4826 1.4826 2.2239 2.9652 2.9652 2.9652 2.9652 2.9652 2.9652
#> [11] 2.9652 4.4478 4.4478 4.4478 2.9652 4.4478 4.4478 4.4478 4.4478 4.4478
#> [21] 4.4478 4.4478 4.4478 4.4478 5.9304 4.4478 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)
#>  [1] 0.666275 0.509675 0.430750 0.946525 1.191650 2.028475 1.257800 1.896225
#>  [9] 5.533325 0.784725 3.262475 1.880175 1.010100 4.318075 0.988975 0.933025
#> [17] 2.333375 1.488475 3.985175 4.024625 1.847125 1.050100 9.625600 3.201950
#> [25] 1.259125 4.338600 1.299100 8.578275 3.036300 1.223500

# 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] 3.778308 2.934232 1.013456 2.498714 1.567576 1.906372 2.351256 2.104134
#>  [9] 2.472085 4.182862 2.853322 4.659280 2.323377 2.963271 2.708225 6.837704
#> [17] 3.610578 4.202738 4.224053 3.099866 3.047793 2.950735 3.058481 3.446446
#> [25] 4.014136 4.053700 3.295288 4.352889 3.261140 4.199747

# 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)
#>  [1] 1.4947729 0.9704791 0.8992562 1.2070092 1.0160102 1.1455992 1.6387944
#>  [8] 1.3675825 0.9119887 1.1332534 0.8784422 1.1145903 1.0148291 1.0837452
#> [15] 1.1700948 0.9981675 0.9083709 1.5319604 1.4120388 1.1495566 1.0371895
#> [22] 1.9524466 1.6075937 1.6541065 1.5318269 1.1835917 1.4587418 1.8032726
#> [29] 0.9201874 1.4254566

# 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)
#>  [1] 0.511952269 0.029466047 0.683174504 0.251234773 0.069930883 0.002775272
#>  [7] 0.306580471 0.248686287 0.588960163 0.876300527 0.106578902 0.680399804
#> [13] 0.140997740 0.034723455 0.145966893 0.149640487 0.061504220 0.587872832
#> [19] 0.179528321 0.061527328 0.863037762 0.219092821 0.213296907 0.016036168
#> [25] 0.539365453 0.857487519 0.054405038 0.125398121 0.001007106 0.934569281

## 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
)
#>  [1] 0.04953453 0.69984876 0.15587556 1.06556106 1.60192031 0.16736199
#>  [7] 0.15367907 0.21238416 0.28381447 1.11574951 0.18490699 0.21133179
#> [13] 0.14312833 0.08597517 0.17491752 0.21629685 0.70924981 0.17201572
#> [19] 0.23240418 0.61631063 0.12438120 0.17599015 0.33036817 0.57736220
#> [25] 0.23133752 0.31802415 1.45102549 0.16380366 0.20064643 0.30821352