Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  InfluenceDiagram 
pyAgrum 0.16.2   
Zipped notebooks   
generation: 2019-10-02 10:58  

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

In [1]:
import os

%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt
In [2]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
In [3]:
diag=gum.loadID(os.path.join("res","diag.bifxml"))
gnb.showInfluenceDiagram(diag)
G chanceVar1 chanceVar1 chanceVar2 chanceVar2 chanceVar1->chanceVar2 utilityVar1 utilityVar1 chanceVar1->utilityVar1 decisionVar2 decisionVar2 chanceVar2->decisionVar2 decisionVar3 decisionVar3 chanceVar2->decisionVar3 chanceVar3 chanceVar3 chanceVar5 chanceVar5 chanceVar3->chanceVar5 chanceVar4 chanceVar4 chanceVar4->chanceVar5 utilityVar2 utilityVar2 chanceVar5->utilityVar2 decisionVar1 decisionVar1 decisionVar1->chanceVar1 decisionVar2->chanceVar4 decisionVar2->utilityVar1 decisionVar3->chanceVar3 decisionVar4 decisionVar4 decisionVar4->utilityVar2
In [4]:
F=diag.addChanceNode(gum.LabelizedVariable("F","F",2))
diag.addArc(diag.idFromName("decisionVar1"),F)

U=diag.addUtilityNode(gum.LabelizedVariable("U","U",1))
diag.addArc(diag.idFromName("decisionVar3"),U)
diag.addArc(diag.idFromName("F"),U)

gnb.showInfluenceDiagram(diag) 
# note the difference between png (above) and svg (here)rendering
G chanceVar1 chanceVar1 chanceVar2 chanceVar2 chanceVar1->chanceVar2 utilityVar1 utilityVar1 chanceVar1->utilityVar1 decisionVar2 decisionVar2 chanceVar2->decisionVar2 decisionVar3 decisionVar3 chanceVar2->decisionVar3 chanceVar3 chanceVar3 chanceVar5 chanceVar5 chanceVar3->chanceVar5 chanceVar4 chanceVar4 chanceVar4->chanceVar5 utilityVar2 utilityVar2 chanceVar5->utilityVar2 F F U U F->U decisionVar1 decisionVar1 decisionVar1->chanceVar1 decisionVar1->F decisionVar2->chanceVar4 decisionVar2->utilityVar1 decisionVar3->chanceVar3 decisionVar3->U decisionVar4 decisionVar4 decisionVar4->utilityVar2
In [5]:
diag.cpt(F)[{'decisionVar1':0}]=[0.9,0.1]
diag.cpt(F)[{'decisionVar1':1}]=[0.3,0.7]

diag.utility(U)[{'F':0,'decisionVar3':0}]=2
diag.utility(U)[{'F':0,'decisionVar3':1}]=4
diag.utility(U)[{'F':1}]=[[0],[5]]
In [6]:
asia=gum.loadID(os.path.join("res","DecAsia.bifxml"))
gnb.showInfluenceDiagram(asia)
G S S B B S->B L L S->L D D B->D E E L->E Uh Uh L->Uh E->D P P E->P Tu Tu Tu->E Tu->Uh Ut Ut Tu->Ut A A A->Tu H H Ta Ta H->Ta H->Uh Ta->P Ta->Ut
In [7]:
oil=gum.loadID(os.path.join("res","OilWildcater.bifxml"))
gnb.showInfluenceDiagram(oil)
G R R D D R->D O O O->R Ud Ud O->Ud T T T->R T->D Ut Ut T->Ut D->Ud
In [8]:
ie=gum.InfluenceDiagramInference(oil)
In [9]:
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
Best decision for T : 1
Best decision for D : 1
For a maxEU : 60.0
In [10]:
print(oil.variable(3))
O<Dry,Wet,Soak>
In [11]:
ie.eraseAllEvidence()
ie.setEvidence({3:0})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
Best decision for T : 1
Best decision for D : 1
For a maxEU : 0.0
In [12]:
ie.eraseAllEvidence()
ie.setEvidence({3:1})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
Best decision for T : 1
Best decision for D : 0
For a maxEU : 60.0
In [13]:
ie.eraseAllEvidence()
ie.setEvidence({3:2})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
Best decision for T : 1
Best decision for D : 0
For a maxEU : 10.0
In [14]:
ie.eraseAllEvidence()
ie.setEvidence({3:[1,0.5,0.1]})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
Best decision for T : 1
Best decision for D : 1
For a maxEU : 30.0
In [ ]: