Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  InfluenceDiagram 
pyAgrum 0.15.1   
Zipped notebooks   
generation: 2019-06-16 19:06  

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,size="6",format='svg')
res/diag.bifxml decisionVar1 decisionVar1 chanceVar1 chanceVar1 decisionVar1->chanceVar1 decisionVar2 decisionVar2 utilityVar1 utilityVar1 decisionVar2->utilityVar1 chanceVar4 chanceVar4 decisionVar2->chanceVar4 decisionVar3 decisionVar3 chanceVar3 chanceVar3 decisionVar3->chanceVar3 decisionVar4 decisionVar4 utilityVar2 utilityVar2 decisionVar4->utilityVar2 chanceVar1->utilityVar1 chanceVar2 chanceVar2 chanceVar1->chanceVar2 chanceVar2->decisionVar2 chanceVar2->decisionVar3 chanceVar5 chanceVar5 chanceVar3->chanceVar5 chanceVar4->chanceVar5 chanceVar5->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,size="6",format="svg") 
# note the difference between png (above) and svg (here)rendering
res/diag.bifxml decisionVar1 decisionVar1 chanceVar1 chanceVar1 decisionVar1->chanceVar1 F F decisionVar1->F decisionVar2 decisionVar2 utilityVar1 utilityVar1 decisionVar2->utilityVar1 chanceVar4 chanceVar4 decisionVar2->chanceVar4 decisionVar3 decisionVar3 U U decisionVar3->U chanceVar3 chanceVar3 decisionVar3->chanceVar3 decisionVar4 decisionVar4 utilityVar2 utilityVar2 decisionVar4->utilityVar2 chanceVar1->utilityVar1 chanceVar2 chanceVar2 chanceVar1->chanceVar2 chanceVar2->decisionVar2 chanceVar2->decisionVar3 chanceVar5 chanceVar5 chanceVar3->chanceVar5 chanceVar4->chanceVar5 chanceVar5->utilityVar2 F->U
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)
res/DecAsia.bifxml H H Ta Ta H->Ta Uh Uh H->Uh Ut Ut Ta->Ut P P Ta->P S S B B S->B L L S->L D D B->D L->Uh E E L->E E->D E->P Tu Tu Tu->Uh Tu->Ut Tu->E A A A->Tu
In [7]:
oil=gum.loadID(os.path.join("res","OilWildcater.bifxml"))
gnb.showInfluenceDiagram(oil)
res/OilWildcater.bifxml T T D D T->D Ut Ut T->Ut R R T->R Ud Ud D->Ud R->D O O O->Ud O->R
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 [ ]: