The discrete module in GTSAM provides a comprehensive framework for probabilistic inference over discrete random variables. This module implements Bayesian networks, factor graphs, and efficient inference algorithms using decision trees and elimination methods.
Overview¶
The discrete inference workflow in GTSAM typically follows this pattern:
Model Definition: Start with Bayesian networks using
DiscreteBayesNet
andDiscreteConditional
Likelihood Integration: Transform to factor graphs with
DiscreteFactorGraph
and various factor typesInference: Eliminate variables to obtain
DiscreteBayesTree
for efficient marginalizationSpecialized Operations: Use advanced classes for specific inference tasks
Bayesian Networks for Modeling¶
DiscreteBayesNet¶
The foundational class for representing Bayesian networks over discrete variables. A Bayes net defines a joint probability distribution as a product of conditional probability distributions.
DiscreteBayesNet bayesNet;
bayesNet.add(conditional1);
bayesNet.add(conditional2);
Notebook: DiscreteBayesNet
DiscreteConditional¶
Represents conditional probability tables P(X|Y1, Y2, ...) in the Bayesian network. These form the building blocks of Bayes nets and encode the conditional dependencies between variables.
DiscreteConditional conditional(key, parents, signature);
Notebook: DiscreteConditional
DiscreteDistribution¶
Represents marginal probability distributions P(X) for discrete variables. Used for priors.
Notebook: DiscreteDistribution
Signature¶
Handy class to define the conditional probability structure for discrete variables, specifying parent-child relationships and cardinalities.
Notebook: Signature.ipynb
Discrete Factor Graphs¶
DiscreteFactorGraph¶
The central class for factor graph representation. Bayes nets can be transformed into factor graphs by converting conditionals to factors, allowing for the integration of likelihood factors from observations.
DiscreteFactorGraph graph = bayesNet.toFactorGraph();
graph.add(likelihoodFactor);
Notebook: DiscreteFactorGraph
DiscreteFactor¶
Abstract base class for all discrete factors. Factors represent arbitrary functions over discrete variables and can encode both prior knowledge and likelihood information.
DecisionTreeFactor¶
A concrete implementation of discrete factors using decision trees for efficient storage and computation. Particularly effective for factors with conditional independence structure.
Notebook: DecisionTreeFactor
TableFactor¶
Represents factors as explicit probability tables. Useful for small factors or when the full joint distribution needs to be stored explicitly.
Inference: Bayes Trees¶
DiscreteBayesTree¶
The result of variable elimination on a factor graph. Bayes trees enable efficient exact inference through their clique tree structure, supporting fast marginalization and conditioning operations.
DiscreteBayesTree bayesTree = graph.eliminateMultifrontal(ordering);
DiscreteValues result = bayesTree.optimize();
DiscreteEliminationTree¶
Intermediate structure used during the elimination process. Represents the computational tree for variable elimination before conversion to a Bayes tree.
DiscreteJunctionTree¶
Alternative tree structure for inference, particularly useful for repeated marginal queries over the same model.
Specialized and Exotic Classes¶
DiscreteMarginals¶
Provides efficient computation of marginal probabilities for all variables in a factor graph or Bayes tree. Essential for uncertainty quantification.
DiscreteLookupDAG¶
Specialized data structure for fast lookup operations in discrete probabilistic models. Optimized for scenarios with repeated queries.
DiscreteSearch¶
Implements search algorithms over discrete probability spaces, including optimization and sampling methods.
AlgebraicDecisionTree¶
Implements decision trees with algebraic operations (addition, multiplication) for efficient factor operations. The computational backbone for many discrete operations.
Assignment and DiscreteValues¶
Core data structures for representing variable assignments and probability values in discrete models.
DiscreteKey¶
Represents discrete random variables with their cardinalities, fundamental for defining the variable space.
Decision Trees and Computational Primitives¶
DecisionTree¶
Template-based decision tree implementation supporting arbitrary value types. Provides the foundation for efficient discrete computations.
Ring¶
Mathematical abstraction for algebraic operations on decision tree values, enabling generic algorithms over different semirings.
Parser and Utilities¶
SignatureParser¶
Parses string representations of conditional probability signatures, enabling convenient model specification from text.
TableDistribution¶
Utility class for working with probability distributions represented as tables.
Example Workflows¶
Basic Bayesian Network¶
Define variables with
DiscreteKey
Create conditionals with
DiscreteConditional
Build network with
DiscreteBayesNet
Query marginals or sample
Factor Graph Inference¶
Start with
DiscreteBayesNet
or buildDiscreteFactorGraph
directlyAdd likelihood factors using
DecisionTreeFactor
orTableFactor
Eliminate variables to get
DiscreteBayesTree
Query results using
DiscreteMarginals
Advanced Operations¶
Use
AlgebraicDecisionTree
for custom factor operationsEmploy
DiscreteLookupDAG
for repeated queriesApply
DiscreteSearch
for optimization problems