Added cpt to BinaryConditional

release/4.3a0
Manohar Paluri 2009-12-06 23:28:46 +00:00
parent 60a3a21d5a
commit b9e15ee789
2 changed files with 20 additions and 5 deletions

View File

@ -14,6 +14,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp> // TODO: make cpp file #include <boost/foreach.hpp> // TODO: make cpp file
#include <boost/serialization/list.hpp> #include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>
#include "Conditional.h" #include "Conditional.h"
namespace gtsam { namespace gtsam {
@ -26,6 +27,7 @@ namespace gtsam {
private: private:
std::list<std::string> parents_; std::list<std::string> parents_;
std::vector<double> cpt_;
public: public:
@ -42,6 +44,8 @@ namespace gtsam {
*/ */
BinaryConditional(const std::string& key, double p) : BinaryConditional(const std::string& key, double p) :
Conditional(key) { Conditional(key) {
cpt_.push_back(1-p);
cpt_.push_back(p);
} }
/** /**
@ -50,6 +54,7 @@ namespace gtsam {
BinaryConditional(const std::string& key, const std::string& parent, const std::vector<double>& cpt) : BinaryConditional(const std::string& key, const std::string& parent, const std::vector<double>& cpt) :
Conditional(key) { Conditional(key) {
parents_.push_back(parent); parents_.push_back(parent);
cpt_ = cpt;
} }
/** print */ /** print */
@ -58,6 +63,9 @@ namespace gtsam {
if (parents_.size()>0) std::cout << " |"; if (parents_.size()>0) std::cout << " |";
BOOST_FOREACH(std::string parent, parents_) std::cout << " " << parent; BOOST_FOREACH(std::string parent, parents_) std::cout << " " << parent;
std::cout << ")" << std::endl; std::cout << ")" << std::endl;
std::cout << "Conditional Probability Table::" << std::endl;
BOOST_FOREACH(double p, cpt_) std::cout << p << "\t";
std::cout<< std::endl;
} }
/** check equality */ /** check equality */
@ -65,12 +73,15 @@ namespace gtsam {
if (!Conditional::equals(c)) return false; if (!Conditional::equals(c)) return false;
const BinaryConditional* p = dynamic_cast<const BinaryConditional*> (&c); const BinaryConditional* p = dynamic_cast<const BinaryConditional*> (&c);
if (p == NULL) return false; if (p == NULL) return false;
return parents_ == p->parents_; return (parents_ == p->parents_ && cpt_ == p->cpt_);
} }
/** return parents */ /** return parents */
std::list<std::string> parents() const { return parents_;} std::list<std::string> parents() const { return parents_;}
/** return Conditional probability table*/
std::vector<double> cpt() const { return cpt_;}
/** find the number of parents */ /** find the number of parents */
size_t nrParents() const { size_t nrParents() const {
return parents_.size(); return parents_.size();
@ -83,6 +94,7 @@ namespace gtsam {
void serialize(Archive & ar, const unsigned int version) { void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this)); ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this));
ar & BOOST_SERIALIZATION_NVP(parents_); ar & BOOST_SERIALIZATION_NVP(parents_);
ar & BOOST_SERIALIZATION_NVP(cpt_);
} }
}; };
} /// namespace gtsam } /// namespace gtsam

View File

@ -38,8 +38,9 @@ struct BinaryConfig {
}; };
double probability(const BinaryBayesNet& bayesNet, const BinaryConfig& config) { double probability(const BinaryBayesNet& bayesNet, const BinaryConfig& config) {
double result = 1.0;
return 0; /* TODO: using config multiply the probabilities */
return result;
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -52,11 +53,13 @@ TEST( BinaryBayesNet, constructor )
// unary conditional for y // unary conditional for y
boost::shared_ptr<BinaryConditional> py(new BinaryConditional("y",0.2)); boost::shared_ptr<BinaryConditional> py(new BinaryConditional("y",0.2));
py->print("py");
// single parent conditional for x // single parent conditional for x
vector<double> cpt; vector<double> cpt;
cpt += 0.3, 0.5; // array index corresponds to binary parent configuration cpt += 0.7, 0.5, 0.3, 0.5 ; // array index corresponds to binary parent configuration
boost::shared_ptr<BinaryConditional> px_y(new BinaryConditional("x","y",cpt)); boost::shared_ptr<BinaryConditional> px_y(new BinaryConditional("x","y",cpt));
px_y->print("px_y");
// push back conditionals in topological sort order (parents last) // push back conditionals in topological sort order (parents last)
BinaryBayesNet bbn; BinaryBayesNet bbn;
@ -64,7 +67,7 @@ TEST( BinaryBayesNet, constructor )
bbn.push_back(px_y); bbn.push_back(px_y);
// Test probability of 00,01,10,11 // 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; 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;
} }
/* ************************************************************************* */ /* ************************************************************************* */