pymc_forecast.prediction#
Predictive drivers: forecasting and in-sample posterior prediction.
Both drivers rebuild the model via build_model()
(forecasting with extended covariates, in-sample with the observed window) and
run pm.sample_posterior_predictive over a posterior. Posteriors are
accepted in any of the shapes the fitting paths produce — an ArviZ
DataTree/InferenceData with a posterior group, or a bare posterior
Dataset.
Prediction outputs are draw-level by contract: every variable in the
predictions (out-of-sample) and posterior_predictive (in-sample)
groups carries chain/draw dims with the full posterior-predictive
samples — nothing is reduced to means or quantiles on the default path.
prediction_samples() extracts that samples Dataset from any result
shape the drivers produce.
- pymc_forecast.prediction.forecast(model_fn, posterior, data, covariates, *, num_samples=None, var_names=None, batch_size=None, random_seed=None, progressbar=False)[source]#
Sample probabilistic forecasts over the covariate horizon.
Rebuilds the model with
covariatesextendingdataalong"time"; the surplus covariate steps are the forecast horizon. The posterior is replayed for in-sample latents while*_futurevariables (absent from it) are drawn fresh.- Parameters:
model_fn – The model body (
(Horizon, covariates) -> Noneor aForecastingModel).posterior – A fitted posterior (
DataTree/InferenceDataorDataset).data – Observed data over the training window.
covariates – Covariates spanning training window plus forecast horizon.
num_samples – If given, subsample the posterior to this many draws first.
var_names – Variables to record. Default:
"forecast", all*_futurelatents, and — for models registered throughpredict()— the noise-free"mu_future"predictor. On very wide panels, restricting this to["forecast"]also shrinks the result’s memory footprint.batch_size – Maximum posterior draws (per chain) per predictive pass. When set, the posterior is processed in consecutive blocks of at most this many draws and the blocks are concatenated along
draw— bounding the working memory of each pass on very wide panels (the port of upstream’s chunked prediction, juanitorduz/numpyro_forecast#65). Per-block seeds are derived fromrandom_seed, so a batched run is deterministic given the seed but draws different (equally valid) noise than an unbatched run.random_seed – Seed for thinning and predictive sampling.
progressbar – Show the sampling progress bar.
- Returns:
DataTree– With apredictionsgroup carryingtime_futurecoords.
- pymc_forecast.prediction.posterior_dataset(posterior)[source]#
Extract the posterior group as a plain
xarray.Dataset.Accepts an ArviZ
DataTree/InferenceData(uses itsposteriorgroup) or a bareDataset(returned unchanged).
- pymc_forecast.prediction.predict_in_sample(model_fn, posterior, data, covariates=None, *, num_samples=None, batch_size=None, random_seed=None, progressbar=False)[source]#
Sample the in-sample posterior predictive of the
"obs"variable.The in-sample counterpart of
forecast(): the model is rebuilt over the observed window only (no forecast horizon) and the observed variable is resampled given replayed latents. For models registered throughpredict(), the noise-free"mu"predictor is recorded alongside"obs".- Parameters:
model_fn – As in
forecast().posterior – As in
forecast().data – As in
forecast().covariates – Covariates covering (at least) the observed window; surplus future steps are dropped.
Nonefor models without covariates.num_samples – As in
forecast().batch_size – As in
forecast().random_seed – As in
forecast().progressbar – As in
forecast().
- Returns:
DataTree– With aposterior_predictivegroup holding"obs".
- pymc_forecast.prediction.prediction_samples(result)[source]#
Extract the draw-level predictive samples from a prediction result.
Accepts any result shape the predictive drivers produce — an ArviZ
DataTree/InferenceDatawith apredictionsgroup (fromforecast()) or aposterior_predictivegroup (frompredict_in_sample()) — or a bareDataset(returned unchanged), and returns the samples as anxarray.Datasetwhose variables retain the fullchain/drawdims. Point summaries are the caller’s choice, e.g.prediction_samples(result)["forecast"].mean(("chain", "draw")).- Raises:
TypeError – If
resultcarries none of the predictive groups.
- pymc_forecast.prediction.thin_draws(posterior, num_samples, random_seed=None)[source]#
Subsample a posterior to
num_samplesdraws (chain-flattened).Draws are selected uniformly without replacement from the flattened
(chain, draw)axes (with replacement only if more draws are requested than exist). The result is a posteriorDatasetwithchain=1, directly consumable bypm.sample_posterior_predictive.