liveBook · Manning

Pyro.do

As explained in Causal AI

An intervention operator is a formal operation on a structural causal model that replaces a target variable's assignment function with a specified intervention value.

Suppose higher side-quest engagement is associated with more in-game purchases. Guild membership affects both, so that association alone does not answer what would happen if every player’s engagement were set to high. `pyro.do` creates a version of a Pyro causal model for that intervention, allowing its consequences to be simulated. section 7.1.6 section 7.1.7

Call `pyro.do(model, {"Side-quest Engagement": tensor(1.)})` to obtain a new model with engagement fixed at high; use `tensor(0.)` for low. Calling either returned model generates outcomes under its specified intervention. Unlike conditioning on players whose observed engagement was high, this operation sets engagement regardless of the causes that would ordinarily determine it. section 7.1.6 section 7.2.6

Operationally, `pyro.do` finds a targeted `pyro.sample` call and replaces its sampled value with the supplied value. This parallels replacing a target’s assignment function in a structural causal model. In a causal graphical model, the corresponding operation removes incoming edges and puts all probability on the intervention value. Other assignments remain unchanged; downstream computations can respond to the new value. The sample-site replacement describes the essential operation, not every implementation detail. section 7.2.2 section 7.2.5 section 7.2.6

Simulate two engagement policies

Create high- and low-engagement models, draw 500 purchase outcomes from each, and compare their means. In one simulation, mean purchases were about 89.9 for high engagement and 130.7 for low engagement; the corresponding experimental means were about 93.0 and 131.4. Agreement is useful evidence for this model, not a guarantee: errors in the causal DAG can make the intervention analysis wrong. section 7.1.6 section 7.1.7

Listing 7.9 Representing interventions with pyro.do
int_engaged_model = pyro.do(    #1
    model,    #1
    {"Side-quest Engagement": tensor(1.)}   #1
)    #1
int_unengaged_model = pyro.do(     #2
    model,    #2
    {"Side-quest Engagement": tensor(0.)}     #2
)    #2

The listing shows the model-and-target-value call that creates separate high- and low-engagement intervention models. section 7.1.6

The returned model also supports counterfactual workflows. For a hypothetical femur length of 46 cm, sample it with the original exogenous distribution to obtain an interventional height distribution; sample the same model with an exogenous posterior informed by observed measurements to obtain a counterfactual distribution. In a Bayesian workflow, combine intervened models with `Predictive` to estimate an average treatment effect and its posterior uncertainty. section 9.3.2 section 9.3.4 section 11.6.6

How Causal AI covers it

How deep it goes
Explains the targeted `pyro.sample` replacement and works through sampling workflows, while deferring additional implementation details.
Examples
Uses gaming engagement and purchases for experiment simulation, then femur length and height for interventional and counterfactual sampling.
What it assumes
Assumes a specified causal model and its assumptions; explains how an ideal intervention changes the target rather than treating observational association as sufficient.
What you can do after
Apply it via Pyro to create fixed-value intervention models and sample their predicted outcomes.
How it connects
Connects ideal interventions to structural causal models, causal graphical models, counterfactual inference, and average treatment effects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Causal AI. The section links point to the parts of the book it draws on.