Some examples of do-calculus
![]() | ![]() |
from IPython.display import display, Math
import pyagrum as gumimport pyagrum.lib.notebook as gnbS. Tikka and J. Karvanen, 2016 [CRAN]
Section titled “S. Tikka and J. Karvanen, 2016 [CRAN]”bn = gum.fastBN("w->x->z->y;w->z")
bn.cpt("w")[:] = [0.7, 0.3]
bn.cpt("x")[:] = [[0.4, 0.6], [0.3, 0.7]]
bn.cpt("z")[{"w": 0, "x": 0}] = [0.2, 0.8]bn.cpt("z")[{"w": 0, "x": 1}] = [0.1, 0.9]bn.cpt("z")[{"w": 1, "x": 0}] = [0.9, 0.1]bn.cpt("z")[{"w": 1, "x": 1}] = [0.5, 0.5]
bn.cpt("y")[:] = [[0.1, 0.9], [0.8, 0.2]]bnd = gum.CausalModel(bn, [("lat1", ["x", "y"])])
gnb.showCausalImpact(d, on="y", doing="x")|
|
| |
|---|---|---|
| 0.5130 | 0.4870 | |
| 0.6460 | 0.3540 | |
Since we have the formula, let us compute by hand this intervention :
((bn.cpt("y") * bn.cpt("x")).sumOut(["x"]) * bn.cpt("w") * bn.cpt("z")).sumOut(["z", "w"]).putFirst("y")|
|
| |
|---|---|---|
| 0.5130 | 0.4870 | |
| 0.6460 | 0.3540 | |
bn = gum.fastBN("Z1->X->Z2->Y")d = gum.CausalModel(bn, [("L1", ["Z1", "X"]), ("L2", ["Z1", "Z2"]), ("L3", ["Z1", "Y"]), ("L4", ["Y", "X"])], True)gnb.showCausalImpact(d, "Y", "X", values={"X": 1})Effect not identifiable
Explanation
No result
Impact
Front door
Section titled “Front door”modele4 = gum.BayesNet()modele4.add(gum.LabelizedVariable("Smoking"))modele4.add(gum.LabelizedVariable("Cancer"))modele4.add(gum.LabelizedVariable("Tar"))
modele4.addArc(0, 2)modele4.addArc(2, 1)modele4.addArc(0, 1)
## Smokingmodele4.cpt(0)[:] = [0.5, 0.5]
## Tarmodele4.cpt(2)[{"Smoking": 0}] = [0.4, 0.6]modele4.cpt(2)[{"Smoking": 1}] = [0.3, 0.6]
## Cancermodele4.cpt(1)[{"Smoking": 0, "Tar": 0}] = [0.1, 0.9] # No Drug, Male -> healed in 0.8 of casesmodele4.cpt(1)[{"Smoking": 0, "Tar": 1}] = [0.15, 0.85] # No Drug, Female -> healed in 0.4 of casesmodele4.cpt(1)[{"Smoking": 1, "Tar": 0}] = [0.2, 0.8] # Drug, Male -> healed 0.7 of casesmodele4.cpt(1)[{"Smoking": 1, "Tar": 1}] = [0.25, 0.75]
d4 = gum.CausalModel(modele4, [("Genotype", ["Smoking", "Cancer"])], False)gnb.showCausalModel(d4)c = gum.CausalImpact(d4, on="Cancer", doing="Smoking")if c.isIdentified(): display(Math(c.toLatex()))
c.toDict(){'op': 'sum', 'var': 'Tar', 'term': {'op': '*', 'op1': {'op': 'P', 'vars': ['Tar'], 'knowing': ['Smoking']}, 'op2': {'op': 'sum', 'var': 'Smoking', 'term': {'op': '*', 'op1': {'op': 'P', 'vars': ['Cancer'], 'knowing': ['Tar', 'Smoking']}, 'op2': {'op': 'P', 'vars': ['Smoking']}}}}}adjj = c.eval()
print(adjj) ║ Cancer │Smokin║0 │1 │──────║─────────│─────────│0 ║ 0.1800 │ 0.8200 │1 ║ 0.1650 │ 0.7350 │formula, adj, exp = gum.causalImpact(d4, on="Cancer", doing="Smoking", values={"Smoking": 0})display(Math(formula.toLatex()))adj
|
|
|
|---|---|
| 0.1800 | 0.8200 |
Last example from R
Section titled “Last example from R”m = gum.fastBN("z2->x->z1->y;z2->z1;z2->z3->y")
m.cpt("z2")[:] = [0.5, 0.5]m.cpt("x")[:] = [ [0.4, 0.6], # z2=0 [0.4, 0.6],] # z2=1m.cpt("z3")[:] = [ [0.3, 0.7], # z2=0 [0.3, 0.7],] # z2=1m.cpt("z1")[{"z2": 0, "x": 0}] = [0.2, 0.8]m.cpt("z1")[{"z2": 0, "x": 1}] = [0.25, 0.75]m.cpt("z1")[{"z2": 1, "x": 0}] = [0.1, 0.9]m.cpt("z1")[{"z2": 1, "x": 1}] = [0.15, 0.85]
m.cpt("y")[{"z1": 0, "z3": 0}] = [0.5, 0.5]m.cpt("y")[{"z1": 0, "z3": 1}] = [0.45, 0.55]m.cpt("y")[{"z1": 1, "z3": 0}] = [0.4, 0.6]m.cpt("y")[{"z1": 1, "z3": 1}] = [0.35, 0.65]
d = gum.CausalModel(m, [("X-Z2", ["x", "z2"]), ("X-Z3", ["x", "z3"]), ("X-Y", ["x", "y"]), ("Y-Z2", ["y", "z2"])], True)
gnb.showCausalModel(d)try: formula, result, msg = gum.causalImpact(d, on={"y", "z2", "z1", "z3"}, doing={"x"})except gum.HedgeException as h: print(h.message)
print(msg)display(Math(formula.toLatex()))Identified via do-calculus (ID/IDC).
## computation for this formula directly in pyAgrumf1 = m.cpt("x") * m.cpt("z2") * m.cpt("z3") * m.cpt("y")f2 = f1.sumOut(["x"])f3 = f1.sumOut(["x", "y"])f4 = f2 / f3pyResult = m.cpt("z3") * m.cpt("z1") * m.cpt("z2") * f4According to [ref], the result should be :
## computation for that formulaie = gum.LazyPropagation(m)refResult = (ie.evidenceJointImpact(["y", "z3"], ["z1", "z2"]) * ie.evidenceJointImpact(["x", "z2"], [])).sumOut( ["x"]) * m.cpt("z1")## the three tensors in the same variable contextr1 = gum.Tensor(result).fillWith(pyResult)r2 = gum.Tensor(result).fillWith(refResult)print( "Maximum error between these 3 versions : {}".format( max((r1 - r2).abs().max(), (r1 - result).abs().max(), (r2 - result).new_abs().max()) ))Maximum error between these 3 versions : 5.551115123125783e-17Unidentifiabilty
Section titled “Unidentifiabilty”m1 = gum.fastBN("z1->x->z2->y")
cdg = gum.CausalModel( m1, [("Z1−X", ["z1", "x"]), ("Z1-Y", ["z1", "y"]), ("Z1-Z1", ["z1", "z2"]), ("X−Y", ["x", "y"])], True)gnb.showCausalModel(cdg)err = gnb.showCausalImpact(cdg, "y", "x", values={"x": 0})Effect not identifiable
Explanation
No result
Impact
another one
Section titled “another one”## EXEMPLE PAGE 17 : <http://ftp.cs.ucla.edu/pub/stat_ser/r350.pdf>
m1 = gum.fastBN("y<-z2->z3<-z1->x<-z3->y<-x")
gnb.showBN(m1)d = gum.CausalModel(m1)display(Math(gum.CausalImpact(d, on={"z1", "z2", "z3", "y"}, doing={"x"}).toLatex()))
display(Math(gum.causalImpact(d, on={"y"}, doing={"x"})[0].toLatex()))
display(Math(gum.CausalImpact(d, on={"z1", "z3", "y"}, doing={"x", "z2"}).toLatex()))
display(Math(gum.CausalImpact(d, on={"y"}, doing={"x", "z2"}).toLatex()))
Other example
Section titled “Other example”## <http://www.stats.ox.ac.uk/~lienart/gml15_causalinference.html>
m1 = gum.fastBN("a->p->b->y<-p;a->y")gnb.showBN(m1)d = gum.CausalModel(m1)display(Math(gum.CausalImpact(d, on={"y"}, doing={"a", "b"}).toLatex()))
example f
Section titled “example f”## <https://cse.sc.edu/~mgv/talks/AIM2010.ppt> , example (f)
m1 = gum.fastBN("X->Z1->Z2->Y<-Z1;X->Y")d = gum.CausalModel(m1, [("l1", ["X", "Z1"]), ("l2", ["Y", "Z1"])], True)gnb.showCausalModel(d)display(Math(gum.CausalImpact(d, on={"Y"}, doing={"X"}).toLatex()))
Example [Pearl,2009] Causality, p66
Section titled “Example [Pearl,2009] Causality, p66”bn = gum.fastBN("Z1->Z2->Z3->Y<-X->Z2;Z2->Y;Z1->X->Z3<-Z1")gnb.showBN(bn)c = gum.CausalModel(bn, [("Z0", ("X", "Z1", "Z3"))], False)gnb.showCausalModel(c)formula, impact, explanation = gum.causalImpact(c, on="Y", doing="X")gnb.showCausalImpact(c, "Y", "X")|
|
| |
|---|---|---|
| 0.4319 | 0.6395 | |
| 0.5681 | 0.3605 | |
Causality without numerical computations
Section titled “Causality without numerical computations”You may want to compute some impact without having a (discrete) quantifications of the model. In that cas, you are not intersted in causalImpact which will include some computations of a numerical (discrete) impact.
f = gum.CausalImpact(c, on="Y", doing="X")print("- Version latex")display(Math(f.toLatex()))print("- Version Abstract Syntax Tree (in a dict)")f.toDict()- Version latex
- Version Abstract Syntax Tree (in a dict)
{'op': 'sum', 'var': 'Z2', 'term': {'op': 'sum', 'var': 'Z3', 'term': {'op': 'sum', 'var': 'Z1', 'term': {'op': '*', 'op1': {'op': 'sum', 'var': 'X', 'term': {'op': '*', 'op1': {'op': 'P', 'vars': ['Z3'], 'knowing': ['Z2']}, 'op2': {'op': '*', 'op1': {'op': 'P', 'vars': ['X'], 'knowing': []}, 'op2': {'op': 'P', 'vars': ['Z1']}}}}, 'op2': {'op': '*', 'op1': {'op': 'P', 'vars': ['Z2'], 'knowing': ['Z1', 'X']}, 'op2': {'op': 'P', 'vars': ['Y'], 'knowing': ['Z3', 'Z2', 'X']}}}}}}f.print_ast()┌────────────┐│ P(Y|do(X)) │└────────────┘ └─ sum ├─ var: Z2 └─ sum ├─ var: Z3 └─ sum ├─ var: Z1 └─ * ├─ sum │ ├─ var: X │ └─ * │ ├─ P │ │ ├─ vars: ['Z3'] │ │ └─ knowing: ['Z2'] │ └─ * │ ├─ P │ │ ├─ vars: ['X'] │ │ └─ knowing: [] │ └─ P │ └─ vars: ['Z1'] └─ * ├─ P │ ├─ vars: ['Z2'] │ └─ knowing: ['Z1', 'X'] └─ P ├─ vars: ['Y'] └─ knowing: ['Z3', 'Z2', 'X']m1 = gum.CausalModel(gum.fastBN("y<-z2->z3<-z1->x<-z3->y<-x"))f1 = gum.CausalImpact(m1, on={"z1", "y"}, doing={"x", "z2"}, knowing={"z3"})f1.print_ast()┌─────────────────────┐│ P(y,z1|do(x,z2),z3) │└─────────────────────┘ └─ / ├─ * │ ├─ P │ │ ├─ vars: ['z3'] │ │ └─ knowing: ['z1', 'z2'] │ └─ * │ ├─ P │ │ ├─ vars: ['y'] │ │ └─ knowing: ['z3', 'z2', 'x'] │ └─ P │ └─ vars: ['z1'] └─ sum ├─ var: z1 └─ * ├─ P │ ├─ vars: ['z3'] │ └─ knowing: ['z1', 'z2'] └─ P └─ vars: ['z1']
