ConditionedModel#

class pymc_marketing.pytensor_utils.ConditionedModel(model, idata, *, intervention=None, frozen_deterministics=None)[source]#

A model after an intervention, and the one owner of its posterior binding.

A caller hands over a model, a posterior and, optionally, an intervention – a mapping of pymc.do() arguments, which this object applies itself. It hands out response graphs conditioned on the posterior and rebinds all of them when the posterior moves.

Every graph reads the draws through one SharedPosterior, so set_posterior() points every graph handed out so far – and every function compiled from one – at the new draws without extracting or compiling again, whatever the number of draws.

Extraction is cached by response names in a bounded LRU: a repeated extract() of a cached key returns the identical graph, and a new derived node per call evicts the least recently used entry. Eviction only drops the cache entry: an evicted graph still reads the shared variables and keeps following every rebind. A new node per call costs an extraction and a cache entry every time, so a caller that derives one should keep the graph it gets back and reuse it.

A posterior is reordered to the model’s coords per dimension on the way in. A variable is validated when a graph first reads it, against its layout in the construction posterior, and on every rebind after that; a variable no graph reads is not validated.

The model is always cloned, by pymc.do() when an intervention is given, so its data are fixed at construction: a later pm.set_data on the model does not reach any graph handed out here. For the same reason a graph that reads a node of the source model, or a shared or random variable from outside the clone, is refused.

Parameters:
  • model (Model) – The model to intervene on. It is cloned, never modified.

  • idata (DataTree | Dataset) – Posterior draws to condition on. A tree’s posterior group is used.

  • intervention (Mapping[str, Variable | ndarray | float] | None) – A mapping of pm.do arguments. None clones the model unchanged.

  • frozen_deterministics (list[str] | None) – Names of Deterministic variables to freeze at their posterior values.

:raises NotImplementedError, ValueError: If PyMC cannot clone model, as for a model with custom initial

values or nested sub-models.

Notes

Cloning copies every input of the model, including a free symbolic input that is not a model variable, so a graph extracted here reads the clone’s copy of it and a function compiled against the original input fails to compile. Pass such an input through intervention instead: pymc.do() keeps the inputs of an intervention expression.

Examples

spend = {"channel_data": new_spend}
conditioned = ConditionedModel(model, idata, intervention=spend)
graph = conditioned.extract("channel_contribution")
fn = pytensor.function([], graph.values)
fn()  # under ``idata``
conditioned.set_posterior(updated_idata)
fn()  # under ``updated_idata``, no recompile

Methods

ConditionedModel.__init__(model, idata, *[, ...])

ConditionedModel.extract(response_vars)

Return response graphs conditioned on the current posterior.

ConditionedModel.set_posterior(idata)

Point every graph handed out so far at the draws in idata.

Attributes

ConditionedModel.model

The clone every response is resolved against, intervened when an intervention was given.

ConditionedModel.posterior

The draws the graphs are currently conditioned on.

ConditionedModel.shared_posterior

The SharedPosterior every graph reads its draws from.