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 covariates extending data along "time"; the surplus covariate steps are the forecast horizon. The posterior is replayed for in-sample latents while *_future variables (absent from it) are drawn fresh.

Parameters:
  • model_fn – The model body ((Horizon, covariates) -> None or a ForecastingModel).

  • posterior – A fitted posterior (DataTree/InferenceData or Dataset).

  • 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 *_future latents, and — for models registered through predict() — 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 from random_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 a predictions group carrying time_future coords.

pymc_forecast.prediction.posterior_dataset(posterior)[source]#

Extract the posterior group as a plain xarray.Dataset.

Accepts an ArviZ DataTree / InferenceData (uses its posterior group) or a bare Dataset (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 through predict(), 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. None for 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 a posterior_predictive group 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 / InferenceData with a predictions group (from forecast()) or a posterior_predictive group (from predict_in_sample()) — or a bare Dataset (returned unchanged), and returns the samples as an xarray.Dataset whose variables retain the full chain / draw dims. Point summaries are the caller’s choice, e.g. prediction_samples(result)["forecast"].mean(("chain", "draw")).

Raises:

TypeError – If result carries none of the predictive groups.

pymc_forecast.prediction.thin_draws(posterior, num_samples, random_seed=None)[source]#

Subsample a posterior to num_samples draws (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 posterior Dataset with chain=1, directly consumable by pm.sample_posterior_predictive.