**Table of Contents**

**A Brief Introduction to Do-Calculus**

This lesson is the 4th in a 5-part series on **Causality in Machine Learning**:

*Introduction to Causality in Machine Learning**Best Machine Learning Datasets**Tools and Methodologies for Studying Causal Effects**A Brief Introduction to Do-Calculus***(this tutorial)***Studying Causal Effect with Microsoft’s Do-Why Library*

**To learn how to get started with do-calculus for causality, just keep reading.**

**A Brief Introduction to Do-Calculus**

Welcome back to Part 4 of the series on **Causality in Machine Learning**. The previous two parts introduced us to the world of causal inference and the various methodologies involved.

In this part, we will discuss a very popular and useful method known as the `do-calculus`

developed by Judea Pearl in 1995. It was developed to propose a foolproof methodology for the identification of causal effects in non-parametric models.

Well, that’s a mouthful. What do we mean by that?

In simple words, this means to identify the effect or effects for a particular cause from data that is continuous rather than having discrete values.

We have learned in the previous blogs that it is impossible to do Causal Inference without having some form of intervention on the provided data. To facilitate this, `do-calculus`

introduces a mathematical operator called , which simulates intervention by removing certain functions from the model and replacing them with a constant . To understand how this plays out, we will first have to look at some of the definitions introduced by Pearl.

**Definitions**

**Definition 1**

The probability distribution of the outcome after the intervention is given by the equation:

where the distribution of the outcome is defined as the probability assigned by the model to each outcome level .

**Definition 2**

This part discusses when and under what conditions a causal query (whether a variable or a group of variables is the cause for a given effect or not) is identifiable.

Given a set of assumptions () that satisfy two fully specified models ( and ), the following is the criteria for identifiability:

This means that whatever the details of the models are, if the distribution of the two models given the same set of assumptions () are equal, then it follows that the causal query for the two models should also be equal. This can be extended to mean that a causal query, under such circumstances, can be expressed in terms of the parameters of .

**The Rules of Do-Calculus**

Now that we have learned about the definitions of `do-calculus`

, let us familiarize ourselves with the three rules that govern the mathematics of `do-calculus`

. But first, we need to understand the necessity of these rules.

In the previous section, we learned under what conditions a causal query will be identifiable, and we also saw how to formulate an expression in terms of a do-expression (e.g., ). So, when a causal query is given to us in the form of a do-expression, there are actual mathematical steps that can be taken to resolve it and find out whether the query is identifiable or not.

Consider the following directed acyclic graph in **Figure 1** () where , , , and are arbitrary disjoint nodes. is the manipulated graph where all incoming edges to have been removed.

Similarly, is the manipulated graph where all outgoing edges to have been removed, as shown in **Figure 2**.

Another useful notation to get familiarized with is the concept of -separation (). In very simple words, given the graph, , the expression means that is conditionally independent of given .

To understand -separation in a more detailed manner, have a look at this single-page explanation.

**Rule 1: Insertion/Deletion of Observation**

if for .

This means that if is -separated from given and , then the expression of probability resolves to . An easier way to understand this is by getting rid of the do-operators on both sides of the equality sign.

if for

The above expression simply implies conditional independence within the variables in the distribution given regular -separation.

**Rule 2: Action/Observation Exchange**

if for .

To simplify the expression above, let us again remove or consider to be an empty set.

if for .

This expression refers to the backdoor-adjustment criteria that we saw in Part 3. Therefore, this rule gives us the interventional distribution for the backdoor adjustment criteria.

**Rule 3: Insertion/Deletion of Action**

if for

where is the set of nodes that are not ancestors of any node in .

Again, for the sake of simplification, let us remove the operator from the above expression.

if for

Let’s pause here and really understand what this means. On the paper, it means that we can remove the intervention term provided there is no causal association flowing from to in the graph .

But that’s not all. We have a strange term called , which doesn’t quite fit in.

The simplified expression should have been:

if for

where removal of incoming edges to should result in the -separation of and , and no causal association should flow from to . However, instead of this simple term, we end up with an expression containing . To understand this better, let us consider **Figure 3**:

Now, the intuitive idea is to remove incoming edges to (). But if we do that, then we risk changing the distribution of altogether through the backdoor path consisting of and .

Instead, what we can do is take a sub-node of , say , which is not an ancestor of any node in , and then remove all the incoming edges to it (). This is shown in **Figure 4**.

### What's next? We recommend PyImageSearch University.

**Course information:**

84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024

★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could master computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s *not* the case.

All you need to master computer vision and deep learning is for someone to explain things to you in *simple, intuitive* terms. *And that’s exactly what I do*. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to *successfully* and *confidently* apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

- ✓
**84 courses**on essential computer vision, deep learning, and OpenCV topics - ✓
**84 Certificates**of Completion - ✓
**114+ hours**of on-demand video - ✓
**Brand new courses released**, ensuring you can keep up with state-of-the-art techniques*regularly* - ✓
**Pre-configured Jupyter Notebooks in Google Colab** - ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to
**centralized code repos for**on PyImageSearch*all*536+ tutorials - ✓
**Easy one-click downloads**for code, datasets, pre-trained models, etc. - ✓
**Access**on mobile, laptop, desktop, etc.

**Summary**

The rules and definitions of `do-calculus`

provide a general structure for identifying Causal queries. The final query should be free of any do-operator. This can be achieved by repeatedly applying the three rules. It is also complete, meaning if there exists a Causal Query that is identifiable, then it can be identified using `do-calculus`

.

This lesson was aimed at introducing `do-calculus`

very briefly and laying down the rules of the game. The idea is not to intimidate any newcomer with a whole lot of mathematical jargon but to provide insight into an essentially simple yet powerful framework for causal inference.

**References**

**Citation Information**

**A. R. Gosthipaty and R. Raha. **“A Brief Introduction to Do-Calculus,” *PyImageSearch*, P. Chugh, S. Huot, and K. Kidriavsteva, eds., 2023, https://pyimg.co/h3q2n

@incollection{ARG-RR_2023_IntroDoCalculus, author = {Aritra Roy Gosthipaty and Ritwik Raha}, title = {A Brief Introduction to Do-Calculus}, booktitle = {PyImageSearch}, editor = {Puneet Chugh and Susan Huot and Kseniia Kidriavsteva}, year = {2023}, url = {https://pyimg.co/h3q2n}, }

### Unleash the potential of computer vision with Roboflow - Free!

- Step into the realm of the future by signing up or logging into your Roboflow account. Unlock a wealth of innovative dataset libraries and revolutionize your computer vision operations.
- Jumpstart your journey by choosing from our broad array of datasets, or benefit from PyimageSearch’s comprehensive library, crafted to cater to a wide range of requirements.
- Transfer your data to Roboflow in any of the 40+ compatible formats. Leverage cutting-edge model architectures for training, and deploy seamlessly across diverse platforms, including API, NVIDIA, browser, iOS, and beyond. Integrate our platform effortlessly with your applications or your favorite third-party tools.
- Equip yourself with the ability to train a potent computer vision model in a mere afternoon. With a few images, you can import data from any source via API, annotate images using our superior cloud-hosted tool, kickstart model training with a single click, and deploy the model via a hosted API endpoint. Tailor your process by opting for a code-centric approach, leveraging our intuitive, cloud-based UI, or combining both to fit your unique needs.
- Embark on your journey today with absolutely no credit card required. Step into the future with Roboflow.

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.

## Comment section

Hey, Adrian Rosebrock here, author and creator of PyImageSearch. While I love hearing from readers, a couple years ago I made the tough decision to no longer offer 1:1 help over blog post comments.

At the time I was receiving 200+ emails per day and another 100+ blog post comments. I simply did not have the time to moderate and respond to them all, and the sheer volume of requests was taking a toll on me.

Instead, my goal is to

do the most goodfor the computer vision, deep learning, and OpenCV community at large by focusing my time on authoring high-quality blog posts, tutorials, and books/courses.If you need help learning computer vision and deep learning, I suggest you refer to my full catalog of books and courses— they have helped tens of thousands of developers, students, and researchersjust like yourselflearn Computer Vision, Deep Learning, and OpenCV.Click here to browse my full catalog.