pymc_forecast.priors#
Interop with the pymc-extras Prior API: declarative, user-injectable priors.
A pymc-extras Prior is accepted anywhere the model
primitives take a factory callable, so priors live as inspectable data on the
model object instead of inside lambdas:
from pymc_extras.prior import Prior
drift = time_series(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 — e.g.
drift_mu, obs_sigma — 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.time_series("drift", self.prior_config["drift"])— or creates a standalone variable withcreate_prior(). Mixed intoForecastingModelandStatespaceModel.- Parameters:
priors – Named overrides merged over
default_priors; values are pymc-extrasPriorobjects, the factory callables the model primitives accept, or — forcreate_prior()— anyname -> RVcallable.
- default_priors = {}#
Class-level default priors, overridable per instance via
priors=.
- property prior_config#
default_priorsmerged with overrides.Falls back to the defaults when a subclass
__init__does not callsuper().__init__().- Type:
The effective priors
- 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).
- 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 theObsFactoryprotocol.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.