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 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. 3
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);
}