Search in sources :

Example 41 with EdgeListGraph

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

the class TestDM method test4.

// Three latent fork case
@Test
public void test4() {
    // setting seed for debug.
    RandomUtil.getInstance().setSeed(29483818483L);
    Graph graph = emptyGraph(6);
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X3"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X3"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X4"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X5"));
    graph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("X5"));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(100000, false);
    DMSearch search = new DMSearch();
    search.setInputs(new int[] { 0, 1, 2 });
    search.setOutputs(new int[] { 3, 4, 5 });
    search.setData(data);
    search.setTrueInputs(search.getInputs());
    Graph foundGraph = search.search();
    print("Three Latent Fork Case");
    Graph trueGraph = new EdgeListGraph();
    trueGraph.addNode(new ContinuousVariable("X0"));
    trueGraph.addNode(new ContinuousVariable("X1"));
    trueGraph.addNode(new ContinuousVariable("X2"));
    trueGraph.addNode(new ContinuousVariable("X3"));
    trueGraph.addNode(new ContinuousVariable("X4"));
    trueGraph.addNode(new ContinuousVariable("X5"));
    trueGraph.addNode(new ContinuousVariable("L0"));
    trueGraph.addNode(new ContinuousVariable("L1"));
    trueGraph.addNode(new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L1"), new ContinuousVariable("X4"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L2"), new ContinuousVariable("X5"));
    assertTrue(foundGraph.equals(trueGraph));
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) DMSearch(edu.cmu.tetrad.search.DMSearch) Test(org.junit.Test)

Example 42 with EdgeListGraph

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

the class TestDM method test6.

// Four latent case.
@Test
public void test6() {
    // setting seed for debug.
    RandomUtil.getInstance().setSeed(29483818483L);
    Graph graph = emptyGraph(8);
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X4"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X5"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X5"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X3"), new ContinuousVariable("X7"));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(1000, false);
    DMSearch search = new DMSearch();
    search.setInputs(new int[] { 0, 1, 2, 3 });
    search.setOutputs(new int[] { 4, 5, 6, 7 });
    search.setData(data);
    search.setTrueInputs(search.getInputs());
    Graph foundGraph = search.search();
    print("Four Latent Case");
    print("search.getDmStructure().latentStructToEdgeListGraph(search.getDmStructure())");
    Graph trueGraph = new EdgeListGraph();
    trueGraph.addNode(new ContinuousVariable("X0"));
    trueGraph.addNode(new ContinuousVariable("X1"));
    trueGraph.addNode(new ContinuousVariable("X2"));
    trueGraph.addNode(new ContinuousVariable("X3"));
    trueGraph.addNode(new ContinuousVariable("X4"));
    trueGraph.addNode(new ContinuousVariable("X5"));
    trueGraph.addNode(new ContinuousVariable("X6"));
    trueGraph.addNode(new ContinuousVariable("X7"));
    trueGraph.addNode(new ContinuousVariable("L0"));
    trueGraph.addNode(new ContinuousVariable("L1"));
    trueGraph.addNode(new ContinuousVariable("L2"));
    trueGraph.addNode(new ContinuousVariable("L3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X4"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L1"), new ContinuousVariable("X5"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L2"), new ContinuousVariable("X6"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("L3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X3"), new ContinuousVariable("L3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L3"), new ContinuousVariable("X7"));
    assertTrue(foundGraph.equals(trueGraph));
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) DMSearch(edu.cmu.tetrad.search.DMSearch) Test(org.junit.Test)

Example 43 with EdgeListGraph

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

the class TestDM method test1.

@Test
public void test1() {
    // setting seed for debug.
    RandomUtil.getInstance().setSeed(29483818483L);
    NodeEqualityMode.setEqualityMode(NodeEqualityMode.Type.NAME);
    Graph graph = emptyGraph(4);
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X2"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X3"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X2"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X3"));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(100000, false);
    DMSearch search = new DMSearch();
    search.setInputs(new int[] { 0, 1 });
    search.setOutputs(new int[] { 2, 3 });
    search.setData(data);
    search.setTrueInputs(search.getInputs());
    Graph foundGraph = search.search();
    print("Test Case 1");
    // System.out.println(search.getDmStructure());
    // System.out.println(foundGraph);
    Graph trueGraph = new EdgeListGraph();
    trueGraph.addNode(new ContinuousVariable("X0"));
    trueGraph.addNode(new ContinuousVariable("X1"));
    trueGraph.addNode(new ContinuousVariable("X2"));
    trueGraph.addNode(new ContinuousVariable("X3"));
    trueGraph.addNode(new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X3"));
    assertEquals(trueGraph, foundGraph);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) DMSearch(edu.cmu.tetrad.search.DMSearch) Test(org.junit.Test)

Example 44 with EdgeListGraph

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

the class TestDM method test3.

@Test
public void test3() {
    // setting seed for debug.
    RandomUtil.getInstance().setSeed(29483818483L);
    Graph graph = emptyGraph(12);
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X2"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X3"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X2"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X3"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X10"));
    graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X11"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X10"));
    graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X11"));
    graph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("X6"));
    graph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("X7"));
    graph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("X10"));
    graph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("X11"));
    graph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("X10"));
    graph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("X11"));
    graph.addDirectedEdge(new ContinuousVariable("X8"), new ContinuousVariable("X10"));
    graph.addDirectedEdge(new ContinuousVariable("X8"), new ContinuousVariable("X11"));
    graph.addDirectedEdge(new ContinuousVariable("X9"), new ContinuousVariable("X10"));
    graph.addDirectedEdge(new ContinuousVariable("X9"), new ContinuousVariable("X11"));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(100000, false);
    DMSearch search = new DMSearch();
    search.setInputs(new int[] { 0, 1, 4, 5, 8, 9 });
    search.setOutputs(new int[] { 2, 3, 6, 7, 10, 11 });
    search.setData(data);
    search.setTrueInputs(search.getInputs());
    Graph foundGraph = search.search();
    print("Test Case 3");
    Graph trueGraph = new EdgeListGraph();
    trueGraph.addNode(new ContinuousVariable("X0"));
    trueGraph.addNode(new ContinuousVariable("X1"));
    trueGraph.addNode(new ContinuousVariable("X2"));
    trueGraph.addNode(new ContinuousVariable("X3"));
    trueGraph.addNode(new ContinuousVariable("X4"));
    trueGraph.addNode(new ContinuousVariable("X5"));
    trueGraph.addNode(new ContinuousVariable("X6"));
    trueGraph.addNode(new ContinuousVariable("X7"));
    trueGraph.addNode(new ContinuousVariable("X8"));
    trueGraph.addNode(new ContinuousVariable("X9"));
    trueGraph.addNode(new ContinuousVariable("X10"));
    trueGraph.addNode(new ContinuousVariable("X11"));
    trueGraph.addNode(new ContinuousVariable("L0"));
    trueGraph.addNode(new ContinuousVariable("L1"));
    trueGraph.addNode(new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L1"), new ContinuousVariable("X2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L1"), new ContinuousVariable("X3"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("L2"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L2"), new ContinuousVariable("X6"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L2"), new ContinuousVariable("X7"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X8"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X9"), new ContinuousVariable("L0"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X10"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X11"));
    assertTrue(foundGraph.equals(trueGraph));
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) DMSearch(edu.cmu.tetrad.search.DMSearch) Test(org.junit.Test)

Example 45 with EdgeListGraph

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

the class TestDM method finishRenaming.

public void finishRenaming(int penalty) {
    String currentLine;
    try {
        FileReader file = new FileReader("src/edu/cmu/tetradproj/amurrayw/final_run/renamed_graph_penalty" + penalty + ".r.txt");
        BufferedReader br = new BufferedReader(file);
        String[] varNames = null;
        int nVar = 0;
        int lineNumber = 0;
        Graph graph = new EdgeListGraph();
        while ((currentLine = br.readLine()) != null) {
            // finds/gets variable names and adds to graph. Note that it assumes no whitespace in variable names.
            if (lineNumber == 0) {
                varNames = currentLine.split("\\s+");
                nVar = varNames.length;
                for (String nodeName : varNames) {
                    graph.addNode(new ContinuousVariable(nodeName));
                }
            } else {
                // splits line to single
                String[] adjInfoString = currentLine.split("\\s+");
                for (int i = 0; i < nVar; i++) {
                    ;
                    if (Integer.parseInt(adjInfoString[i]) > 1) {
                        print(adjInfoString[i]);
                    } else if (Integer.parseInt(adjInfoString[i]) < 0) {
                        print(adjInfoString[i]);
                    }
                    // System.out.println(adjInfoString[i]);
                    if (Integer.parseInt(adjInfoString[i]) == 1) {
                        // System.out.println("i");
                        // System.out.println(i);
                        // 
                        // System.out.println("varNames[i]");
                        // System.out.println(varNames[i]);
                        // 
                        // 
                        // System.out.println("lineNumber");
                        // System.out.println(lineNumber);
                        // 
                        // System.out.println("varNames.length");
                        // System.out.println(varNames.length);
                        // 
                        // 
                        // System.out.println("varNames[lineNumber]");
                        // System.out.println(varNames[lineNumber]);
                        graph.addDirectedEdge(graph.getNode(varNames[lineNumber - 1]), graph.getNode(varNames[i]));
                    // graph.addDirectedEdge(graph.getNode(varNames[i]), graph.getNode(varNames[lineNumber]));
                    }
                }
            }
            lineNumber++;
        // System.out.println(currentLine);
        }
        File outfile = new File("src/edu/cmu/tetradproj/amurrayw/final_run/renamed_final_output_" + penalty + "_.txt");
        try {
            FileOutputStream out = new FileOutputStream(outfile);
            PrintStream outStream = new PrintStream(out);
            outStream.println(graph);
        // outStream.println();
        } catch (java.io.FileNotFoundException e) {
            String x = "Can't write to file.";
            print(x);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) IOException(java.io.IOException) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) ContinuousTabularDataFileReader(edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)46 Graph (edu.cmu.tetrad.graph.Graph)36 Node (edu.cmu.tetrad.graph.Node)31 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)11 DataSet (edu.cmu.tetrad.data.DataSet)10 Edge (edu.cmu.tetrad.graph.Edge)8 GraphNode (edu.cmu.tetrad.graph.GraphNode)8 ArrayList (java.util.ArrayList)8 DMSearch (edu.cmu.tetrad.search.DMSearch)6 Test (org.junit.Test)6 SemIm (edu.cmu.tetrad.sem.SemIm)5 SemPm (edu.cmu.tetrad.sem.SemPm)5 List (java.util.List)5 IOException (java.io.IOException)4 RandomGraph (edu.cmu.tetrad.algcomparison.graph.RandomGraph)3 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)3 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)2 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)2 SemBicScore (edu.cmu.tetrad.algcomparison.score.SemBicScore)2 TakesInitialGraph (edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph)2