View source: R/statistical_tests.R
| compute_crps | R Documentation |
Calculates the Continuous Ranked Probability Score (CRPS) using the energy score (Monte Carlo) approximation for a single forecast period.
compute_crps(forecast_density, target_realization)
forecast_density |
|
target_realization |
|
The CRPS is a strictly proper scoring rule that jointly rewards calibration and sharpness of a probabilistic forecast. It is computed via the energy score identity:
CRPS = E|X - y| - \frac{1}{2} E|X - X'|
where X, X' are independent draws from the forecast distribution and y is
the realization. Lower values are better: a CRPS of 0 indicates a perfect
point-mass forecast at the true realization.
numeric scalar representing the CRPS loss, or NA if
input is invalid. Lower values indicate better probabilistic forecast accuracy.
Gneiting, T., & Raftery, A. E. (2007). Strictly Proper Scoring Rules, Prediction, and Estimation. Journal of the American Statistical Association, 102(477), 359–378. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1198/016214506000001437")}
data(metals)
# metals: 165 x 15; columns 1-14 are competing forecasts, column 15 is the benchmark
# CRPS for forecast 1, period 1:
# Use the cross-sectional spread of all competing forecasts at period t=1 as the density
density_samples <- as.numeric(metals[1, 1:14])
realized_value <- metals[1, 15]
compute_crps(density_samples, realized_value)
# In practice, iterate over all forecasts and periods.
# For forecast k and period t, the predictive density is approximated by shifting the
# cross-sectional spread of all K competing forecasts so that it is centred at the
# cross-sectional mean of forecasts at period t. Specifically, for each forecast k:
# density_samples_tk = (forecasts of all K forecast at t) - forecast_k(t) +
mean # (all K forecasts at t)
# This preserves the spread (diversity) across forecasts while recentring around the
# cross-sectional mean rather than around forecast k's own point forecast. It is an
# empirical approximation to the predictive distribution when no parametric density
# is available.
P <- nrow(metals)
K <- ncol(metals) - 1L # 14 competing forecasts
crps_matrix <- matrix(NA_real_, nrow = P, ncol = K,
dimnames = list(NULL, colnames(metals)[1:K]))
for (t in seq_len(P)) {
for (k in seq_len(K)) {
density_samples_tk <- as.numeric(metals[t, 1:K]) - metals[t, k] + mean(metals[t, 1:K])
crps_matrix[t, k] <- compute_crps(as.numeric(density_samples_tk),
target_realization = metals[t, ncol(metals)])
}
}
head(crps_matrix)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.