Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Pruning #1862

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gtsam/hybrid/HybridBayesNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ HybridBayesNet HybridBayesNet::prune(size_t maxNrLeaves) const {
// per pruned Discrete joint.
for (auto &&conditional : *this) {
if (auto hgc = conditional->asHybrid()) {
// Make a copy of the hybrid Gaussian conditional and prune it!
// Prune the hybrid Gaussian conditional!
auto prunedHybridGaussianConditional = hgc->prune(pruned);

// Type-erase and add to the pruned Bayes Net fragment.
Expand Down
12 changes: 9 additions & 3 deletions gtsam/hybrid/HybridGaussianFactorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ discreteElimination(const HybridGaussianFactorGraph &factors,
// In this case, compute discrete probabilities.
auto logProbability =
[&](const GaussianFactor::shared_ptr &factor) -> double {
if (!factor) return 0.0;
// If the factor is null, it is has been pruned hence return ∞
// so that the exp(-∞)=0.
if (!factor) return std::numeric_limits<double>::infinity();
return factor->error(VectorValues());
};
AlgebraicDecisionTree<Key> logProbabilities =
Expand Down Expand Up @@ -300,11 +302,15 @@ static std::shared_ptr<Factor> createDiscreteFactor(
auto negLogProbability = [&](const Result &pair) -> double {
const auto &[conditional, factor] = pair;
static const VectorValues kEmpty;
// If the factor is not null, it has no keys, just contains the residual.
if (!factor) return 1.0; // TODO(dellaert): not loving this.
// If the factor is null, it has been pruned, hence return ∞
// so that the exp(-∞)=0.
if (!factor) return std::numeric_limits<double>::infinity();

// Negative logspace version of:
// exp(-factor->error(kEmpty)) / conditional->normalizationConstant();
// = exp(-factor->error(kEmpty)) * \sqrt{|2πΣ|};
// log = -(-factor->error(kEmpty) + log(\sqrt{|2πΣ|}))
// = factor->error(kEmpty) - log(\sqrt{|2πΣ|})
// negLogConstant gives `-log(k)`
// which is `-log(k) = log(1/k) = log(\sqrt{|2πΣ|})`.
return factor->error(kEmpty) - conditional->negLogConstant();
Expand Down
Loading