TableDistribution was a DiscreteConditional that stored its probabilities in a sparse table. A DiscreteConditional can now store its probabilities in any DiscreteFactor, so constructing one from a TableFactor gives the same sparse representation without a separate class.
TableDistribution is deprecated in GTSAM 4.3. In C++ it is available only when GTSAM_ALLOW_DEPRECATED_SINCE_V43 is enabled, and it is no longer wrapped in Python. This page shows the replacement for each use.
import gtsam
import numpy as npCreating a distribution¶
Normalize a TableFactor into a DiscreteConditional, taking all of its keys as frontals. The conditional keeps the sparse table, which factor() returns.
weather = (gtsam.symbol("W", 0), 3)
weather_keys = gtsam.DiscreteKeys()
weather_keys.push_back(weather)
# Previously: gtsam.TableDistribution(weather, [6.0, 3.0, 1.0])
distribution = gtsam.DiscreteConditional(
1, gtsam.TableFactor(weather_keys, [6.0, 3.0, 1.0]))
print("stored as:", type(distribution.factor()).__name__)
print("number of values:", distribution.nrValues())Evaluating and choosing values¶
The conditional supports evaluate(), logProbability(), argmax(), and sample(). evaluate() expects DiscreteValues; argmax() returns the most likely frontal value for the supplied parent assignment.
values = gtsam.DiscreteValues()
values[weather[0]] = 0
probability = distribution.evaluate(values)
print("P(W0=0):", probability)
print("most likely state:", distribution.argmax(gtsam.DiscreteValues()))
assert np.isclose(probability, 0.6)Multiple keys and table access¶
For a joint distribution over several keys, take all of them as frontals. toTableFactor() replaces table(), and toTableFactor().argmax() replaces argmax() for the most likely assignment to all keys. As before, choose(given) fixes parent values and marginal(key) removes other frontals by summation.
umbrella = (gtsam.symbol("U", 0), 2)
keys = gtsam.DiscreteKeys()
keys.push_back(weather)
keys.push_back(umbrella)
# Previously: gtsam.TableDistribution(keys, "6 1 3 2 1 7")
joint = gtsam.DiscreteConditional(2, gtsam.TableFactor(keys, "6 1 3 2 1 7"))
print("joint assignments:", joint.nrValues())
print("most likely assignment:", joint.toTableFactor().argmax())Source¶
DiscreteConditional.h, TableFactor.h, and the deprecated TableDistribution.h.
AI assistance caveat¶
AI was used to help draft this documentation, and inaccuracies could be present.