liveBook · Manning

DoWhy

As explained in Causal AI

DoWhy is an open-source Python library for causal inference that structures causal analysis around modeling assumptions, identification, estimation, and refutation.

DoWhy is an open-source Python library for causal inference. It turns a causal question into an explicit workflow: define the target effect, model assumptions with a DAG, identify an estimand, estimate it, and test its robustness. This separates causal reasoning from the statistical and computational details of estimation. section 11.0 section 2.3.4

A typical DoWhy analysis proceeds through four steps:

  • Specify a causal query, such as an average treatment effect (ATE) or conditional average treatment effect (CATE). section 11.0
  • Represent domain assumptions as a DAG and load the data and relationships into a CausalModel. section 11.7
  • Identify an estimand using backdoor adjustment, front-door adjustment, or instrumental variables; each depends on different causal assumptions. section 11.7
  • Estimate the selected estimand with regression, propensity scores, or machine-learning methods such as double machine learning and meta-learners. section 11.7

The library makes alternative estimators easy to compare, but it does not remove the need to justify the DAG or validate assumptions. Larger datasets may favor machine-learning estimators, while confidence intervals can be computationally expensive; prediction quality alone cannot establish causal validity. section 11.7 section 2.3.4

Smoking, lung cancer, and cigarette price

A Python example constructs a CausalModel for smoking and lung cancer, treats cigarette price as an instrumental variable, identifies the causal effect, and estimates it. The generated code is illustrative: real analyses require imported data, domain-grounded DAGs, rigorous instrumental-variable checks, robustness analysis, and version-aware debugging. section 13.1.2

Listing 13.1 DoWhy code generated by ChatGPT (verbatim)
import numpy as np
import pandas as pd
import dowhy
from dowhy import CausalModel
from dowhy.datasets import linear_dataset
from dowhy.do_why import CausalModel    #1

# Let's create a sample dataset for illustration purposes
# Normally, you'd import your own dataset: data = pd.read_csv('your_dataset.csv')
n_points = 1000
data = pd.DataFrame({
    "S": np.random.binomial(n=1, p=0.5, size=n_points),     #2
    "LC": np.random.binomial(n=1, p=0.5, size=n_points),    #3
    "Price": np.random.normal(loc=5, scale=1, size=n_points),     #4
})

# Create a causal DAG
model=CausalModel(
        data = data,
        treatment='S',
        outcome='LC',
        common_causes=['G', 'A', 'E', 'O'],  # Potential confounders
        instruments=['Price']  # Instrumental Variable
)

# Identify the causal effect
identified_estimand = model.identify_effect(proceed_when_unidentifiable=True)

# Estimate the causal effect using Instrumental Variable method
estimate = model.estimate_effect(identified_estimand,
                                 method_name="iv.instrumental_variable",
                                 method_params={'iv_instrument_name': 'Price'})

# Print the causal effect estimate
print(estimate)

Inspect the generated Python listing to see dataset setup, CausalModel construction, instrumental-variable configuration, and an example of a version-related import error. section 13.1.2

How Causal AI covers it

How deep it goes
Traces a complete workflow through DAG specification and multiple estimators, while deferring rigorous assumption checks and robustness testing.
Examples
Uses smoking and lung cancer with cigarette price as an instrumental variable, plus generated Python code and simulated data setup.
What it assumes
Assumes causal assumptions and domain knowledge for DAGs while teaching the workflow from target query through estimation.
What you can do after
Apply it via Python to specify causal models, identify estimands, and estimate average or conditional treatment effects.
How it connects
Connects causal effects with DAGs, CausalModel, backdoor adjustment, front-door adjustment, instrumental variables, propensity scores, and machine learning.

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.