PyMCForecastModel#

class causalpy.pymc_forecast_models.PyMCForecastModel[source]#

Wrap a pymc_forecast model as a CausalPy time-series backend.

Parameters:
  • model_fn (Any) – The forecasting model body (Horizon, covariates) -> None or a pymc_forecast.ForecastingModel instance. Priors flow through this object (pymc-extras Prior), preserving the transparent-prior ethos.

  • forecaster (type | None) – The pymc_forecast forecaster class used to fit the model. Defaults to pymc_forecast.HMCForecaster (NUTS), matching CausalPy’s native PyMC backends. Pass pymc_forecast.Forecaster (ADVI) or pymc_forecast.PathfinderForecaster for faster approximate inference — but check convergence before trusting the counterfactual. pymc_forecast.StatespaceForecaster is 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(h, covariates):
...     drift = pymc_forecast.time_series(
...         h, "drift", lambda name, dims: pm.Normal(name, 0, 0.1, dims=dims)
...     )
...     sigma = pm.HalfNormal("sigma", 1)
...     pymc_forecast.predict(
...         h,
...         lambda name, mu, dims, observed: pm.Normal(
...             name, mu, sigma, dims=dims, observed=observed
...         ),
...         pt.cumsum(drift),
...     )
>>> result = cp.InterruptedTimeSeries(
...     df,
...     treatment_time,
...     formula="y ~ 0",
...     model=cp.pymc_forecast_models.PyMCForecastModel(local_level),
... )

Methods

PyMCForecastModel.calculate_cumulative_impact(impact)

Cumulative sum of pointwise causal impact along obs_ind.

PyMCForecastModel.calculate_impact(y_true, ...)

Causal impact as observed minus counterfactual, at the draw level.

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.

PyMCForecastModel.print_coefficients(labels)

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_idata

Full inference result of the underlying forecaster fit.

__init__(model_fn, forecaster=None, forecaster_kwargs=None, num_samples=500, random_seed=None)[source]#
Parameters:
  • model_fn (Any)

  • forecaster (type | None)

  • forecaster_kwargs (dict[str, Any] | None)

  • num_samples (int)

  • random_seed (int | None)

Return type:

None

classmethod __new__(*args, **kwargs)#