38 using namespace gtsam;
73 const auto pAsia = std::make_shared<DiscreteConditional>(
Asia,
"4/6");
90 std::mt19937_64
rng(42);
132 gc1 =
hgc->choose(one.discrete());
134 GaussianConditional::CheckInvariants(*gc0,
vv);
135 GaussianConditional::CheckInvariants(*gc1,
vv);
136 GaussianConditional::CheckInvariants(*
px,
vv);
137 HybridGaussianConditional::CheckInvariants(*
hgc,
zero);
138 HybridGaussianConditional::CheckInvariants(*
hgc, one);
149 const double logP0 = chosen0.logProbability(
vv) +
log(0.4);
150 const double logP1 = chosen1.logProbability(
vv) +
log(0.6);
167 CHECK(pruned.at(1)->asHybrid());
172 const double error0 = chosen0.error(
vv) + gc0->negLogConstant() -
173 px->negLogConstant() -
log(0.4);
174 const double error1 = chosen1.error(
vv) + gc1->negLogConstant() -
175 px->negLogConstant() -
log(0.6);
209 auto df = std::dynamic_pointer_cast<DecisionTreeFactor>(
result.second);
212 std::vector<double>
ratio(2);
224 auto fgPosterior = fg.discretePosterior(xv);
231 const auto gc = GaussianConditional::sharedMeanAndStddev(
X(0), 2 * I_1x1,
X(1),
236 const auto hgc = std::make_shared<HybridGaussianConditional>(
Asia,
X(1),
parms);
238 const auto prior = std::make_shared<DiscreteConditional>(
Asia,
"99/1");
240 return std::make_shared<HybridConditional>(
c);
254 const double mixtureProbability =
hgc->evaluate(
values);
266 const auto [hybridBayesNet, remainingFactorGraph] =
267 s.linearizedFactorGraph().eliminatePartialSequential(
ordering);
270 assignment[
M(0)] = 1;
271 assignment[
M(1)] = 1;
272 assignment[
M(2)] = 0;
295 const auto [hybridBayesNet, remainingFactorGraph] =
296 s.linearizedFactorGraph().eliminatePartialSequential(
ordering);
299 assignment[
M(0)] = 1;
300 assignment[
M(1)] = 1;
301 assignment[
M(2)] = 1;
309 expected_delta.
insert(make_pair(
X(0), -Vector1::Ones()));
310 expected_delta.
insert(make_pair(
X(1), -Vector1::Ones()));
311 expected_delta.
insert(make_pair(
X(2), -Vector1::Ones()));
312 expected_delta.
insert(make_pair(
X(3), -Vector1::Ones()));
320 Switching s(4, 1.0, 0.1, {0, 1, 2, 3},
"1/1 1/1");
323 s.linearizedFactorGraph().eliminateSequential();
329 expectedAssignment[
M(0)] = 1;
330 expectedAssignment[
M(1)] = 1;
331 expectedAssignment[
M(2)] = 1;
335 expectedValues.
insert(
X(0), -Vector1::Ones());
336 expectedValues.
insert(
X(1), -Vector1::Ones());
337 expectedValues.
insert(
X(2), -Vector1::Ones());
338 expectedValues.
insert(
X(3), -Vector1::Ones());
351 s.linearizedFactorGraph().eliminateSequential();
358 auto discretePosterior = posterior->discretePosterior(
delta.continuous());
362 std::vector<double> leaves = {0.095516068, 0.31800092, 0.27798511, 0.3084979};
369 double logProbability = 0;
370 logProbability += posterior->at(0)->asHybrid()->logProbability(hybridValues);
371 logProbability += posterior->at(1)->asHybrid()->logProbability(hybridValues);
372 logProbability += posterior->at(2)->asHybrid()->logProbability(hybridValues);
374 posterior->at(3)->asDiscrete()->logProbability(hybridValues);
376 posterior->at(4)->asDiscrete()->logProbability(hybridValues);
380 double negLogConstant = posterior->negLogConstant(discrete_values);
385 [](
double error) { return exp(-error); })
389 double density =
exp(logProbability + negLogConstant) / normalizer;
393 auto prunedBayesNet = posterior->prune(2);
394 auto prunedTree = prunedBayesNet.discretePosterior(
delta.continuous());
397 std::vector<double> pruned_leaves = {0.0, 0.50758422, 0.0, 0.49241578};
402 double pruned_logProbability = 0;
403 pruned_logProbability +=
404 prunedBayesNet.at(0)->asDiscrete()->logProbability(hybridValues);
405 pruned_logProbability +=
406 prunedBayesNet.at(1)->asHybrid()->logProbability(hybridValues);
407 pruned_logProbability +=
408 prunedBayesNet.at(2)->asHybrid()->logProbability(hybridValues);
409 pruned_logProbability +=
410 prunedBayesNet.at(3)->asHybrid()->logProbability(hybridValues);
412 double pruned_negLogConstant = prunedBayesNet.negLogConstant(discrete_values);
415 double pruned_normalizer =
417 [](
double error) { return exp(-error); })
419 double pruned_density =
420 exp(pruned_logProbability + pruned_negLogConstant) / pruned_normalizer;
430 s.linearizedFactorGraph().eliminateSequential();
435 auto prunedBayesNet = posterior->prune(2);
448 s.linearizedFactorGraph().eliminateSequential();
452 for (
auto&& conditional : posterior->discreteMarginal()) {
453 joint = joint * (*conditional);
456 size_t maxNrLeaves = 3;
457 auto prunedDecisionTree = joint.
prune(maxNrLeaves);
459 #ifdef GTSAM_DT_MERGING
461 prunedDecisionTree.nrLeaves());
470 std::vector<double>{0, 0, 0, 0.28739288, 0, 0.43106901, 0, 0.2815381});
474 auto pruned = posterior->prune(maxNrLeaves);
478 double probability) ->
double {
481 if (prunedDecisionTree(choices) == 0) {
490 CHECK(pruned.at(0)->asDiscrete());
491 auto pruned_discrete_conditionals = pruned.at(0)->asDiscrete();
492 auto discrete_conditional_tree =
493 std::dynamic_pointer_cast<DecisionTreeFactor::ADT>(
494 pruned_discrete_conditionals);
497 discrete_conditional_tree->apply(checker);
509 std::make_shared<BetweenFactor<double>>(
X(0),
X(1), 0,
noise_model);
511 std::make_shared<BetweenFactor<double>>(
X(0),
X(1), 1,
noise_model);
514 std::vector<NoiseModelFactor::shared_ptr>{zero_motion, one_motion});
520 double z0 = 0.0,
z1 = 1.0;
530 std::mt19937_64 gen(11);
533 vector<double> discrete_samples;
536 size_t num_samples = 1000;
537 for (
size_t i = 0;
i < num_samples;
i++) {
541 discrete_samples.push_back(sample.
discrete().at(
M(0)));
574 Key z0 =
Z(0), f01 =
F(0);
579 auto prior_model = noiseModel::Isotropic::Sigma(1, 1
e-1);
580 auto measurement_model = noiseModel::Isotropic::Sigma(1, 2.0);
591 double sigma0 = 1e2,
sigma1 = 1
e-2;
592 auto model0 = noiseModel::Isotropic::Sigma(1, sigma0);
594 auto c0 = make_shared<GaussianConditional>(f01,
Vector1(
mu), I_1x1,
x1, I_1x1,
596 c1 = make_shared<GaussianConditional>(f01,
Vector1(
mu), I_1x1,
x1, I_1x1,
606 given.insert(f01,
Vector1(0.0));