Binary Bayes Net, incomplete
parent
2a4e90a283
commit
60a3a21d5a
|
@ -0,0 +1,88 @@
|
||||||
|
/**
|
||||||
|
* @file DiscreteConditional.h
|
||||||
|
* @brief Discrete Conditional node for use in Bayes nets
|
||||||
|
* @author Manohar Paluri
|
||||||
|
*/
|
||||||
|
|
||||||
|
// \callgraph
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/foreach.hpp> // TODO: make cpp file
|
||||||
|
#include <boost/serialization/list.hpp>
|
||||||
|
#include "Conditional.h"
|
||||||
|
|
||||||
|
namespace gtsam {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conditional node for use in a Bayes net
|
||||||
|
*/
|
||||||
|
class BinaryConditional: public Conditional {
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::list<std::string> parents_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/** convenience typename for a shared pointer to this class */
|
||||||
|
typedef boost::shared_ptr<BinaryConditional> shared_ptr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty Constructor to make serialization possible
|
||||||
|
*/
|
||||||
|
BinaryConditional(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No parents
|
||||||
|
*/
|
||||||
|
BinaryConditional(const std::string& key, double p) :
|
||||||
|
Conditional(key) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single parent
|
||||||
|
*/
|
||||||
|
BinaryConditional(const std::string& key, const std::string& parent, const std::vector<double>& cpt) :
|
||||||
|
Conditional(key) {
|
||||||
|
parents_.push_back(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** print */
|
||||||
|
void print(const std::string& s = "BinaryConditional") const {
|
||||||
|
std::cout << s << " P(" << key_;
|
||||||
|
if (parents_.size()>0) std::cout << " |";
|
||||||
|
BOOST_FOREACH(std::string parent, parents_) std::cout << " " << parent;
|
||||||
|
std::cout << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** check equality */
|
||||||
|
bool equals(const Conditional& c, double tol = 1e-9) const {
|
||||||
|
if (!Conditional::equals(c)) return false;
|
||||||
|
const BinaryConditional* p = dynamic_cast<const BinaryConditional*> (&c);
|
||||||
|
if (p == NULL) return false;
|
||||||
|
return parents_ == p->parents_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** return parents */
|
||||||
|
std::list<std::string> parents() const { return parents_;}
|
||||||
|
|
||||||
|
/** find the number of parents */
|
||||||
|
size_t nrParents() const {
|
||||||
|
return parents_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/** Serialization function */
|
||||||
|
friend class boost::serialization::access;
|
||||||
|
template<class Archive>
|
||||||
|
void serialize(Archive & ar, const unsigned int version) {
|
||||||
|
ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this));
|
||||||
|
ar & BOOST_SERIALIZATION_NVP(parents_);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} /// namespace gtsam
|
|
@ -85,6 +85,12 @@ testSymbolicFactorGraph_LDADD = libgtsam.la
|
||||||
testSymbolicBayesNet_SOURCES = $(example) testSymbolicBayesNet.cpp
|
testSymbolicBayesNet_SOURCES = $(example) testSymbolicBayesNet.cpp
|
||||||
testSymbolicBayesNet_LDADD = libgtsam.la
|
testSymbolicBayesNet_LDADD = libgtsam.la
|
||||||
|
|
||||||
|
# Binary Inference
|
||||||
|
headers += BinaryConditional.h
|
||||||
|
check_PROGRAMS += testBinaryBayesNet
|
||||||
|
testBinaryBayesNet_SOURCES = testBinaryBayesNet.cpp
|
||||||
|
testBinaryBayesNet_LDADD = libgtsam.la
|
||||||
|
|
||||||
# Gaussian inference
|
# Gaussian inference
|
||||||
headers += GaussianFactorSet.h
|
headers += GaussianFactorSet.h
|
||||||
sources += VectorConfig.cpp GaussianFactor.cpp GaussianFactorGraph.cpp GaussianConditional.cpp GaussianBayesNet.cpp
|
sources += VectorConfig.cpp GaussianFactor.cpp GaussianFactorGraph.cpp GaussianConditional.cpp GaussianBayesNet.cpp
|
||||||
|
|
|
@ -1,27 +1,72 @@
|
||||||
/**
|
/**
|
||||||
* @file testBinaryBayesNet.cpp
|
* @file testBinaryBayesNet.cpp
|
||||||
* @brief Unit tests for Bayes Tree
|
* @brief Unit tests for BinaryBayesNet
|
||||||
* @author Frank Dellaert
|
* @author Manohar Paluri
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// STL/C++
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <CppUnitLite/TestHarness.h>
|
#include <CppUnitLite/TestHarness.h>
|
||||||
|
#include <boost/tuple/tuple.hpp>
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
|
||||||
|
#include <boost/assign/std/vector.hpp> // for operator +=
|
||||||
|
using namespace boost::assign;
|
||||||
|
|
||||||
|
#ifdef HAVE_BOOST_SERIALIZATION
|
||||||
|
#include <boost/archive/text_oarchive.hpp>
|
||||||
|
#include <boost/archive/text_iarchive.hpp>
|
||||||
|
#endif //HAVE_BOOST_SERIALIZATION
|
||||||
|
|
||||||
|
#include "BinaryConditional.h"
|
||||||
|
#include "BayesNet-inl.h"
|
||||||
|
#include "smallExample.h"
|
||||||
|
#include "Ordering.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
using namespace gtsam;
|
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) {
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
TEST( BinaryBayesNet, constructor )
|
TEST( BinaryBayesNet, constructor )
|
||||||
{
|
{
|
||||||
map<string,BinaryCPT> tables;
|
// small Bayes Net x <- y
|
||||||
BinaryCPT pA(0.01);tables.insert("A",pA);
|
// p(y) = 0.2
|
||||||
BinaryCPT pB("S",0.6,0.3);
|
// p(x|y=0) = 0.3
|
||||||
BinaryBayesNet binaryBayesNet(tables);
|
// p(x|y=1) = 0.5
|
||||||
BinaryConfig allFalse(false,false,false,...);
|
|
||||||
DOUBLES_EQUAL(0.12,binaryBayesNet.probability(allFalse));
|
// unary conditional for y
|
||||||
|
boost::shared_ptr<BinaryConditional> py(new BinaryConditional("y",0.2));
|
||||||
|
|
||||||
|
// single parent conditional for x
|
||||||
|
vector<double> cpt;
|
||||||
|
cpt += 0.3, 0.5; // array index corresponds to binary parent configuration
|
||||||
|
boost::shared_ptr<BinaryConditional> px_y(new BinaryConditional("x","y",cpt));
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
int main() {
|
int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
|
||||||
TestResult tr;
|
|
||||||
return TestRegistry::runAllTests(tr);
|
|
||||||
}
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
|
Loading…
Reference in New Issue