Search in sources :

Example 16 with SemPm

use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.

the class TestDeltaTetradTest method makePm.

private SemPm makePm() {
    List<Node> variableNodes = new ArrayList<>();
    ContinuousVariable x1 = new ContinuousVariable("X1");
    ContinuousVariable x2 = new ContinuousVariable("X2");
    ContinuousVariable x3 = new ContinuousVariable("X3");
    ContinuousVariable x4 = new ContinuousVariable("X4");
    ContinuousVariable x5 = new ContinuousVariable("X5");
    variableNodes.add(x1);
    variableNodes.add(x2);
    variableNodes.add(x3);
    variableNodes.add(x4);
    variableNodes.add(x5);
    Graph _graph = new EdgeListGraph(variableNodes);
    SemGraph graph = new SemGraph(_graph);
    graph.addDirectedEdge(x5, x1);
    graph.addDirectedEdge(x5, x2);
    graph.addDirectedEdge(x5, x3);
    graph.addDirectedEdge(x5, x4);
    return new SemPm(graph);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) ArrayList(java.util.ArrayList) SemPm(edu.cmu.tetrad.sem.SemPm)

Example 17 with SemPm

use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.

the class TestDeltaTetradTest method getFigure4aSem.

private SemIm getFigure4aSem() {
    Graph graph = new EdgeListGraph();
    Node xi1 = new GraphNode("xi1");
    Node x1 = new GraphNode("x1");
    Node x2 = new GraphNode("x2");
    Node x3 = new GraphNode("x3");
    Node x4 = new GraphNode("x4");
    graph.addNode(xi1);
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.addNode(x4);
    graph.addDirectedEdge(xi1, x1);
    graph.addDirectedEdge(xi1, x2);
    graph.addDirectedEdge(xi1, x3);
    graph.addDirectedEdge(xi1, x4);
    SemPm pm = new SemPm(graph);
    Parameters params = new Parameters();
    return new SemIm(pm, params);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 18 with SemPm

use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.

the class TestFci method testSearch15.

@Test
public void testSearch15() {
    int numVars = 80;
    int numEdges = 80;
    int sampleSize = 1000;
    boolean latentDataSaved = false;
    int numLatents = 40;
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < numVars; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Dag trueGraph = new Dag(GraphUtils.randomGraph(nodes, numLatents, numEdges, 7, 5, 5, false));
    SemPm bayesPm = new SemPm(trueGraph);
    SemIm bayesIm = new SemIm(bayesPm);
    DataSet dataSet = bayesIm.simulateData(sampleSize, latentDataSaved);
    IndependenceTest test = new IndTestFisherZ(dataSet, 0.05);
    Cfci search = new Cfci(test);
    // Run search
    search.search();
}
Also used : ArrayList(java.util.ArrayList) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 19 with SemPm

use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.

the class TestFci method testFciAnc.

// @Test
public void testFciAnc() {
    int numMeasures = 50;
    double edgeFactor = 2.0;
    int numRuns = 10;
    double alpha = 0.01;
    double penaltyDiscount = 4.0;
    int numVarsToMarginalize = 5;
    int numLatents = 10;
    System.out.println("num measures = " + numMeasures);
    System.out.println("edge factor = " + edgeFactor);
    System.out.println("alpha = " + alpha);
    System.out.println("penaltyDiscount = " + penaltyDiscount);
    System.out.println("num runs = " + numRuns);
    System.out.println("num vars to marginalize = " + numVarsToMarginalize);
    System.out.println("num latents = " + numLatents);
    System.out.println();
    for (int i = 0; i < numRuns; i++) {
        int numEdges = (int) (edgeFactor * (numMeasures + numLatents));
        List<Node> nodes = new ArrayList<>();
        for (int r = 0; r < numMeasures + numLatents; r++) {
            String name = "X" + (r + 1);
            nodes.add(new ContinuousVariable(name));
        }
        Graph dag = GraphUtils.randomGraphRandomForwardEdges(nodes, numLatents, numEdges, 10, 10, 10, false);
        SemPm pm = new SemPm(dag);
        SemIm im = new SemIm(pm);
        DataSet data = im.simulateData(1000, false);
        Graph pag = getPag(alpha, penaltyDiscount, data);
        DataSet marginalData = data.copy();
        List<Node> variables = marginalData.getVariables();
        Collections.shuffle(variables);
        for (int m = 0; m < numVarsToMarginalize; m++) {
            marginalData.removeColumn(marginalData.getColumn(variables.get(m)));
        }
        Graph margPag = getPag(alpha, penaltyDiscount, marginalData);
        int ancAnc = 0;
        int ancNanc = 0;
        int nancAnc = 0;
        int nancNanc = 0;
        int ambAnc = 0;
        int ambNanc = 0;
        int totalAncMarg = 0;
        int totalNancMarg = 0;
        for (Node n1 : marginalData.getVariables()) {
            for (Node n2 : marginalData.getVariables()) {
                if (n1 == n2)
                    continue;
                if (ancestral(n1, n2, margPag)) {
                    if (ancestral(n1, n2, pag)) {
                        ancAnc++;
                    } else if (nonAncestral(n1, n2, pag)) {
                        nancAnc++;
                    } else {
                        ambAnc++;
                    }
                    totalAncMarg++;
                } else if (nonAncestral(n1, n2, margPag)) {
                    if (ancestral(n1, n2, pag)) {
                        ancNanc++;
                    } else if (nonAncestral(n1, n2, pag)) {
                        nancNanc++;
                    } else {
                        ambNanc++;
                    }
                    totalNancMarg++;
                }
            }
        }
        // {
        // TextTable table = new TextTable(5, 3);
        // table.setToken(0, 1, "Ancestral");
        // table.setToken(0, 2, "Nonancestral");
        // table.setToken(1, 0, "Ancestral");
        // table.setToken(2, 0, "Nonancestral");
        // table.setToken(3, 0, "Ambiguous");
        // table.setToken(4, 0, "Total");
        // 
        // table.setToken(1, 1, ancAnc + "");
        // table.setToken(2, 1, nancAnc + "");
        // table.setToken(3, 1, ambAnc + "");
        // table.setToken(1, 2, ancNanc + "");
        // table.setToken(2, 2, nancNanc + "");
        // table.setToken(3, 2, ambNanc + "");
        // table.setToken(4, 1, totalAncMarg + "");
        // table.setToken(4, 2, totalNancMarg + "");
        // 
        // System.out.println(table);
        // }
        {
            TextTable table = new TextTable(5, 3);
            table.setToken(0, 1, "Ancestral");
            table.setToken(0, 2, "Nonancestral");
            table.setToken(1, 0, "Ancestral");
            table.setToken(2, 0, "Nonancestral");
            table.setToken(3, 0, "Ambiguous");
            table.setToken(4, 0, "Total");
            NumberFormat nf = new DecimalFormat("0.00");
            table.setToken(1, 1, nf.format(ancAnc / (double) totalAncMarg) + "");
            table.setToken(2, 1, nf.format(nancAnc / (double) totalAncMarg) + "");
            table.setToken(3, 1, nf.format(ambAnc / (double) totalAncMarg) + "");
            table.setToken(1, 2, nf.format(ancNanc / (double) totalNancMarg) + "");
            table.setToken(2, 2, nf.format(nancNanc / (double) totalNancMarg) + "");
            table.setToken(3, 2, nf.format(ambNanc / (double) totalNancMarg) + "");
            table.setToken(4, 1, totalAncMarg + "");
            table.setToken(4, 2, totalNancMarg + "");
            System.out.println(table);
        }
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) TextTable(edu.cmu.tetrad.util.TextTable) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) NumberFormat(java.text.NumberFormat)

Example 20 with SemPm

use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.

the class TestSemProposition method testEvidence.

@Test
public void testEvidence() {
    Graph graph = constructGraph1();
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    List nodes = semIm.getVariableNodes();
    SemProposition proposition = SemProposition.tautology(semIm);
    for (int i = 0; i < semIm.getVariableNodes().size(); i++) {
        assertTrue(Double.isNaN(proposition.getValue(i)));
    }
    proposition.setValue(1, 0.5);
    assertEquals(0.5, proposition.getValue(1), 0.0);
    Node node4 = (Node) nodes.get(3);
    proposition.setValue(node4, 0.7);
    assertEquals(0.7, proposition.getValue(node4), 0.0);
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) SemProposition(edu.cmu.tetrad.sem.SemProposition) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) SemPm(edu.cmu.tetrad.sem.SemPm) List(java.util.List) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Aggregations

SemPm (edu.cmu.tetrad.sem.SemPm)77 SemIm (edu.cmu.tetrad.sem.SemIm)71 Test (org.junit.Test)44 ArrayList (java.util.ArrayList)29 DataSet (edu.cmu.tetrad.data.DataSet)28 Graph (edu.cmu.tetrad.graph.Graph)25 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)18 Node (edu.cmu.tetrad.graph.Node)18 SemEstimator (edu.cmu.tetrad.sem.SemEstimator)16 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)15 Dag (edu.cmu.tetrad.graph.Dag)10 DMSearch (edu.cmu.tetrad.search.DMSearch)9 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)9 NumberFormat (java.text.NumberFormat)7 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)6 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5 GraphNode (edu.cmu.tetrad.graph.GraphNode)5 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)4 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)4 Parameters (edu.cmu.tetrad.util.Parameters)4