Used a map for config
parent
b9e15ee789
commit
28eb550781
|
@ -57,6 +57,15 @@ namespace gtsam {
|
|||
cpt_ = cpt;
|
||||
}
|
||||
|
||||
double probability( std::map<std::string,bool> 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_;
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
// STL/C++
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <CppUnitLite/TestHarness.h>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
@ -30,36 +31,28 @@ using namespace gtsam;
|
|||
/** A Bayes net made from binary conditional probability tables */
|
||||
typedef BayesNet<BinaryConditional> 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
|
||||
// p(x|y=1) = 0.6
|
||||
|
||||
map<string,bool> config;
|
||||
config["y"] = false;
|
||||
config["x"] = false;
|
||||
// unary conditional for y
|
||||
boost::shared_ptr<BinaryConditional> py(new BinaryConditional("y",0.2));
|
||||
py->print("py");
|
||||
DOUBLES_EQUAL(0.8,py->probability(config),0.01);
|
||||
|
||||
// single parent conditional for x
|
||||
vector<double> cpt;
|
||||
cpt += 0.7, 0.5, 0.3, 0.5 ; // array index corresponds to binary parent configuration
|
||||
cpt += 0.7, 0.4, 0.3, 0.6 ; // array index corresponds to binary parent configuration
|
||||
boost::shared_ptr<BinaryConditional> 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;
|
||||
|
@ -67,7 +60,7 @@ TEST( BinaryBayesNet, constructor )
|
|||
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;
|
||||
//DOUBLES_EQUAL(0.56,bbn.probability(config),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56;
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
Loading…
Reference in New Issue