Search in sources :

Example 1 with EdgeListGraph

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

the class SpecialGraphClark method createGraph.

@Override
public Graph createGraph(Parameters parameters) {
    Node x = new ContinuousVariable("X");
    Node y = new ContinuousVariable("Y");
    Node z = new ContinuousVariable("Z");
    Graph g = new EdgeListGraph();
    g.addNode(x);
    g.addNode(y);
    g.addNode(z);
    // g.addDirectedEdge(x, y);
    // g.addDirectedEdge(z, x);
    // g.addDirectedEdge(z, y);
    g.addDirectedEdge(x, y);
    g.addDirectedEdge(x, z);
    g.addDirectedEdge(y, z);
    // 
    return g;
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph)

Example 2 with EdgeListGraph

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

the class TestDM method test2.

@Test
public void test2() {
    // setting seed for debug.
    RandomUtil.getInstance().setSeed(29483818483L);
    Graph graph = emptyGraph(8);
    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("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"));
    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 });
    search.setOutputs(new int[] { 2, 3, 6, 7 });
    search.setData(data);
    search.setTrueInputs(search.getInputs());
    Graph foundGraph = search.search();
    print("Test Case 2");
    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.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"));
    // trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X4"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("X5"), new ContinuousVariable("L1"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L1"), new ContinuousVariable("X6"));
    trueGraph.addDirectedEdge(new ContinuousVariable("L1"), 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 3 with EdgeListGraph

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

the class GlassoRunner method execute.

// ===================PUBLIC METHODS OVERRIDING ABSTRACT================//
public void execute() {
    Object dataModel = getDataModel();
    Parameters params = getParams();
    if (dataModel instanceof DataSet) {
        DataSet dataSet = (DataSet) dataModel;
        DoubleMatrix2D cov = new DenseDoubleMatrix2D(dataSet.getCovarianceMatrix().toArray());
        Glasso glasso = new Glasso(cov);
        glasso.setMaxit((int) params.get("maxit", 10000));
        glasso.setIa(params.getBoolean("ia", false));
        glasso.setIs(params.getBoolean("is", false));
        glasso.setItr(params.getBoolean("itr", false));
        glasso.setIpen(params.getBoolean("ipen", false));
        glasso.setThr(params.getDouble("thr", 1e-4));
        glasso.setRhoAllEqual(1.0);
        Glasso.Result result = glasso.search();
        TetradMatrix wwi = new TetradMatrix(result.getWwi().toArray());
        List<Node> variables = dataSet.getVariables();
        Graph resultGraph = new EdgeListGraph(variables);
        for (int i = 0; i < variables.size(); i++) {
            for (int j = i + 1; j < variables.size(); j++) {
                if (wwi.get(i, j) != 0.0 && wwi.get(i, j) != 0.0) {
                    resultGraph.addUndirectedEdge(variables.get(i), variables.get(j));
                }
            }
        }
        setResultGraph(resultGraph);
    }
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 4 with EdgeListGraph

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

the class LoadContinuousDataSmithSim method readGraph.

public Graph readGraph(File file) {
    try {
        DataReader reader = new DataReader();
        reader.setVariablesSupplied(false);
        reader.setDelimiter(DelimiterType.COMMA);
        DataSet data = reader.parseTabular(file);
        List<Node> variables = data.getVariables();
        Graph graph = new EdgeListGraph(variables);
        for (int i = 0; i < variables.size(); i++) {
            for (int j = 0; j < variables.size(); j++) {
                if (i == j)
                    continue;
                if (data.getDouble(i, j) != 0) {
                    graph.addDirectedEdge(variables.get(i), variables.get(j));
                }
            }
        }
        return graph;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) IOException(java.io.IOException)

Example 5 with EdgeListGraph

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

the class WGfci method search.

public Graph search() {
    test.setAlpha(alpha);
    Graph g = gfci.search();
    Graph out = new EdgeListGraph(searchVariables);
    for (int i = 0; i < searchVariables.size(); i++) {
        for (int j = i + 1; j < searchVariables.size(); j++) {
            Node x = searchVariables.get(i);
            Node y = searchVariables.get(j);
            List<Node> xNodes = variablesPerNode.get(x);
            List<Node> yNodes = variablesPerNode.get(y);
            int left = 0;
            int right = 0;
            int total = 0;
            for (int k = 0; k < xNodes.size(); k++) {
                for (int l = 0; l < yNodes.size(); l++) {
                    Edge e = g.getEdge(xNodes.get(k), yNodes.get(l));
                    if (e != null) {
                        total++;
                        if (e.pointsTowards(xNodes.get(k)))
                            left++;
                        if (e.pointsTowards(yNodes.get(l)))
                            right++;
                    }
                }
            }
            if (total > 0) {
                if (left == total)
                    out.addDirectedEdge(y, x);
                else if (right == total)
                    out.addDirectedEdge(x, y);
                else
                    out.addUndirectedEdge(x, y);
            }
        }
    }
    return out;
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Edge(edu.cmu.tetrad.graph.Edge)

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