GTSAM
4.0.2
C++ library for smoothing and mapping (SAM)
|
#include <DiscreteConditional.h>
Public Types | |
typedef DiscreteConditional | This |
Typedef to this class. | |
typedef std::shared_ptr< This > | shared_ptr |
shared_ptr to this class | |
typedef DecisionTreeFactor | BaseFactor |
Typedef to our factor base class. | |
typedef Conditional< BaseFactor, This > | BaseConditional |
Typedef to our conditional base class. | |
using | Values = DiscreteValues |
backwards compatibility | |
typedef DiscreteFactor | Base |
Typedef to base class. | |
typedef AlgebraicDecisionTree< Key > | ADT |
typedef KeyVector::iterator | iterator |
Iterator over keys. | |
typedef KeyVector::const_iterator | const_iterator |
Const iterator over keys. | |
using | LabelFormatter = std::function< std::string(Key)> |
using | ValueFormatter = std::function< std::string(double)> |
using | CompareFunc = std::function< bool(const double &, const double &)> |
using | Unary = std::function< double(const double &)> |
using | UnaryAssignment = std::function< double(const Assignment< Key > &, const double &)> |
using | Binary = std::function< double(const double &, const double &)> |
using | LabelC = std::pair< Key, size_t > |
using | NodePtr = typename Node::Ptr |
typedef std::pair< typename DecisionTreeFactor ::const_iterator, typename DecisionTreeFactor ::const_iterator > | ConstFactorRange |
typedef ConstFactorRangeIterator | Frontals |
typedef ConstFactorRangeIterator | Parents |
Wrapper support | |
using | Names = DiscreteValues::Names |
Translation table from values to strings. | |
Public Member Functions | |
bool | equals (const AlgebraicDecisionTree &other, double tol=1e-9) const |
Equality method customized to value type double . | |
void | print (const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const |
print method customized to value type double . | |
AlgebraicDecisionTree | operator* (const AlgebraicDecisionTree &g) const |
AlgebraicDecisionTree | operator/ (const AlgebraicDecisionTree &g) const |
AlgebraicDecisionTree | sum (const Key &label, size_t cardinality) const |
AlgebraicDecisionTree | sum (const typename Base::LabelC &labelC) const |
AlgebraicDecisionTree | operator+ (const AlgebraicDecisionTree &g) const |
Standard Constructors | |
DiscreteConditional () | |
Default constructor needed for serialization. | |
DiscreteConditional (size_t nFrontals, const DecisionTreeFactor &f) | |
Construct from factor, taking the first nFrontals keys as frontals. | |
DiscreteConditional (size_t nFrontals, const DiscreteKeys &keys, const ADT &potentials) | |
DiscreteConditional (const Signature &signature) | |
DiscreteConditional (const DiscreteKey &key, const DiscreteKeys &parents, const Signature::Table &table) | |
DiscreteConditional (const DiscreteKey &key, const DiscreteKeys &parents, const std::string &spec) | |
DiscreteConditional (const DiscreteKey &key, const std::string &spec) | |
No-parent specialization; can also use DiscreteDistribution. | |
DiscreteConditional (const DecisionTreeFactor &joint, const DecisionTreeFactor &marginal) | |
construct P(X|Y) = f(X,Y)/f(Y) from f(X,Y) and f(Y) Assumes but does not check that f(Y)=sum_X f(X,Y). | |
DiscreteConditional (const DecisionTreeFactor &joint, const DecisionTreeFactor &marginal, const Ordering &orderedKeys) | |
construct P(X|Y) = f(X,Y)/f(Y) from f(X,Y) and f(Y) Assumes but does not check that f(Y)=sum_X f(X,Y). Makes sure the keys are ordered as given. Does not check orderedKeys. | |
DiscreteConditional | operator* (const DiscreteConditional &other) const |
Combine two conditionals, yielding a new conditional with the union of the frontal keys, ordered by gtsam::Key. More... | |
DiscreteConditional | marginal (Key key) const |
Testable | |
void | print (const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override |
GTSAM-style print. | |
bool | equals (const DiscreteFactor &other, double tol=1e-9) const override |
GTSAM-style equals. | |
Standard Interface | |
double | logProbability (const DiscreteValues &x) const |
Log-probability is just -error(x). | |
void | printSignature (const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const |
print index signature only | |
double | evaluate (const DiscreteValues &values) const |
Evaluate, just look up in AlgebraicDecisonTree. | |
shared_ptr | choose (const DiscreteValues &given) const |
< DiscreteValues version More... | |
DecisionTreeFactor::shared_ptr | likelihood (const DiscreteValues &frontalValues) const |
DecisionTreeFactor::shared_ptr | likelihood (size_t frontal) const |
size_t | sample (const DiscreteValues &parentsValues) const |
size_t | sample (size_t parent_value) const |
Single parent version. | |
size_t | sample () const |
Zero parent version. | |
size_t | argmax () const |
Return assignment that maximizes distribution. More... | |
Advanced Interface | |
void | sampleInPlace (DiscreteValues *parentsValues) const |
sample in place, stores result in partial solution | |
std::vector< DiscreteValues > | frontalAssignments () const |
Return all assignments for frontal variables. | |
std::vector< DiscreteValues > | allAssignments () const |
Return all assignments for frontal and parent variables. | |
Wrapper support | |
std::string | markdown (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override |
Render as markdown table. | |
std::string | html (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override |
Render as html table. | |
HybridValues methods. | |
double | evaluate (const HybridValues &x) const override |
double | logProbability (const HybridValues &x) const override |
< HybridValues version More... | |
double | logNormalizationConstant () const override |
Testable | |
bool | equals (const This &other, double tol=1e-9) const |
check equality | |
virtual void | printKeys (const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const |
print only keys | |
Testable | |
bool | equals (const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const |
void | print (const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const |
GTSAM-style print. More... | |
Standard Interface | |
const double & | operator() (const Assignment< Key > &x) const |
DecisionTree | apply (const Unary &op) const |
DecisionTree | apply (const UnaryAssignment &op) const |
Apply Unary operation "op" to f while also providing the corresponding assignment. More... | |
DecisionTree | apply (const DecisionTree &g, const Binary &op) const |
DecisionTree | combine (const Key &label, size_t cardinality, const Binary &op) const |
DecisionTree | combine (const LabelC &labelC, const Binary &op) const |
void | dot (std::ostream &os, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const |
void | dot (const std::string &name, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const |
std::string | dot (const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const |
bool | empty () const |
Check if tree is empty. | |
bool | operator== (const DecisionTree &q) const |
void | visit (Func f) const |
Visit all leaves in depth-first fashion. More... | |
void | visitLeaf (Func f) const |
Visit all leaves in depth-first fashion. More... | |
void | visitWith (Func f) const |
Visit all leaves in depth-first fashion. More... | |
size_t | nrLeaves () const |
Return the number of leaves in the tree. | |
X | fold (Func f, X x0) const |
Fold a binary function over the tree, returning accumulator. More... | |
std::set< Key > | labels () const |
DecisionTree | choose (const Key &label, size_t index) const |
HybridValues methods. | |
double | error (const HybridValues &values) const override |
Advanced Interface | |
DecisionTreeFactor | apply (const DecisionTreeFactor &f, ADT::Binary op) const |
shared_ptr | combine (size_t nrFrontals, ADT::Binary op) const |
shared_ptr | combine (const Ordering &keys, ADT::Binary op) const |
std::vector< std::pair< DiscreteValues, double > > | enumerate () const |
Enumerate all values into a map from values to double. | |
DiscreteKeys | discreteKeys () const |
Return all the discrete keys associated with this factor. | |
DecisionTreeFactor | prune (size_t maxNrAssignments) const |
Prune the decision tree of discrete variables. More... | |
Wrapper support | |
void | dot (std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const |
void | dot (const std::string &name, const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const |
std::string | dot (const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const |
Standard Interface | |
bool | empty () const |
Whether the factor is empty (involves zero variables). | |
Key | front () const |
First key. | |
Key | back () const |
Last key. | |
const_iterator | find (Key key) const |
find | |
const KeyVector & | keys () const |
Access the factor's involved variable keys. | |
const_iterator | begin () const |
const_iterator | end () const |
size_t | size () const |
Advanced Interface | |
KeyVector & | keys () |
iterator | begin () |
iterator | end () |
Advanced Interface | |
NodePtr | compose (Iterator begin, Iterator end, const Key &label) const |
Testable | |
bool | equals (const This &c, double tol=1e-9) const |
Standard Interface | |
size_t | nrFrontals () const |
size_t | nrParents () const |
Key | firstFrontalKey () const |
Frontals | frontals () const |
Parents | parents () const |
double | operator() (const HybridValues &x) const |
Evaluate probability density, sugar. | |
double | normalizationConstant () const |
Public Attributes | |
NodePtr | root_ |
A DecisionTree just contains the root. TODO(dellaert): make protected. | |
Protected Member Functions | |
DiscreteConditional::ADT | choose (const DiscreteValues &given, bool forceComplete) const |
Internal version of choose. | |
NodePtr | create (It begin, It end, ValueIt beginY, ValueIt endY) const |
NodePtr | convertFrom (const typename DecisionTree< M, X >::NodePtr &f, std::function< Key(const M &)> L_of_M, std::function< double(const X &)> Y_of_X) const |
Convert from a DecisionTree<M, X> to DecisionTree<L, Y>. More... | |
Static Protected Member Functions | |
static bool | DefaultCompare (const double &a, const double &b) |
Default method for comparison of two objects of type Y. | |
Standard Constructors | |
template<typename CONTAINER > | |
static Factor | FromKeys (const CONTAINER &keys) |
template<typename ITERATOR > | |
static Factor | FromIterators (ITERATOR first, ITERATOR last) |
Protected Attributes | |
std::map< Key, size_t > | cardinalities_ |
KeyVector | keys_ |
The keys involved in this factor. | |
size_t | nrFrontals_ |
Standard Interface | |
double | operator() (const DiscreteValues &values) const override |
Evaluate probability distribution, sugar. | |
double | error (const DiscreteValues &values) const |
Calculate error for DiscreteValues x , is -log(probability). | |
DecisionTreeFactor | operator* (const DecisionTreeFactor &f) const override |
multiply two factors | |
size_t | cardinality (Key j) const |
DecisionTreeFactor | operator/ (const DecisionTreeFactor &f) const |
divide by factor f (safely) | |
DecisionTreeFactor | toDecisionTreeFactor () const override |
Convert into a decisiontree. | |
shared_ptr | sum (size_t nrFrontals) const |
Create new factor by summing all values with the same separator values. | |
shared_ptr | sum (const Ordering &keys) const |
Create new factor by summing all values with the same separator values. | |
shared_ptr | max (size_t nrFrontals) const |
Create new factor by maximizing over all values with the same separator. | |
shared_ptr | max (const Ordering &keys) const |
Create new factor by maximizing over all values with the same separator. | |
static double | safe_div (const double &a, const double &b) |
Advanced Interface | |
size_t & | nrFrontals () |
DecisionTreeFactor ::const_iterator | beginFrontals () const |
DecisionTreeFactor ::iterator | beginFrontals () |
DecisionTreeFactor ::const_iterator | endFrontals () const |
DecisionTreeFactor ::iterator | endFrontals () |
DecisionTreeFactor ::const_iterator | beginParents () const |
DecisionTreeFactor ::iterator | beginParents () |
DecisionTreeFactor ::const_iterator | endParents () const |
DecisionTreeFactor ::iterator | endParents () |
static bool | CheckInvariants (const DiscreteConditional &conditional, const VALUES &x) |
Discrete Conditional Density Derives from DecisionTreeFactor
represent a discrete conditional distribution over discrete variables.
|
inherited |
A mini implementation of an iterator range, to share const views of frontals and parents.
|
inherited |
View of the frontal keys (call frontals())
|
inherited |
A label annotated with cardinality
|
inherited |
------------------—— Node base class ---------------------—— A function is a shared pointer to the root of a DT
|
inherited |
View of the separator keys (call parents())
|
inherited |
Handy typedefs for unary and binary function types
gtsam::DiscreteConditional::DiscreteConditional | ( | size_t | nFrontals, |
const DiscreteKeys & | keys, | ||
const ADT & | potentials | ||
) |
Construct from DiscreteKeys and AlgebraicDecisionTree, taking the first nFrontals
keys as frontals, in the order given.
|
explicit |
Construct from signature
|
inline |
Construct from key, parents, and a Signature::Table specifying the conditional probability table (CPT) in 00 01 10 11 order. For three-valued, it would be 00 01 02 10 11 12 20 21 22, etc....
Example: DiscreteConditional P(D, {B,E}, table);
|
inline |
Construct from key, parents, and a string specifying the conditional probability table (CPT) in 00 01 10 11 order. For three-valued, it would be 00 01 02 10 11 12 20 21 22, etc....
The string is parsed into a Signature::Table.
Example: DiscreteConditional P(D, {B,E}, "9/1 2/8 3/7 1/9");
|
inherited |
Apply binary operator (*this) "op" f
f | the second argument for op |
op | a binary operator that operates on AlgebraicDecisionTree |
|
inherited |
apply Unary operation "op" to f
|
inherited |
Apply Unary operation "op" to f while also providing the corresponding assignment.
Apply unary operator with assignment.
op | Function which takes Assignment<L> and Y as input and returns object of type Y. |
|
inherited |
apply binary operation "op" to f and g
size_t gtsam::DiscreteConditional::argmax | ( | ) | const |
Return assignment that maximizes distribution.
|
inlineinherited |
Iterator at beginning of involved variable keys
|
inlineinherited |
Iterator at beginning of involved variable keys
|
inlineinherited |
Iterator pointing to first frontal key.
|
inlineinherited |
Mutable iterator pointing to first frontal key.
|
inlineinherited |
Iterator pointing to the first parent key.
|
inlineinherited |
Mutable iterator pointing to the first parent key.
|
staticinherited |
Check invariants of this conditional, given the values x
. It tests:
conditional | The conditional to test, as a reference to the derived type. |
VALUES | HybridValues, or a more narrow type like DiscreteValues. |
shared_ptr gtsam::DiscreteConditional::choose | ( | const DiscreteValues & | given | ) | const |
< DiscreteValues version
restrict to given parent values.
Note: does not need be complete set. Examples:
P(C|D,E) + . -> P(C|D,E) P(C|D,E) + E -> P(C|D) P(C|D,E) + D -> P(C|E) P(C|D,E) + D,E -> P(C) P(C|D,E) + C -> error!
|
inlineinherited |
create a new function where value(label)==index It's like "restrict" in Darwiche09book pg329, 330?
|
inherited |
Combine frontal variables using binary operator "op"
nrFrontals | nr. of frontal to combine variables in this factor |
op | a binary operator that operates on AlgebraicDecisionTree |
|
inherited |
Combine frontal variables in an Ordering using binary operator "op"
nrFrontals | nr. of frontal to combine variables in this factor |
op | a binary operator that operates on AlgebraicDecisionTree |
|
inherited |
combine subtrees on key with binary operation "op"
|
inlineinherited |
combine with LabelC for convenience
|
protectedinherited |
Convert from a DecisionTree<M, X> to DecisionTree<L, Y>.
M | The previous label type. |
X | The previous value type. |
f | The node pointer to the root of the previous DecisionTree. |
L_of_M | Functor to convert from label type M to type L. |
Y_of_X | Functor to convert from value type X to type Y. |
|
protectedinherited |
Internal recursive function to create from keys, cardinalities, and Y values
|
inherited |
output to graphviz format, stream version
|
inherited |
output to graphviz format, open a file
|
inherited |
output to graphviz format string
|
inherited |
output to graphviz format, stream version
|
inherited |
output to graphviz format, open a file
|
inherited |
output to graphviz format string
|
inlineinherited |
Iterator at end of involved variable keys
|
inlineinherited |
Iterator at end of involved variable keys
|
inlineinherited |
Iterator pointing past the last frontal key.
|
inlineinherited |
Mutable iterator pointing past the last frontal key.
|
inlineinherited |
Iterator pointing past the last parent key.
|
inlineinherited |
Mutable iterator pointing past the last parent key.
|
inherited |
check equality
|
overridevirtualinherited |
Calculate error for HybridValues x
, is -log(probability) Simply dispatches to DiscreteValues version.
Reimplemented from gtsam::Factor.
|
overridevirtual |
Calculate probability for HybridValues x
. Dispatches to DiscreteValues version.
Reimplemented from gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >.
|
inlineinherited |
Convenience function to get the first frontal key
|
inherited |
Fold a binary function over the tree, returning accumulator.
X | type for accumulator. |
f | binary function: Y * X -> X returning an updated accumulator. |
x0 | initial value for accumulator. |
Example: auto add = [](const double& y, double x) { return y + x; }; double sum = tree.fold(add, 0.0);
|
inlinestaticprotectedinherited |
Construct factor from iterator keys. This is called internally from derived factor static factor methods, as a workaround for not being able to call the protected constructors above.
|
inlinestaticprotectedinherited |
Construct factor from container of keys. This is called internally from derived factor static factor methods, as a workaround for not being able to call the protected constructors above.
|
inlineinherited |
return a view of the frontal keys
|
inlineinherited |
|
inherited |
Retrieve all unique labels as a set.
Get (partial) labels by performing a visit.
This method performs a depth-first search to go to every leaf and records the keys assignment which leads to that leaf. Since the tree can be pruned, there might be a leaf at a lower depth which results in a partial assignment (i.e. not all keys are specified).
E.g. given a tree with 3 keys, there may be a branch where the 3rd key has the same values for all the leaves. This leads to the branch being pruned so we get a leaf which is arrived at by just the first 2 keys and their assignments.
DecisionTreeFactor::shared_ptr gtsam::DiscreteConditional::likelihood | ( | const DiscreteValues & | frontalValues | ) | const |
Convert to a likelihood factor by providing value before bar.
DecisionTreeFactor::shared_ptr gtsam::DiscreteConditional::likelihood | ( | size_t | frontal | ) | const |
Single variable version of likelihood.
|
inlineoverridevirtual |
logNormalizationConstant K is just zero, such that logProbability(x) = log(evaluate(x)) = - error(x) and hence error(x) = - log(evaluate(x)) > 0 for all x.
Reimplemented from gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >.
|
inlineoverridevirtual |
< HybridValues version
Calculate log-probability log(evaluate(x)) for HybridValues x
. This is actually just -error(x).
Reimplemented from gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >.
DiscreteConditional gtsam::DiscreteConditional::marginal | ( | Key | key | ) | const |
Calculate marginal on given key, no parent case.
|
inherited |
Non-virtual, exponentiate logNormalizationConstant.
|
inlineinherited |
return the number of frontals
|
inlineinherited |
Mutable version of nrFrontals
|
inlineinherited |
return the number of parents
|
inherited |
evaluate
DiscreteConditional gtsam::DiscreteConditional::operator* | ( | const DiscreteConditional & | other | ) | const |
Combine two conditionals, yielding a new conditional with the union of the frontal keys, ordered by gtsam::Key.
The two conditionals must make a valid Bayes net fragment, i.e., the frontal variables cannot overlap, and must be acyclic: Example of correct use: P(A,B) = P(A|B) * P(B) P(A,B|C) = P(A|B) * P(B|C) P(A,B,C) = P(A,B|C) * P(C) Example of incorrect use: P(A|B) * P(A|C) = ? P(A|B) * P(B|A) = ? We check for overlapping frontals, but do not check for cyclic.
|
inlineinherited |
product
|
inlineinherited |
sum
|
inlineinherited |
division
|
inherited |
equality
|
inlineinherited |
return a view of the parent keys
|
inherited |
GTSAM-style print.
s | Prefix string. |
labelFormatter | Functor to format the node label. |
valueFormatter | Functor to format the node value. |
|
inherited |
Prune the decision tree of discrete variables.
Pruning will set the leaves to be "pruned" to 0 indicating a 0 probability. An assignment is pruned if it is not in the top maxNrAssignments
values.
A violation can occur if there are more duplicate values than maxNrAssignments
. A violation here is the need to un-prune the decision tree (e.g. all assignment values are 1.0). We could have another case where some subset of duplicates exist (e.g. for a tree with 8 assignments we have 1, 1, 1, 1, 0.8, 0.7, 0.6, 0.5), but this is not a violation since the for maxNrAssignments=5
the top values are (1, 0.8).
maxNrAssignments | The maximum number of assignments to keep. |
size_t gtsam::DiscreteConditional::sample | ( | const DiscreteValues & | parentsValues | ) | const |
sample
parentsValues | Known values of the parents |
|
inlineinherited |
|
inlineinherited |
sum out variable
|
inlineinherited |
sum out variable
|
inherited |
Visit all leaves in depth-first fashion.
f | (side-effect) Function taking the value of the leaf node. |
Example: int sum = 0; auto visitor = [&](int y) { sum += y; }; tree.visit(visitor);
|
inherited |
Visit all leaves in depth-first fashion.
f | (side-effect) Function taking the leaf node pointer. |
Example: int sum = 0; auto visitor = [&](const Leaf& leaf) { sum += leaf.constant(); }; tree.visitLeaf(visitor);
|
inherited |
Visit all leaves in depth-first fashion.
f | (side-effect) Function taking an assignment and a value. |
Example: int sum = 0; auto visitor = [&](const Assignment<L>& assignment, int y) { sum += y; }; tree.visitWith(visitor);
|
protectedinherited |
The first nrFrontal variables are frontal and the rest are parents.