Search in sources :

Example 26 with ChoiceGenerator

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

the class HitonMb method hitonPc.

private List<Node> hitonPc(Node t) {
    LinkedList<Node> variables = new LinkedList<>(sortedVariables);
    variables.remove(t);
    List<Node> cpc = new ArrayList<>();
    while (!variables.isEmpty()) {
        Node vi = variables.removeFirst();
        cpc.add(vi);
        VARS: for (Node x : new LinkedList<>(cpc)) {
            cpc.remove(x);
            for (int d = 0; d <= Math.min(cpc.size(), depth); d++) {
                ChoiceGenerator generator = new ChoiceGenerator(cpc.size(), d);
                int[] choice;
                while ((choice = generator.next()) != null) {
                    List<Node> s = new LinkedList<>();
                    for (int index : choice) {
                        s.add(cpc.get(index));
                    }
                    // Only do new ones.
                    if (!(x == vi || s.contains(vi))) {
                        continue;
                    }
                    // If it's independent of the target given this
                    // subset...
                    numIndTests++;
                    if (independenceTest.isIndependent(x, t, s)) {
                        // Leave it removed.
                        continue VARS;
                    }
                }
            }
            // Stick it back.
            cpc.add(x);
        }
    }
    return cpc;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 27 with ChoiceGenerator

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

the class HitonVariant method hitonPc.

// public List<Node> findMb(String targetName) {
// final Node t = getVariableForName(targetName);
// 
// // Sort variables by decreasing association with the target.
// sortedVariables = new LinkedList<Node>(variables);
// 
// Collections.sort(sortedVariables, new Comparator<Node>() {
// public int compare(Node o1, Node o2) {
// double score1 = o1 == t ? 1.0 : association(o1, t);
// double score2 = o2 == t ? 1.0 : association(o2, t);
// 
// if (score1 < score2) {
// return 1;
// }
// else if (score1 > score2) {
// return -1;
// }
// else {
// return 0;
// }
// }
// });
// 
// Set<Node> pc = new HashSet<Node>(hitonPc(t));
// Set<Node> pcpc = new HashSet<Node>();
// 
// for (Node node : pc) {
// pcpc.addAll(hitonPc(node));
// }
// 
// Set<Node> currentMb = new HashSet<Node>(pc);
// currentMb.addAll(pcpc);
// currentMb.remove(t);
// 
// // For each x...
// 
// X:
// for (Node x : new HashSet<Node>(currentMb)) {
// 
// // Look for a y such that conditioning on any set containing y
// // renders x dependent on the target...
// 
// Y:
// for (Node y : pc) {
// if (x == y) continue;
// 
// List<Node> b = new LinkedList<Node>(currentMb);
// b.remove(x);
// b.remove(y);
// 
// // We're going to add y to it, so go on lower.
// for (int d = 0; d <= Math.min(b.size(), depth) - 1; d++) {
// ChoiceGenerator generator =
// new ChoiceGenerator(b.size(), d);
// int[] choice;
// 
// while ((choice = generator.next()) != null) {
// List<Node> s = new LinkedList<Node>();
// 
// for (int index : choice) {
// s.add(b.get(index));
// }
// 
// s.add(y);
// 
// if (independenceTest.isIndependent(t, x, s)) {
// continue Y;
// }
// }
// }
// 
// // If there is such a y, leave x in.
// continue X;
// }
// 
// // Otherwise, take x out.
// currentMb.remove(x);
// }
// 
// return new ArrayList<Node>(currentMb);
// }
private List<Node> hitonPc(Node t) {
    LinkedList<Node> variables = new LinkedList<>(sortedVariables);
    variables.remove(t);
    List<Node> currentPc = new ArrayList<>();
    while (!variables.isEmpty()) {
        Node vi = variables.removeFirst();
        currentPc.add(vi);
        VARS: for (Node x : new LinkedList<>(currentPc)) {
            currentPc.remove(x);
            for (int d = 0; d <= Math.min(currentPc.size(), depth); d++) {
                ChoiceGenerator generator = new ChoiceGenerator(currentPc.size(), d);
                int[] choice;
                while ((choice = generator.next()) != null) {
                    List<Node> s = new LinkedList<>();
                    for (int index : choice) {
                        s.add(currentPc.get(index));
                    }
                    // Only do new ones.
                    if (!(x == vi || s.contains(vi))) {
                        continue;
                    }
                    // subset...
                    if (independenceTest.isIndependent(x, t, s)) {
                        // Leave it removed.
                        continue VARS;
                    }
                }
            }
            // Stick it back.
            currentPc.add(x);
        }
    }
    return currentPc;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 28 with ChoiceGenerator

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

the class TsFges2 method calculateArrowsForward.

// Calculates the new arrows for an a->b edge.
private void calculateArrowsForward(Node a, Node b) {
    if (mode == Mode.heuristicSpeedup && !effectEdgesGraph.isAdjacentTo(a, b))
        return;
    if (adjacencies != null && !adjacencies.isAdjacentTo(a, b))
        return;
    this.neighbors.put(b, getNeighbors(b));
    if (existsKnowledge()) {
        if (getKnowledge().isForbidden(a.getName(), b.getName())) {
            return;
        }
    }
    Set<Node> naYX = getNaYX(a, b);
    if (!GraphUtils.isClique(naYX, this.graph))
        return;
    List<Node> TNeighbors = getTNeighbors(a, b);
    int _maxIndegree = maxIndegree == -1 ? 1000 : maxIndegree;
    final int _max = Math.min(TNeighbors.size(), _maxIndegree - graph.getIndegree(b));
    Set<Set<Node>> previousCliques = new HashSet<>();
    previousCliques.add(new HashSet<Node>());
    Set<Set<Node>> newCliques = new HashSet<>();
    FOR: for (int i = 0; i <= _max; i++) {
        final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
        int[] choice;
        while ((choice = gen.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            Set<Node> T = GraphUtils.asSet(choice, TNeighbors);
            Set<Node> union = new HashSet<>(naYX);
            union.addAll(T);
            boolean foundAPreviousClique = false;
            for (Set<Node> clique : previousCliques) {
                if (union.containsAll(clique)) {
                    foundAPreviousClique = true;
                    break;
                }
            }
            if (!foundAPreviousClique) {
                break FOR;
            }
            if (!GraphUtils.isClique(union, this.graph))
                continue;
            newCliques.add(union);
            double bump = insertEval(a, b, T, naYX, hashIndices);
            if (bump > 0.0) {
                addArrow(a, b, naYX, T, bump);
            }
        // if (mode == Mode.heuristicSpeedup && union.isEmpty() && score.isEffectEdge(bump) &&
        // !effectEdgesGraph.isAdjacentTo(a, b) && graph.getParents(b).isEmpty()) {
        // effectEdgesGraph.addUndirectedEdge(a, b);
        // }
        }
        previousCliques = newCliques;
        newCliques = new HashSet<>();
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 29 with ChoiceGenerator

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

the class VcpcAlt method orientUnshieldedTriples.

private void orientUnshieldedTriples(IKnowledge knowledge, IndependenceTest test, int depth) {
    TetradLogger.getInstance().log("info", "Starting Collider Orientation:");
    // System.out.println("orientUnshieldedTriples 1");
    colliderTriples = new HashSet<>();
    noncolliderTriples = new HashSet<>();
    ambiguousTriples = new HashSet<>();
    List<Node> 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) {
            Node x = adjacentNodes.get(combination[0]);
            Node z = adjacentNodes.get(combination[1]);
            if (this.graph.isAdjacentTo(x, z)) {
                continue;
            }
            getAllTriples().add(new Triple(x, y, z));
            SearchGraphUtils.CpcTripleType type = SearchGraphUtils.getCpcTripleType(x, y, z, test, depth, graph, verbose);
            if (type == SearchGraphUtils.CpcTripleType.COLLIDER) {
                if (colliderAllowed(x, y, z, knowledge)) {
                    graph.setEndpoint(x, y, Endpoint.ARROW);
                    graph.setEndpoint(z, y, Endpoint.ARROW);
                    TetradLogger.getInstance().log("colliderOrientations", SearchLogUtils.colliderOrientedMsg(x, y, z));
                }
                colliderTriples.add(new Triple(x, y, z));
            } else if (type == SearchGraphUtils.CpcTripleType.AMBIGUOUS) {
                Triple triple = new Triple(x, y, z);
                ambiguousTriples.add(triple);
                graph.addAmbiguousTriple(triple.getX(), triple.getY(), triple.getZ());
                Edge edge = Edges.undirectedEdge(x, z);
                definitelyNonadjacencies.add(edge);
            } else {
                noncolliderTriples.add(new Triple(x, y, z));
            }
        }
    }
    TetradLogger.getInstance().log("info", "Finishing Collider Orientation.");
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 30 with ChoiceGenerator

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

the class SepsetsConservativeMajority method getSepsetsLists.

// The published version.
public List<List<List<Node>>> getSepsetsLists(Node x, Node y, Node z, IndependenceTest test, int depth, boolean verbose) {
    List<List<Node>> sepsetsContainingY = new ArrayList<>();
    List<List<Node>> sepsetsNotContainingY = new ArrayList<>();
    List<Node> _nodes = graph.getAdjacentNodes(x);
    _nodes.remove(z);
    int _depth = depth;
    if (_depth == -1) {
        _depth = 1000;
    }
    _depth = Math.min(_depth, _nodes.size());
    for (int d = 0; d <= _depth; d++) {
        ChoiceGenerator cg = new ChoiceGenerator(_nodes.size(), d);
        int[] choice;
        while ((choice = cg.next()) != null) {
            List<Node> cond = GraphUtils.asList(choice, _nodes);
            if (test.isIndependent(x, z, cond)) {
                if (verbose) {
                    System.out.println("Indep: " + x + " _||_ " + z + " | " + cond);
                }
                if (cond.contains(y)) {
                    sepsetsContainingY.add(cond);
                } else {
                    sepsetsNotContainingY.add(cond);
                }
            }
        }
    }
    _nodes = graph.getAdjacentNodes(z);
    _nodes.remove(x);
    _depth = depth;
    if (_depth == -1) {
        _depth = 1000;
    }
    _depth = Math.min(_depth, _nodes.size());
    for (int d = 0; d <= _depth; d++) {
        ChoiceGenerator cg = new ChoiceGenerator(_nodes.size(), d);
        int[] choice;
        while ((choice = cg.next()) != null) {
            List<Node> cond = GraphUtils.asList(choice, _nodes);
            if (test.isIndependent(x, z, cond)) {
                if (cond.contains(y)) {
                    sepsetsContainingY.add(cond);
                } else {
                    sepsetsNotContainingY.add(cond);
                }
            }
        }
    }
    List<List<List<Node>>> ret = new ArrayList<>();
    ret.add(sepsetsContainingY);
    ret.add(sepsetsNotContainingY);
    return ret;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) List(java.util.List) ArrayList(java.util.ArrayList)

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