Search in sources :

Example 86 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable 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 87 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable 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)

Example 88 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestDagInPatternIterator method test5.

@Test
public void test5() {
    RandomUtil.getInstance().setSeed(34828384L);
    List<Node> nodes1 = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        nodes1.add(new ContinuousVariable("X" + (i + 1)));
    }
    Dag dag1 = new Dag(GraphUtils.randomGraph(nodes1, 0, 3, 30, 15, 15, false));
    Graph pattern = SearchGraphUtils.patternForDag(dag1);
    List<Node> nodes = pattern.getNodes();
    // Make random knowedge.
    int numTiers = 6;
    IKnowledge knowledge = new Knowledge2();
    for (Node node : nodes) {
        int tier = RandomUtil.getInstance().nextInt(numTiers);
        if (tier < 2)
            continue;
        knowledge.addToTier(tier, node.getName());
    }
    if (!knowledge.isViolatedBy(pattern)) {
        DagInPatternIterator iterator1 = new DagInPatternIterator(pattern);
        Graph dag0 = null;
        while (iterator1.hasNext()) {
            Graph dag = iterator1.next();
            if (!knowledge.isViolatedBy(dag)) {
                dag0 = dag;
            }
        }
        if (dag0 == null) {
            fail("Inconsistent knowledge.");
        }
    }
    if (!knowledge.isViolatedBy(pattern)) {
        DagInPatternIterator iterator2 = new DagInPatternIterator(pattern, knowledge);
        while (iterator2.hasNext()) {
            Graph dag = iterator2.next();
            if (knowledge.isViolatedBy(dag)) {
                throw new IllegalArgumentException("Knowledge violated");
            }
        }
    }
    DagInPatternIterator iterator3 = new DagInPatternIterator(pattern);
    int count = 0;
    while (iterator3.hasNext()) {
        iterator3.next();
        count++;
    }
    assertEquals(6, count);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) IKnowledge(edu.cmu.tetrad.data.IKnowledge) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2) DagInPatternIterator(edu.cmu.tetrad.search.DagInPatternIterator) Test(org.junit.Test)

Example 89 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestColtDataSet method testRemoveColumn.

@Test
public void testRemoveColumn() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        variables.add(new ContinuousVariable("X" + i));
    }
    DataSet dataSet = new ColtDataSet(rows, variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    int[] _cols = new int[2];
    _cols[0] = 1;
    _cols[1] = 2;
    dataSet.removeCols(_cols);
    List<Node> _variables = new LinkedList<>(variables);
    _variables.remove(2);
    _variables.remove(1);
    assertEquals(dataSet.getVariables(), _variables);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 90 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestColtDataSet method testRowSubset.

@Test
public void testRowSubset() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        variables.add(new ContinuousVariable("X" + i));
    }
    DataSet dataSet = new ColtDataSet(rows, variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    double d = dataSet.getDouble(2, 0);
    DataSet _dataSet = dataSet.subsetRows(new int[] { 2, 3, 4 });
    assertEquals(3, _dataSet.getNumRows());
    assertEquals(d, _dataSet.getDouble(0, 0), 0.001);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)91 DataSet (edu.cmu.tetrad.data.DataSet)48 Node (edu.cmu.tetrad.graph.Node)46 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 Graph (edu.cmu.tetrad.graph.Graph)22 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)19 SemPm (edu.cmu.tetrad.sem.SemPm)18 SemIm (edu.cmu.tetrad.sem.SemIm)16 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)15 LinkedList (java.util.LinkedList)13 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)12 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)8 DMSearch (edu.cmu.tetrad.search.DMSearch)7 Dag (edu.cmu.tetrad.graph.Dag)6 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)5 RandomUtil (edu.cmu.tetrad.util.RandomUtil)5 ParseException (java.text.ParseException)4 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)3 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)3