Search in sources :

Example 6 with ChoiceGenerator

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

the class GFciMax 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);
            } 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) List(java.util.List)

Example 7 with ChoiceGenerator

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

the class FgesD 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 (a == b)
        throw new IllegalArgumentException();
    if (existsKnowledge()) {
        if (getKnowledge().isForbidden(a.getName(), b.getName())) {
            return;
        }
    }
    Set<Node> naYX = getNaYX(a, b);
    if (!isClique(naYX))
        return;
    List<Node> TNeighbors = getTNeighbors(a, b);
    Set<Set<Node>> previousCliques = new HashSet<>();
    previousCliques.add(new HashSet<Node>());
    Set<Set<Node>> newCliques = new HashSet<>();
    FOR: for (int i = 0; i <= TNeighbors.size(); i++) {
        final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
        int[] choice;
        while ((choice = gen.next()) != null) {
            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 (!isClique(union))
                continue;
            newCliques.add(union);
            double bump = insertEval(a, b, T, naYX, hashIndices);
            if (bump > 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 8 with ChoiceGenerator

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

the class FgesD method calculateArrowsBackward.

// Calculates the arrows for the removal in the backward direction.
private void calculateArrowsBackward(Node a, Node b) {
    if (existsKnowledge()) {
        if (!getKnowledge().noEdgeRequired(a.getName(), b.getName())) {
            return;
        }
    }
    Set<Node> naYX = getNaYX(a, b);
    List<Node> _naYX = new ArrayList<>(naYX);
    final int _depth = _naYX.size();
    for (int i = 0; i <= _depth; i++) {
        final ChoiceGenerator gen = new ChoiceGenerator(_naYX.size(), i);
        int[] choice;
        while ((choice = gen.next()) != null) {
            Set<Node> diff = GraphUtils.asSet(choice, _naYX);
            Set<Node> h = new HashSet<>(_naYX);
            h.removeAll(diff);
            if (existsKnowledge()) {
                if (!validSetByKnowledge(b, h)) {
                    continue;
                }
            }
            double bump = deleteEval(a, b, diff, naYX, hashIndices);
            if (bump > 0.0) {
                addArrow(a, b, naYX, h, bump);
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 9 with ChoiceGenerator

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

the class FgesOld method calculateArrowsForward.

// Calculates the new arrows for an a->b edge.
private void calculateArrowsForward(Node a, Node b) {
    if (isHeuristicSpeedup() && !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);
    final int _depth = Math.min(TNeighbors.size(), depth == -1 ? 1000 : depth);
    Set<Set<Node>> previousCliques = new HashSet<>();
    previousCliques.add(new HashSet<Node>());
    Set<Set<Node>> newCliques = new HashSet<>();
    FOR: for (int i = 0; i <= _depth; i++) {
        final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
        int[] choice;
        while ((choice = gen.next()) != null) {
            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 (isHeuristicSpeedup() && union.isEmpty() && fgesScore.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 10 with ChoiceGenerator

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

the class GFci 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);
            } 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) List(java.util.List)

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