/** * @file testBinaryBayesNet.cpp * @brief Unit tests for BinaryBayesNet * @author Manohar Paluri */ // STL/C++ #include #include #include #include #include #include #include // for operator += using namespace boost::assign; #ifdef HAVE_BOOST_SERIALIZATION #include #include #endif //HAVE_BOOST_SERIALIZATION #define GTSAM_MAGIC_KEY #include "BinaryConditional.h" #include "BayesNet-inl.h" #include "smallExample.h" #include "Ordering.h" #include "SymbolMap.h" using namespace std; using namespace gtsam; /** A Bayes net made from binary conditional probability tables */ typedef BayesNet BinaryBayesNet; double probability( BinaryBayesNet & bbn, SymbolMap & 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 ) { // small Bayes Net x <- y // p(y) = 0.2 // p(x|y=0) = 0.3 // p(x|y=1) = 0.6 SymbolMap config; config["y"] = false; config["x"] = false; // unary conditional for y boost::shared_ptr py(new BinaryConditional("y",0.2)); 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)); 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); // Test probability of 00,01,10,11 DOUBLES_EQUAL(0.56,probability(bbn,config),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56; } /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr);} /* ************************************************************************* */