Influence diagram
![]() | ![]() |
import math
import pyagrum as gumimport pyagrum.lib.notebook as gnbBuild a Influence Diagram
Section titled “Build a Influence Diagram”fast build with string
Section titled “fast build with string”gum.fastID("A->*B->$C<-D<-*E->*G->H->*I<-D")bifxml format file
Section titled “bifxml format file”diag = gum.loadID("res/diag.bifxml")gnb.showInfluenceDiagram(diag)diagthe hard way :-)
Section titled “the hard way :-)”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)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}] = 2diag.utility(U)[{"F": 0, "decisionVar3": 1}] = 4diag.utility(U)[{"F": 1}] = [[0], [5]]Optimization in an influence diagram (actually LIMID)
Section titled “Optimization in an influence diagram (actually LIMID)”oil = gum.loadID("res/OilWildcatter.bifxml")gnb.flow.row(oil, gnb.getInference(oil))Inference in the LIMID optimizing the decisions nodes
Section titled “Inference in the LIMID optimizing the decisions nodes”## a function to show results on decision nodes T and Ddef show_decisions(ie): gnb.flow.row( ie.optimalDecision("Testing"), ie.optimalDecision("Drilling"), f"$${ie.MEU()['mean']:5.3f}\\ (stdev : {math.sqrt(ie.MEU()['variance']):5.3f})$$", captions=["Strategy for T", "Strategy for D", "MEU and its standard deviation"], ) gnb.flow.row( ie.posterior("Testing"), ie.posteriorUtility("Testing"), ie.posterior("Drilling"), ie.posteriorUtility("Drilling"), captions=[ "Final decision for Testing", "Final reward for Testing", "Final decision for Drilling", "Final reward for Drilling", ], )
ie = gum.ShaferShenoyLIMIDInference(oil)ie.makeInference()show_decisions(ie)|
|
|
|---|---|
| 1.0000 | 0.0000 |
|
|
| |
|---|---|---|
| 1.0000 | 0.0000 | |
| 1.0000 | 0.0000 | |
| 0.0000 | 1.0000 | |
|
|
|
|---|---|
| 1.0000 | 0.0000 |
|
|
|
|---|---|
| 22.5000 | 20.0000 |
|
|
|
|---|---|
| 0.5900 | 0.4100 |
|
|
|
|---|---|
| 45.0847 | -10.0000 |
Graphical inference with evidence and targets (developped nodes)
Section titled “Graphical inference with evidence and targets (developped nodes)”gnb.sideBySide( oil, gnb.getInference(oil, evs={"TestResult": "closed"}), gnb.getInference(oil, evs={"TestResult": "open"}), gnb.getInference(oil, evs={"TestResult": "diffuse"}), oil, gnb.getInference(oil, evs={"OilContents": "Dry"}), gnb.getInference(oil, evs={"OilContents": "Wet"}), gnb.getInference(oil, evs={"OilContents": "Soaking"}), ncols=4,)Soft evidence on chance node
Section titled “Soft evidence on chance node”gnb.showInference(oil, evs={"OilContents": [0.7, 0.5, 0.8]})Forced decision
Section titled “Forced decision”gnb.showInference(oil, evs={"Drilling": "Yes"})LIMID versus Influence Diagram
Section titled “LIMID versus Influence Diagram”The default inference for influence diagram actually an inference for LIMIDs. In order to use it for classical (and solvable) influence diagram, do not forget to add the sequence of decision nodes using addNoForgettingAssumption.
infdiag = gum.fastID("Chance->*Decision1->Chance2->$Utility<-Chance3<-*Decision2<-Chance->Utility")infdiagie = gum.ShaferShenoyLIMIDInference(infdiag)try: ie.makeInference()except gum.GumException as e: print(e)[pyAgrum] Fatal error: This LIMID/Influence Diagram is not solvable.ie.addNoForgettingAssumption(["Decision1", "Decision2"])gnb.sideBySide(ie.reducedLIMID(), ie.junctionTree(), gnb.getInference(infdiag, engine=ie))Customizing visualization of the results
Section titled “Customizing visualization of the results”Using pyagrum.config, it is possible to adapt the graphical representations for Influence Diagram (see the notebook 99-Tools_configForPyAgrum.ipynb).
gum.config.reset()gnb.showInference(infdiag, engine=ie, size="7!")Many visual options can be changed when displaing an inference (especially for influence diagrams)
## do not show inference timegum.config["notebook", "show_inference_time"] = False## more digits for probabilitiesgum.config["notebook", "histogram_horizontal_visible_digits"] = 3
gnb.showInference(infdiag, engine=ie, size="7!")## specificic for influence diagram :## more digits for utilitiesgum.config["influenceDiagram", "utility_visible_digits"] = 5## disabling stdev for utility and MEUgum.config["influenceDiagram", "utility_show_stdev"] = False## showing loss (=-utility) and mEL (minimum Expected Loss) instead of MEUgum.config["influenceDiagram", "utility_show_loss"] = True
gnb.showInference(infdiag, engine=ie, size="7!")## visual changes for influence diagram and inferencegum.config.reset()gum.config.push() # keep the current stategum.config["notebook", "graph_rankdir"] = "LR"gnb.sideBySide(infdiag, gnb.getInference(infdiag, engine=ie, targets=["Decision1", "Chance3"]))## more visual changes for influence diagram and inferencegum.config.pop() # back to the last state
## shape (https://graphviz.org/doc/info/shapes.html)gum.config["influenceDiagram", "chance_shape"] = "cylinder"gum.config["influenceDiagram", "utility_shape"] = "star"gum.config["influenceDiagram", "decision_shape"] = "box3d"
## colorsgum.config["influenceDiagram", "default_chance_bgcolor"] = "green"gum.config["influenceDiagram", "default_utility_bgcolor"] = "MediumVioletRed"gum.config["influenceDiagram", "default_decision_bgcolor"] = "DarkSalmon"
gum.config["influenceDiagram", "utility_show_stdev"] = False
gnb.sideBySide(infdiag, gnb.getInference(infdiag, engine=ie, targets=["Decision1", "Chance3"]))
