Causal Inference and Discovery in Python - Aleksander Molak - E-Book

Causal Inference and Discovery in Python E-Book

Aleksander Molak

0,0
28,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Causal methods present unique challenges compared to traditional machine learning and statistics. Learning causality can be challenging, but it offers distinct advantages that elude a purely statistical mindset. Causal Inference and Discovery in Python helps you unlock the potential of causality.
You’ll start with basic motivations behind causal thinking and a comprehensive introduction to Pearlian causal concepts, such as structural causal models, interventions, counterfactuals, and more. Each concept is accompanied by a theoretical explanation and a set of practical exercises with Python code. Next, you’ll dive into the world of causal effect estimation, consistently progressing towards modern machine learning methods. Step-by-step, you’ll discover Python causal ecosystem and harness the power of cutting-edge algorithms. You’ll further explore the mechanics of how “causes leave traces” and compare the main families of causal discovery algorithms. The final chapter gives you a broad outlook into the future of causal AI where we examine challenges and opportunities and provide you with a comprehensive list of resources to learn more.
By the end of this book, you will be able to build your own models for causal inference and discovery using statistical and machine learning techniques as well as perform basic project assessment.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 641

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Causal Inference and Discovery in Python – Machine Learning and Pearlian Perspective

Unlock the secrets of modern causal machine learning with DoWhy, EconML, PyTorch and more

Aleksander Molak

BIRMINGHAM—MUMBAI

Causal Inference and Discovery in Python – Machine Learning and Pearlian Perspective

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Ali Abidi

Publishing Product Manager: Dinesh Chaudhary

Senior Editor: Tazeen Shaikh

Technical Editor: Rahul Limbachiya

Copy Editor: Safis Editing

Project Coordinator: Farheen Fathima

Proofreader: Safis Editing

Indexer: Pratik Shirodkar

Production Designer: Shankar Kalbhor

Marketing Coordinators: Shifa Ansari and Vinishka Kalra

First published: June 2023

Production reference: 3180324

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80461-298-9

www.packtpub.com

To my wife, Katia. You cause me to smile. I am grateful for every day we spend together.

Foreword

I have been following Aleksander Molak’s work on causality for a while.

I have been using libraries for causal inference, such as DoWhy, in my teaching at the University of Oxford, and causality is one of the key topics I teach in my course.

Based on the discussions with Aleksander, I have invited him to present a session at Oxford in our course in Fall 23.

Hence, I am pleased to write the foreword for Aleksander’s new book, Causal Inference and Discovery in Python.

Despite causality becoming a key topic for AI and increasingly also for generative AI, most developers are not familiar with concepts such as causal graphs and counterfactual queries.

Aleksander’s book makes the journey into the world of causality easier for developers. The book spans both technical concepts and code and provides recommendations for the choice of approaches and algorithms to address specific causal scenarios.

This book is comprehensive yet accessible. Machine learning engineers, data scientists, and machine learning researchers who want to extend their data science toolkit to include causal machine learning will find this book most useful.

Looking to the future of AI, I find the sections on causal machine learning and LLMs especially relevant to both readers and our work.

Ajit Jaokar

Visiting Fellow, Department of Engineering Science, University of Oxford, and Course Director, Artificial Intelligence: Cloud and Edge Implementations, University of Oxford

Contributors

About the author

Aleksander Molak is an independent machine learning researcher and consultant. Aleksander gained experience working with Fortune 100, Fortune 500, and Inc. 5000 companies across Europe, the USA, and Israel, helping them to build and design large-scale machine learning systems. On a mission to democratize causality for businesses and machine learning practitioners, Aleksander is a prolific writer, creator, and international speaker. As a co-founder of Lespire.io, an innovative provider of AI and machine learning training for corporate teams, Aleksander is committed to empowering businesses to harness the full potential of cutting-edge technologies that allow them to stay ahead of the curve.

This book has been co-authored by many people whose ideas, love, and support left a significant trace in my life. I am deeply grateful to each one of you.

About the reviewers

Nicole Königstein is an experienced data scientist and quantitative researcher, currently working as data science and technology lead at impactvise, an ESG analytics company, and as a technology lead and head quantitative researcher at Quantmate, an innovative FinTech start-up focused on alternative data in predictive modeling. As a guest lecturer, she shares her expertise in Python, machine learning, and deep learning at various universities. Nicole is a regular speaker at renowned conferences, where she conducts workshops and educational sessions. She also serves as a regular reviewer of books in her field, further contributing to the community. Nicole is the author of the well-received online course Math for Machine Learning, and the author of the book Transformers in Action.

Mike Hankin is a data scientist and statistician, with a B.S. from Columbia University and a Ph.D. from the University of Southern California (dissertation topic: sequential testing of multiple hypotheses). He spent 5 years at Google working on a wide variety of causal inference projects. In addition to causal inference, he works on Bayesian models, non-parametric statistics, and deep learning (including contributing to TensorFlow/Keras). In 2021, he took a principal data scientist role at VideoAmp, where he works as a high-level tech lead, overseeing all methodology development. On the side, he volunteers with a schizophrenia lab at the Veterans Administration, working on experiment design and multimodal data analysis.

Amit Sharma is a principal researcher at Microsoft Research India. His work bridges causal inference techniques with machine learning to enhance the generalization, explainability, and avoidance of hidden biases in machine learning models. To achieve these goals, Amit has co-led the development of the open-source DoWhy library for causal inference and the DiCE library for counterfactual explanations. The broader theme of his work revolves around leveraging machine learning for improved decision-making. Amit received his Ph.D. in computer science from Cornell University and his B.Tech. in computer science and engineering from the Indian Institute of Technology (IIT) Kharagpur.

Acknowledgments

There’s only one name listed on the front cover of this book, but this book would not exist without many other people whose names you won’t find on the cover.

I want to thank my wife, Katia, for the love, support, and understanding that she provided me with throughout the year-long process of working on this book.

I want to thank Shailesh Jain, who was the first person at Packt with whom I shared the idea about this book.

The wonderful team at Packt made writing this book a much less challenging experience than it would have been otherwise. I thank Dinesh Chaudhary for managing the process, being open to non-standard ideas, and making the entire journey so smooth.

I want to thank my editor, Tazeen Shaikh, and my project manager, Kirti Pisat. Your support, patience, amazing energy, and willingness to go the extra mile are hard to overstate. I am grateful that I had an opportunity to work with you!

Three technical reviewers provided me with invaluable feedback that made this book a better version of itself. I am immensely grateful to Amit Sharma (Microsoft Research), Nicole Königstein (impactvise), and Mike Hankin (VideoAmp) for their comments and questions that gave me valuable hints, sometimes challenged me, and – most importantly – gave me an opportunity to see this book through their eyes.

I want to thank all the people, who provided me with clarifications, and additional information, agreed to include their materials in the book, or provided valuable feedback regarding parts of this book outside of the formal review process: Kevin Hillstrom, Matheus Facure, Rob Donnelly, Mehmet Süzen, Ph.D., Piotr Migdał, Ph.D., Quentin Gallea, Ph.D., Uri Itai, Ph.D., prof. Judea Pearl, Alicia Curth.

I want to thank my friends, Uri Itai, Natan Katz, and Leah Bar, with whom we analyzed and discussed some of the papers mentioned in this book.

Additionally, I want to thank Prof. Frank Harrell and Prof. Stephen Senn for valuable exchanges on Twitter that gave me many insights into experimentation and causal modeling as seen through the lens of biostatistics and medical statistics.

I am grateful to the CausalPython.io community members who shared their feedback regarding the contents of this book: Marcio Minicz; Elie Kawerk, Ph.D.; Dr. Tony Diana; David Jensen; and Michael Wexler.

I received a significant amount of support from causalpython.io members and people on LinkedIn and Twitter who shared their ideas, questions, and excitement, or expressed their support for me writing this book by following me or liking and sharing the content related to this book. Thank you!

I also want to thank all the readers who offered valuable feedback and identified errors in the book. Your contributions have improved this work for the entire community.

Finally, I want to thank Rahul Limbachiya, Vinishka Kalra, Farheen Fathima, Shankar Kalbhor, and the entire Packt team for their engagement and great work on this project, and the team at Safis Editing, for their helpful suggestions.

I did my best not to miss anyone from this list. Nonetheless, if I missed your name, the next line is for you.

Thank you!

I also want to thank you for buying this book.

Congratulations on starting your causal journey today!

Table of Contents

Preface

Part 1: Causality – an Introduction

1

Causality – Hey, We Have Machine Learning, So Why Even Bother?

A brief history of causality

Why causality? Ask babies!

Interacting with the world

Confounding – relationships that are not real

How not to lose money… and human lives

A marketer’s dilemma

Let’s play doctor!

Associations in the wild

Wrapping it up

References

Join our book's Discord space

2

Judea Pearl and the Ladder of Causation

From associations to logic and imagination – the Ladder of Causation

Associations

Let’s practice!

What are interventions?

Changing the world

Correlation and causation

What are counterfactuals?

Let’s get weird (but formal)!

The fundamental problem of causal inference

Computing counterfactuals

Time to code!

Extra – is all machine learning causally the same?

Causality and reinforcement learning

Causality and semi-supervised and unsupervised learning

Wrapping it up

References

3

Regression, Observations, and Interventions

Starting simple – observational data and linear regression

Linear regression

p-values and statistical significance

Geometric interpretation of linear regression

Reversing the order

Should we always control for all available covariates?

Navigating the maze

If you don’t know where you’re going, you might end up somewhere else

Get involved!

To control or not to control?

Regression and structural models

SCMs

Linear regression versus SCMs

Finding the link

Regression and causal effects

Wrapping it up

References

4

Graphical Models

Graphs, graphs, graphs

Types of graphs

Graph representations

Graphs in Python

What is a graphical model?

DAG your pardon? Directed acyclic graphs in the causal wonderland

Definitions of causality

DAGs and causality

Let’s get formal!

Limitations of DAGs

Sources of causal graphs in the real world

Causal discovery

Expert knowledge

Combining causal discovery and expert knowledge

Extra – is there causality beyond DAGs?

Dynamical systems

Cyclic SCMs

Wrapping it up

References

5

Forks, Chains, and Immoralities

Graphs and distributions and how to map between them

How to talk about independence

Choosing the right direction

Conditions and assumptions

Chains, forks, and colliders or…immoralities

A chain of events

Chains

Forks

Colliders, immoralities, or v-structures

Ambiguous cases

Forks, chains, colliders, and regression

Generating the chain dataset

Generating the fork dataset

Generating the collider dataset

Fitting the regression models

Wrapping it up

References

Join our book's Discord space

Part 2: Causal Inference

6

Nodes, Edges, and Statistical (In)dependence

You’re gonna keep ‘em d-separated

Practice makes perfect – d-separation

Estimand first!

We live in a world of estimators

So, what is an estimand?

The back-door criterion

What is the back-door criterion?

Back-door and equivalent estimands

The front-door criterion

Can GPS lead us astray?

London cabbies and the magic pebble

Opening the front door

Three simple steps toward the front door

Front-door in practice

Are there other criteria out there? Let’s do-calculus!

The three rules of do-calculus

Instrumental variables

Wrapping it up

Answer

References

7

The Four-Step Process of Causal Inference

Introduction to DoWhy and EconML

Python causal ecosystem

Why DoWhy?

Oui, mon ami, but what is DoWhy?

How about EconML?

Step 1 – modeling the problem

Creating the graph

Building a CausalModel object

Step 2 – identifying the estimand(s)

Step 3 – obtaining estimates

Step 4 – where’s my validation set? Refutation tests

How to validate causal models

Introduction to refutation tests

Full example

Step 1 – encode the assumptions

Step 2 – getting the estimand

Step 3 – estimate!

Step 4 – refute them!

Wrapping it up

References

Join our book's Discord space

8

Causal Models – Assumptions and Challenges

I am the king of the world! But am I?

In between

Identifiability

Lack of causal graphs

Not enough data

Unverifiable assumptions

An elephant in the room – hopeful or hopeless?

Let’s eat the elephant

Positivity

Exchangeability

Exchangeable subjects

Exchangeability versus confounding

…and more

Modularity

SUTVA

Consistency

Call me names – spurious relationships in the wild

Names, names, names

Should I ask you or someone who’s not here?

DAG them!

More selection bias

Wrapping it up

References

9

Causal Inference and Machine Learning – from Matching to Meta-Learners

The basics I – matching

Types of matching

Treatment effects – ATE versus ATT/ATC

Matching estimators

Implementing matching

The basics II – propensity scores

Matching in the wild

Reducing the dimensionality with propensity scores

Propensity score matching (PSM)

Inverse probability weighting (IPW)

Many faces of propensity scores

Formalizing IPW

Implementing IPW

IPW – practical considerations

S-Learner – the Lone Ranger

The devil’s in the detail

Mom, Dad, meet CATE

Jokes aside, say hi to the heterogeneous crowd

Waving the assumptions flag

You’re the only one – modeling with S-Learner

Small data

S-Learner’s vulnerabilities

T-Learner – together we can do more

Forcing the split on treatment

T-Learner in four steps and a formula

Implementing T-Learner

X-Learner – a step further

Squeezing the lemon

Reconstructing the X-Learner

X-Learner – an alternative formulation

Implementing X-Learner

Wrapping it up

References

10

Causal Inference and Machine Learning – Advanced Estimators, Experiments, Evaluations, and More

Doubly robust methods – let’s get more!

Do we need another thing?

Doubly robust is not equal to bulletproof…

…but it can bring a lot of value

The secret doubly robust sauce

Doubly robust estimator versus assumptions

DR-Learner – crossing the chasm

DR-Learners – more options

Targeted maximum likelihood estimator

If machine learning is cool, how about double machine learning?

Why DML and what’s so double about it?

DML with DoWhy and EconML

Hyperparameter tuning with DoWhy and EconML

Is DML a golden bullet?

Doubly robust versus DML

What’s in it for me?

Causal Forests and more

Causal trees

Forests overflow

Advantages of Causal Forests

Causal Forest with DoWhy and EconML

Heterogeneous treatment effects with experimental data – the uplift odyssey

The data

Choosing the framework

We don’t know half of the story

Kevin’s challenge

Opening the toolbox

Uplift models and performance

Other metrics for continuous outcomes with multiple treatments

Confidence intervals

Kevin’s challenge’s winning submission

When should we use CATE estimators for experimental data?

Model selection – a simplified guide

Extra – counterfactual explanations

Bad faith or tech that does not know?

Wrapping it up

References

11

Causal Inference and Machine Learning – Deep Learning, NLP, and Beyond

Going deeper – deep learning for heterogeneous treatment effects

CATE goes deeper

SNet

Transformers and causal inference

The theory of meaning in five paragraphs

Making computers understand language

From philosophy to Python code

LLMs and causality

The three scenarios

CausalBert

Causality and time series – when an econometrician goes Bayesian

Quasi-experiments

Twitter acquisition and our googling patterns

The logic of synthetic controls

A visual introduction to the logic of synthetic controls

Starting with the data

Synthetic controls in code

Challenges

Wrapping it up

References

Part 3: Causal Discovery

12

Can I Have a Causal Graph, Please?

Sources of causal knowledge

You and I, oversaturated

The power of a surprise

Scientific insights

The logic of science

Hypotheses are a species

One logic, many ways

Controlled experiments

Randomized controlled trials (RCTs)

From experiments to graphs

Simulations

Personal experience and domain knowledge

Personal experiences

Domain knowledge

Causal structure learning

Wrapping it up

References

Join our book's Discord space

13

Causal Discovery and Machine Learning – from Assumptions to Applications

Causal discovery – assumptions refresher

Gearing up

Always trying to be faithful…

…but it’s difficult sometimes

Minimalism is a virtue

The four (and a half) families

The four streams

Introduction to gCastle

Hello, gCastle!

Synthetic data in gCastle

Fitting your first causal discovery model

Visualizing the model

Model evaluation metrics

Constraint-based causal discovery

Constraints and independence

Leveraging the independence structure to recover the graph

PC algorithm – hidden challenges

PC algorithm for categorical data

Score-based causal discovery

Tabula rasa – starting fresh

GES – scoring

GES in gCastle

Functional causal discovery

The blessings of asymmetry

ANM model

Assessing independence

LiNGAM time

Gradient-based causal discovery

What exactly is so gradient about you?

Shed no tears

GOLEMs don’t cry

The comparison

Encoding expert knowledge

What is expert knowledge?

Expert knowledge in gCastle

Wrapping it up

References

14

Causal Discovery and Machine Learning – Advanced Deep Learning and Beyond

Advanced causal discovery with deep learning

From generative models to causality

Looking back to learn who you are

DECI’s internal building blocks

DECI in code

DECI is end-to-end

Causal discovery under hidden confounding

The FCI algorithm

Other approaches to confounded data

Extra – going beyond observations

ENCO

ABCI

Causal discovery – real-world applications, challenges, and open problems

Wrapping it up!

References

15

Epilogue

What we’ve learned in this book

Five steps to get the best out of your causal project

Starting with a question

Obtaining expert knowledge

Generating hypothetical graph(s)

Check identifiability

Falsifying hypotheses

Causality and business

How causal doers go from vision to implementation

Toward the future of causal ML

Where are we now and where are we heading?

Causal benchmarks

Causal data fusion

Intervening agents

Causal structure learning

Imitation learning

Learning causality

Let’s stay in touch

Wrapping it up

References

Join our book's Discord space

Index

Other Books You May Enjoy

Part 1: Causality – an Introduction

Part 1 of this book will equip us with a set of tools necessary to understand and tackle the challenges of causal inference and causal discovery.

We’ll learn about the differences between observational, interventional, and counterfactual queries and distributions. We’ll demonstrate connections between linear regression, graphs, and causal models.

Finally, we’ll learn about the important properties of graphical structures that play an essential role in almost any causal endeavor.

This part comprises the following chapters:

Chapter 1, Causality – Hey, We Have Machine Learning, So Why Even Bother?Chapter 2, Judea Pearl and the Ladder of CausationChapter 3, Regression, Observations, and InterventionsChapter 4, Graphical ModelsChapter 5, Forks, Chains, and Immoralities