Go to the documentation of this file.
38 using std::stringstream;
43 template class GTSAM_EXPORT
47 DiscreteConditional::DiscreteConditional(
const size_t nrFrontals,
49 :
BaseFactor((
f /
f.sum(nrFrontals))->toDecisionTreeFactor()),
55 const ADT& potentials)
70 keys_.insert(
keys_.end(), orderedKeys.begin(), orderedKeys.end());
75 :
BaseFactor(signature.discreteKeys(), signature.cpt()),
89 std::set<Key> newFrontals;
91 for (
auto&&
key :
other.frontals()) newFrontals.insert(
key);
95 throw std::invalid_argument(
96 "DiscreteConditional::operator* called with overlapping frontal keys.");
125 throw std::invalid_argument(
126 "DiscreteConditional::marginal: single argument version only valid for "
127 "fully specified joint distributions (i.e., no parents).");
181 }
catch (std::out_of_range&) {
183 given.
print(
"parentsValues: ");
185 "DiscreteConditional::choose: parent value missing");
204 if (given.count(
j) == 0) {
208 return std::make_shared<DiscreteConditional>(
nrFrontals(), dKeys, adt);
220 value = frontalValues.at(
j);
222 }
catch (exception&) {
223 frontalValues.
print(
"frontalValues: ");
224 throw runtime_error(
"DiscreteConditional::choose: frontal value missing");
233 return std::make_shared<DecisionTreeFactor>(
discreteKeys, adt);
238 size_t frontal)
const {
240 throw std::invalid_argument(
241 "Single value likelihood can only be invoked on single-variable "
261 double pValueS = (*this)(
values);
263 if (pValueS > maxP) {
273 std::mt19937_64*
rng)
const {
276 throw std::invalid_argument(
277 "DiscreteConditional::sampleInPlace can only be called on single "
278 "variable conditionals");
283 throw std::invalid_argument(
284 "DiscreteConditional::sampleInPlace: values already contains j");
287 (*values)[
j] = sampled;
292 std::mt19937_64*
rng)
const {
298 throw std::invalid_argument(
299 "DiscreteConditional::sample can only be called on single variable "
304 vector<double>
p(nj);
317 std::discrete_distribution<size_t> distribution(
p.begin(),
p.end());
318 return distribution(*
rng);
323 std::mt19937_64*
rng)
const {
325 throw std::invalid_argument(
326 "Single value sample() can only be invoked on single-parent "
336 throw std::invalid_argument(
337 "sample() can only be invoked on no-parent prior");
344 vector<pair<Key, size_t>> pairs;
346 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
352 vector<pair<Key, size_t>> pairs;
355 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
367 if (!first) *
ss <<
",";
368 *
ss << keyFormatter(
key);
375 if (!first) *
ss <<
",";
376 *
ss << keyFormatter(parent);
389 ss <<
"*\n" << std::endl;
399 ss <<
"*" << keyFormatter(parent) <<
"*|";
405 size_t index =
a.at(*it);
424 size_t index =
a.at(*it);
429 count = (count + 1) %
n;
430 if (count == 0)
ss <<
"\n";
439 ss <<
"<div>\n<p> <i>";
449 ss <<
"<table class='DiscreteConditional'>\n <thead>\n";
454 ss <<
"<th><i>" << keyFormatter(parent) <<
"</i></th>";
460 size_t index =
a.at(*it);
468 ss <<
" </thead>\n <tbody>\n";
476 size_t index =
a.at(*it);
481 count = (count + 1) %
n;
482 if (count == 0)
ss <<
"</tr>\n";
486 ss <<
" </tbody>\n</table>\n</div>";
519 if (given.count(
key) == 0) {
520 frontals.emplace_back(key, this->cardinalities_.at(key));
527 if (given.count(
key) == 0) {
528 parents.emplace_back(key, this->cardinalities_.at(key));
536 this->
keys_ = allDkeys.indices();
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print required by Testable.
Concept check for values that can be used in unit tests.
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
DiscreteConditional operator*(const DiscreteConditional &other) const
Combine two conditionals, yielding a new conditional with the union of the frontal keys,...
DecisionTreeFactor prune(size_t maxNrAssignments) const
Prune the decision tree of discrete variables.
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
GTSAM-style equals.
const KeyFormatter & formatter
DiscreteKeys is a set of keys that can be assembled using the & operator.
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
DecisionTree choose(const L &label, size_t index) const
const_iterator begin() const
std::shared_ptr< DecisionTreeFactor > shared_ptr
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
static double mul(const double &a, const double &b)
double operator()(const HybridValues &x) const
Evaluate probability density, sugar.
static std::stringstream ss
DecisionTreeFactor ::const_iterator endParents() const
DiscreteConditional marginal(Key key) const
std::vector< DiscreteValues > allAssignments() const
Return all assignments for frontal and parent variables.
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
void print(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
DecisionTreeFactor ::const_iterator endFrontals() const
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
signatures for conditional densities
std::vector< DiscreteValues > frontalAssignments() const
Return all assignments for frontal variables.
static void streamSignature(const DiscreteConditional &conditional, const KeyFormatter &keyFormatter, stringstream *ss)
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
equality
void sampleInPlace(DiscreteValues *parentsValues, std::mt19937_64 *rng=nullptr) const
Sample in place with optional PRNG, stores result in partial solution.
double negLogConstant() const override
double evaluate(const HybridValues &x) const override
DecisionTreeFactor ::const_iterator beginFrontals() const
size_t argmax(const DiscreteValues &parentsValues=DiscreteValues()) const
Return assignment for single frontal variable that maximizes value.
DecisionTree apply(const Unary &op) const
const_iterator end() const
DecisionTreeFactor::shared_ptr likelihood(const DiscreteValues &frontalValues) const
size_t nrFrontals() const
double sum() const
Compute sum of all values.
const gtsam::Symbol key('X', 0)
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
DecisionTreeFactor ::const_iterator beginParents() const
size_t cardinality(Key j) const
Key firstFrontalKey() const
KeyVector::const_iterator const_iterator
Const iterator over keys.
double max() const override
Find the maximum value in the factor.
KeyVector keys_
The keys involved in this factor.
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
static std::mt19937_64 kRandomNumberGenerator(42)
Global default pseudo-random number generator object. In wrappers we can access std::mt19937_64 via g...
DiscreteValues::Names Names
Translation table from values to strings.
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
const KeyVector & keys() const
Access the factor's involved variable keys.
const DiscreteValues & discrete() const
Return the discrete values.
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
void product(const MatrixType &m)
DiscreteConditional()
Default constructor needed for serialization.
shared_ptr choose(const DiscreteValues &given) const
< DiscreteValues version
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
virtual size_t sample(const DiscreteValues &parentsValues, std::mt19937_64 *rng=nullptr) const
std::uint64_t Key
Integer nonlinear key type.
void removeDiscreteModes(const DiscreteValues &given)
Remove the discrete modes whose assignments are given to us. Only applies to discrete conditionals.
Frontals frontals() const
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
static constexpr double k
virtual void prune(size_t maxNrAssignments)
Prune the conditional.
gtsam
Author(s):
autogenerated on Wed May 28 2025 03:01:13