Search in sources :

Example 11 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class GFci method search.

// ========================PUBLIC METHODS==========================//
public Graph search() {
    long time1 = System.currentTimeMillis();
    List<Node> nodes = getIndependenceTest().getVariables();
    logger.log("info", "Starting FCI algorithm.");
    logger.log("info", "Independence test = " + getIndependenceTest() + ".");
    this.graph = new EdgeListGraphSingleConnections(nodes);
    Fges fges = new Fges(score);
    fges.setKnowledge(getKnowledge());
    fges.setVerbose(verbose);
    fges.setNumPatternsToStore(0);
    fges.setFaithfulnessAssumed(faithfulnessAssumed);
    fges.setMaxDegree(maxDegree);
    fges.setOut(out);
    graph = fges.search();
    Graph fgesGraph = new EdgeListGraphSingleConnections(graph);
    sepsets = new SepsetsGreedy(fgesGraph, independenceTest, null, maxDegree);
    for (Node b : nodes) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        List<Node> adjacentNodes = fgesGraph.getAdjacentNodes(b);
        if (adjacentNodes.size() < 2) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            Node a = adjacentNodes.get(combination[0]);
            Node c = adjacentNodes.get(combination[1]);
            if (graph.isAdjacentTo(a, c) && fgesGraph.isAdjacentTo(a, c)) {
                if (sepsets.getSepset(a, c) != null) {
                    graph.removeEdge(a, c);
                }
            }
        }
    }
    modifiedR0(fgesGraph);
    FciOrient fciOrient = new FciOrient(sepsets);
    fciOrient.setVerbose(verbose);
    fciOrient.setOut(out);
    fciOrient.setKnowledge(getKnowledge());
    fciOrient.setCompleteRuleSetUsed(completeRuleSetUsed);
    fciOrient.setMaxPathLength(maxPathLength);
    fciOrient.doFinalOrientation(graph);
    GraphUtils.replaceNodes(graph, independenceTest.getVariables());
    long time2 = System.currentTimeMillis();
    elapsedTime = time2 - time1;
    graph.setPag(true);
    return graph;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 12 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class GPc method modifiedR0.

// Due to Spirtes.
public void modifiedR0(Graph fgesGraph) {
    graph.reorientAllWith(Endpoint.TAIL);
    pcOrientBk(knowledge, graph, graph.getNodes());
    List<Node> nodes = graph.getNodes();
    for (Node b : nodes) {
        List<Node> adjacentNodes = graph.getAdjacentNodes(b);
        if (adjacentNodes.size() < 2) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            Node a = adjacentNodes.get(combination[0]);
            Node c = adjacentNodes.get(combination[1]);
            if (fgesGraph.isDefCollider(a, b, c)) {
                graph.setEndpoint(a, b, Endpoint.ARROW);
                graph.setEndpoint(c, b, Endpoint.ARROW);
            } else if (fgesGraph.isAdjacentTo(a, c) && !graph.isAdjacentTo(a, c)) {
                List<Node> sepset = sepsets.getSepset(a, c);
                if (sepset != null && !sepset.contains(b)) {
                    graph.setEndpoint(a, b, Endpoint.ARROW);
                    graph.setEndpoint(c, b, Endpoint.ARROW);
                }
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 13 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class FciMax method doNode.

private void doNode(Graph graph, Map<Triple, Double> scores, Node b) {
    List<Node> adjacentNodes = graph.getAdjacentNodes(b);
    if (adjacentNodes.size() < 2) {
        return;
    }
    ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
    int[] combination;
    while ((combination = cg.next()) != null) {
        Node a = adjacentNodes.get(combination[0]);
        Node c = adjacentNodes.get(combination[1]);
        // Skip triples that are shielded.
        if (graph.isAdjacentTo(a, c)) {
            continue;
        }
        List<Node> adja = graph.getAdjacentNodes(a);
        double score = Double.POSITIVE_INFINITY;
        List<Node> S = null;
        DepthChoiceGenerator cg2 = new DepthChoiceGenerator(adja.size(), -1);
        int[] comb2;
        while ((comb2 = cg2.next()) != null) {
            List<Node> s = GraphUtils.asList(comb2, adja);
            independenceTest.isIndependent(a, c, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        List<Node> adjc = graph.getAdjacentNodes(c);
        DepthChoiceGenerator cg3 = new DepthChoiceGenerator(adjc.size(), -1);
        int[] comb3;
        while ((comb3 = cg3.next()) != null) {
            List<Node> s = GraphUtils.asList(comb3, adjc);
            independenceTest.isIndependent(c, a, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        // S actually has to be non-null here, but the compiler doesn't know that.
        if (S != null && !S.contains(b)) {
            scores.put(new Triple(a, b, c), score);
        }
    }
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 14 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class FciOrientT method rulesR1R2cycle.

// Does all 3 of these rules at once instead of going through all
// triples multiple times per iteration of doFinalOrientation.
public void rulesR1R2cycle(Graph graph) {
    List<Node> nodes = graph.getNodes();
    for (Node B : nodes) {
        List<Node> adj = graph.getAdjacentNodes(B);
        if (adj.size() < 2) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adj.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            Node A = adj.get(combination[0]);
            Node C = adj.get(combination[1]);
            // choice gen doesnt do diff orders, so must switch A & C around.
            ruleR1(A, B, C, graph);
            ruleR1(C, B, A, graph);
            ruleR2(A, B, C, graph);
            ruleR2(C, B, A, graph);
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 15 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class FciOrientT method ruleR6R7.

/**
 * Implements Zhang's rules R6 and R7, applies them over the graph once. Orient single tails. R6: If A---Bo-*C then
 * A---B--*C. R7: If A--oBo-*C and A,C nonadjacent, then A--oB--*C
 */
public void ruleR6R7(Graph graph) {
    List<Node> nodes = graph.getNodes();
    for (Node b : nodes) {
        List<Node> adjacents = graph.getAdjacentNodes(b);
        if (adjacents.size() < 2)
            continue;
        ChoiceGenerator cg = new ChoiceGenerator(adjacents.size(), 2);
        for (int[] choice = cg.next(); choice != null; choice = cg.next()) {
            Node a = adjacents.get(choice[0]);
            Node c = adjacents.get(choice[1]);
            if (graph.isAdjacentTo(a, c))
                continue;
            if (!(graph.getEndpoint(b, a) == Endpoint.TAIL))
                continue;
            if (!(graph.getEndpoint(c, b) == Endpoint.CIRCLE))
                continue;
            if (graph.getEndpoint(a, b) == Endpoint.TAIL) {
                // We know A---Bo-*C: R6 applies!
                graph.setEndpoint(c, b, Endpoint.TAIL);
                logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Single tails (tail)", graph.getEdge(c, b)));
                changeFlag = true;
            }
            if (graph.getEndpoint(a, b) == Endpoint.CIRCLE) {
                // if (graph.isAdjacentTo(a, c)) continue;
                logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Single tails (tail)", graph.getEdge(c, b)));
                // We know A--oBo-*C and A,C nonadjacent: R7 applies!
                graph.setEndpoint(c, b, Endpoint.TAIL);
                changeFlag = true;
            }
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Aggregations

ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)161 Node (edu.cmu.tetrad.graph.Node)37 ArrayList (java.util.ArrayList)20 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)15 List (java.util.List)11 LinkedList (java.util.LinkedList)10 ExecutorService (java.util.concurrent.ExecutorService)5 NumberFormat (java.text.NumberFormat)2 HashSet (java.util.HashSet)2 StringTokenizer (java.util.StringTokenizer)2 Test (org.junit.Test)2 DataSet (edu.cmu.tetrad.data.DataSet)1 Triple (edu.cmu.tetrad.graph.Triple)1 DeltaSextadTest (edu.cmu.tetrad.search.DeltaSextadTest)1 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)1 IntSextad (edu.cmu.tetrad.search.IntSextad)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 IndependenceResult (edu.cmu.tetradapp.model.IndependenceResult)1 Iterator (java.util.Iterator)1 SortedSet (java.util.SortedSet)1