lopensemble.Rmd
The lopensemble
package provides an easy way to combine
predictions from individual time series or panel data models to an
ensemble. lopensemble
stacks Models according to the
Continuous Ranked Probability Score (CRPS) over k-step ahead
predictions. It is therefore especially suited for timeseries and panel
data. A function for leave-one-out CRPS may be added in the future.
Predictions need to be predictive distributions represented by
predictive samples. Usually, these will be sets of posterior predictive
simulation draws generated by an MCMC algorithm.
Given some training data with true observed values as well as
predictive samples generated from different models,
crps_weights
finds the optimal (in the sense of minimizing
expected cross-validation predictive error) weights to form an ensemble
from these models. Using these weights,
mixture_from_samples
can then provide samples from the
optimal model mixture by drawing from the predictice samples of the
individual models in the correct proportion. This gives a mixture model
solely based on predictive samples and is in this regard superior to
other ensembling techniques like Bayesian Model Averaging.
Here is an example of how to use lopensemble
with a toy
data set. The data contains true observed values as well as predictive
samples generated by different models.
library("data.table")
splitdate <- as.Date("2020-03-28")
print(example_data)
#> geography model sample_id date predicted observed
#> <char> <char> <num> <Date> <num> <num>
#> 1: Tatooine ARIMA 1 2020-03-14 1.719445 1.655068
#> 2: Tatooine ARIMA 2 2020-03-14 1.896555 1.655068
#> 3: Tatooine ARIMA 3 2020-03-14 1.766821 1.655068
#> 4: Tatooine ARIMA 4 2020-03-14 1.714007 1.655068
#> 5: Tatooine ARIMA 5 2020-03-14 1.726421 1.655068
#> ---
#> 103996: Coruscant Naive 496 2020-04-08 1.460421 1.543976
#> 103997: Coruscant Naive 497 2020-04-08 1.057846 1.543976
#> 103998: Coruscant Naive 498 2020-04-08 1.433936 1.543976
#> 103999: Coruscant Naive 499 2020-04-08 1.719357 1.543976
#> 104000: Coruscant Naive 500 2020-04-08 0.781818 1.543976
traindata <- example_data[date <= splitdate]
testdata <- example_data[date > splitdate]
Obtain weights based on trainin data, create mixture based on predictive samples in the testing data.
weights <- lopensemble::crps_weights(traindata)
test_mixture <- lopensemble::mixture_from_samples(testdata, weights = weights)
print(test_mixture)
#> Key: <geography, sample_id, date, observed>
#> geography sample_id date observed predicted model
#> <char> <num> <Date> <num> <num> <char>
#> 1: Coruscant 1 2020-03-29 1.5002089 1.6056708 CRPS_Mixture
#> 2: Coruscant 1 2020-03-30 1.4713551 1.8436852 CRPS_Mixture
#> 3: Coruscant 1 2020-03-31 1.4154616 1.2926579 CRPS_Mixture
#> 4: Coruscant 1 2020-04-01 1.3426560 0.4212479 CRPS_Mixture
#> 5: Coruscant 1 2020-04-02 1.3298251 1.3518078 CRPS_Mixture
#> ---
#> 10996: Tatooine 500 2020-04-04 0.6750499 0.6838004 CRPS_Mixture
#> 10997: Tatooine 500 2020-04-05 0.7225369 0.4405134 CRPS_Mixture
#> 10998: Tatooine 500 2020-04-06 0.7532836 0.4858075 CRPS_Mixture
#> 10999: Tatooine 500 2020-04-07 0.7598158 0.9897033 CRPS_Mixture
#> 11000: Tatooine 500 2020-04-08 0.7719879 0.4106171 CRPS_Mixture
Score predictions using the scoringutils
package
(install from CRAN using
install.packages("scoringutils")
).
# combine data.frame with mixture with predictions from other models
score_df <- data.table::rbindlist(list(testdata, test_mixture), fill = TRUE)
# CRPS score for all predictions using scoringutils
score_df[, crps := scoringutils::crps(unique(observed), t(predicted)),
by = .(geography, model, date)
]
# summarise scores
score_df[, mean(crps), by = model][, data.table::setnames(.SD, "V1", "CRPS")]
Display other metrics
Given a cumulative distribution function (CDF) with a finite first moment of a probabilistic forecast, the corresponding Continuous Ranked Probability Score can be written as
The CRPS can be used to stack different models to obtain one ensemble mixture model. Let us assume we have data from time points in regions . Observations are denoted . Predictive samples are generated from different models . For every observation the predictive samples are denoted .
Using these predictive draws, we can compute the CRPS of the -th model for the observation at time in region as
Now we want to aggregate predictions from these models. When the prediction is a mixture of the models with weights , the CRPS can be expressed as
This expression is quadratic in . We only need to compute , , and for all pairs once and store them for all weight values in the optimization.
The CRPS for the mixture of all models for all observations can simply obtained by summing up the individual results from equation 3 over all regions and time points. However, we can also weight predictions from different time points and regions differently. This makes sense for example if we have very little data in the beginning or if current older observations are less characteristic of the current and future dynamics. In this case we might want to downweight the early phase in the final model evaluation. Similarly, we might want to give more or less weight to certain regions. Mathematically we can introduce a time-varying weight , e.g.Β to penalize earler estimates. Likewise we can introduce a region-specific weight .
To obtain the CRPS weights we finally solve a quadratic optimization: