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: object

Mixin: named, user-overridable priors on a model object.

Subclasses declare their defaults in default_priors; callers override any subset with the priors= constructor argument, and the model body reads the effective mapping from prior_config — e.g. self.time_series("drift", self.prior_config["drift"]) — or creates a standalone variable with create_prior(). Mixed into ForecastingModel and StatespaceModel.

Parameters:

priors – Named overrides merged over default_priors; values are pymc-extras Prior objects, the factory callables the model primitives accept, or — for create_prior() — any name -> RV callable.

default_priors = {}#

Class-level default priors, overridable per instance via priors=.

property prior_config#

default_priors merged with overrides.

Falls back to the defaults when a subclass __init__ does not call super().__init__().

Type:

The effective priors

create_prior(name)[source]#

Create the model variable name from its configured prior.

Must run inside a pm.Model context. The configured specification either exposes create_variable(name) (a pymc-extras Prior, created with its own dims) or is a callable name -> RV, e.g. lambda name: pm.Normal(name, 0, 1).

pymc_forecast.priors.is_prior_like(obj)[source]#

Whether obj structurally matches the pymc-extras Prior API.

pymc_forecast.priors.prior_obs_factory(prior, base_name)[source]#

Adapt a Prior to the ObsFactory protocol.

The prior’s distribution becomes the likelihood: its location (mu) is the latent predictor handed in by predict(), and nested hyper-priors — e.g. the noise scale — are materialized once under base_name and 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.

pymc_forecast.priors.prior_rv_factory(prior, base_name)[source]#

Adapt a Prior to the RVFactory protocol.

The returned factory creates each segment variable (base_name, {base_name}_future) from prior with the dims it is handed, materializing nested hyper-priors once under base_name on first use.