pymc_forecast.forecaster#
Forecaster, HMCForecaster, and PathfinderForecaster call fit_vi,
fit_mcmc, and fit_pathfinder. Constructors and attributes did not change.
A model body passed to them is (covariates, data=None).
Forecaster classes: fit a forecasting model, then draw probabilistic forecasts.
Three inference backends behind one interface: Forecaster
(variational, ADVI by default), HMCForecaster (MCMC via
pm.sample), and PathfinderForecaster (pymc-extras Pathfinder).
Construction fits the model on (data, covariates); forecast
then rebuilds the model over extended covariates and samples the horizon.
Construct without data to defer the fit (call fit later).
- class pymc_forecast.forecaster.Forecaster(model_fn, data=None, covariates=None, *, method='advi', optimizer=None, backend=None, num_steps=10000, random_seed=None, progressbar=None, fit_kwargs=None)[source]#
Bases:
BaseForecasterFit a forecasting model with variational inference (ADVI by default).
Mean-field ADVI can underconverge silently — the posterior looks fine but is biased and overconfident. A post-fit heuristic warns when the median ELBO loss over the final 10% of steps (at least 10) is still clearly below the median over a same-sized window starting mid-run; absence of the warning is not proof of convergence, so check
losseshas plateaued before trusting results, and preferHMCForecasterwhen accuracy matters more than speed.- Parameters:
model_fn (
callableorpymc_forecast.model.ForecastingModel) – The model body (seeBaseForecaster).data (
xarray.DataArray,pandas.Series,pandas.DataFrameorarray_like, optional) – Observed training series, normalized withas_dataarray().Noneconstructs the forecaster unfitted; callfitlater.covariates (
xarray.DataArray,pandas.Series,pandas.DataFrameorarray_like, optional) – Covariates on the same"time"coordinate asdata; rows past the training window are dropped during fitting.Nonefor models without covariates.method (
strorpymc.variational.Inference, default"advi") – VI method:"advi"(mean-field),"fullrank_advi","svgd"or"asvgd"(case-insensitive, forwarded topm.fit), or apymc.variational.Inferenceinstance — which fits the model it was built on and ignores this forecaster’s model andrandom_seed. The JAX backend accepts only"advi".optimizer (
floatorcallable, optional) –None(Adam with lr0.01), a positive learning rate, or a PyMC optimizer such aspm.adam(learning_rate=...). The JAX backend acceptsNoneor a positive learning rate.backend (
{"pytensor", "jax"}, optional) –Noneor"pytensor"usespm.fit."jax"runs mean-field ADVI and Adam as one JAXlax.scanon the selected accelerator (GPU when a CUDA JAX is installed), while retaining PyMC’s ordinary approximation object for posterior sampling. The optionaljaxextra is required.num_steps (
int, default10_000) – Number of optimization steps.random_seed (
int, optional) – Seed for the fit; also the default seed for laterfitcalls.progressbar (
bool, optional) – Show the fit progress bar;Nonemeans off. Ignored by the JAX backend. May instead be given infit_kwargs, but not both.fit_kwargs (
mapping, optional) – Extra keyword arguments forpm.fit.progressbaris accepted here for compatibility, but the direct argument is preferred (passing both raises). Must be empty withbackend="jax".
- model_fn#
The model body passed to the constructor.
- Type:
callableorpymc_forecast.model.ForecastingModel
- approx#
The fitted approximation (set by fitting).
- losses#
The per-step loss history (
approx.hist); empty for"svgd"and"asvgd", which record no loss (set by fitting).- Type:
- Raises:
pymc_forecast.exceptions.MethodResolutionError – If, on construction,
backendis unknown,optimizeris not a positive learning rate or (pytensor backend) a callable, or, withbackend="jax",methodis not"advi"orfit_kwargsis non-empty; or if, when fitting,methodis an unknown name.ValueError – If
progressbaris given both directly and infit_kwargs, ifcovariatesis given withoutdata, or (JAX backend) ifnum_stepsis not positive.pymc_forecast.exceptions.AlignmentError – If (when fitting)
dataorcovariatescannot be normalized or do not align along"time".pymc_forecast.exceptions.HorizonError – If (when fitting) the model body does not register
"obs"or misuses the model primitives.pymc_forecast.exceptions.OptionalDependencyError – If
backend="jax"and JAX is not installed (when fitting).
- Warns:
UserWarning – When the post-fit heuristic finds the ELBO still descending, or the loss history contains non-finite values so convergence cannot be assessed.
- class pymc_forecast.forecaster.HMCForecaster(model_fn, data=None, covariates=None, *, draws=1000, tune=1000, chains=2, nuts_sampler='pymc', random_seed=None, progressbar=None, sample_kwargs=None)[source]#
Bases:
BaseForecasterFit a forecasting model with MCMC (NUTS by default).
- Parameters:
model_fn (
callableorpymc_forecast.model.ForecastingModel) – The model body (seeBaseForecaster).data (
xarray.DataArray,pandas.Series,pandas.DataFrameorarray_like, optional) – Observed training series, normalized withas_dataarray().Noneconstructs the forecaster unfitted; callfitlater.covariates (
xarray.DataArray,pandas.Series,pandas.DataFrameorarray_like, optional) – Covariates on the same"time"coordinate asdata; rows past the training window are dropped during fitting.Nonefor models without covariates.draws (
int, default1000) – Number of posterior draws per chain.tune (
int, default1000) – Number of tuning steps per chain.chains (
int, default2) – Number of chains.nuts_sampler (
{"pymc", "nutpie", "numpyro", "blackjax"}, default"pymc") – NUTS backend, forwarded topm.sample; non-PyMC samplers need their optional package installed.random_seed (
intornumpy.random.Generator, optional) – Seed for the fit, forwarded topm.sample; also the default seed for laterfitcalls.progressbar (
bool, optional) – Show the sampling progress bar;Nonemeans off. May instead be given insample_kwargs, but not both.sample_kwargs (
mapping, optional) – Extra keyword arguments forpm.sample.progressbaris accepted here for compatibility, but the direct argument is preferred (passing both raises).
- model_fn#
The model body passed to the constructor.
- Type:
callableorpymc_forecast.model.ForecastingModel
- idata#
The full MCMC result (posterior, sample stats, …), as returned by
pm.sample(set by fitting).- Type:
xarray.DataTreeorarviz.InferenceData
- Raises:
ValueError – If
progressbaris given both directly and insample_kwargs, orcovariatesis given withoutdata.pymc_forecast.exceptions.AlignmentError – If (when fitting)
dataorcovariatescannot be normalized or do not align along"time".pymc_forecast.exceptions.HorizonError – If (when fitting) the model body does not register
"obs"or misuses the model primitives.
- class pymc_forecast.forecaster.PathfinderForecaster(model_fn, data=None, covariates=None, *, random_seed=None, progressbar=None, pathfinder_kwargs=None)[source]#
Bases:
BaseForecasterFit a forecasting model with Pathfinder variational inference.
A thin wrapper over
pymc_extras.fit_pathfinder. pymc-extras is imported only when fitting, so constructing without data does not require it.- Parameters:
model_fn (
callableorpymc_forecast.model.ForecastingModel) – The model body (seeBaseForecaster).data (
xarray.DataArray,pandas.Series,pandas.DataFrameorarray_like, optional) – Observed training series, normalized withas_dataarray().Noneconstructs the forecaster unfitted; callfitlater.covariates (
xarray.DataArray,pandas.Series,pandas.DataFrameorarray_like, optional) – Covariates on the same"time"coordinate asdata; rows past the training window are dropped during fitting.Nonefor models without covariates.random_seed (
int, optional) – Seed for the fit, forwarded topymc_extras.fit_pathfinder; also the default seed for laterfitcalls.progressbar (
bool, optional) – Show the fit progress bar;Nonemeans off. May instead be given inpathfinder_kwargs, but not both.pathfinder_kwargs (
mapping, optional) – Extra keyword arguments forpymc_extras.fit_pathfinder(e.g.num_paths,num_draws).progressbaris accepted here for compatibility, but the direct argument is preferred (passing both raises).
- model_fn#
The model body passed to the constructor.
- Type:
callableorpymc_forecast.model.ForecastingModel
- idata#
The Pathfinder result with its
posteriorgroup (set by fitting).- Type:
xarray.DataTreeorarviz.InferenceData
- Raises:
ValueError – If
progressbaris given both directly and inpathfinder_kwargs, orcovariatesis given withoutdata.pymc_forecast.exceptions.AlignmentError – If (when fitting)
dataorcovariatescannot be normalized or do not align along"time".pymc_forecast.exceptions.HorizonError – If (when fitting) the model body does not register
"obs"or misuses the model primitives.pymc_forecast.exceptions.OptionalDependencyError – If pymc-extras is not installed (when fitting).