Esempio n. 1
0
void run(unsigned int size, unsigned int select) {
	BayesNet<> bn;

	map<vector<int>, map<int, double>> cpt;
	map<int, double> dist;

	map<vector<int>, map<int, double>> sinkCPT;

	for (int i = 0; i < size; i++) {
		if (i == select) {
			dist.insert(make_pair(0, 0.0));
			dist.insert(make_pair(1, 1.0));
			cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
			bn.add_node(i, CondProb<>(cpt));

			cpt.clear();

			dist.insert(make_pair(0, 0.0));
			dist.insert(make_pair(1, 1.0));
			vector<int> parents(size, 0);
			parents[i] = 1;
			sinkCPT.insert(CondProb<>::CondCase(parents, dist));
		} else {
			dist.insert(make_pair(0, 1.0));
			dist.insert(make_pair(1, 0.0));
			cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
			bn.add_node(i, CondProb<>(cpt));

			cpt.clear();

			dist.insert(make_pair(0, 1.0));
			dist.insert(make_pair(1, 0.0));
			vector<int> parents(size, 0);
			parents[i] = 1;
			sinkCPT.insert(CondProb<>::CondCase(parents, dist));
		}

		cpt.clear();
		dist.clear();
	}

	set<int> parents;
	for (int i = 0; i < size; i++)
		parents.insert(i);
	bn.add_node(size, parents, CondProb<>(sinkCPT));

	map<int, int> values;
	for (int i = 0; i < size; i++)
		values.insert(make_pair(0, 0));
	values.insert(make_pair(select, 1));
	map<std::map<int, int>, double> marginal_dist = bn.marginal_dist(values, 512, SampleStrategy::GIBBS);
	assertEquals(marginal_dist[values], (double)1.0);
}
Esempio n. 2
0
void canMarkovBlanket() {
	BayesNet<> bn;

	map<vector<int>, map<int, double>> cpt;
	map<int, double> dist;
	dist.insert(make_pair(0, 1.0));
	dist.insert(make_pair(1, 0.0));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(0, CondProb<>(cpt));

	cpt.clear();
	dist.clear();
	dist.insert(make_pair(0, 1.0));
	dist.insert(make_pair(1, 0.0));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(1, CondProb<>(cpt));

	cpt.clear();
	dist.clear();
	dist.insert(make_pair(1, 0.7));
	dist.insert(make_pair(0, 0.3));
	cpt.insert(CondProb<>::CondCase(vector<int> {0, 0}, dist));
	dist.insert(make_pair(1, 0.5));
	dist.insert(make_pair(0, 0.5));
	cpt.insert(CondProb<>::CondCase(vector<int> {0, 1}, dist));
	dist.insert(make_pair(1, 0.5));
	dist.insert(make_pair(0, 0.5));
	cpt.insert(CondProb<>::CondCase(vector<int> {1, 0}, dist));
	dist.insert(make_pair(1, 0.3));
	dist.insert(make_pair(0, 0.7));
	cpt.insert(CondProb<>::CondCase(vector<int> {1, 1}, dist));
	bn.add_node(2, {0, 1}, CondProb<int>({1, 2}, cpt));

	cpt.clear();
	dist.clear();
	dist.insert(make_pair(1, 0.7));
	dist.insert(make_pair(0, 0.3));
	cpt.insert(CondProb<>::CondCase(vector<int> {0}, dist));
	dist.insert(make_pair(1, 0.5));
	dist.insert(make_pair(0, 0.5));
	cpt.insert(CondProb<>::CondCase(vector<int> {1}, dist));
	bn.add_node(3, {2}, CondProb<int>({2}, cpt));

	set<int> blanket = bn.markov_blanket(2);
	assertEquals(blanket, set<int> {2, 0, 1, 3});
}
Esempio n. 3
0
void canSample() {
	BayesNet<> bn;

	map<vector<int>, map<int, double>> cpt;
	map<int, double> dist;
	dist.insert(make_pair(0, 1.0));
	dist.insert(make_pair(1, 0.0));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(0, CondProb<>(cpt));

	assertEquals(bn.sample_node(0), 0);

	cpt.clear();
	dist.clear();
	dist.insert(make_pair(0, 0.0));
	dist.insert(make_pair(1, 1.0));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(1, CondProb<>(cpt));

	assertEquals(bn.sample_node(1), 1);
}
Esempio n. 4
0
void canMarginalizeNode() {
	BayesNet<> bn;

	map<vector<int>, map<int, double>> cpt;
	map<int, double> dist;
	dist.insert(make_pair(0, 0.7));
	dist.insert(make_pair(1, 0.3));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(0, CondProb<>(cpt));

	map<int, double> marginal_dist = bn.marginal_dist(0, 512);
	assertTrue(marginal_dist[0] > 0.65 && marginal_dist[0] < 0.75);
	assertTrue(marginal_dist[1] > 0.25 && marginal_dist[1] < 0.35);
}
Esempio n. 5
0
void canAverage() {
	BayesNet<> bn;

	map<vector<int>, map<int, double>> cpt;
	map<int, double> dist;
	dist.insert(make_pair(0, 1.0));
	dist.insert(make_pair(1, 0.0));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(0, CondProb<>(cpt));

	cpt.clear();
	dist.clear();
	dist.insert(make_pair(0, 1.0));
	dist.insert(make_pair(1, 0.0));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(1, CondProb<>(cpt));

	cpt.clear();
	dist.clear();
	dist.insert(make_pair(1, 0.7));
	dist.insert(make_pair(0, 0.3));
	cpt.insert(CondProb<>::CondCase(vector<int> {0, 0}, dist));
	dist.insert(make_pair(1, 0.5));
	dist.insert(make_pair(0, 0.5));
	cpt.insert(CondProb<>::CondCase(vector<int> {0, 1}, dist));
	dist.insert(make_pair(1, 0.5));
	dist.insert(make_pair(0, 0.5));
	cpt.insert(CondProb<>::CondCase(vector<int> {1, 0}, dist));
	dist.insert(make_pair(1, 0.3));
	dist.insert(make_pair(0, 0.7));
	cpt.insert(CondProb<>::CondCase(vector<int> {1, 1}, dist));
	bn.add_node(2, {0, 1}, CondProb<int>({1, 2}, cpt));

	double ev = bn.average_value(2, 1024);
	assertTrue(ev > 0.6 && ev < 0.8);
}
Esempio n. 6
0
void canMarginalizeNetwork() {
	BayesNet<> bn;

	map<vector<int>, map<int, double>> cpt;
	map<int, double> dist;
	dist.insert(make_pair(0, 0.7));
	dist.insert(make_pair(1, 0.3));
	cpt.insert(CondProb<>::CondCase(vector<int>(), dist));
	bn.add_node(0, CondProb<>(cpt));

	map<int, int> values;
	values.insert(make_pair(0, 0));
	map<std::map<int, int>, double> marginal_dist = bn.marginal_dist(values, 512, SampleStrategy::GIBBS);
	assertTrue(marginal_dist[values] > 0.65 && marginal_dist[values] < 0.75);
}