PyMCForecastModel#
- class causalpy.pymc_forecast_models.PyMCForecastModel[source]#
Wrap a
pymc_forecastmodel as a CausalPy time-series backend.- Parameters:
model_fn (
Any) – The upstream 0.3 forecasting model body(covariates, data=None) -> Noneor apymc_forecast.ForecastingModelinstance. Priors flow through this object (pymc-extrasPrior), preserving the transparent-prior ethos.forecaster (
type|None) – Thepymc_forecastforecaster class used to fit the model. Defaults topymc_forecast.HMCForecaster(NUTS), matching CausalPy’s native PyMC backends. Passpymc_forecast.Forecaster(ADVI) orpymc_forecast.PathfinderForecasterfor faster approximate inference — but check convergence before trusting the counterfactual.pymc_forecast.StatespaceForecasteris not supported yet (its outputs carry no noise-free latent; see pymc-forecast#50).forecaster_kwargs (
dict[str,Any] |None) – Extra keyword arguments for the forecaster constructor (e.g.{"draws": 500}for MCMC,{"num_steps": 20_000}for ADVI, or{"progressbar": True}— accepted uniformly by every forecaster). The forecaster is constructed immediately (unfitted), so invalid options fail at construction rather than at experiment time.num_samples (
int) – Number of posterior draws, drawn once at fit time and shared by every predictive call so that in-sample prediction and counterfactual are conditioned on the same parameter draws.random_seed (
int|None) – Seed passed to fitting, the posterior subsample, and every predictive call.
Examples
>>> import causalpy as cp >>> import pandas as pd >>> import pymc as pm >>> import pytensor.tensor as pt >>> import pymc_forecast >>> def local_level(covariates, data=None): ... h = pymc_forecast.Horizon.from_data(covariates, data) ... drift = pymc_forecast.innovations(h, "drift", pm.Normal.dist(0, 0.1)) ... sigma = pm.HalfNormal("sigma", 1) ... pymc_forecast.predict( ... h, ... pm.Normal.dist(0, sigma), ... pt.cumsum(drift), ... expected_observation=pt.cumsum(drift), ... ) >>> result = cp.InterruptedTimeSeries( ... df, ... treatment_time, ... formula="y ~ 0", ... model=cp.pymc_forecast_models.PyMCForecastModel(local_level), ... )
Methods
PyMCForecastModel.fit(X, y[, coords])Construct and fit the forecasting model on the pre-period.
PyMCForecastModel.predict(X[, coords, ...])Predict in-sample (pre-period) or forecast the counterfactual.
Print posterior means and HDIs of the model's scalar parameters.
PyMCForecastModel.score(X, y[, coords])Bayesian \(R^2\) of the in-sample posterior predictive vs
y.Attributes
fit_idataFull inference result of the underlying forecaster fit.
- __init__(model_fn, forecaster=None, forecaster_kwargs=None, num_samples=500, random_seed=None)[source]#
- classmethod __new__(*args, **kwargs)#