This pyAgrum's notebook is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License

Author: **Aymen Merrouche** and Pierre-Henri Wuillemin.

**Do-caclculus**

In [1]:

```
from IPython.display import display, Math, Latex,HTML
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
gnb.forDarkTheme()
import pyAgrum.causal as csl
import pyAgrum.causal.notebook as cslnb
import os
```

The corresponding causal diagram is the following:

We're facing the following situation and we want to measure the causal effect of $X$ on $Y$:

In [2]:

```
fd = gum.fastBN("w->z->x->y;w->x;w->y")
fd
```

Out[2]:

We suspect the presence of some

unmeasuredconfounders, that could explain the correlation between $W$ and $X$ and between $W$ and $Y$:

In [3]:

```
fdModele = csl.CausalModel(fd, [("u1", ["w","x"]),("u2", ["w","y"])],False) #(<latent variable name>, <list of affected variablesâ€™ ids>).
fdModele
```

Out[3]:

- We can measure the causal effect of $Z$ on $Y$ using the back-door adjustment:

In [4]:

```
print(" + Back-door doing Z on Y :"+str(fdModele.backDoor("z","y")))
```

- We can measure the causal effect of $W$ on $X$ using the front-door formula:

In [5]:

```
print(" + Front-door doing W on X :"+str(fdModele.frontDoor("w","x")))
```

- In order to measure the causal effect of $X$ on $Y$, we can use neither the back-door adjustment nor the front-door formula:

In [6]:

```
print(" + Backdoor doing X on Y :"+str(fdModele.backDoor("x","y")))
print(" + Frontdoor doing X on Y :"+str(fdModele.frontDoor("x","y")))
```

- In this case, the only way to measure the causal effect of $X$ on $Y$ is to use the do-calculus:

In [7]:

```
cslnb.showCausalImpact(fdModele,on="y",doing="x")
```

In [ ]:

```
```