pymc_forecast_models#

Adapter that lets a pymc_forecast forecasting model act as a model provider behind CausalPy’s experiment API.

CausalPy keeps identification, counterfactual construction, and placebo methods; pymc_forecast provides the fitted forecasting model. The wrapper maps CausalPy’s backend protocol onto the pymc_forecast drivers:

  • fit(X, y) constructs and fits the forecasting model on the pre-period.

  • predict(X) (in-sample) uses predict_in_sample().

  • predict(X, out_of_sample=True) draws the counterfactual with forecast(future_covariates=...) when the design matrix has columns, or forecast(future_index=...) for a covariate-free trend/seasonal model.

  • Draw-level samples are extracted with prediction_samples() and the documented output schema dims are renamed onto CausalPy coords (time / time_future -> obs_ind, series -> treated_units).

When to reach for this backend vs the existing PyMCModel classes: use PyMCForecastModel when the counterfactual is best expressed as a proper forecasting model (local level / trend, stochastic seasonality, ARIMA- style dynamics) built with pymc_forecast primitives, and you want its priors, inference backends (ADVI / NUTS / Pathfinder), and forecasting machinery. Stick with the native PyMCModel classes (e.g. LinearRegression, BayesianBasisExpansionTimeSeries) for plain regression-style counterfactuals or when you need model coefficients tied to the patsy design matrix.

Requires the optional dependency pymc-forecast (pip install causalpy[forecast]).

Notes

Causal impact uses upstream expected_observation / expected_observation_future when provided: the conditional expected outcome in observed units, including parameter and latent uncertainty but excluding observation-level noise. Otherwise CausalPy uses mu / mu_future under the model author’s responsibility that this latent is already an outcome-scale expectation. Models with a link function should pass expected_observation=inverse_link(latent) to pymc_forecast.predict. The adapter never infers or inverts links. The draw-level posterior predictive of the observed variable is reported separately as y_hat. One posterior subsample is drawn at fit time and shared by every predictive call, so draw i of the pre-period fit and draw i of the counterfactual come from the same parameter draw (upstream posterior= passthrough).

StatespaceForecaster models are rejected for now: their upstream outputs carry no separate noise-free latent, so the impact convention above cannot be honoured without silently substituting the noisy predictive. Tracked upstream as pymc-forecast#50.

Inference diagnostics: PyMCForecastModel.idata holds the thinned, draw-coherent posterior subsample used for prediction; the full fit result (e.g. the complete NUTS DataTree with sample stats) is exposed as PyMCForecastModel.fit_idata.

Classes

PyMCForecastModel

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