Search in sources :

Example 36 with BayesPm

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]);
        }
    }
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 37 with BayesPm

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);
}
Also used : BayesProperties(edu.cmu.tetrad.bayes.BayesProperties) Graph(edu.cmu.tetrad.graph.Graph) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) DataSet(edu.cmu.tetrad.data.DataSet) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 38 with BayesPm

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);
}
Also used : BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Aggregations

BayesPm (edu.cmu.tetrad.bayes.BayesPm)38 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)23 BayesIm (edu.cmu.tetrad.bayes.BayesIm)18 Test (org.junit.Test)17 Node (edu.cmu.tetrad.graph.Node)14 Graph (edu.cmu.tetrad.graph.Graph)10 DataSet (edu.cmu.tetrad.data.DataSet)6 Dag (edu.cmu.tetrad.graph.Dag)6 DisplayNode (edu.cmu.tetradapp.workbench.DisplayNode)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 BayesProperties (edu.cmu.tetrad.bayes.BayesProperties)4 LargeScaleSimulation (edu.cmu.tetrad.sem.LargeScaleSimulation)4 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)3 GraphNode (edu.cmu.tetrad.graph.GraphNode)3 Parameters (edu.cmu.tetrad.util.Parameters)3 GeneralBootstrapTest (edu.pitt.dbmi.algo.bootstrap.GeneralBootstrapTest)3 NumberFormat (java.text.NumberFormat)3 RandomGraph (edu.cmu.tetrad.algcomparison.graph.RandomGraph)2 ChiSquare (edu.cmu.tetrad.algcomparison.independence.ChiSquare)2