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.

DecisionTree

DecisionTree is mostly an internal data structure used to provide discrete probability distributions. It represents a function whose output depends on a sequence of discrete choices. GTSAM uses it underneath hybrid conditionals and factors; Python exposes useful concrete specializations rather than a class named DecisionTree.

Open In Colab
import gtsam
import numpy as np
from gtsam.symbol_shorthand import M, X
from IPython.display import Markdown, display

Concrete decision trees in Python

DecisionTree<L, Y> is a C++ template, so there is no gtsam.DecisionTree constructor. The wrapper currently provides two concrete specializations:

  • HybridNonlinearFactorValuePairs, whose leaves hold nonlinear-factor/weight pairs.

  • HybridGaussianConditionalConditionals, whose leaves hold Gaussian conditionals.

Both constructors take the discrete labels that index the tree followed by one leaf value for each joint assignment. empty() and nrLeaves() are the principal structural queries exposed for these specializations.

mode = (M(0), 2)
noise = gtsam.noiseModel.Isotropic.Sigma(3, 0.1)
pose_prior = gtsam.PriorFactorPose2(X(0), gtsam.Pose2(), noise)

# M0=0 selects the first pair; M0=1 selects the second.
factor_tree = gtsam.HybridNonlinearFactorValuePairs(
    [mode], [(pose_prior, 0.0), (pose_prior, 2.0)]
)
print("empty:", factor_tree.empty())
print("number of leaves:", factor_tree.nrLeaves())

Gaussian-conditional leaves

The Gaussian specialization is normally produced while eliminating a hybrid graph. The example constructs two one-dimensional Gaussian conditionals explicitly to show the same label-first construction pattern.

conditional0 = gtsam.GaussianConditional(
    X(0), np.array([0.0]), np.array([[1.0]])
)
conditional1 = gtsam.GaussianConditional(
    X(0), np.array([1.0]), np.array([[1.0]])
)
conditional_tree = gtsam.HybridGaussianConditionalConditionals(
    [mode], [conditional0, conditional1]
)
print("empty:", conditional_tree.empty())
print("number of leaves:", conditional_tree.nrLeaves())

When to use it

Most users do not manipulate these specializations directly. They are most useful when implementing hybrid factors or inspecting the piecewise Gaussian/nonlinear objects returned by hybrid elimination. For scalar probability tables, DecisionTreeFactor provides the higher-level interface.

Source

DecisionTree.h

AI assistance caveat

AI was used to help draft this documentation, and inaccuracies could be present.