def testFromWHY19(self): bn = gum.fastBN("X->Y;U") d = csl.CausalModel(bn, [('Z', ["X", "Y", "U"])], True) _, pot, _ = csl.causalImpact(d, on="Y", doing="X") self.assertIsNone(pot) # HedgeException # Causality, Pearl, 2009, p66 bn = gum.fastBN("Z1->Z2->Z3->Y<-X->Z2;Z2->Y;Z1->X->Z3<-Z1") c = csl.CausalModel(bn, [("Z0", ("X", "Z1", "Z3"))], False) _, pot, explanation = csl.causalImpact(c, "Y", "X") self.assertIsNotNone(pot) self.assertEqual(explanation, "Do-calculus computations")
def test_simpson(self): m1 = gum.fastBN("Gender->Drug->Patient;Gender->Patient") # Gender = 0 == male # Gender = 1 == female # Drug == 0 - not taking the Drug # Drug == 1 - taking the Drug # Patient = 0 -- not healed # Patient = 1 - healed m1.cpt("Gender")[:] = [0.5, 0.5] m1.cpt("Drug")[:] = [[0.25, 0.75], # Gender=0 [0.75, 0.25]] # Gender=1 m1.cpt("Patient")[{'Drug': 0, 'Gender': 0}] = [ 0.2, 0.8] # No Drug, Male -> healed in 0.8 of cases # No Drug, Female -> healed in 0.4 of cases m1.cpt("Patient")[{'Drug': 0, 'Gender': 1}] = [0.6, 0.4] m1.cpt("Patient")[{'Drug': 1, 'Gender': 0}] = [ 0.3, 0.7] # Drug, Male -> healed 0.7 of cases m1.cpt("Patient")[{'Drug': 1, 'Gender': 1}] = [ 0.8, 0.2] # Drug, Female -> healed in 0.2 of cases d1 = csl.CausalModel(m1) # from formula : \sum_{ Gender }{ P(Patient\mid Drug,Gender) \cdot P(Gender) } margPatient = (m1.cpt("Patient") * m1.cpt("Gender")).margSumOut(["Gender"]) lat, pot, expl = csl.causalImpact( d1, on="Patient", doing={"Drug"}) # when drug=1 self.assertEqual(pot, margPatient)
def testCounterfactual(self): # experience=10-4*education+Ux # salaire=65+2.5*experience+5*education+Us # # Ux : [-2,10], sans a priori # Us : [0,25], sans a priori # education : 0,1,2 (low, medium, high), a priori [0.4,0.40.2] # experience : [0,20] # salaire : [65,150] edex = gum.fastBN( "Ux[-2,10]->experience[0,20]<-education{low|medium|high}->salary[65,150]<-Us[0,25];experience->salary") edex.cpt("Us").fillWith(1).normalize() edex.cpt("Ux").fillWith(1).normalize() edex.cpt("education")[:] = [0.4, 0.4, 0.2] edex.cpt("experience").fillWithFunction("10-4*education+Ux") edex.cpt("salary").fillWithFunction("round(65+2.5*experience+5*education+Us)") pot = csl.counterfactual(cm=csl.CausalModel(edex), profile={'experience': 8, 'education': 'low', 'salary': '86'}, whatif={"education"}, on={"salary"}, values={"education": "medium"}) self.assertEqual(pot[81 - 65], 1.0)
def test_CRAN1(self): bn = gum.fastBN("w->x->z->y;w->z") bn.cpt("w")[:] = [0.5, 0.5] 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.4, 0.6] bn.cpt("z")[{'w': 1, 'x': 1}] = [0.5, 0.5] bn.cpt("y")[:] = [[0.1, 0.9], [0.2, 0.8]] d = csl.CausalModel(bn, [("lat1", ["x", "y"])]) # from the formula \sum_{ z ,w }{ P(z\mid w,x) \cdot P(w) \cdot # \sum_{ x' }{ P(y\mid w,x',z) \cdot P(x'\mid w) } # } marg = (bn.cpt("x") * bn.cpt("y")).margSumOut(["x"]) marg = (marg * bn.cpt("w") * bn.cpt("z")).margSumOut(["z", "w"]) _, pot, _ = csl.causalImpact(d, on={"y"}, doing={"x"}) # when x=1 self.assertEqual(pot, marg) _, pot, _ = csl.causalImpact(d, on="y", doing="x") # when x=1 self.assertEqual(pot, marg)
def test_DoCalculusp213(self): fd = gum.fastBN("w->z->x->y") fdModele = csl.CausalModel(fd, [("u1", ["w", "x"]), ("u2", ["w", "y"])], True) formula, impact, explanation = csl.causalImpact(fdModele, on="y", doing="x")
def setUp(self): 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=1 m.cpt("z3")[:] = [ [0.3, 0.7], # z2=0 [0.3, 0.7] ] # z2=1 m.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] self.d = csl.CausalModel(m, [("X-Z2", ["x", "z2"]), ("X-Z3", ["x", "z3"]), ("X-Y", ["x", "y"]), ("Y-Z2", ["y", "z2"])], True) try: formula, result, msg = csl.causalImpact(self.d, on={"y", "z2", "z1", "z3"}, doing={"x"}) except csl.HedgeException as h: self.fail("Should not raise")
def test_CRAN2(self): bn = gum.fastBN("Z1->X->Z2->Y") d = csl.CausalModel(bn, [("L1", ["Z1", "X"]), ("L2", ["Z1", "Z2"]), ("L3", ["Z1", "Y"]), ("L4", ["Y", "X"])], True) _, pot, _ = csl.causalImpact(d, on="Y", doing={"X"}) self.assertIsNone(pot)
def test_tobacco1(self): obs1 = gum.fastBN("Smoking->Cancer") obs1.cpt("Smoking")[:] = [0.6, 0.4] obs1.cpt("Cancer")[{"Smoking": 0}] = [0.9, 0.1] obs1.cpt("Cancer")[{"Smoking": 1}] = [0.7, 0.3] modele1 = csl.CausalModel(obs1) lat, pot, expl = csl.causalImpact( modele1, "Cancer", "Smoking") # when doing is 1 self.assertEqual(pot, obs1.cpt("Cancer")) modele2 = csl.CausalModel(obs1, [("Genotype", ["Smoking", "Cancer"])]) lat, pot, expl = csl.causalImpact( modele2, "Cancer", {"Smoking"}, values={"Smoking": 1}) margCancer = (obs1.cpt("Smoking") * obs1.cpt("Cancer") ).margSumOut(["Smoking"]) self.assertEqual(pot, margCancer) modele3 = csl.CausalModel( obs1, [("Genotype", ["Smoking", "Cancer"])], True) lat, pot, expl = csl.causalImpact( modele3, "Cancer", {"Smoking"}, values={"Smoking": 1}) self.assertIsNone(pot)
def testFromNotebooks(self): 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]] d = csl.CausalModel(bn, [("lat1", ["x", "y"])]) _, pot, _ = csl.causalImpact(d, on="y", doing="x") self.assertIsNotNone(pot)
def test_Carouselp115(self): ab = gum.fastBN('Elapsed time[11]->Bag on Carousel<-Bag on Plane') ab.cpt("Bag on Plane").fillWith(1).normalize() ab.cpt("Elapsed time").fillWith(1).normalize() ab.cpt("Bag on Carousel").fillWith( [1.0, 0.0] * 11 + [1 - i / 20 if i % 2 == 0 else (i - 1) / 20 for i in range(22)]) abModele = csl.CausalModel(ab) formula, impact, explanation = csl.causalImpact( abModele, on={"Bag on Plane"}, doing={"Elapsed time"}, knowing={"Bag on Carousel"}, values={ "Elapsed time": 7, "Bag on Carousel": 0 }) self.assertAlmostEqual(impact[0], 0.7692, 4)
def test_tobacco2(self): obs2 = gum.fastBN("Smoking->Tar->Cancer;Smoking->Cancer") obs2.cpt("Smoking")[:] = [0.6, 0.4] obs2.cpt("Tar")[{"Smoking": 0}] = [0.9, 0.1] obs2.cpt("Tar")[{"Smoking": 1}] = [0.7, 0.3] obs2.cpt("Cancer")[{"Tar": 0, "Smoking": 0}] = [0.9, 0.1] obs2.cpt("Cancer")[{"Tar": 1, "Smoking": 0}] = [0.8, 0.2] obs2.cpt("Cancer")[{"Tar": 0, "Smoking": 1}] = [0.7, 0.3] obs2.cpt("Cancer")[{"Tar": 1, "Smoking": 1}] = [0.6, 0.4] modele4 = csl.CausalModel(obs2, [("Genotype", ["Smoking", "Cancer"])]) # from formula : \sum_{ Tar }{ P(Tar\mid Smoking) \cdot # \sum_{ Smoking' }{ P(Cancer\mid Smoking',Tar) \cdot P(Smoking') } # } margCancer = (obs2.cpt("Cancer") * obs2.cpt("Smoking") ).margSumOut(['Smoking']) margCancer = (margCancer * obs2.cpt("Tar")).margSumOut(['Tar']) lat, pot, _ = csl.causalImpact( modele4, "Cancer", "Smoking") # when smoking=1 self.assertEqual(pot, margCancer)
def setUp(self): m1 = gum.fastBN("Gender->Drug->Patient;Gender->Patient") m1.cpt("Gender")[:] = [0.5, 0.5] m1.cpt("Drug")[:] = [ [0.25, 0.75], # Gender=0 [0.75, 0.25] ] # Gender=1 m1.cpt("Patient")[{ 'Drug': 0, 'Gender': 0 }] = [0.2, 0.8] # No Drug, Male -> healed in 0.8 of cases # No Drug, Female -> healed in 0.4 of cases m1.cpt("Patient")[{'Drug': 0, 'Gender': 1}] = [0.6, 0.4] m1.cpt("Patient")[{ 'Drug': 1, 'Gender': 0 }] = [0.3, 0.7] # Drug, Male -> healed 0.7 of cases m1.cpt("Patient")[{ 'Drug': 1, 'Gender': 1 }] = [0.8, 0.2] # Drug, Female -> healed in 0.2 of cases self.model = csl.CausalModel(m1)
mod.add(gum.LabelizedVariable("tar")) mod.add(gum.LabelizedVariable("cancer")) mod.addArc(0, 1) # smoking -> tar -> cancer; smoking -> cancer mod.addArc(1, 2) mod.addArc(0, 2) # Smoking mod.cpt(0)[:] = [0.5, 0.5] # Tar deposits mod.cpt(1)[:] = [0.4, 0.6], [0.3, 0.6] # Lung Cancer mod.cpt(2)[{"smoking": 0, "tar": 0}] = [0.1, 0.9] mod.cpt(2)[{"smoking": 0, "tar": 1}] = [0.15, 0.85] mod.cpt(2)[{"smoking": 1, "tar": 0}] = [0.2, 0.8] mod.cpt(2)[{"smoking": 1, "tar": 1}] = [0.25, 0.75] d = csl.CausalModel(mod, [("Genotype", ["smoking", "cancer"])], False) # False == do not keep arcs cslnb.showCausalModel(d) try: a = csl.doCalculusWithObservation(d, "cancer", {"smoking"}) except csl.HedgeException as h: print(h.message) # the variable "a" shows us the front-door adjustment formula for smoking causes lung cancer display(Math(a.toLatex()))
def test_AccentsInVariables(self): bn = gum.fastBN("héhé->hoho") cm = csl.CausalModel(bn) formula, impact, explanation = csl.causalImpact(cm, "héhé", "hoho")
bn = gum.fastBN("w->x->z->y;w->z") # -- conditional probability tables 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] # **** Causal model plot **** # d is a causal model! It receives a bayesian network and the unmeasured variables in it along # with the variables that are affected by these latent variables # -- "lat1" is an unmeasured variable that affects X and Y (its parent of both -- the common cause in the fork) d = csl.CausalModel(bn, [("lat1", ['x', 'y'])]) # -- causal impact of X on Y when we do(X = 0) # this works but the representation of the causal impact returned isn't very good ###### csl.causalImpact(d, 'y', 'x', '', {'x':0}) # shows the causal impact of X on Y, when we do(X = 0), on the causal model D cslnb.showCausalImpact(d, "y", "x", values={"x": 0}) # causal impact of X on Y when do(X = 1), on causal model d cslnb.showCausalImpact(d, "y", "x", values={"x": 1})
# This example is from the book --- Causal Inference in Statistics: A Primer # page 62, figure 3.6 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 bn = gum.fastBN("x->y<-w") # -- conditional probabilities table bn.cpt('w')[:] = [0.7, 0.3] bn.cpt('x')[:] = [0.4, 0.6] bn.cpt('y')[{'x': 0, 'w': 0}] = [0.2, 0.8] bn.cpt('y')[{'x': 0, 'w': 1}] = [0.1, 0.9] bn.cpt('y')[{'x': 1, 'w': 0}] = [0.9, 0.1] bn.cpt('y')[{'x': 1, 'w': 0}] = [0.5, 0.5] # ** making the Causal Model d = csl.CausalModel(bn, [('z', ['x', 'w'])]) cslnb.showCausalImpact(d, 'y', 'x', values={'x': 0}) cslnb.showCausalImpact(d, 'y', 'x', values={'x': 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 bn = gum.fastBN("smoking->tar->cancer") # -- conditional probabilities table -- bn.cpt("smoking")[:] = [0.5, 0.5] bn.cpt("tar")[{"smoking": 0}] = [380 / 400, 20 / 400] bn.cpt("tar")[{"smoking": 1}] = [20 / 400, 380 / 400] bn.cpt("cancer")[{"tar": 0, "smoking": 0}] = [0.1, 0.9] bn.cpt("cancer")[{"tar": 0, "smoking": 1}] = [0.9, 0.1] bn.cpt("cancer")[{"tar": 1, "smoking": 0}] = [0.05, 0.95] bn.cpt("cancer")[{"tar": 1, "smoking": 1}] = [0.85, 0.15] d = csl.CausalModel(bn, [("genes", ["smoking", "cancer"])]) cslnb.showCausalImpact(d, "cancer", "smoking", values={"smoking": 1}) cslnb.showCausalImpact(d, "cancer", "smoking", values={"smoking": 0})