pymc_forecast.priors#
Interop with the pymc-extras Prior API: declarative, user-injectable priors.
innovations and predict accept a pymc-extras
pymc_extras.prior.Prior, so priors live as inspectable data on the
model object instead of inside lambdas:
from pymc_extras.prior import Prior
drift = innovations(h, "drift", Prior("Normal", mu=0, sigma=0.1))
predict(h, Prior("Normal", sigma=Prior("HalfNormal", sigma=1)), pt.cumsum(drift))
The adapters preserve the package’s replay mechanism: nested hyper-priors
(Prior-valued parameters) are materialized once per base name as
{base_name}_{param} — e.g. obs_sigma above, or drift_mu if the
drift prior had a Prior-valued mu — and shared between the in-sample and
*_future segments, so pm.sample_posterior_predictive replays them from
the posterior while the future latents are drawn conditional on them. A naive
per-segment create_variable would instead give the forecast segment fresh
hyper-priors silently drawn from the prior.
On model objects, PriorConfig turns those specifications into a
named, overridable configuration: subclasses declare default_priors and
callers replace any subset with priors={...} at construction time.
pymc-extras is never imported here — Prior objects are recognized
structurally — so the core package keeps that dependency optional.
- class pymc_forecast.priors.PriorConfig(priors=None)[source]#
Bases:
objectMixin: named, user-overridable priors on a model object.
Subclasses declare their defaults in
default_priors; callers override any subset with thepriors=constructor argument, and the model body reads the effective mapping fromprior_config— e.g.self.innovations("drift", self.prior_config["drift"])— or creates a standalone variable withcreate_prior(). Mixed intoForecastingModelandStatespaceModel.- Parameters:
priors (
mapping, optional) – Named overrides merged overdefault_priors(Nonekeeps the defaults); values are pymc-extrasPriorobjects, the factory callables the model primitives accept, or — forcreate_prior()— anyname -> RVcallable. Values are not validated.
- default_priors = {}#
Class-level default priors, overridable per instance via
priors=.
- property prior_config#
The effective priors,
default_priorsmerged with overrides.A
dictmapping names to specifications. This is the instance’s live mapping, not a copy, so mutating it changes the configuration. Falls back to a fresh copy of the defaults when a subclass__init__does not callsuper().__init__().
- create_prior(name)[source]#
Create the model variable
namefrom its configured prior.Must run inside a
pm.Modelcontext. The configured specification either exposescreate_variable(name)(a pymc-extrasPrior, created with its own dims) or is a callablename -> RV, e.g.lambda name: pm.Normal(name, 0, 1).- Parameters:
name (
str) – Key ofprior_configand name of the created variable.- Returns:
pytensor.tensor.TensorVariable– Whatever the specification creates, normally the registered model random variable.- Raises:
- pymc_forecast.priors.is_prior_like(obj)[source]#
Whether
objstructurally matches the pymc-extrasPriorAPI.
- pymc_forecast.priors.prior_obs_factory(prior, base_name)[source]#
Adapt a
Priorto thepymc_forecast.model.ObsFactoryprotocol.The prior’s distribution becomes the likelihood: its location (
mu) is the latent predictor handed in bypredict(), and nested hyper-priors — e.g. the noise scale — are materialized once underbase_nameand shared by the observed and forecast segments.The likelihood is built directly from the materialized parameters rather than through
Prior.create_likelihood_variable, which deep-copies the prior and would clone the shared hyper-prior variables out of the model graph.- Parameters:
prior (
pymc_extras.prior.Prior) – The observation prior;mumust be unset and its distribution must accept amukeyword.base_name (
str) – Prefix for the hyper-prior variables, named{base_name}_{param}.
- Returns:
callable– A factoryobs_fn(name, latent, dims, observed)that registers and returns the likelihood variable in the activepm.Model. Its first call raisesHorizonErrorif a nested hyper-prior has a"time"or"time_future"dim.- Raises:
ValueError – If
priorsetsmu(checked immediately).
- pymc_forecast.priors.prior_rv_factory(prior, base_name)[source]#
Adapt a
Priorto thepymc_forecast.model.RVFactoryprotocol.The returned factory creates whatever variable
nameit is called with (e.g.base_nameand{base_name}_future) frompriorwith the dims it is handed, materializing nested hyper-priors once underbase_nameon first use. It is stateful, so build a new factory for each model.- Parameters:
prior (
pymc_extras.prior.Prior) – The prior to adapt; it is deep-copied, not mutated.base_name (
str) – Prefix for the hyper-prior variables, named{base_name}_{param}.
- Returns:
callable– A factoryrv_fn(name, dims)that registers and returns the variable in the activepm.Model. Its first call raisesHorizonErrorif a nested hyper-prior has a"time"or"time_future"dim.