openturns-agrum: a new module for OpenTURNS, that allows to create a distribution from a Bayesian Network using the aGrUM library

Hi, in collaboration with Pierre-Henri WUILLEMIN (LIP6, aGrUM team member) and Anne DUTFOY (EDF R&D, OpenTURNS team member), I have written a new OpenTURNS module that allows to create 1D OpenTURNS distributions as conditional marginal distributions of a Bayesian network built using aGrUM ( It is very useful when one has conditional information about an uncertain quantity but not statistical data directly related to this quantity.

This module is compatible with OpenTURNS 0.13.2 and aGrUM revision 382.

Here an example of a python script to use this module:

from openturns import * from pyAgrum import * from otagrum import *

  1. Create an empty BN

myBN = BayesNet()

  1. Create light variable

light = LabelizedVar("Light", "Light", 0) light.addLabel("Dim") light.addLabel("Bright") lightSize = len(light)

  1. Create moisture vriable

moisture = LabelizedVar("Moisture", "Moisture", 0) moisture.addLabel("Dry") moisture.addLabel("Wet") moistureSize = len(moisture)

  1. Create height variable

height = DiscretizedVar("Height", "Height") data = NumericalPoint(range(10, 100, 10)) heightWhenDimAndDry = Uniform(0.0, 40.0) heightWhenDimAndWet = Uniform(10.0, 60.0) heightWhenBrightAndDry = Uniform(0.0, 20.0) heightWhenBrightAndWet = Uniform(40.0, 100.0) data = BayesNetAgrum.AdaptGrid(DistributionCollection(heightWhenDimAndDry, heightWhenDimA...), data) heightSize = data.getSize() - 1 for i in list(data):


  1. Add variables to the net

indexLight = myBN.add(light) indexMoisture = myBN.add(moisture) indexHeight = myBN.add(height)

  1. Create arcs

myBN.insertArc(indexLight, indexMoisture) myBN.insertArc(indexLight, indexHeight) myBN.insertArc(indexMoisture, indexHeight)

  1. Create conditional probability tables

myBN.cpt(indexLight):= 0.5, 0.5 myBN.cpt(indexMoisture){'Light' : 'Dim'} = 0.2, 0.8 myBN.cpt(indexMoisture){'Light' : 'Bright'} = 0.6, 0.4 myBN.cpt(indexHeight){'Light': 'Dim', 'Moisture': 'Dry'} = BayesNetAgrum().Discretize(heightWhenDimAndDry, data) myBN.cpt(indexHeight){'Light': 'Bright', 'Moisture': 'Dr... = BayesNetAgrum().Discretize(heightWhenBrightAndDry, data) myBN.cpt(indexHeight){'Light': 'Dim', 'Moisture': 'Wet'} = BayesNetAgrum().Discretize(heightWhenDimAndWet, data) myBN.cpt(indexHeight){'Light': 'Bright', 'Moisture': 'We... = BayesNetAgrum().Discretize(heightWhenBrightAndWet, data)

  1. Create a BayesNetAgrum object

otbn = BayesNetAgrum(myBN)

  1. Export to BIF file


  1. Perform inference

for evidenceValue in "Bright", "Dim":

   # First, set evidence
   otbn.setEvidence("Light", evidenceValue)
   # Second, get a marginal distribution of interrest
   heightDistribution = otbn.getMarginal("Height")
   heightDistribution.setName("Height_Light_" + evidenceValue)
   # Use the distribution the way you want
   print "heightDistribution=", heightDistribution
   heightDistribution.drawPDF(-10.0, 110.0, 512).draw("PDF_" + heightDistribution.getName())
   heightDistribution.drawCDF(-10.0, 110.0, 512).draw("CDF_" + heightDistribution.getName())


No comments.