35 using std::stringstream;
40 template class GTSAM_EXPORT
44 DiscreteConditional::DiscreteConditional(
const size_t nrFrontals,
51 const ADT& potentials)
66 keys_.insert(
keys_.end(), orderedKeys.begin(), orderedKeys.end());
78 std::set<Key> newFrontals;
84 throw std::invalid_argument(
85 "DiscreteConditional::operator* called with overlapping frontal keys.");
95 std::sort(discreteKeys.begin(), discreteKeys.end());
105 for (
auto&& dk : parents) discreteKeys.push_back(dk);
114 throw std::invalid_argument(
115 "DiscreteConditional::marginal: single argument version only valid for " 116 "fully specified joint distributions (i.e., no parents).");
151 if (!dynamic_cast<const DecisionTreeFactor*>(&other)) {
170 }
catch (std::out_of_range&) {
172 given.print(
"parentsValues: ");
174 "DiscreteConditional::choose: parent value missing");
193 if (given.count(j) == 0) {
197 return std::make_shared<DiscreteConditional>(
nrFrontals(), dKeys, adt);
209 value = frontalValues.at(
j);
211 }
catch (exception&) {
212 frontalValues.print(
"frontalValues: ");
213 throw runtime_error(
"DiscreteConditional::choose: frontal value missing");
222 return std::make_shared<DecisionTreeFactor>(
discreteKeys, adt);
227 size_t frontal)
const {
229 throw std::invalid_argument(
230 "Single value likelihood can only be invoked on single-variable " 233 values.emplace(
keys_[0], frontal);
249 if (pValueS > maxP) {
261 size_t sampled =
sample(*values);
262 (*values)[
j] = sampled;
267 static mt19937
rng(2);
274 throw std::invalid_argument(
275 "DiscreteConditional::sample can only be called on single variable " 280 vector<double>
p(nj);
284 p[
value] = pFS(frontals);
285 if (p[
value] == 1.0) {
289 std::discrete_distribution<size_t> distribution(p.begin(), p.end());
290 return distribution(rng);
296 throw std::invalid_argument(
297 "Single value sample() can only be invoked on single-parent " 300 values.emplace(
keys_.back(), parent_value);
307 throw std::invalid_argument(
308 "sample() can only be invoked on no-parent prior");
315 vector<pair<Key, size_t>> pairs;
317 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
323 vector<pair<Key, size_t>> pairs;
326 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
338 if (!first) *ss <<
",";
339 *ss << keyFormatter(
key);
346 if (!first) *ss <<
",";
347 *ss << keyFormatter(parent);
360 ss <<
"*\n" << std::endl;
370 ss <<
"*" << keyFormatter(parent) <<
"*|";
376 size_t index =
a.at(*it);
385 size_t n = frontalAssignments.size();
395 size_t index =
a.at(*it);
400 count = (count + 1) % n;
401 if (count == 0) ss <<
"\n";
410 ss <<
"<div>\n<p> <i>";
420 ss <<
"<table class='DiscreteConditional'>\n <thead>\n";
425 ss <<
"<th><i>" << keyFormatter(parent) <<
"</i></th>";
431 size_t index =
a.at(*it);
439 ss <<
" </thead>\n <tbody>\n";
442 size_t count = 0,
n = frontalAssignments.size();
447 size_t index =
a.at(*it);
452 count = (count + 1) %
n;
453 if (count == 0) ss <<
"</tr>\n";
457 ss <<
" </tbody>\n</table>\n</div>";
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
const gtsam::Symbol key('X', 0)
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
void sampleInPlace(DiscreteValues *parentsValues) const
sample in place, stores result in partial solution
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
Concept check for values that can be used in unit tests.
signatures for conditional densities
DiscreteConditional()
Default constructor needed for serialization.
double evaluate(const DiscreteValues &values) const
Evaluate, just look up in AlgebraicDecisonTree.
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
double mul(const double &a, const double &b)
shared_ptr choose(const DiscreteValues &given) const
< DiscreteValues version
DecisionTreeFactor ::const_iterator endFrontals() const
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
DecisionTreeFactor ::const_iterator beginParents() const
KeyVector keys_
The keys involved in this factor.
size_t nrFrontals() const
Frontals frontals() const
DecisionTreeFactor::shared_ptr likelihood(const DiscreteValues &frontalValues) const
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
const KeyFormatter & formatter
DecisionTreeFactor ::const_iterator beginFrontals() const
DiscreteConditional operator*(const DiscreteConditional &other) const
Combine two conditionals, yielding a new conditional with the union of the frontal keys...
size_t cardinality(Key j) const
double operator()(const DiscreteValues &values) const override
Evaluate probability distribution, sugar.
DecisionTreeFactor ::const_iterator endParents() const
static void streamSignature(const DiscreteConditional &conditional, const KeyFormatter &keyFormatter, stringstream *ss)
std::map< Key, size_t > cardinalities_
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
DecisionTree apply(const Unary &op) const
size_t sample() const
Zero parent version.
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
static std::stringstream ss
DiscreteValues::Names Names
Translation table from values to strings.
std::shared_ptr< DecisionTreeFactor > shared_ptr
size_t argmax() const
Return assignment that maximizes distribution.
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
GTSAM-style equals.
const DiscreteValues & discrete() const
Return the discrete values.
const KeyVector & keys() const
Access the factor's involved variable keys.
std::vector< DiscreteValues > allAssignments() const
Return all assignments for frontal and parent variables.
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
equality
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Key firstFrontalKey() const
KeyVector::const_iterator const_iterator
Const iterator over keys.
DiscreteConditional marginal(Key key) const
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
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::uint64_t Key
Integer nonlinear key type.
void print(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
std::vector< DiscreteValues > frontalAssignments() const
Return all assignments for frontal variables.
void product(const MatrixType &m)
DiscreteKeys is a set of keys that can be assembled using the & operator.