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
