From e244e22c6ac37980507d8b836bccf8407db6ded5 Mon Sep 17 00:00:00 2001 From: Manohar Paluri Date: Mon, 7 Dec 2009 07:12:01 +0000 Subject: [PATCH] added probability function that computes the probability of a BinaryBayesNet given a config. --- cpp/testBinaryBayesNet.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cpp/testBinaryBayesNet.cpp b/cpp/testBinaryBayesNet.cpp index 2b67939e7..ee4fc18f3 100644 --- a/cpp/testBinaryBayesNet.cpp +++ b/cpp/testBinaryBayesNet.cpp @@ -31,6 +31,18 @@ using namespace gtsam; /** A Bayes net made from binary conditional probability tables */ typedef BayesNet BinaryBayesNet; + +double probability( BinaryBayesNet & bbn, map & config) +{ + double result = 1.0; + BinaryBayesNet::const_iterator it = bbn.begin(); + while( it != bbn.end() ){ + result *= (*it)->probability(config); + it++; + } + return result; +} + /************************************************************************** */ TEST( BinaryBayesNet, constructor ) { @@ -40,19 +52,19 @@ TEST( BinaryBayesNet, constructor ) // p(x|y=1) = 0.6 map config; - config["y"] = true; - config["x"] = true; + config["y"] = false; + config["x"] = false; // unary conditional for y boost::shared_ptr py(new BinaryConditional("y",0.2)); py->print("py"); - DOUBLES_EQUAL(0.2,py->probability(config),0.01); + DOUBLES_EQUAL(0.8,py->probability(config),0.01); // single parent conditional for x vector cpt; cpt += 0.3, 0.6 ; // array index corresponds to binary parent configuration boost::shared_ptr px_y(new BinaryConditional("x","y",cpt)); px_y->print("px_y"); - DOUBLES_EQUAL(0.6,px_y->probability(config),0.01); + DOUBLES_EQUAL(0.7,px_y->probability(config),0.01); // push back conditionals in topological sort order (parents last) BinaryBayesNet bbn; @@ -60,7 +72,7 @@ TEST( BinaryBayesNet, constructor ) bbn.push_back(px_y); // Test probability of 00,01,10,11 - //DOUBLES_EQUAL(0.56,bbn.probability(config),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56; + DOUBLES_EQUAL(0.56,probability(bbn,config),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56; } /* ************************************************************************* */