Go to the documentation of this file.
37 using std::stringstream;
42 template class GTSAM_EXPORT
46 DiscreteConditional::DiscreteConditional(
const size_t nrFrontals,
48 :
BaseFactor((
f /
f.sum(nrFrontals))->toDecisionTreeFactor()),
54 const ADT& potentials)
69 keys_.insert(
keys_.end(), orderedKeys.begin(), orderedKeys.end());
74 :
BaseFactor(signature.discreteKeys(), signature.cpt()),
81 std::set<Key> newFrontals;
83 for (
auto&&
key :
other.frontals()) newFrontals.insert(
key);
87 throw std::invalid_argument(
88 "DiscreteConditional::operator* called with overlapping frontal keys.");
117 throw std::invalid_argument(
118 "DiscreteConditional::marginal: single argument version only valid for "
119 "fully specified joint distributions (i.e., no parents).");
173 }
catch (std::out_of_range&) {
175 given.
print(
"parentsValues: ");
177 "DiscreteConditional::choose: parent value missing");
196 if (given.count(
j) == 0) {
200 return std::make_shared<DiscreteConditional>(
nrFrontals(), dKeys, adt);
212 value = frontalValues.at(
j);
214 }
catch (exception&) {
215 frontalValues.
print(
"frontalValues: ");
216 throw runtime_error(
"DiscreteConditional::choose: frontal value missing");
225 return std::make_shared<DecisionTreeFactor>(
discreteKeys, adt);
230 size_t frontal)
const {
232 throw std::invalid_argument(
233 "Single value likelihood can only be invoked on single-variable "
253 double pValueS = (*this)(
values);
255 if (pValueS > maxP) {
267 throw std::invalid_argument(
268 "DiscreteConditional::sampleInPlace can only be called on single "
269 "variable conditionals");
274 throw std::invalid_argument(
275 "DiscreteConditional::sampleInPlace: values already contains j");
278 (*values)[
j] = sampled;
283 static mt19937
rng(2);
290 throw std::invalid_argument(
291 "DiscreteConditional::sample can only be called on single variable "
296 vector<double>
p(nj);
305 std::discrete_distribution<size_t> distribution(
p.begin(),
p.end());
306 return distribution(
rng);
312 throw std::invalid_argument(
313 "Single value sample() can only be invoked on single-parent "
323 throw std::invalid_argument(
324 "sample() can only be invoked on no-parent prior");
331 vector<pair<Key, size_t>> pairs;
333 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
339 vector<pair<Key, size_t>> pairs;
342 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
354 if (!first) *
ss <<
",";
355 *
ss << keyFormatter(
key);
362 if (!first) *
ss <<
",";
363 *
ss << keyFormatter(parent);
376 ss <<
"*\n" << std::endl;
386 ss <<
"*" << keyFormatter(parent) <<
"*|";
392 size_t index =
a.at(*it);
411 size_t index =
a.at(*it);
416 count = (count + 1) %
n;
417 if (count == 0)
ss <<
"\n";
426 ss <<
"<div>\n<p> <i>";
436 ss <<
"<table class='DiscreteConditional'>\n <thead>\n";
441 ss <<
"<th><i>" << keyFormatter(parent) <<
"</i></th>";
447 size_t index =
a.at(*it);
455 ss <<
" </thead>\n <tbody>\n";
463 size_t index =
a.at(*it);
468 count = (count + 1) %
n;
469 if (count == 0)
ss <<
"</tr>\n";
473 ss <<
" </tbody>\n</table>\n</div>";
size_t sample() const
Zero parent version.
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,...
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
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.
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
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
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.
void sampleInPlace(DiscreteValues *parentsValues) const
sample in place, stores result in partial solution
KeyVector keys_
The keys involved in this factor.
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 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.
std::uint64_t Key
Integer nonlinear key type.
Frontals frontals() const
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
gtsam
Author(s):
autogenerated on Tue Jan 7 2025 04:02:11