5. Bayesian Network

5.1. Network

class pybn.network.Network(name)[source]

Bayesian Network

Attributes :
  • name (str): Name of the network
addNode(node)[source]

Add one node to the network

Args :
  • node (Node): Node element
addNodes(nodes)[source]

Add a list of nodes to the network

Args :
  • nodes (list): A list of Node elements
computeBeliefs()[source]

Compute beliefs of the network

getBeliefs(vars=None)[source]

Returns all beliefs of the network

Returns :
  • beliefs: Return a list with all beliefs of the network.
getEvidence()[source]

Return information about the evidence

Returns :
  • evidence (list): Return a list with all observed nodes. First entry is the node number, second entry the stage which is observed.
reset()[source]

Reset all values

setEvidence(name, value)[source]

Set evidence for a Node element

Args :
  • name (str): Name of the Node
  • value (int): Number of stage which is observed. Starting with 0.
writeFile(filename)[source]

Write an output file

Args :
  • filename (str): Name of the outputfile, If no name is defined the name of the network will be used.
Returns :
  • outputfile: The output files is saved in the local folder.
Raises :
  • Error: No nodes connected to the network!

  • Error: Node ‘xy’ has no outcomes!

  • Error: Probabilities for ‘xy’ doesn’t match!

    Len of probabilities should be a but is b

  • Error: Probabilities for ‘xy’ doesn’t sum up to 1.0!

5.2. Node

class pybn.network.Node(name)[source]

Node element for the Bayesian Network

Attributes :
  • name (str): Name of the node
addOutcome(name)[source]

Add one outcome to the node

Args :
  • name (str): Name of the outcome
addOutcomes(names)[source]

Add a list of outcomes to the node

Args :
  • names (list): A list of names for the outcomes
bar_active = None

View as Bar Chart

Default :
  • False: i.e. GeNIe will show Icons instead Bar Charts by default
getBeliefs()[source]

Returns beliefs for the node

Args :
  • beliefs (list): Returns a list of beliefs for the node
getCard()[source]

Returns the cardinality of the factor

Returns :
  • card (list): Returns a list of cardinalities corresponding to variables of the network.
getInput()[source]

Returns the input values for the Bayesian network

Retunrs :
  • var,card,val (tuple): var variables in the factor

    card cardinalities corresponding to var

    val value of the node

getOutcomes()[source]

Returns a list of outcomes

Returns :
  • outcomes (list): Returns a list of outcomes
getProbabilities()[source]

Returns a list of probabilities

Returns :
  • probabilities (list): Returns a list of probabilities
getProbability(index)[source]

Returns a single probabilitiy

Args :
  • index (int): Position in the list of probabilities
Returns :
  • probabilitiy (float): Returns a single probabilitiy out of the list of probabilities
getTableSize()[source]

Returns the size of the probability table

Retunrs :
  • m,n (tuple): m represents the rows (number of outcomes)

    n represents the columns (depending from the parents and their outcomes)

getVal()[source]

Returns values of the node

Returns :
  • val (list): Return a list of values for the node
getVar()[source]

Returns the variables in the factor

Returns :
  • var (list): List of variables (nodes) in the factor
setBarActive(bar_active)[source]

View node as Icon or Bar Chart

Args :
  • bar_active (boolean): True for Bar Chart view

    False for Icon view

setBeliefs(beliefs)[source]

Set beliefs for the node

Args :
  • beliefs (list): List of beliefs for the node
setCard(card)[source]

Set cardinality of the factor

Args :
  • card (list): Cardinality of the factor
setFontColor(font_color)[source]

Set font color

Args :
  • font_color (str): Font color given as hex string
Example :
>>>
# red: rgb(255, 0, 0) -> ff0000
xy.setFontColor('ff0000')
setFontName(font_name)[source]

Set font name

Args :
  • font_name (str): Type of font, e.g. ‘Arial’, ‘Times New Roman’
setFontSize(font_size)[source]

Set font size

Args :
  • font_size (int): Size of font, e.g. 8, 12, ...
setInteriorColor(interior_color)[source]

Set interior color

Args :
  • interior_color (str): Interior color given as hex string
Example :
>>>
# red: rgb(255, 0, 0) -> ff0000
xy.setInteriorColor('ff0000')
setNodePosition(x, y)[source]

Set position of the node

Args :
  • x (int): Position in x direction
  • y (int): Position in y direction
setNodeSize(x, y)[source]

Set size of the node

Args :
  • x (int): Size in x direction
  • y (int): Size in y direction
setOutlineColor(outline_color)[source]

Set outline color

Args :
  • outline_color (str): Outline color given as hex string
Example :
>>>
# red: rgb(255, 0, 0) -> ff0000
xy.setOutlineColor('ff0000')
setProbabilities(probabilities)[source]

Set the probabilities for the node

The order of these probabilities is given by considering the state of the first parent of the node as the most significant coordinate, then the second parent, then the third (and so on), and finally considering the coordinate of the node itself as the least significant one.

Args :
  • probabilities (list): A list of probabilities for the node
setVal(val)[source]

Set values of the node

Args :
  • val (list): List of values for the node
setVar(var)[source]

Set variables in the factor

Args :
  • var (list): List of variables (nodes) in the factor
transformProbabilities()[source]

Transform the probabilities for the node

Returns :
  • probabilities (list): Returns a list of transformed probabilities

5.3. Arc

class pybn.network.Arc(from_node, to_node)[source]

Arc between two nodes

Attributes :
  • from_node (Node): Node where the arc starts
  • to_node (Node): Node where the arc points

Table Of Contents

Previous topic

4. Theoretical Background

Next topic

6. Calculations

This Page