use optional DiscreteValues

release/4.3a0
Varun Agrawal 2024-11-03 15:32:21 -05:00
parent 8aacfa95f3
commit 5c63ac833c
2 changed files with 18 additions and 30 deletions

View File

@ -198,29 +198,24 @@ AlgebraicDecisionTree<Key> HybridBayesNet::errorTree(
}
/* ************************************************************************* */
double HybridBayesNet::negLogConstant() const {
double HybridBayesNet::negLogConstant(
const std::optional<DiscreteValues> &discrete) const {
double negLogNormConst = 0.0;
// Iterate over each conditional.
for (auto &&conditional : *this) {
negLogNormConst += conditional->negLogConstant();
}
return negLogNormConst;
}
/* ************************************************************************* */
double HybridBayesNet::negLogConstant(const DiscreteValues &discrete) const {
double negLogNormConst = 0.0;
// Iterate over each conditional.
for (auto &&conditional : *this) {
if (auto gm = conditional->asHybrid()) {
negLogNormConst += gm->choose(discrete)->negLogConstant();
} else if (auto gc = conditional->asGaussian()) {
negLogNormConst += gc->negLogConstant();
} else if (auto dc = conditional->asDiscrete()) {
negLogNormConst += dc->choose(discrete)->negLogConstant();
if (discrete.has_value()) {
if (auto gm = conditional->asHybrid()) {
negLogNormConst += gm->choose(*discrete)->negLogConstant();
} else if (auto gc = conditional->asGaussian()) {
negLogNormConst += gc->negLogConstant();
} else if (auto dc = conditional->asDiscrete()) {
negLogNormConst += dc->choose(*discrete)->negLogConstant();
} else {
throw std::runtime_error(
"Unknown conditional type when computing negLogConstant");
}
} else {
throw std::runtime_error(
"Unknown conditional type when computing negLogConstant");
negLogNormConst += conditional->negLogConstant();
}
}
return negLogNormConst;

View File

@ -237,22 +237,15 @@ class GTSAM_EXPORT HybridBayesNet : public BayesNet<HybridConditional> {
using BayesNet::logProbability; // expose HybridValues version
/**
* @brief Get the negative log of the normalization constant corresponding
* to the joint density represented by this Bayes net.
*
* @return double
*/
double negLogConstant() const;
/**
* @brief Get the negative log of the normalization constant
* corresponding to the joint Gaussian density represented by
* this Bayes net indexed by `discrete`.
* corresponding to the joint density represented by this Bayes net.
* Optionally index by `discrete`.
*
* @param discrete Optional DiscreteValues
* @return double
*/
double negLogConstant(const DiscreteValues &discrete) const;
double negLogConstant(const std::optional<DiscreteValues> &discrete) const;
/**
* @brief Compute normalized posterior P(M|X=x) and return as a tree.