Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

BayesNet

A BayesNet in GTSAM represents a directed graphical model, created by running sequential variable elimination (like Cholesky or QR factorization) on a FactorGraph or constructing from scratch.

It is essentially a collection of Conditional objects, ordered according to the elimination order. Each conditional represents P(variable∣parents)P(\text{variable} | \text{parents}), where the parents are variables that appear later in the elimination ordering.

A Bayes net represents the joint probability distribution as a product of conditional probabilities stored in the net:

P(X1,X2,…,XN)=∏i=1NP(Xi∣Parents(Xi))P(X_1, X_2, \dots, X_N) = \prod_{i=1}^N P(X_i | \text{Parents}(X_i))

The total log-probability of an assignment is the sum of the log-probabilities of its conditionals:

log⁡P(X1,…,XN)=∑i=1Nlog⁡P(Xi∣Parents(Xi))\log P(X_1, \dots, X_N) = \sum_{i=1}^N \log P(X_i | \text{Parents}(X_i))

Like FactorGraph, BayesNet is templated on the type of conditional it stores (e.g., GaussianBayesNet, DiscreteBayesNet).

Open In Colab

Creating a BayesNet (via Elimination)

BayesNets are typically obtained by eliminating a FactorGraph.

Original Factor Graph:

size: 3
factor 0: 
  A[x0] = [
	-1
]
  b = [ 0 ]
  Noise model: unit (1) 
factor 1: 
  A[x0] = [
	-1
]
  A[x1] = [
	1
]
  b = [ 0 ]
  Noise model: unit (1) 
factor 2: 
  A[x1] = [
	-1
]
  A[x2] = [
	1
]
  b = [ 0 ]
  Noise model: unit (1) 

Resulting BayesNet:

size: 3
conditional 0:  p(x0 | x1)
  R = [ 1.41421 ]
  S[x1] = [ -0.707107 ]
  d = [ 0 ]
  logNormalizationConstant: -0.572365
  No noise model
conditional 1:  p(x1 | x2)
  R = [ 1.22474 ]
  S[x2] = [ -0.816497 ]
  d = [ 0 ]
  logNormalizationConstant: -0.716206
  No noise model
conditional 2:  p(x2)
  R = [ 0.57735 ]
  d = [ 0 ]
  mean: 1 elements
  x2: 0
  logNormalizationConstant: -1.46824
  No noise model

Properties and Access

A BayesNet provides access to its constituent conditionals and basic properties.

BayesNet size: 3
Conditional at index 1: 
GaussianConditional p(x1 | x2)
  R = [ 1.22474 ]
  S[x2] = [ -0.816497 ]
  d = [ 0 ]
  logNormalizationConstant: -0.716206
  No noise model
Keys in BayesNet: x0x1x2

Evaluation and Solution

The logProbability(Values) method computes the log probability of a variable assignment given the conditional distributions in the Bayes net. For Gaussian Bayes nets, the optimize() method can be used to find the maximum likelihood estimate (MLE) solution via back-substitution.

Log Probability at 0,0,0]: -2.7568155996140185
Optimized Solution (MLE):
VectorValues: 3 elements
  x0: 0
  x1: 0
  x2: 0

Visualization

Bayes nets can also be visualized using Graphviz.

Loading...

Example: DiscreteBayesNet (Asia Network)

While the previous examples focused on GaussianBayesNet, GTSAM also supports DiscreteBayesNet for representing probability distributions over discrete variables. Here we construct the classic ‘Asia’ network example directly by adding DiscreteConditional objects.

Asia Bayes Net:
DiscreteBayesNet
 
size: 8
conditional 0:  P( D1 | E3 B4 ):
 Choice(E3) 
 0 Choice(D1) 
 0 0 Choice(B4) 
 0 0 0 Leaf  0.9
 0 0 1 Leaf  0.2
 0 1 Choice(B4) 
 0 1 0 Leaf  0.1
 0 1 1 Leaf  0.8
 1 Choice(D1) 
 1 0 Choice(B4) 
 1 0 0 Leaf  0.3
 1 0 1 Leaf  0.1
 1 1 Choice(B4) 
 1 1 0 Leaf  0.7
 1 1 1 Leaf  0.9

conditional 1:  P( X2 | E3 ):
 Choice(X2) 
 0 Choice(E3) 
 0 0 Leaf 0.95
 0 1 Leaf 0.02
 1 Choice(E3) 
 1 0 Leaf 0.05
 1 1 Leaf 0.98

conditional 2:  P( E3 | T6 L5 ):
 Choice(T6) 
 0 Choice(L5) 
 0 0 Choice(E3) 
 0 0 0 Leaf    1
 0 0 1 Leaf    0
 0 1 Choice(E3) 
 0 1 0 Leaf    0
 0 1 1 Leaf    1
 1 Choice(L5) 
 1 0 Choice(E3) 
 1 0 0 Leaf    0
 1 0 1 Leaf    1
 1 1 Choice(E3) 
 1 1 0 Leaf    0
 1 1 1 Leaf    1

conditional 3:  P( B4 | S7 ):
 Choice(S7) 
 0 Choice(B4) 
 0 0 Leaf  0.7
 0 1 Leaf  0.3
 1 Choice(B4) 
 1 0 Leaf  0.4
 1 1 Leaf  0.6

conditional 4:  P( L5 | S7 ):
 Choice(S7) 
 0 Choice(L5) 
 0 0 Leaf 0.99
 0 1 Leaf 0.01
 1 Choice(L5) 
 1 0 Leaf  0.9
 1 1 Leaf  0.1

conditional 5:  P( T6 | A8 ):
 Choice(T6) 
 0 Choice(A8) 
 0 0 Leaf 0.99
 0 1 Leaf 0.95
 1 Choice(A8) 
 1 0 Leaf 0.01
 1 1 Leaf 0.05

conditional 6:  P( S7 ):
 Leaf  0.5

conditional 7:  P( A8 ):
 Choice(A8) 
 0 Leaf 0.99
 1 Leaf 0.01

Loading...
Log Probability of all zeros: -1.2366269421045588
Sampled Values (basic print):
DiscreteValues{4683743612465315848: 0, 4755801206503243780: 1, 4899916394579099649: 1, 4971973988617027587: 0, 5476377146882523141: 0, 5980780305148018695: 1, 6052837899185946630: 0, 6341068275337658370: 0}
Sampled Values (pretty print):
  A8: 0
  B4: 1
  D1: 1
  E3: 0
  L5: 0
  S7: 1
  T6: 0
  X2: 0