use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestBayesIm method testAddRemoveValues.
/**
* Tests whether the BayesIm does the right thing in a very simple case
* where values of a nodes are added or removed from the BayesPm. Start with
* graph a -> b <- c, construct and fill in probability tables in BayesIm.
* Then add edge c -> b "manually." This should create a table of values for
* c that is unspecified, and it should double up the rows from b. Then
* remove the node c. Now the table for b should be completely unspecified.
*/
@Test
public void testAddRemoveValues() {
Node a = new GraphNode("a");
Node b = new GraphNode("b");
Node c = new GraphNode("c");
Dag dag = new Dag();
dag.addNode(a);
dag.addNode(b);
dag.addNode(c);
dag.addDirectedEdge(a, b);
dag.addDirectedEdge(c, b);
assertTrue(Edges.isDirectedEdge(dag.getEdge(a, b)));
BayesPm bayesPm = new BayesPm(dag, 3, 3);
BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
bayesPm.setNumCategories(a, 4);
bayesPm.setNumCategories(c, 4);
BayesIm bayesIm2 = new MlBayesIm(bayesPm, bayesIm, MlBayesIm.MANUAL);
bayesPm.setNumCategories(a, 2);
BayesIm bayesIm3 = new MlBayesIm(bayesPm, bayesIm2, MlBayesIm.MANUAL);
bayesPm.setNumCategories(b, 2);
BayesIm bayesIm4 = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
for (int node = 0; node < bayesIm4.getNumNodes(); node++) {
for (int row = 0; row < bayesIm4.getNumRows(node); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(node); col++) {
bayesIm4.setProbability(node, row, col, Double.NaN);
}
}
}
double[][] aTable = { { .2, .8 } };
double[][] bTable = { { .1, .9 }, { .7, .3 }, { .3, .7 }, { .5, .5 }, { .09, .91 }, { .6, .4 }, { .2, .8 }, { .8, .2 } };
double[][] cTable = { { .1, .2, .3, .4 } };
int _a = bayesIm.getNodeIndex(a);
for (int row = 0; row < bayesIm4.getNumRows(_a); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(_a); col++) {
bayesIm4.setProbability(_a, row, col, aTable[row][col]);
}
}
int _b = bayesIm.getNodeIndex(b);
for (int row = 0; row < bayesIm4.getNumRows(_b); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(_b); col++) {
bayesIm4.setProbability(_b, row, col, bTable[row][col]);
}
}
int _c = bayesIm.getNodeIndex(c);
for (int row = 0; row < bayesIm4.getNumRows(_c); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(_c); col++) {
bayesIm4.setProbability(_c, row, col, cTable[row][col]);
}
}
}
use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestBayesBicScorer method testPValue.
@Test
public void testPValue() {
RandomUtil.getInstance().setSeed(492834924L);
// Graph graph = GraphConverter.convert("X1,X2,X4,X4,X5,X6,X7,X8");
Graph graph = GraphConverter.convert("X1-->X2,X2-->X3,X3-->X6,X6-->X7");
// Graph graph2 = GraphConverter.convert("X1,X2,X3,X7-->X6,X9,X10,X11,X12");
int numCategories = 8;
BayesPm pm = new BayesPm(graph, numCategories, numCategories);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(1000, false);
BayesProperties scorer = new BayesProperties(data);
double lik = scorer.getLikelihoodRatioP(graph);
assertEquals(1, lik, 0.001);
}
use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestBayesPm method testEquals.
@Test
public void testEquals() {
Graph graph = GraphConverter.convert("X1-->X2,X1-->X3,X2-->X4,X3-->X4");
Dag dag = new Dag(graph);
BayesPm bayesPm = new BayesPm(dag, 3, 3);
assertEquals(bayesPm, bayesPm);
}
Aggregations