Do-Calculus (p213)

Creative Commons License

aGrUM

interactive online version

Authors: Aymen Merrouche and Pierre-Henri Wuillemin.

This notebook follows the example from “The Book Of Why” (Pearl, 2018) chapter 7 page 213

In [1]:
from IPython.display import display, Math, Latex,HTML

import pyAgrum as gum
import pyAgrum.lib.notebook as gnb

import pyAgrum.causal as csl
import pyAgrum.causal.notebook as cslnb

import os

the causal diagram

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]:
G z z x x z->x y y w w w->z w->y w->x x->y

We suspect the presence of some unmeasured confounders, 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>).
gnb.show(fdModele)
../_images/notebooks_BoW-c7p213-doCalculus_5_0.svg

Even with two umeasured confounders :

  • 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")))
 + Back-door doing Z on Y :{'w'}
  • 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")))
 + Front-door doing W on X :{'z'}
  • 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")))
 + Backdoor doing X on Y :None
 + Frontdoor doing X on Y :None
  • 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")
G u1 w w u1->w x x u1->x u2 u2->w y y u2->y z z w->z z->x x->y
Causal Model
$$\begin{equation*}P( y \mid \text{do}(x)) = \frac {\sum_{w}{P\left(x\mid w,z\right) \cdot P\left(y\mid w,x,z\right) \cdot P\left(w\right)}}{\sum_{w,y'}{P\left(x\mid w,z\right) \cdot P\left(y'\mid w,x,z\right) \cdot P\left(w\right)}}\end{equation*}$$
Explanation : Do-calculus computations
y
z
x
0
1
0
0
0.73710.2629
1
0.51470.4853
1
0
0.34060.6594
1
0.41700.5830

Impact
In [ ]: