Package: chirho
Causal Reasoning with ChiRhoChiRho is a causal extension to the Pyro probabilistic programming
language. It was built to bridge the gap between the capabilities of
modern probabilistic programming systems, such as Pyro, and the needs of
policymakers, scientists, and AI researchers, who often want to use
models to answer their questions about cause-and-effect relationships.
As a non-exhaustive set of examples, ChiRho makes it easier to
answer the following kinds of causal questions that appear frequently in
practice.Interventional:How many COVID-19 hospitalizations will occur if
the USA imposes a national mask mandate?Counterfactual:Given that 100,000 people were infected with
COVID-19 in the past month, how many would have been infected if a
mask mandate had been in place?Explanation:Why were 100,000 people infected with COVID-19 in
the past month?Causal structure discovery:What individual attributes influence
risk of COVID-19 hospitalization?To see how ChiRho supports causal reasoning, take a look at ourTutorial.InstallationInstall using pip:pipinstallchirhoInstall from source:gitclonegit@github.com:BasisResearch/chirho.gitcdchirhogitcheckoutmasterpipinstall.Install with extra packages:To install the dependencies required to run the tutorials inexamples/tutorialsdirectories, please use the following
command:pipinstallchirho[extras]Make sure that the models come from the same release version of theChiRho source
codeas you
have installed.Getting StartedBelow is a simple example of how to use ChiRho to answer an
interventional question. For more in-depth examples, go toLearn
more.importtorchimportpyroimportpyro.distributionsasdistfromchirho.interventional.handlersimportdopyro.set_rng_seed(101)# Define a causal model with single confounder hdefmodel():h=pyro.sample("h",dist.Normal(0,1))x=pyro.sample("x",dist.Normal(h,1))y=pyro.sample("y",dist.Normal(x+h,1))returny# Define a causal query (here intervening on x)defqueried_model():returndo(model,{"x":1})# Generate 10,000 samples from the observational distribution P(y) ~ N(0, 2)obs_samples=pyro.infer.Predictive(model,num_samples=1000)()["y"]# Generate 10,000 samples from the interventional distribution P(y | do(X=1)) ~ N(1, 1)int_samples=pyro.infer.Predictive(queried_model(),num_samples=1000)()["y"]Learn moreWe have written a number of tutorials and examples for ChiRho. We
have tried to choose simple examples that would be of interest to both
the causal inference and probabilistic programming communities: they
collectively span Pearl’s causal hierarchy (Pearl 2009), and
most are broadly applicable, empirically validated, have an
unconventional or limited identification result, and make use of modern
probabilistic machine learning tools, like neural networks or stochastic
variational inference.Our examples demonstrate how real-world causal assumptions can be expressed as probabilistic programs
and real-world causal estimands can be expressed as program transformations.
These example illustrate how ChiRho is compatible with any inference method
implemented in Pyro, including the kinds of scalable gradient-based
approximations that power much of the modern probabilistic machine learning landscape.Tutorial- Key observations inspiring ChiRho’s design and outlines a causal Bayesian workflow for using ChiRho to answer causal questionsExample: Backdoor Adjustment Criteria- Adjusting for observed confoundersExample: Causal Effect Variational Autoencoder- Causal inference with deep models and proxy variablesExample: Mediation analysis and (in)direct effects- Mediation analysis for path specific effectsExample: Deep structural causal model counterfactuals- Counterfactuals with normalizing flowsExample: Structured Latent Confounders- Causal effect estimation when latent confounders are shared across groupsExample: Synthetic difference-in-differences- Counterfactual estimation from longitudinal dataExample: Robust estimation with the DR learner- Heterogeneous causal effect estimation with a misspecified modelExample: Estimating the effects of drugs on gene expression- Causal inference with single-cell RNA-seq dataExample: Causal reasoning in dynamical systems- Causal inference with continuous-time dynamical systemsDesign notes- Technical implementation details of ChiRho using effect handlersNote: These tutorials and examples assume some familiarity with Pyro and
probabilistic programming. For introductory Pyro tutorials, please seeAdditional background reading
materialbelow.DocumentationCounterfactual- Effect handlers for counterfactual world splittingInterventional- Effect handlers for performing interventionsObservational- Effect handler utilities for computing probabilistic quantities for
partially deterministic models which is useful for counterfactual reasoningIndexed- Effect handler utilities for named indices in ChiRho which is useful for manipluating
and tracking counterfactual worldsDynamical- Operations and effect handlers for counterfactual reasoning in dynamical systemsCaveatsChiRho does not answer causal questions by magic. In fact, there is
no escaping the fact thatbehind any causal conclusion there must lie some causal assumption,a phrase made famous by Judea Pearl (Pearl 2009). Instead,
ChiRho provides a substrate for writing causal assumptions as
probabilistic programs, and for writing causal questions in terms of
program transformations.Additional background reading materialCausal Probabilistic Programming Without Tearshttps://drive.google.com/file/d/1Uzjg-vX77BdSnAcfpUcb-aIXxhnAPI24/view?usp=sharingIntroduction to Pyro:http://pyro.ai/examples/intro_long.htmlTensor shapes in Pyro:http://pyro.ai/examples/tensor_shapes.htmlA guide to programming with effect handlers in
Pyrohttp://pyro.ai/examples/effect_handlers.htmlMinipyro:http://pyro.ai/examples/minipyro.htmlReparameterization of Pyro
programs:https://docs.pyro.ai/en/stable/infer.reparam.htmlOptional: getting started with
NumPyrohttps://num.pyro.ai/en/stable/getting_started.htmlReferencesPearl, Judea.Causality: Models, Reasoning and Inference. 2nd ed. USA: Cambridge University Press, 2009.