Quickstart: Python API
This page shows the fastest direct path from a pandas dataset to a fitted
PanelMMM.
If you have not prepared your dataset yet, read Data Preparation first.
Load a dataset
The repository includes bundled demo datasets under data/demo/. The
timeseries bundle is the simplest starting point because it has no extra panel
dimensions.
Construct PanelMMM
This example uses a plain timeseries. If your dataset has panel dimensions such
as geo or brand, add them with dims=(...) and keep those columns in X.
Fit the model
You can call fit() directly. If the model graph has not been built yet,
Abacus builds it for you.
fit() returns an arviz.InferenceData object and also stores it on the model
instance as mmm.idata.
Prior and posterior predictive checks
You can sample prior predictive draws before fitting:
After fitting, you can sample posterior predictive draws:
By default, this also stores posterior predictive draws on mmm.idata.
When to call build_model()
Call build_model(X, y) explicitly when you want to inspect or modify the PyMC
graph before sampling.
For example, you might build first so that you can add stored original-scale deterministics:
After that, fit the already-built model:
Reusing a built or fitted model
An existing model graph can only be fitted with the data used to construct it.
This applies to both fit() and approximate_fit(), including models restored
with PanelMMM.load(). Equal copies of the training inputs are accepted;
changed outcomes, predictors, dates or panel units raise ValueError before
inference starts. Use a fresh model instance to fit a different dataset.
Abacus checks the retained training values and coordinates, not Python object identity. Mutating the original DataFrame or array after construction does not change the recorded training data. The check also rejects fitting after an in-place update of the graph’s training data. Posterior prediction clones the PanelMMM graph by default; keep that default if you intend to fit it again.
Building explicitly and then fitting identical data preserves additions to the graph, including calibration terms and extra deterministics. Abacus does not silently rebuild the graph when fitting inputs change.
Supply the real target when constructing a graph for prior predictive checks that you intend to fit later:
Omitting y during prior construction creates a graph with zero targets. A
later fit with different target values is rejected; construct a fresh instance
for that fit.
This guard does not retrospectively validate saved results. If an earlier fit reused a graph with changed inputs, rerun it from a fresh instance.
Basic outputs
After fitting, common next steps are:
You can also inspect:
mmm.posteriormmm.posterior_predictivemmm.summarymmm.diagnostics
Next steps
- Read Quickstart: YAML Builder if you want to move model configuration into YAML.
- Read Model Fitting for fitting, save/load, and predictive-check workflows in more detail.