DiscreteBayesNet::mode method to get maximizing assignment

release/4.3a0
Varun Agrawal 2024-07-10 23:44:43 -04:00
parent 89f7f7f721
commit 1657262c87
3 changed files with 42 additions and 0 deletions

View File

@ -62,6 +62,14 @@ DiscreteValues DiscreteBayesNet::sample(DiscreteValues result) const {
return result; return result;
} }
DiscreteValues DiscreteBayesNet::mode() const {
DiscreteValues result;
for (auto it = begin(); it != end(); ++it) {
result[(*it)->firstFrontalKey()] = (*it)->argmax(result);
}
return result;
}
/* *********************************************************************** */ /* *********************************************************************** */
std::string DiscreteBayesNet::markdown( std::string DiscreteBayesNet::markdown(
const KeyFormatter& keyFormatter, const KeyFormatter& keyFormatter,

View File

@ -124,6 +124,14 @@ class GTSAM_EXPORT DiscreteBayesNet: public BayesNet<DiscreteConditional> {
*/ */
DiscreteValues sample(DiscreteValues given) const; DiscreteValues sample(DiscreteValues given) const;
/**
* @brief Compute the discrete assignment which gives the highest
* probability for the DiscreteBayesNet.
*
* @return DiscreteValues
*/
DiscreteValues mode() const;
///@} ///@}
/// @name Wrapper support /// @name Wrapper support
/// @{ /// @{

View File

@ -122,6 +122,32 @@ TEST(DiscreteBayesNet, Asia) {
EXPECT(assert_equal(expectedSample, actualSample)); EXPECT(assert_equal(expectedSample, actualSample));
} }
/* ************************************************************************* */
TEST(DiscreteBayesNet, Mode) {
DiscreteBayesNet asia;
asia.add(Asia, "99/1");
asia.add(Smoking % "50/50"); // Signature version
asia.add(Tuberculosis | Asia = "99/1 95/5");
asia.add(LungCancer | Smoking = "99/1 90/10");
asia.add(Bronchitis | Smoking = "70/30 40/60");
asia.add((Either | Tuberculosis, LungCancer) = "F T T T");
asia.add(XRay | Either = "95/5 2/98");
asia.add((Dyspnea | Either, Bronchitis) = "9/1 2/8 3/7 1/9");
DiscreteValues actual = asia.mode();
// NOTE: Examined the DBN and found the optimal assignment.
DiscreteValues expected{
{Asia.first, 0}, {Smoking.first, 0}, {Tuberculosis.first, 0},
{LungCancer.first, 0}, {Bronchitis.first, 0}, {Either.first, 0},
{XRay.first, 0}, {Dyspnea.first, 0},
};
EXPECT(assert_equal(expected, actual));
}
/* ************************************************************************* */ /* ************************************************************************* */
TEST(DiscreteBayesNet, Sugar) { TEST(DiscreteBayesNet, Sugar) {
DiscreteKey T(0, 2), L(1, 2), E(2, 2), C(8, 3), S(7, 2); DiscreteKey T(0, 2), L(1, 2), E(2, 2), C(8, 3), S(7, 2);