Diagonal backtest
backtest.RdThe retrospective validation of Meyers (2015): each model is fitted on a
triangle cut back to an earlier calendar diagonal and forecasts the
incremental values of the diagonal that came in next, and the forecast
is scored against what happened. This is done for the latest
n_diagonals calendar diagonals (oldest first), for every model in
models, with the same n_sims joint draws from seed for each model
and split, so the models share their random numbers.
Usage
backtest(raw)
diagonal_backtest(
triangle,
column,
models,
n_diagonals,
premium = NULL,
n_sims = 1000,
seed = 0,
interval = 0.9
)Arguments
- raw
The Rust result; used internally.
- triangle
A cumulative triangle with one segment.
- column
Name of the column to forecast.
- models
A list of
odp_glm_spec(),bayes_odp_spec(),meyers_spec()andcompartmental_candidate()objects. Its names are the models' names in the results; an entry without a name is called"odp","bayes_odp","meyers_"and the model's name ("meyers_crc") or the candidate's name ("compartmental_gm1"). Two models cannot share a name.- n_diagonals
Number of latest calendar diagonals to hold out, one split each.
Premium of each origin, for
meyers_spec(): one number per origin, or the name of a column oftrianglewhose latest value per origin is taken asmeyers()takes it.NULLwhen no model needs it.- n_sims
Number of joint draws of each forecast.
- seed
Seed of the sampler chains and of the draws, a non-negative whole number.
- interval
Width of the central interval of the
coveragemetric, in (0, 1).
Value
A backtest object with properties:
models (the names), metrics, n_splits, diagonals (the calendar
period held out in each split, as a label such as "2021", as the
calendar column of the cells labels it, oldest first), scores (a data.frame in long form: model, diagonal,
metric, value), mean (a data.frame: model and one column per
metric, the mean over the diagonals), excluded (held-out cells left
out of each split), scored_rows (per split, the scored cells as
positions in the row-major grid of origins by development ages, from
1; Python's scored_rows count from 0), log_densities (a list by model of the held-out log densities of
every scored cell, the splits in order and each model's values for
the same cells, NULL where the model gives none), diagnostics (a
data.frame: model, diagonal, max_rhat, min_ess_bulk,
min_ess_tail, divergences and n_warnings; one row for every model
and split, NA for a model with no sampler) and warnings (a
data.frame: model, diagonal, message).
Details
A held-out cell whose origin or development age has no training cell
that every model fits on cannot be forecast by a model with origin and
development effects (the newest origin and the oldest age on the
diagonal, or an age whose only training cell is a negative increment the
Poisson GLM drops). It is left out for every model: excluded counts
them, and scored_rows lists the cells that were scored.
The metrics, per model and held-out diagonal (scores, and their means
over the diagonals in mean):
crps: the mean continuous ranked probability score of the cells' draws.coverage: the share of cells inside the centralintervalof their draws.actual_vs_expected: actual over expected on the diagonal's total.total_crps: the CRPS of the diagonal total, from the draws summed per simulation.mean_log_density: the mean over the scored cells of the model's held-out log density (NAfor a model that gives none).total_percentile: the share of the simulated diagonal totals at or below the actual total, the probability integral transform of the diagonal: uniform over many triangles for a calibrated model.
Meyers' own statistic, the percentile of the outcome's ultimate, is a
different calculation: it comes from a fit on the whole upper triangle,
and is the cdf of that fit's predictive total at the known outcome:
cdf(total(fit@ultimates), outcome) for meyers() (cdf(), total()),
or cdf(total(fit@reserves), outcome - latest) for bayes_odp(), with
latest the total of the triangle's latest diagonal.
A Bayesian fit whose MCMC diagnostics fail (R-hat above 1.01, bulk or
tail ESS below 100 per chain, a divergent transition) is still scored,
unlike bayes_odp(), which warns: a study over many triangles wants
every split and a list of the doubtful ones. diagnostics has one row
per model and split, and warnings lists every message. A model that
fails to fit is an error naming it. No R warning is raised for a failed
diagnostic; read warnings.
A compartmental_candidate() is scored on the paid increments here, and
on the held-out outstanding by compartmental_outstanding_backtest(),
which reuses the fits of this backtest.
Errors: a models that is not a non-empty list of odp_glm_spec(),
bayes_odp_spec(), meyers_spec() and compartmental_candidate()
objects; two models with the same name (checked before any fit); a
compartmental_candidate() built on another triangle or paid column than
triangle and column; a triangle with several segments or with fewer
than n_diagonals + 1 calendar diagonals; premium missing for
meyers_spec(), or not positive and finite; an invalid sampler setting.
Examples
long <- data.frame(year = rep(2016:2021, 6:1),
age = unlist(lapply(6:1, function(k) 12 * seq_len(k))),
paid = c(952, 1529, 2813, 3647, 3724, 3832, 849, 1564, 2202, 2432,
2468, 983, 2211, 2830, 3832, 1657, 2685, 3169, 932, 1940, 1162))
tri <- triangle(long, "year", "age", "paid")
bt <- diagonal_backtest(tri, "paid", list(odp = odp_glm_spec()), n_diagonals = 2,
n_sims = 500, seed = 1)
bt@mean
#> model crps coverage actual_vs_expected total_crps mean_log_density
#> 1 odp 280.3087 0.875 0.7376618 652.0173 -7.357155
#> total_percentile
#> 1 0.23
bt@diagnostics
#> model diagonal max_rhat min_ess_bulk min_ess_tail divergences n_warnings
#> 1 odp 2020 NA NA NA NA NA
#> 2 odp 2021 NA NA NA NA NA