arviz_stats.lfo_cv#
- arviz_stats.lfo_cv(data, wrapper, min_observations, forecast_horizon, time_dim='time', pointwise=None, var_name=None, method='approx', k_threshold=0.7)[source]#
Compute leave-future-out cross-validation (LFO-CV) for time series models.
Estimates the expected log predictive density (elpd) of M-step-ahead predictions for time series models, where M is set by
forecast_horizon. The model is trained on the observations up to each forecast origin and scored on the joint log predictive density of the next M observations, so predictions are never conditioned on data from the future. By default, the posterior is carried between forecast origins with Pareto-smoothed importance sampling (PSIS) and the model is only refit when the approximation becomes unreliable (seemethod).The PSIS-LFO-CV method is described in [1] and builds on the PSIS-LOO-CV method described in [2] and [3].
- Parameters:
- data
xarray.DataTreeorInferenceData Input data containing the posterior and log_likelihood groups from the full model fit. Must have a time dimension. Will be converted to DataTree.
- wrapper
SamplingWrapper An instance of
SamplingWrapper(or subclass) handling model refitting. Must implementsel_observations,sample,get_inference_data, andlog_likelihood__i. Following the same pattern asloo_kfold,sel_observationsreceives an array of integer indices to exclude from training and returns(modified_observed_data, excluded_observed_data). The forecast window is selected fromexcluded_observed_datainternally. Each refit excludes all observations from the forecast origin through the end. The approximate method also uses later excluded values to compute subsequent importance ratios.- min_observations
int Minimum number of observations required before making predictions. The first prediction is made at time min_observations.
- forecast_horizon
int Forecast horizon: number of steps ahead to predict.
- time_dim
str, default=”time” Name of the time dimension in the data.
- pointwisebool, optional
If True, return pointwise estimates. Defaults to
rcParams["stats.ic_pointwise"].- var_name
str, optional The name of the variable in log_likelihood group storing the pointwise log likelihood data to use for computation.
- method
str, default=”approx” Whether to refit the model at every forecast origin (“exact”) or to carry the posterior forward with Pareto-smoothed importance sampling (PSIS) [3], refitting only when the importance weights become unreliable (“approx”).
- k_threshold
float, default=0.7 Pareto k threshold for triggering refit. If k > k_threshold, refit the model. Recommended values: 0.6 (conservative) or 0.7 (default).
- data
- Returns:
ELPDDataLFOObject with the following attributes:
kind: “lfo_cv”
elpd: expected log pointwise predictive density
se: standard error of the elpd
p: effective number of parameters
n_samples: number of posterior samples
n_data_points: number of forecast origins evaluated
scale: “log”
warning: bool - True if more than half of the forecast origins required a refit (only if
method="approx", otherwise False)good_k: float - The k-threshold value used for refit decisions (only if
method="approx", otherwise None)elpd_i:
DataArraywith pointwise predictive accuracy, only ifpointwise=Truepareto_k:
DataArraywith Pareto k diagnostics, only ifmethod="approx"andpointwise=Truep_lfo_i:
DataArraywith pointwise effective number of parameters, only ifpointwise=Trueforecast_horizon: forecast horizon
min_observations: minimum observations
refits: array of time indices where refits occurred. For
method="approx"the initial fit at the first forecast origin is not counted as a refitn_refits: number of refits performed
Notes
When
forecast_horizonis greater than 1, each forecast origin is scored with the joint log predictive density of the nextforecast_horizonobservations. If the likelihood depends on lagged values of the response, as in autoregressive models, there is more than one way to evaluate this joint density.lfo_cvuses the chain rule factorization, where each observation in the block is scored conditional on the observed values of the observations before it. This measures how well the model predicts the block that actually occurred given the training data.The alternative is to score each observation in the block against simulated values of its predecessors drawn from the model, letting predictive uncertainty accumulate across the horizon. This measures how well the model forecasts the block when the intermediate observations are never revealed, and is common in software oriented toward forecast evaluation. The two approaches coincide when
forecast_horizon=1and whenever the likelihood does not depend on lagged values of the response. Otherwise they estimate different quantities, so results computed one way should not be compared with results computed the other way.References
[1]Bürkner et al. Approximate leave-future-out cross-validation for Bayesian time series models. Journal of Statistical Computation and Simulation. 90(14) (2020) 2499-2523. https://doi.org/10.1080/00949655.2020.1783262 arXiv preprint https://arxiv.org/abs/1902.06281
[2]Vehtari et al. Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. Statistics and Computing. 27(5) (2017) https://doi.org/10.1007/s11222-016-9696-4 arXiv preprint https://arxiv.org/abs/1507.04544.
[3] (1,2)Vehtari et al. Pareto Smoothed Importance Sampling. Journal of Machine Learning Research, 25(72) (2024) https://jmlr.org/papers/v25/19-556.html arXiv preprint https://arxiv.org/abs/1507.02646