$ show py-why/dowhy
DoWhy is a mature Python library for causal inference that implements a systematic four-step workflow: model (encode causal graph), identify (derive estimand), estimate (compute causal effect), and refute (validate robustness). The library supports two major frameworks: (1) classical potential-outcomes-based estimation with ATE/ATT/CATE via backdoor, frontdoor, instrumental variables, and mediation analysis, leveraging multiple estimators including linear regression, propensity score methods, doubly robust, instrumental variable, regression discontinuity, and integrations with EconML and CausalML; (2) Graphical Causal Model (GCM) based inference for what-if analysis, counterfactual sampling, distribution change attribution, arrow strength, intrinsic causal influence, anomaly attribution, and root cause analysis. The architecture is modular with abstract base classes for estimators, refuters, interpreters, and GCM components. The main entry point is the CausalModel class, which orchestrates identification, estimation, and refutation via a consistent API. Key dependencies include scikit-learn, statsmodels, networkx, sympy, pandas, numpy, and optional integrations with Torch, EconML, Causal-learn, and TabPFN. The library is production-ready with comprehensive test suites, CI/CD, and extensive documentation, and is actively maintained with a large contributor base.
The LLM identified 3 files worth highlighting for reuse:
This module automatically assigns causal mechanisms (e.g., additive noise, post-nonlinear, classifier) to each node based on data characteristics (continuous, categorical, binary). It uses scikit-learn's model selection to score multiple regressors/classifiers and picks the best by MSE or log loss. The pattern is reusable for any project requiring adaptive model assignment in a structural causal model, and is demonstrated through clear abstractions and configurable thresholds.
Implements Shapley value estimation with multiple approximation strategies ( EXACT, PERMUTATION, EARLY_STOPPING, SUBSET_SAMPLING, EXACT_FAST) for feature relevance and causal attribution. The implementation is self-contained, uses joblib for parallelisation, and provides a clean interface for re-use in other domains requiring cooperative game theory attributions. Demonstrates non-obvious skill in handling of exponential complexity through intelligent sampling strategies.
Provides an Encoders class that consistently handles one-hot encoding and target encoding of categorical variables across estimation, refutation, and GCM modules. Integrates with pandas DataFrames and pandas Categoricals. The pattern is isolated and re-usable for any Python project needing robust, configurable encoding of mixed-type data with fallback strategies.