added probability function that computes the probability of a BinaryBayesNet given a config.
parent
115d9a8adf
commit
e244e22c6a
|
@ -31,6 +31,18 @@ using namespace gtsam;
|
|||
/** A Bayes net made from binary conditional probability tables */
|
||||
typedef BayesNet<BinaryConditional> BinaryBayesNet;
|
||||
|
||||
|
||||
double probability( BinaryBayesNet & bbn, map<string,bool> & 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<string,bool> config;
|
||||
config["y"] = true;
|
||||
config["x"] = true;
|
||||
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.2,py->probability(config),0.01);
|
||||
DOUBLES_EQUAL(0.8,py->probability(config),0.01);
|
||||
|
||||
// single parent conditional for x
|
||||
vector<double> cpt;
|
||||
cpt += 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.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;
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
Loading…
Reference in New Issue