From 28eb550781e87f9a4309513061d22a0a50bda4ee Mon Sep 17 00:00:00 2001 From: Manohar Paluri Date: Mon, 7 Dec 2009 00:49:13 +0000 Subject: [PATCH] Used a map for config --- cpp/BinaryConditional.h | 9 +++++++ cpp/testBinaryBayesNet.cpp | 55 +++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/cpp/BinaryConditional.h b/cpp/BinaryConditional.h index 31257b9e8..0a9001d40 100644 --- a/cpp/BinaryConditional.h +++ b/cpp/BinaryConditional.h @@ -57,6 +57,15 @@ namespace gtsam { cpt_ = cpt; } + double probability( std::map config) { + int index = 0, count = 0; + BOOST_FOREACH( std::string parent, parents_) + index += pow(2,count++)*(int)(config[parent]); + if( config.find(key_)->second ) + index += pow(2,count); + return cpt_[index]; + } + /** print */ void print(const std::string& s = "BinaryConditional") const { std::cout << s << " P(" << key_; diff --git a/cpp/testBinaryBayesNet.cpp b/cpp/testBinaryBayesNet.cpp index 09fa9c3e6..131b0c01b 100644 --- a/cpp/testBinaryBayesNet.cpp +++ b/cpp/testBinaryBayesNet.cpp @@ -7,6 +7,7 @@ // STL/C++ #include #include +#include #include #include #include @@ -30,44 +31,36 @@ using namespace gtsam; /** A Bayes net made from binary conditional probability tables */ typedef BayesNet BinaryBayesNet; -struct BinaryConfig { - bool px_; - bool py_; - - BinaryConfig( bool px, bool py ):px_(px), py_(py){} -}; - -double probability(const BinaryBayesNet& bayesNet, const BinaryConfig& config) { - double result = 1.0; - /* TODO: using config multiply the probabilities */ - return result; -} - /* ************************************************************************* */ TEST( BinaryBayesNet, constructor ) { - // small Bayes Net x <- y - // p(y) = 0.2 - // p(x|y=0) = 0.3 - // p(x|y=1) = 0.5 + // small Bayes Net x <- y + // p(y) = 0.2 + // p(x|y=0) = 0.3 + // p(x|y=1) = 0.6 - // unary conditional for y - boost::shared_ptr py(new BinaryConditional("y",0.2)); - py->print("py"); + map config; + 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.8,py->probability(config),0.01); - // single parent conditional for x - vector cpt; - cpt += 0.7, 0.5, 0.3, 0.5 ; // array index corresponds to binary parent configuration - boost::shared_ptr px_y(new BinaryConditional("x","y",cpt)); - px_y->print("px_y"); + // single parent conditional for x + vector cpt; + cpt += 0.7, 0.4, 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.7,px_y->probability(config),0.01); - // push back conditionals in topological sort order (parents last) - BinaryBayesNet bbn; - bbn.push_back(py); - bbn.push_back(px_y); + // push back conditionals in topological sort order (parents last) + BinaryBayesNet bbn; + bbn.push_back(py); + bbn.push_back(px_y); - // Test probability of 00,01,10,11 - DOUBLES_EQUAL(0.56,probability(bbn,BinaryConfig(false,false)),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56; + // 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; } /* ************************************************************************* */