Search in sources :

Example 31 with EdgeListGraph

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

the class RegressionRunner method execute.

// =================PUBLIC METHODS OVERRIDING ABSTRACT=================//
/**
 * Executes the algorithm, producing (at least) a result workbench. Must be
 * implemented in the extending class.
 */
public void execute() {
    if (regressorNames.size() == 0 || targetName == null) {
        outGraph = new EdgeListGraph();
        return;
    }
    if (regressorNames.contains(targetName)) {
        outGraph = new EdgeListGraph();
        return;
    }
    Regression regression;
    Node target;
    List<Node> regressors;
    if (getDataModel() instanceof DataSet) {
        DataSet _dataSet = (DataSet) getDataModel();
        regression = new RegressionDataset(_dataSet);
        target = _dataSet.getVariable(targetName);
        regressors = new LinkedList<>();
        for (String regressorName : regressorNames) {
            regressors.add(_dataSet.getVariable(regressorName));
        }
        double alpha = params.getDouble("alpha", 0.001);
        regression.setAlpha(alpha);
        result = regression.regress(target, regressors);
        outGraph = regression.getGraph();
    } else if (getDataModel() instanceof ICovarianceMatrix) {
        ICovarianceMatrix covariances = (ICovarianceMatrix) getDataModel();
        regression = new RegressionCovariance(covariances);
        target = covariances.getVariable(targetName);
        regressors = new LinkedList<>();
        for (String regressorName : regressorNames) {
            regressors.add(covariances.getVariable(regressorName));
        }
        double alpha = params.getDouble("alpha", 0.001);
        regression.setAlpha(alpha);
        result = regression.regress(target, regressors);
        outGraph = regression.getGraph();
    }
    setResultGraph(outGraph);
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance)

Example 32 with EdgeListGraph

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

the class DagIterator method next.

/**
 * Successive calls to this method return successive DAGs in the pattern, in a more or less natural enumeration of
 * them in which an arbitrary undirected edge is picked, oriented one way, Meek rules applied, then a remaining
 * unoriented edge is picked, oriented one way, and so on, until a DAG is obtained, and then by backtracking the
 * other orientation of each chosen edge is tried. Nonrecursive, obviously.
 * <p>
 *
 * @return a Graph instead of a DAG because sometimes, due to faulty patterns, a cyclic graph is produced, and the
 * end-user may need to decide what to do with it. The simplest thing is to construct a DAG (Dag(graph)) and catch
 * an exception.
 */
public Graph next() {
    if (storedDag != null) {
        Graph temp = storedDag;
        storedDag = null;
        return temp;
    }
    if (decoratedGraphs.isEmpty()) {
        return null;
    }
    // If it's a DAG
    if (!decoratedGraphs.getLast().hasUndirectedEdge()) {
        // oriented right, and add a graph with that oriented left.
        while (true) {
            if (decoratedGraphs.isEmpty()) {
                return null;
            }
            DecoratedGraph graph = decoratedGraphs.removeLast();
            if (graph.hasUndirectedEdge() && !graph.wasDirectedRight()) {
                throw new IllegalStateException();
            }
            if (decoratedGraphs.isEmpty() && !graph.hasUndirectedEdge()) {
                return new EdgeListGraph(graph.getGraph());
            }
            if (graph.wasDirectedRight() && !graph.wasDirectedLeft()) {
                decoratedGraphs.add(graph);
                DecoratedGraph graph1 = graph.directLeft();
                if (graph1 == null) {
                    continue;
                }
                decoratedGraphs.add(graph1);
                break;
            }
        }
    }
    // Apply right orientations and Meek orientations until there's a DAG.
    while (decoratedGraphs.getLast().hasUndirectedEdge()) {
        DecoratedGraph graph = decoratedGraphs.getLast().directRight();
        if (graph == null) {
            continue;
        }
        decoratedGraphs.add(graph);
    }
    // Return the DAG.
    return new EdgeListGraph(decoratedGraphs.getLast().getGraph());
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph)

Example 33 with EdgeListGraph

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

the class TestSem method constructGraph2.

private Graph constructGraph2() {
    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");
    Node x6 = new GraphNode("X6");
    Node x7 = new GraphNode("X7");
    Node x8 = new GraphNode("X8");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.addNode(x4);
    graph.addNode(x5);
    graph.addNode(x6);
    graph.addNode(x7);
    graph.addNode(x8);
    graph.addDirectedEdge(x1, x2);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x2, x6);
    graph.addDirectedEdge(x3, x7);
    graph.addDirectedEdge(x4, x5);
    graph.addDirectedEdge(x4, x6);
    graph.addDirectedEdge(x5, x3);
    graph.addDirectedEdge(x6, x8);
    graph.addDirectedEdge(x6, x7);
    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 34 with EdgeListGraph

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

the class TestSem 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 35 with EdgeListGraph

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

the class TestSemUpdater 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)

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