Search in sources :

Example 36 with GraphNode

use of edu.cmu.tetrad.graph.GraphNode in project tetrad by cmu-phil.

the class TestUpdaterJointMarginal method testEstimate1.

@Test
public void testEstimate1() {
    Dag graph = new Dag();
    Node L1 = new GraphNode("L1");
    Node X1 = new GraphNode("X1");
    Node X2 = new GraphNode("X2");
    Node X3 = new GraphNode("X3");
    L1.setNodeType(NodeType.MEASURED);
    X1.setNodeType(NodeType.MEASURED);
    X2.setNodeType(NodeType.MEASURED);
    X3.setNodeType(NodeType.MEASURED);
    graph.addNode(L1);
    graph.addNode(X1);
    graph.addNode(X2);
    graph.addNode(X3);
    graph.addDirectedEdge(L1, X1);
    graph.addDirectedEdge(L1, X2);
    graph.addDirectedEdge(L1, X3);
    BayesPm bayesPm = new BayesPm(graph);
    bayesPm.setNumCategories(L1, 2);
    bayesPm.setNumCategories(X1, 2);
    bayesPm.setNumCategories(X2, 2);
    bayesPm.setNumCategories(X3, 2);
    BayesIm estimatedIm = new MlBayesIm(bayesPm);
    Node l1Node = graph.getNode("L1");
    // int l1Index = bayesImMixed.getNodeIndex(l1Node);
    int l1Index = estimatedIm.getNodeIndex(l1Node);
    Node x1Node = graph.getNode("X1");
    // int x1Index = bayesImMixed.getNodeIndex(x1Node);
    int x1Index = estimatedIm.getNodeIndex(x1Node);
    Node x2Node = graph.getNode("X2");
    // int x2Index = bayesImMixed.getNodeIndex(x2Node);
    int x2Index = estimatedIm.getNodeIndex(x2Node);
    Node x3Node = graph.getNode("X3");
    // int x3Index = bayesImMixed.getNodeIndex(x3Node);
    int x3Index = estimatedIm.getNodeIndex(x3Node);
    // bayesImMixed.setProbability(l1Index, 0, 0, 0.5);
    // bayesImMixed.setProbability(l1Index, 0, 1, 0.5);
    estimatedIm.setProbability(l1Index, 0, 0, 0.5);
    estimatedIm.setProbability(l1Index, 0, 1, 0.5);
    // bayesImMixed.setProbability(x1Index, 0, 0, 0.33333);
    // bayesImMixed.setProbability(x1Index, 0, 1, 0.66667);
    estimatedIm.setProbability(x1Index, 0, 0, // p(x1 = 0 | l1 = 0)
    0.33333);
    estimatedIm.setProbability(x1Index, 0, 1, // p(x1 = 1 | l1 = 0)
    0.66667);
    estimatedIm.setProbability(x1Index, 1, 0, // p(x1 = 0 | l1 = 1)
    0.66667);
    estimatedIm.setProbability(x1Index, 1, 1, // p(x1 = 1 | l1 = 1)
    0.33333);
    // bayesImMixed.setProbability(x2Index, 1, 0, 0.66667);
    // bayesImMixed.setProbability(x2Index, 1, 1, 0.33333);
    estimatedIm.setProbability(x2Index, 1, 0, // p(x2 = 0 | l1 = 1)
    0.66667);
    estimatedIm.setProbability(x2Index, 1, 1, // p(x2 = 1 | l1 = 1)
    0.33333);
    estimatedIm.setProbability(x2Index, 0, 0, // p(x2 = 0 | l1 = 0)
    0.33333);
    estimatedIm.setProbability(x2Index, 0, 1, // p(x2 = 1 | l1 = 0)
    0.66667);
    // bayesImMixed.setProbability(x3Index, 1, 0, 0.66667);
    // bayesImMixed.setProbability(x3Index, 1, 1, 0.33333);
    estimatedIm.setProbability(x3Index, 1, 0, // p(x3 = 0 | l1 = 1)
    0.66667);
    estimatedIm.setProbability(x3Index, 1, 1, // p(x3 = 1 | l1 = 1)
    0.33333);
    estimatedIm.setProbability(x3Index, 0, 0, // p(x3 = 0 | l1 = 0)
    0.33333);
    estimatedIm.setProbability(x3Index, 0, 1, // p(x3 = 1 | l1 = 0)
    0.66667);
    Evidence evidence = Evidence.tautology(estimatedIm);
    evidence.getProposition().setCategory(x1Index, 0);
    evidence.getProposition().setCategory(x2Index, 0);
    evidence.getProposition().setCategory(x3Index, 0);
    RowSummingExactUpdater rseu = new RowSummingExactUpdater(estimatedIm);
    rseu.setEvidence(evidence);
    int[] vars1 = { l1Index };
    int[] vals1 = { 0 };
    double p1 = rseu.getJointMarginal(vars1, vals1);
    assertEquals(0.1111, p1, 0.0001);
    int[] vars2 = { l1Index, x1Index };
    int[] vals2 = { 0, 0 };
    double p2 = rseu.getJointMarginal(vars2, vals2);
    assertEquals(0.1111, p2, 0.0001);
    int[] vals3 = { 1, 0 };
    double p3 = rseu.getJointMarginal(vars2, vals3);
    assertEquals(0.8888, p3, 0.0001);
}
Also used : GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) Dag(edu.cmu.tetrad.graph.Dag) Test(org.junit.Test)

Example 37 with GraphNode

use of edu.cmu.tetrad.graph.GraphNode in project tetrad by cmu-phil.

the class TestModelGenerator method testGenerate.

@Test
public void testGenerate() {
    Node x1 = new GraphNode("x1");
    Node x2 = new GraphNode("x2");
    Node x3 = new GraphNode("x3");
    Node x4 = new GraphNode("x4");
    Node x5 = new GraphNode("x5");
    // graph = new EdgeListGraph();
    Graph graph = new EdgeListGraph();
    graph.clear();
    // Add and remove some nodes.
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.addNode(x4);
    graph.addNode(x5);
    graph.addDirectedEdge(x1, x2);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x3, x4);
    List<Graph> variants1 = ModelGenerator.generate(graph);
    assertEquals(17, variants1.size());
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) GraphNode(edu.cmu.tetrad.graph.GraphNode) Test(org.junit.Test)

Example 38 with GraphNode

use of edu.cmu.tetrad.graph.GraphNode in project tetrad by cmu-phil.

the class TestIndependenceFact method testSimpleCase.

@Test
public void testSimpleCase() {
    Node x = new GraphNode("X");
    Node y = new GraphNode("Y");
    Node w = new GraphNode("W");
    IndependenceFact fact1 = new IndependenceFact(x, y);
    IndependenceFact fact2 = new IndependenceFact(y, x);
    assertEquals(fact1, fact2);
    IndependenceFact fact3 = new IndependenceFact(x, w);
    assertNotEquals(fact1, fact3);
    List<IndependenceFact> facts = new ArrayList<>();
    facts.add(fact1);
    assertTrue(facts.contains(fact2));
}
Also used : GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) GraphNode(edu.cmu.tetrad.graph.GraphNode) IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact) Test(org.junit.Test)

Example 39 with GraphNode

use of edu.cmu.tetrad.graph.GraphNode in project tetrad by cmu-phil.

the class TestSemProposition method constructGraph1.

private Graph constructGraph1() {
    Graph graph = new EdgeListGraph();
    Node x1 = new GraphNode("X1");
    Node x2 = new GraphNode("X2");
    Node x3 = new GraphNode("X3");
    Node x4 = new GraphNode("X4");
    Node x5 = new GraphNode("X5");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.addNode(x4);
    graph.addNode(x5);
    graph.addDirectedEdge(x1, x2);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x3, x4);
    graph.addDirectedEdge(x1, x4);
    graph.addDirectedEdge(x4, x5);
    return graph;
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) GraphNode(edu.cmu.tetrad.graph.GraphNode)

Example 40 with GraphNode

use of edu.cmu.tetrad.graph.GraphNode in project tetrad by cmu-phil.

the class TestCptInvariantUpdater method testUpdate5.

@Test
public void testUpdate5() {
    Node x0Node = new GraphNode("X0");
    Node x1Node = new GraphNode("X1");
    Node x2Node = new GraphNode("X2");
    Node x3Node = new GraphNode("X3");
    Node x4Node = new GraphNode("X4");
    Dag graph = new Dag();
    graph.addNode(x0Node);
    graph.addNode(x1Node);
    graph.addNode(x2Node);
    graph.addNode(x3Node);
    graph.addNode(x4Node);
    graph.addDirectedEdge(x0Node, x1Node);
    graph.addDirectedEdge(x0Node, x2Node);
    graph.addDirectedEdge(x1Node, x3Node);
    graph.addDirectedEdge(x2Node, x3Node);
    graph.addDirectedEdge(x4Node, x0Node);
    graph.addDirectedEdge(x4Node, x2Node);
    BayesPm bayesPm = new BayesPm(graph);
    MlBayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
    int x1 = bayesIm.getNodeIndex(x1Node);
    int x2 = bayesIm.getNodeIndex(x2Node);
    int x3 = bayesIm.getNodeIndex(x3Node);
    Evidence evidence = Evidence.tautology(bayesIm);
    evidence.getProposition().setCategory(x1, 1);
    evidence.getProposition().setCategory(x2, 0);
    evidence.getNodeIndex("X1");
    BayesUpdater updater1 = new CptInvariantUpdater(bayesIm);
    updater1.setEvidence(evidence);
    BayesUpdater updater2 = new RowSummingExactUpdater(bayesIm);
    updater2.setEvidence(evidence);
    double marginal1 = updater1.getMarginal(x3, 0);
    double marginal2 = updater2.getMarginal(x3, 0);
    assertEquals(marginal1, marginal2, 0.000001);
}
Also used : GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) Dag(edu.cmu.tetrad.graph.Dag) Test(org.junit.Test)

Aggregations

GraphNode (edu.cmu.tetrad.graph.GraphNode)41 Node (edu.cmu.tetrad.graph.Node)37 Dag (edu.cmu.tetrad.graph.Dag)20 Test (org.junit.Test)11 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)7 Graph (edu.cmu.tetrad.graph.Graph)7 DisplayNode (edu.cmu.tetradapp.workbench.DisplayNode)6 BayesIm (edu.cmu.tetrad.bayes.BayesIm)3 BayesPm (edu.cmu.tetrad.bayes.BayesPm)3 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)3 IndependenceFact (edu.cmu.tetrad.graph.IndependenceFact)3 ArrayList (java.util.ArrayList)3 FruchtermanReingoldLayout (edu.cmu.tetrad.graph.FruchtermanReingoldLayout)2 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)1 IndependenceFacts (edu.cmu.tetrad.data.IndependenceFacts)1 Endpoint (edu.cmu.tetrad.graph.Endpoint)1 SemGraph (edu.cmu.tetrad.graph.SemGraph)1 TimeLagGraph (edu.cmu.tetrad.graph.TimeLagGraph)1 IndependenceFactsModel (edu.cmu.tetradapp.model.IndependenceFactsModel)1 BufferedReader (java.io.BufferedReader)1