Causal Inference in Python

Book description

How many buyers will an additional dollar of online marketing bring in? Which customers will only buy when given a discount coupon? How do you establish an optimal pricing strategy? The best way to determine how the levers at our disposal affect the business metrics we want to drive is through causal inference.

In this book, author Matheus Facure, senior data scientist at Nubank, explains the largely untapped potential of causal inference for estimating impacts and effects. Managers, data scientists, and business analysts will learn classical causal inference methods like randomized control trials (A/B tests), linear regression, propensity score, synthetic controls, and difference-in-differences. Each method is accompanied by an application in the industry to serve as a grounding example.

With this book, you will:

  • Learn how to use basic concepts of causal inference
  • Frame a business problem as a causal inference problem
  • Understand how bias gets in the way of causal inference
  • Learn how causal effects can differ from person to person
  • Use repeated observations of the same customers across time to adjust for biases
  • Understand how causal effects differ across geographic locations
  • Examine noncompliance bias and effect dilution

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Prerequisites
    2. Outline
    3. Conventions Used in This Book
    4. Using Code Examples
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  2. I. Fundamentals
  3. 1. Introduction to Causal Inference
    1. What Is Causal Inference?
    2. Why We Do Causal Inference
    3. Machine Learning and Causal Inference
    4. Association and Causation
      1. The Treatment and the Outcome
      2. The Fundamental Problem of Causal Inference
      3. Causal Models
      4. Interventions
      5. Individual Treatment Effect
      6. Potential Outcomes
      7. Consistency and Stable Unit Treatment Values
      8. Causal Quantities of Interest
      9. Causal Quantities: An Example
    5. Bias
      1. The Bias Equation
      2. A Visual Guide to Bias
    6. Identifying the Treatment Effect
      1. The Independence Assumption
      2. Identification with Randomization
    7. Key Ideas
  4. 2. Randomized Experiments and Stats Review
    1. Brute-Force Independence with Randomization
    2. An A/B Testing Example
    3. The Ideal Experiment
    4. The Most Dangerous Equation
    5. The Standard Error of Our Estimates
    6. Confidence Intervals
    7. Hypothesis Testing
      1. Null Hypothesis
      2. Test Statistic
    8. p-values
    9. Power
    10. Sample Size Calculation
    11. Key Ideas
  5. 3. Graphical Causal Models
    1. Thinking About Causality
      1. Visualizing Causal Relationships
      2. Are Consultants Worth It?
    2. Crash Course in Graphical Models
      1. Chains
      2. Forks
      3. Immorality or Collider
      4. The Flow of Association Cheat Sheet
      5. Querying a Graph in Python
    3. Identification Revisited
    4. CIA and the Adjustment Formula
    5. Positivity Assumption
    6. An Identification Example with Data
    7. Confounding Bias
      1. Surrogate Confounding
      2. Randomization Revisited
    8. Selection Bias
      1. Conditioning on a Collider
      2. Adjusting for Selection Bias
      3. Conditioning on a Mediator
    9. Key Ideas
  6. II. Adjusting for Bias
  7. 4. The Unreasonable Effectiveness of Linear Regression
    1. All You Need Is Linear Regression
      1. Why We Need Models
      2. Regression in A/B Tests
      3. Adjusting with Regression
    2. Regression Theory
      1. Single Variable Linear Regression
      2. Multivariate Linear Regression
    3. Frisch-Waugh-Lovell Theorem and Orthogonalization
      1. Debiasing Step
      2. Denoising Step
      3. Standard Error of the Regression Estimator
      4. Final Outcome Model
      5. FWL Summary
    4. Regression as an Outcome Model
    5. Positivity and Extrapolation
    6. Nonlinearities in Linear Regression
      1. Linearizing the Treatment
      2. Nonlinear FWL and Debiasing
    7. Regression for Dummies
      1. Conditionally Random Experiments
      2. Dummy Variables
      3. Saturated Regression Model
      4. Regression as Variance Weighted Average
      5. De-Meaning and Fixed Effects
    8. Omitted Variable Bias: Confounding Through the Lens of Regression
    9. Neutral Controls
      1. Noise Inducing Control
      2. Feature Selection: A Bias-Variance Trade-Off
    10. Key Ideas
  8. 5. Propensity Score
    1. The Impact of Management Training
    2. Adjusting with Regression
    3. Propensity Score
      1. Propensity Score Estimation
      2. Propensity Score and Orthogonalization
      3. Propensity Score Matching
      4. Inverse Propensity Weighting
      5. Variance of IPW
      6. Stabilized Propensity Weights
      7. Pseudo-Populations
      8. Selection Bias
      9. Bias-Variance Trade-Off
      10. Positivity
    4. Design- Versus Model-Based Identification
    5. Doubly Robust Estimation
      1. Treatment Is Easy to Model
      2. Outcome Is Easy to Model
    6. Generalized Propensity Score for Continuous Treatment
    7. Key Ideas
  9. III. Effect Heterogeneity and Personalization
  10. 6. Effect Heterogeneity
    1. From ATE to CATE
    2. Why Prediction Is Not the Answer
    3. CATE with Regression
    4. Evaluating CATE Predictions
    5. Effect by Model Quantile
    6. Cumulative Effect
    7. Cumulative Gain
    8. Target Transformation
    9. When Prediction Models Are Good for Effect Ordering
      1. Marginal Decreasing Returns
      2. Binary Outcomes
    10. CATE for Decision Making
    11. Key Ideas
  11. 7. Metalearners
    1. Metalearners for Discrete Treatments
      1. T-Learner
      2. X-Learner
    2. Metalearners for Continuous Treatments
      1. S-Learner
      2. Double/Debiased Machine Learning
    3. Key Ideas
  12. IV. Panel Data
  13. 8. Difference-in-Differences
    1. Panel Data
    2. Canonical Difference-in-Differences
      1. Diff-in-Diff with Outcome Growth
      2. Diff-in-Diff with OLS
      3. Diff-in-Diff with Fixed Effects
      4. Multiple Time Periods
      5. Inference
    3. Identification Assumptions
      1. Parallel Trends
      2. No Anticipation Assumption and SUTVA
      3. Strict Exogeneity
      4. No Time Varying Confounders
      5. No Feedback
      6. No Carryover and No Lagged Dependent Variable
    4. Effect Dynamics over Time
    5. Diff-in-Diff with Covariates
    6. Doubly Robust Diff-in-Diff
      1. Propensity Score Model
      2. Delta Outcome Model
      3. All Together Now
    7. Staggered Adoption
      1. Heterogeneous Effect over Time
      2. Covariates
    8. Key Ideas
  14. 9. Synthetic Control
    1. Online Marketing Dataset
    2. Matrix Representation
    3. Synthetic Control as Horizontal Regression
    4. Canonical Synthetic Control
    5. Synthetic Control with Covariants
    6. Debiasing Synthetic Control
    7. Inference
    8. Synthetic Difference-in-Differences
      1. DID Refresher
      2. Synthetic Controls Revisited
      3. Estimating Time Weights
      4. Synthetic Control and DID
    9. Key Ideas
  15. V. Alternative Experimental Designs
  16. 10. Geo and Switchback Experiments
    1. Geo-Experiments
    2. Synthetic Control Design
      1. Trying a Random Set of Treated Units
      2. Random Search
    3. Switchback Experiment
      1. Potential Outcomes of Sequences
      2. Estimating the Order of Carryover Effect
      3. Design-Based Estimation
      4. Optimal Switchback Design
      5. Robust Variance
    4. Key Ideas
  17. 11. Noncompliance and Instruments
    1. Noncompliance
    2. Extending Potential Outcomes
    3. Instrument Identification Assumptions
    4. First Stage
    5. Reduced Form
    6. Two-Stage Least Squares
    7. Standard Error
    8. Additional Controls and Instruments
      1. 2SLS by Hand
      2. Matrix Implementation
    9. Discontinuity Design
      1. Discontinuity Design Assumptions
      2. Intention to Treat Effect
      3. The IV Estimate
      4. Bunching
    10. Key Ideas
  18. 12. Next Steps
    1. Causal Discovery
    2. Sequential Decision Making
    3. Causal Reinforcement Learning
    4. Causal Forecasting
    5. Domain Adaptation
    6. Closing Thoughts
  19. Index
  20. About the Author

Product information

  • Title: Causal Inference in Python
  • Author(s): Matheus Facure
  • Release date: July 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098140250