Search in sources :

Example 61 with ChoiceGenerator

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

the class Mbfs method orientUnshieldedTriples.

private void orientUnshieldedTriples(IKnowledge knowledge, Graph graph, IndependenceTest test, int depth, List<Node> nodes) {
    logger.log("info", "Starting Collider Orientation:");
    colliderTriples = new HashSet<>();
    noncolliderTriples = new HashSet<>();
    ambiguousTriples = new HashSet<>();
    if (nodes == null) {
        nodes = graph.getNodes();
    }
    for (Node y : nodes) {
        List<Node> adjacentNodes = graph.getAdjacentNodes(y);
        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 x = adjacentNodes.get(combination[0]);
            Node z = adjacentNodes.get(combination[1]);
            if (graph.isAdjacentTo(x, z)) {
                continue;
            }
            allTriples.add(new Triple(x, y, z));
            TripleType type = getTripleType(graph, x, y, z, test, depth);
            if (type == TripleType.COLLIDER) {
                if (colliderAllowed(x, y, z, knowledge)) {
                    graph.setEndpoint(x, y, Endpoint.ARROW);
                    graph.setEndpoint(z, y, Endpoint.ARROW);
                    logger.log("tripleClassifications", "Collider oriented: " + Triple.pathString(graph, x, y, z));
                }
                colliderTriples.add(new Triple(x, y, z));
            } else if (type == TripleType.AMBIGUOUS) {
                Triple triple = new Triple(x, y, z);
                ambiguousTriples.add(triple);
                graph.addAmbiguousTriple(triple.getX(), triple.getY(), triple.getZ());
                logger.log("tripleClassifications", "tripleClassifications: " + Triple.pathString(graph, x, y, z));
            } else {
                noncolliderTriples.add(new Triple(x, y, z));
                logger.log("tripleClassifications", "tripleClassifications: " + Triple.pathString(graph, x, y, z));
            }
        }
    }
    logger.log("info", "Finishing Collider Orientation.");
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 62 with ChoiceGenerator

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

the class Mbfs method prune.

/**
 * Tries node remove the edge node---from using adjacent nodes of node 'from', then tries node remove each other
 * edge adjacent node 'from' using remaining edges adjacent node 'from.' If the edge 'node' is removed, the method
 * immediately returns.
 *
 * @param node  The node about which pruning it to take place.
 * @param graph The getModel search graph, to be modified by pruning.
 * @param depth The maximum number of conditioning variables.
 */
private void prune(Node node, Graph graph, int depth) {
    logger.log("pruning", "Trying to remove edges adjacent to node " + node + ", depth = " + depth + ".");
    // Otherwise, try removing all other edges adjacent node node. Return
    // true if more edges could be removed at the next depth.
    List<Node> a = new LinkedList<>(graph.getAdjacentNodes(node));
    NEXT_EDGE: for (Node y : a) {
        List<Node> adjNode = new LinkedList<>(graph.getAdjacentNodes(node));
        adjNode.remove(y);
        adjNode = possibleParents(node, adjNode);
        if (adjNode.size() < depth) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adjNode.size(), depth);
        int[] choice;
        while ((choice = cg.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            List<Node> condSet = GraphUtils.asList(choice, adjNode);
            if (independent(node, y, condSet) && !edgeRequired(node, y)) {
                graph.removeEdge(node, y);
                // The target itself must not be removed.
                if (graph.getEdges(y).isEmpty() && y != getTarget()) {
                    graph.removeNode(y);
                }
                continue NEXT_EDGE;
            }
        }
    }
    int numAdjacents = graph.getAdjacentNodes(node).size();
    noteMaxAtDepth(depth, numAdjacents, node);
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 63 with ChoiceGenerator

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

the class MeekRulesPattern method meekR3.

/**
 * Meek's rule R3. If a--b, a--c, a--d, c-->b, d-->b, then orient a-->b.
 */
public boolean meekR3(Graph graph, IKnowledge IKnowledge) {
    List<Node> nodes = graph.getNodes();
    boolean changed = false;
    for (Node a : nodes) {
        List<Node> adjacentNodes = graph.getAdjacentNodes(a);
        if (adjacentNodes.size() < 3) {
            continue;
        }
        for (Node b : adjacentNodes) {
            List<Node> otherAdjacents = new LinkedList<>(adjacentNodes);
            otherAdjacents.remove(b);
            if (!graph.isUndirectedFromTo(a, b)) {
                continue;
            }
            ChoiceGenerator cg = new ChoiceGenerator(otherAdjacents.size(), 2);
            int[] combination;
            while ((combination = cg.next()) != null) {
                Node c = otherAdjacents.get(combination[0]);
                Node d = otherAdjacents.get(combination[1]);
                if (graph.isAdjacentTo(c, d)) {
                    continue;
                }
                if (!graph.isUndirectedFromTo(a, c)) {
                    continue;
                }
                if (!graph.isUndirectedFromTo(a, d)) {
                    continue;
                }
                if (!isUnshieldedNoncollider(c, a, d, graph)) {
                    continue;
                }
                if (graph.isDirectedFromTo(c, b) && graph.isDirectedFromTo(d, b)) {
                    if (isArrowpointAllowed(a, b, IKnowledge) && !createsCycle(a, b, graph)) {
                        graph.setEndpoint(a, b, Endpoint.ARROW);
                        this.logger.log("impliedOrientation", SearchLogUtils.edgeOrientedMsg("Meek R3", graph.getEdge(a, b)));
                        changed = true;
                        meekR2(graph, IKnowledge);
                        break;
                    }
                }
            }
        }
    }
    return changed;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) LinkedList(java.util.LinkedList)

Example 64 with ChoiceGenerator

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

the class MeekRulesPattern method meekR1Locally.

public boolean meekR1Locally(Graph graph, IKnowledge IKnowledge) {
    List<Node> nodes = graph.getNodes();
    boolean changed = false;
    for (Node a : nodes) {
        List<Node> adjacentNodes = graph.getAdjacentNodes(a);
        if (adjacentNodes.size() < 2) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            Node b = adjacentNodes.get(combination[0]);
            Node c = adjacentNodes.get(combination[1]);
            // Skip triples that are shielded.
            if (graph.isAdjacentTo(b, c)) {
                continue;
            }
            if (graph.getEndpoint(b, a) == Endpoint.ARROW && graph.isUndirectedFromTo(a, c)) {
                if (!isUnshieldedNoncollider(b, a, c, graph)) {
                    continue;
                }
                if (isArrowpointAllowed(a, c, IKnowledge) && !createsCycle(a, c, graph)) {
                    graph.setEndpoint(a, c, Endpoint.ARROW);
                    this.logger.log("impliedOrientation", SearchLogUtils.edgeOrientedMsg("Meek R1 triangle (" + b + "-->" + a + "---" + c + ")", graph.getEdge(a, c)));
                    changed = true;
                    meekR2(graph, IKnowledge);
                }
            } else if (graph.getEndpoint(c, a) == Endpoint.ARROW && graph.isUndirectedFromTo(a, b)) {
                if (!isUnshieldedNoncollider(b, a, c, graph)) {
                    continue;
                }
                if (isArrowpointAllowed(a, b, IKnowledge) && !createsCycle(a, b, graph)) {
                    graph.setEndpoint(a, b, Endpoint.ARROW);
                    this.logger.log("impliedOrientation", SearchLogUtils.edgeOrientedMsg("Meek R1 triangle (" + c + "-->" + a + "---" + b + ")", graph.getEdge(a, b)));
                    changed = true;
                    meekR2(graph, IKnowledge);
                }
            }
        }
    }
    return changed;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 65 with ChoiceGenerator

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

the class PurifyTetradBased method listCrossConstructPValues.

private List<Double> listCrossConstructPValues(List<int[]> clustering, boolean[] eliminated, double cutoff) {
    List<Double> allPValues = new ArrayList<>();
    boolean countable = false;
    for (int p1 = 0; p1 < clustering.size(); p1++) {
        for (int p2 = p1 + 1; p2 < clustering.size(); p2++) {
            int[] cluster1 = clustering.get(p1);
            int[] cluster2 = clustering.get(p2);
            if (cluster1.length >= 3 && cluster2.length >= 1) {
                ChoiceGenerator gen1 = new ChoiceGenerator(cluster1.length, 3);
                int[] choice1;
                while ((choice1 = gen1.next()) != null) {
                    ChoiceGenerator gen2 = new ChoiceGenerator(cluster2.length, 1);
                    int[] choice2;
                    while ((choice2 = gen2.next()) != null) {
                        List<Integer> crossCluster = new ArrayList<>();
                        for (int i : choice1) crossCluster.add(cluster1[i]);
                        for (int i : choice2) crossCluster.add(cluster2[i]);
                        List<Double> pValues = listPValues(crossCluster, eliminated, cutoff);
                        if (pValues != null) {
                            countable = true;
                            allPValues.addAll(pValues);
                        }
                    }
                }
            }
            if (cluster2.length >= 3 && cluster1.length >= 1) {
                ChoiceGenerator gen1 = new ChoiceGenerator(cluster2.length, 3);
                int[] choice1;
                while ((choice1 = gen1.next()) != null) {
                    ChoiceGenerator gen2 = new ChoiceGenerator(cluster1.length, 1);
                    int[] choice2;
                    while ((choice2 = gen2.next()) != null) {
                        List<Integer> crossCluster = new ArrayList<>();
                        for (int i : choice1) crossCluster.add(cluster2[i]);
                        for (int i : choice2) crossCluster.add(cluster1[i]);
                        List<Double> pValues = listPValues(crossCluster, eliminated, cutoff);
                        if (pValues != null) {
                            countable = true;
                            allPValues.addAll(pValues);
                        }
                    }
                }
            }
            if (cluster1.length >= 2 && cluster2.length >= 2) {
                ChoiceGenerator gen1 = new ChoiceGenerator(cluster1.length, 2);
                int[] choice1;
                while ((choice1 = gen1.next()) != null) {
                    ChoiceGenerator gen2 = new ChoiceGenerator(cluster2.length, 2);
                    int[] choice2;
                    while ((choice2 = gen2.next()) != null) {
                        List<Integer> crossCluster = new ArrayList<>();
                        for (int i : choice1) crossCluster.add(cluster1[i]);
                        for (int i : choice2) crossCluster.add(cluster2[i]);
                        List<Double> pValues = listPValues2by2(crossCluster, eliminated, cutoff);
                        if (pValues != null) {
                            countable = true;
                            allPValues.addAll(pValues);
                        }
                    }
                }
            }
        }
    }
    return countable ? allPValues : null;
}
Also used : ArrayList(java.util.ArrayList) 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