gtsam
Loading...
Searching...
No Matches
gtsam::HybridBayesNet Class Reference

Detailed Description

A hybrid Bayes net is a collection of HybridConditionals, which can have discrete conditionals, hybrid Gaussian conditionals, or pure Gaussian conditionals.

Inheritance diagram for gtsam::HybridBayesNet:

Public Member Functions

Standard Constructors
 HybridBayesNet ()=default
 Construct empty Bayes net.
 HybridBayesNet (std::initializer_list< HybridConditional::shared_ptr > conditionals)
 Constructor that takes an initializer list of shared pointers.
Testable
void print (const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
 GTSAM-style printing.
bool equals (const This &fg, double tol=1e-9) const
 GTSAM-style equals.
Standard Interface
void push_back (std::shared_ptr< HybridConditional > conditional)
 Add a hybrid conditional using a shared_ptr.
void push_back (HybridConditional &&conditional)
 Move a HybridConditional into a shared pointer and add.
template<class CONDITIONAL>
void push_back (const std::shared_ptr< CONDITIONAL > &conditional)
 Add a conditional to the Bayes net.
template<class CONDITIONAL, class... Args>
void emplace_shared (Args &&...args)
 Preferred: Emplace a conditional directly using arguments.
DiscreteBayesNet discreteMarginal () const
 Get the discrete Bayes Net P(M).
GaussianBayesNet choose (const DiscreteValues &assignment) const
 Get the Gaussian Bayes net P(X|M=m) corresponding to a specific assignment m for the discrete variables M.
double evaluate (const HybridValues &values) const
 Evaluate hybrid probability density for given HybridValues.
double operator() (const HybridValues &values) const
 Evaluate hybrid probability density for given HybridValues, sugar.
DiscreteValues mpe () const
 Compute the Most Probable Explanation (MPE) of the discrete variables.
HybridValues optimize () const
 Solve the HybridBayesNet by first computing the MPE of all the discrete variables and then optimizing the continuous variables based on the MPE assignment.
VectorValues optimize (const DiscreteValues &assignment) const
 Given the discrete assignment, return the optimized estimate for the selected Gaussian BayesNet.
HybridValues sample (const HybridValues &given, std::mt19937_64 *rng=nullptr) const
 Sample from an incomplete BayesNet, given missing variables.
HybridValues sample (std::mt19937_64 *rng=nullptr) const
 Sample using ancestral sampling.
HybridBayesNet prune (size_t maxNrLeaves, const std::optional< double > &marginalThreshold={}, DiscreteValues *fixedValues=nullptr) const
 Prune the Bayes Net such that we have at most maxNrLeaves leaves.
AlgebraicDecisionTree< Key > errorTree (const VectorValues &continuousValues) const
 Compute the negative log posterior log P'(M|x) of all assignments up to a constant, returning the result as an algebraic decision tree.
double negLogConstant (const std::optional< DiscreteValues > &discrete={}) const
 Get the negative log of the normalization constant corresponding to the joint density represented by this Bayes net.
AlgebraicDecisionTree< Key > discretePosterior (const VectorValues &continuousValues) const
 Compute normalized posterior P(M|X=x) and return as a tree.
HybridGaussianFactorGraph toFactorGraph (const VectorValues &measurements) const
 Convert a hybrid Bayes net to a hybrid Gaussian factor graph by converting all conditionals with instantiated measurements into likelihood factors.
double logProbability (const HybridValues &x) const
Public Member Functions inherited from gtsam::BayesNet< HybridConditional >
void dot (std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
 Output to graphviz format, stream version.
std::string dot (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
 Output to graphviz format string.
void saveGraph (const std::string &filename, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
 output to file with graphviz format.
double logProbability (const HybridValues &x) const
double evaluate (const HybridValues &c) const
virtual ~FactorGraph ()=default
 Default destructor Public and virtual so boost serialization can call it.
template<class DERIVEDFACTOR, typename = IsDerived<DERIVEDFACTOR>>
 FactorGraph (std::initializer_list< std::shared_ptr< DERIVEDFACTOR > > sharedFactors)
 Constructor that takes an initializer list of shared pointers.
void reserve (size_t size)
 Reserve space for the specified number of factors if you know in advance how many there will be (works like FastVector::reserve).
template<class DERIVEDFACTOR>
IsDerived< DERIVEDFACTOR > push_back (std::shared_ptr< DERIVEDFACTOR > factor)
 Add a factor directly using a shared_ptr.
template<class DERIVEDFACTOR, class... Args>
IsDerived< DERIVEDFACTOR > emplace_shared (Args &&... args)
 Emplace a shared pointer to factor of given type.
template<class DERIVEDFACTOR>
IsDerived< DERIVEDFACTOR > push_back (const DERIVEDFACTOR &factor)
 Add a factor by value, will be copy-constructed (use push_back with a shared_ptr to avoid the copy).
template<class DERIVEDFACTOR>
IsDerived< DERIVEDFACTOR > add (std::shared_ptr< DERIVEDFACTOR > factor)
 add is a synonym for push_back.
template<class DERIVEDFACTOR>
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, This >::type & operator+= (std::shared_ptr< DERIVEDFACTOR > factor)
 Append factor to factor graph.
template<class DERIVEDFACTOR>
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, This >::type & operator, (std::shared_ptr< DERIVEDFACTOR > factor)
 Overload comma operator to allow for append chaining.
template<typename ITERATOR>
HasDerivedElementType< ITERATOR > push_back (ITERATOR firstFactor, ITERATOR lastFactor)
 Push back many factors with an iterator over shared_ptr (factors are not copied).
template<typename ITERATOR>
HasDerivedValueType< ITERATOR > push_back (ITERATOR firstFactor, ITERATOR lastFactor)
 Push back many factors with an iterator (factors are copied).
template<typename CONTAINER>
HasDerivedElementType< CONTAINER > push_back (const CONTAINER &container)
 Push back many factors as shared_ptr's in a container (factors are not copied).
template<typename CONTAINER>
HasDerivedValueType< CONTAINER > push_back (const CONTAINER &container)
 Push back non-pointer objects in a container (factors are copied).
template<class FACTOR_OR_CONTAINER>
void add (const FACTOR_OR_CONTAINER &factorOrContainer)
 Add a factor or container of factors, including STL collections, BayesTrees, etc.
template<class FACTOR_OR_CONTAINER>
This & operator+= (const FACTOR_OR_CONTAINER &factorOrContainer)
 Add a factor or container of factors, including STL collections, BayesTrees, etc.
template<class CLIQUE>
std::enable_if< std::is_base_of< This, typenameCLIQUE::FactorGraphType >::value >::type push_back (const BayesTree< CLIQUE > &bayesTree)
 Push back a BayesTree as a collection of factors.
template<typename CONTAINER, typename = HasDerivedElementType<CONTAINER>>
FactorIndices add_factors (const CONTAINER &factors, bool useEmptySlots=false)
 Add new factors to a factor graph and returns a list of new factor indices, optionally finding and reusing empty factor slots.
bool equals (const This &fg, double tol=1e-9) const
 Check equality up to tolerance.
size_t size () const
 return the number of factors (including any null factors set by remove() ).
bool empty () const
 Check if the graph is empty (null factors set by remove() will cause this to return false).
const sharedFactor at (size_t i) const
 Get a specific factor by index (this checks array bounds and may throw an exception, as opposed to operator[] which does not).
sharedFactor & at (size_t i)
 Get a specific factor by index (this checks array bounds and may throw an exception, as opposed to operator[] which does not).
template<typename F>
std::shared_ptr< F > at (size_t i)
 Get a specific factor by index and typecast to factor type F (this checks array bounds and may throw an exception, as opposed to operator[] which does not).
template<typename F>
const std::shared_ptr< F > at (size_t i) const
 Const version of templated at method.
const sharedFactor operator[] (size_t i) const
 Get a specific factor by index (this does not check array bounds, as opposed to at() which does).
sharedFactor & operator[] (size_t i)
 Get a specific factor by index (this does not check array bounds, as opposed to at() which does).
const_iterator begin () const
 Iterator to beginning of factors.
const_iterator end () const
 Iterator to end of factors.
sharedFactor front () const
 Get the first factor.
sharedFactor back () const
 Get the last factor.
double error (const HybridValues &values) const
 Add error for all factors.
iterator begin ()
 non-const STL-style begin()
iterator end ()
 non-const STL-style end()
virtual void resize (size_t size)
 Directly resize the number of factors in the graph.
void remove (size_t i)
 delete factor without re-arranging indexes by inserting a nullptr pointer
void replace (size_t index, sharedFactor factor)
 replace a factor by index
iterator erase (iterator item)
 Erase factor and rearrange other factors to take up the empty space.
iterator erase (iterator first, iterator last)
 Erase factors and rearrange other factors to take up the empty space.
void dot (std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
 Output to graphviz format, stream version.
std::string dot (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
 Output to graphviz format string.
void saveGraph (const std::string &filename, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
 output to file with graphviz format.
size_t nrFactors () const
 return the number of non-null factors
KeySet keys () const
 Potentially slow function to return all keys involved, sorted, as a set.
KeyVector keyVector () const
 Potentially slow function to return all keys involved, sorted, as a vector.
bool exists (size_t idx) const
 MATLAB interface utility: Checks whether a factor index idx exists in the graph and is a live pointer.

Public Types

using Base = BayesNet<HybridConditional>
using This = HybridBayesNet
using ConditionalType = HybridConditional
using shared_ptr = std::shared_ptr<HybridBayesNet>
using sharedConditional = std::shared_ptr<ConditionalType>
Public Types inherited from gtsam::BayesNet< HybridConditional >
typedef std::shared_ptr< HybridConditional > sharedConditional
 A shared pointer to a conditional.
Public Types inherited from gtsam::FactorGraph< FACTOR >
typedef FACTOR FactorType
 factor type
typedef std::shared_ptr< FACTOR > sharedFactor
 Shared pointer to a factor.
typedef sharedFactor value_type
typedef FastVector< sharedFactor >::iterator iterator
typedef FastVector< sharedFactor >::const_iterator const_iterator

Additional Inherited Members

Protected Member Functions inherited from gtsam::BayesNet< HybridConditional >
 BayesNet ()
 Default constructor as an empty BayesNet.
 BayesNet (ITERATOR firstConditional, ITERATOR lastConditional)
 Construct from iterator over conditionals.
 BayesNet (std::initializer_list< sharedConditional > conditionals)
 Constructor that takes an initializer list of shared pointers.
Protected Member Functions inherited from gtsam::FactorGraph< FACTOR >
bool isEqual (const FactorGraph &other) const
 Check exact equality of the factor pointers. Useful for derived ==.
 FactorGraph ()
 Default constructor.
template<typename ITERATOR>
 FactorGraph (ITERATOR firstFactor, ITERATOR lastFactor)
 Constructor from iterator over factors (shared_ptr or plain objects).
template<class CONTAINER>
 FactorGraph (const CONTAINER &factors)
 Construct from container of factors (shared_ptr or plain objects).
Protected Attributes inherited from gtsam::FactorGraph< FACTOR >
FastVector< sharedFactor > factors_
 concept check, makes sure FACTOR defines print and equals

Member Function Documentation

◆ choose()

GaussianBayesNet gtsam::HybridBayesNet::choose ( const DiscreteValues & assignment) const

Get the Gaussian Bayes net P(X|M=m) corresponding to a specific assignment m for the discrete variables M.

As the hybrid Bayes net defines P(X,M) = P(X|M) P(M), this method returns the posterior p(X|M=m).

Parameters
assignmentThe discrete value assignment for the discrete keys.
Returns
Gaussian posterior P(X|M=m) as a GaussianBayesNet.

◆ discreteMarginal()

DiscreteBayesNet gtsam::HybridBayesNet::discreteMarginal ( ) const

Get the discrete Bayes Net P(M).

As the hybrid Bayes net defines P(X,M) = P(X|M) P(M), this method returns the marginal distribution on the discrete variables.

Returns
discrete marginal as a DiscreteBayesNet.

◆ discretePosterior()

AlgebraicDecisionTree< Key > gtsam::HybridBayesNet::discretePosterior ( const VectorValues & continuousValues) const

Compute normalized posterior P(M|X=x) and return as a tree.

Note
Not a DiscreteConditional as the cardinalities of the DiscreteKeys, which we would need, are hard to recover.
Parameters
continuousValuesContinuous values x to condition P(M|X=x) on.
Returns
AlgebraicDecisionTree<Key>

◆ emplace_shared()

template<class CONDITIONAL, class... Args>
void gtsam::HybridBayesNet::emplace_shared ( Args &&... args)
inline

Preferred: Emplace a conditional directly using arguments.

Examples: hbn.emplace_shared<HybridGaussianConditional>(...))); hbn.emplace_shared<GaussianConditional>(...))); hbn.emplace_shared<DiscreteConditional>(...)));

◆ errorTree()

AlgebraicDecisionTree< Key > gtsam::HybridBayesNet::errorTree ( const VectorValues & continuousValues) const

Compute the negative log posterior log P'(M|x) of all assignments up to a constant, returning the result as an algebraic decision tree.

Note
The joint P(X,M) is p(X|M) P(M) Then the posterior on M given X=x is is P(M|x) = p(x|M) P(M) / p(x). Ideally we want log P(M|x) = log p(x|M) + log P(M) - log p(x), but unfortunately log p(x) is expensive, so we compute the log of the unnormalized posterior log P'(M|x) = log p(x|M) + log P(M)
Parameters
continuousValuesContinuous values x at which to compute log P'(M|x)
Returns
AlgebraicDecisionTree<Key>

◆ mpe()

DiscreteValues gtsam::HybridBayesNet::mpe ( ) const

Compute the Most Probable Explanation (MPE) of the discrete variables.

Returns
DiscreteValues

◆ negLogConstant()

double gtsam::HybridBayesNet::negLogConstant ( const std::optional< DiscreteValues > & discrete = {}) const

Get the negative log of the normalization constant corresponding to the joint density represented by this Bayes net.

Optionally index by Discrete.

Parameters
DiscreteOptional DiscreteValues
Returns
double

◆ optimize() [1/2]

HybridValues gtsam::HybridBayesNet::optimize ( ) const

Solve the HybridBayesNet by first computing the MPE of all the discrete variables and then optimizing the continuous variables based on the MPE assignment.

Returns
HybridValues

◆ optimize() [2/2]

VectorValues gtsam::HybridBayesNet::optimize ( const DiscreteValues & assignment) const

Given the discrete assignment, return the optimized estimate for the selected Gaussian BayesNet.

Parameters
assignmentAn assignment of discrete values.
Returns
Values

◆ print()

void gtsam::HybridBayesNet::print ( const std::string & s = "",
const KeyFormatter & formatter = DefaultKeyFormatter ) const
overridevirtual

GTSAM-style printing.

Reimplemented from gtsam::BayesNet< HybridConditional >.

◆ prune()

HybridBayesNet gtsam::HybridBayesNet::prune ( size_t maxNrLeaves,
const std::optional< double > & marginalThreshold = {},
DiscreteValues * fixedValues = nullptr ) const

Prune the Bayes Net such that we have at most maxNrLeaves leaves.

Parameters
maxNrLeavesContinuous values at which to compute the error.
marginalThresholdThe threshold to check the mode marginals against.
fixedValuesThe fixed values resulting from dead mode removal.
Note
If marginal greater than this threshold, the mode gets assigned that value and is considered "dead" for hybrid elimination. The mode can then be removed since it only has a single possible assignment.
Returns
A pruned HybridBayesNet

◆ push_back() [1/3]

template<class CONDITIONAL>
void gtsam::HybridBayesNet::push_back ( const std::shared_ptr< CONDITIONAL > & conditional)
inline

Add a conditional to the Bayes net.

Implicitly convert to a HybridConditional.

E.g. hbn.push_back(std::make_shared<DiscreteConditional>(m, "1/1"));

Template Parameters
CONDITIONALType of conditional. This is shared_ptr version.
Parameters
conditionalThe conditional as a shared pointer.

◆ push_back() [2/3]

void gtsam::HybridBayesNet::push_back ( HybridConditional && conditional)
inline

Move a HybridConditional into a shared pointer and add.

Example: HybridGaussianConditional conditional(...); hbn.push_back(conditional); // loses the original conditional

◆ push_back() [3/3]

void gtsam::HybridBayesNet::push_back ( std::shared_ptr< HybridConditional > conditional)
inline

Add a hybrid conditional using a shared_ptr.

This is the "native" push back, as this class stores hybrid conditionals.

◆ sample() [1/2]

HybridValues gtsam::HybridBayesNet::sample ( const HybridValues & given,
std::mt19937_64 * rng = nullptr ) const

Sample from an incomplete BayesNet, given missing variables.

Example: std::mt19937_64 rng(42); VectorValues given = ...; auto sample = bn.sample(given, &rng);

Parameters
givenValues of missing variables.
rngThe optional pseudo-random number generator.
Returns
HybridValues

◆ sample() [2/2]

HybridValues gtsam::HybridBayesNet::sample ( std::mt19937_64 * rng = nullptr) const

Sample using ancestral sampling.

Example: std::mt19937_64 rng(42); auto sample = bn.sample(&rng);

Parameters
rngThe optional pseudo-random number generator.
Returns
HybridValues

The documentation for this class was generated from the following files: