Search in sources :

Example 16 with ChoiceGenerator

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

the class FciOrientT method ruleR3.

/**
 * Implements the double-triangle orientation rule, which states that if D*-oB, A*->B<-*C and A*-oDo-*C, then
 * D*->B.
 * <p>
 * This is Zhang's rule R3.
 */
public void ruleR3(Graph graph) {
    List<Node> nodes = graph.getNodes();
    for (Node B : nodes) {
        List<Node> intoBArrows = graph.getNodesInTo(B, Endpoint.ARROW);
        List<Node> intoBCircles = graph.getNodesInTo(B, Endpoint.CIRCLE);
        for (Node D : intoBCircles) {
            if (intoBArrows.size() < 2) {
                continue;
            }
            ChoiceGenerator gen = new ChoiceGenerator(intoBArrows.size(), 2);
            int[] choice;
            while ((choice = gen.next()) != null) {
                Node A = intoBArrows.get(choice[0]);
                Node C = intoBArrows.get(choice[1]);
                if (graph.isAdjacentTo(A, C)) {
                    continue;
                }
                if (!graph.isAdjacentTo(A, D) || !graph.isAdjacentTo(C, D)) {
                    continue;
                }
                if (graph.getEndpoint(A, D) != Endpoint.CIRCLE) {
                    continue;
                }
                if (graph.getEndpoint(C, D) != Endpoint.CIRCLE) {
                    continue;
                }
                if (!isArrowpointAllowed(D, B, graph)) {
                    continue;
                }
                graph.setEndpoint(D, B, Endpoint.ARROW);
                logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Double triangle", graph.getEdge(D, B)));
                System.out.println(SearchLogUtils.edgeOrientedMsg("Double triangle", graph.getEdge(D, B)));
                changeFlag = true;
            }
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 17 with ChoiceGenerator

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

the class FciOrientT method findCollidersUsingSepsets.

/**
 * Step C of PC; orients colliders using specified sepset. That is, orients x *-* y *-* z as x *-> y <-* z just in
 * case y is in Sepset({x, z}).
 */
public List<Triple> findCollidersUsingSepsets(SepsetProducer sepsetProducer, Graph graph, boolean verbose) {
    TetradLogger.getInstance().log("details", "Starting Collider Orientation:");
    List<Triple> colliders = new ArrayList<>();
    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]);
            // Skip triples that are shielded.
            if (graph.isAdjacentTo(a, c)) {
                continue;
            }
            List<Node> sepset = sepsetProducer.getSepset(a, c);
            // 
            if (sepset == null)
                continue;
            if (!sepset.contains(b)) {
                if (verbose) {
                    System.out.println("Collider orientation <" + a + ", " + b + ", " + c + "> sepset = " + sepset);
                }
                colliders.add(new Triple(a, b, c));
                TetradLogger.getInstance().log("colliderOrientations", SearchLogUtils.colliderOrientedMsg(a, b, c, sepset));
            }
        }
    }
    TetradLogger.getInstance().log("details", "Finishing Collider Orientation.");
    return colliders;
}
Also used : Triple(edu.cmu.tetrad.graph.Triple) Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 18 with ChoiceGenerator

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

the class TsFciOrient method ruleR3.

// /**
// * Implements the double-triangle orientation rule, which states that if D*-oB, A*->B<-*C and A*-oDo-*C, then
// * D*->B.
// * <p/>
// * This is Zhang's rule R3.
// */
// private void ruleR3(Graph graph) {
// List<Node> nodes = graph.getNodes();
// 
// for (Node d : nodes) {
// final List<Node> adjacentNodes = graph.getAdjacentNodes(d);
// 
// if (adjacentNodes.size() < 3) {
// continue;
// }
// 
// ChoiceGenerator gen = new ChoiceGenerator(adjacentNodes.size(), 3);
// int[] choice;
// 
// while ((choice = gen.next()) != null) {
// List<Node> adj = DataGraphUtils.asList(choice, adjacentNodes);
// 
// Node a = adj.get(0);
// Node b = adj.get(1);
// Node c = adj.get(2);
// 
// if (graph.getEndpoint(a, b) == Endpoint.ARROW && graph.getEndpoint(c, b) == Endpoint.ARROW
// && graph.getEndpoint(a, d) == Endpoint.CIRCLE && graph.getEndpoint(c, d) == Endpoint.ARROW
// && !graph.isAdjacentTo(a, c) && graph.getEndpoint(d, b) == Endpoint.CIRCLE) {
// graph.setEndpoint(d, b, Endpoint.ARROW);
// }
// }
// }
// }
/**
 * Implements the double-triangle orientation rule, which states that if D*-oB, A*->B<-*C and A*-oDo-*C, then
 * D*->B.
 * <p>
 * This is Zhang's rule R3.
 */
public void ruleR3(Graph graph) {
    List<Node> nodes = graph.getNodes();
    for (Node B : nodes) {
        List<Node> intoBArrows = graph.getNodesInTo(B, Endpoint.ARROW);
        List<Node> intoBCircles = graph.getNodesInTo(B, Endpoint.CIRCLE);
        for (Node D : intoBCircles) {
            if (intoBArrows.size() < 2) {
                continue;
            }
            ChoiceGenerator gen = new ChoiceGenerator(intoBArrows.size(), 2);
            int[] choice;
            while ((choice = gen.next()) != null) {
                Node A = intoBArrows.get(choice[0]);
                Node C = intoBArrows.get(choice[1]);
                if (graph.isAdjacentTo(A, C)) {
                    continue;
                }
                if (!graph.isAdjacentTo(A, D) || !graph.isAdjacentTo(C, D)) {
                    continue;
                }
                if (graph.getEndpoint(A, D) != Endpoint.CIRCLE) {
                    continue;
                }
                if (graph.getEndpoint(C, D) != Endpoint.CIRCLE) {
                    continue;
                }
                if (!isArrowpointAllowed(D, B, graph)) {
                    continue;
                }
                graph.setEndpoint(D, B, Endpoint.ARROW);
                this.orientSimilarPairs(graph, this.getKnowledge(), D, B, Endpoint.ARROW);
                if (verbose) {
                    logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Double triangle", graph.getEdge(D, B)));
                    System.out.println(SearchLogUtils.edgeOrientedMsg("Double triangle", graph.getEdge(D, B)));
                }
                changeFlag = true;
            }
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 19 with ChoiceGenerator

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

the class TsFciOrient method ruleR0.

/**
 * Orients colliders in the graph.  (FCI Step C)
 * <p>
 * Zhang's step F3, rule R0.
 */
public void ruleR0(Graph graph) {
    graph.reorientAllWith(Endpoint.CIRCLE);
    fciOrientbk(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]);
            // Skip triples that are shielded.
            if (graph.isAdjacentTo(a, c)) {
                continue;
            }
            if (graph.isDefCollider(a, b, c)) {
                continue;
            }
            if (sepsets.isCollider(a, b, c)) {
                if (!isArrowpointAllowed(a, b, graph)) {
                    continue;
                }
                if (!isArrowpointAllowed(c, b, graph)) {
                    continue;
                }
                graph.setEndpoint(a, b, Endpoint.ARROW);
                graph.setEndpoint(c, b, Endpoint.ARROW);
                if (verbose) {
                    logger.log("colliderOrientations", SearchLogUtils.colliderOrientedMsg(a, b, c));
                    System.out.println(SearchLogUtils.colliderOrientedMsg(a, b, c));
                    String location = "R0";
                    printWrongColliderMessage(a, b, c, location, graph);
                }
                this.orientSimilarPairs(graph, knowledge, a, b, Endpoint.ARROW);
                this.orientSimilarPairs(graph, knowledge, c, b, Endpoint.ARROW);
            }
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 20 with ChoiceGenerator

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

the class TsGFci method modifiedR0.

// Due to Spirtes.
public void modifiedR0(Graph fgesGraph) {
    graph.reorientAllWith(Endpoint.CIRCLE);
    fciOrientbk(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);
                /**
                 * orienting similar pairs to enforce repeating structure *
                 */
                orientSimilarPairs(graph, knowledge, a, b, Endpoint.ARROW);
                orientSimilarPairs(graph, knowledge, 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);
                    /**
                     * orienting similar pairs to enforce repeating structure *
                     */
                    orientSimilarPairs(graph, knowledge, a, b, Endpoint.ARROW);
                    orientSimilarPairs(graph, knowledge, c, b, Endpoint.ARROW);
                /**
                 */
                }
            }
        }
    }
}
Also used : 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