Search in sources :

Example 66 with ChoiceGenerator

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

the class PurifyTetradBased method listPValues.

private List<Double> listPValues(List<Integer> cluster, boolean[] eliminated, double cutoff) {
    if (cluster.size() < 4)
        return null;
    boolean countable = false;
    List<Double> pValues = new ArrayList<>();
    ChoiceGenerator gen = new ChoiceGenerator(cluster.size(), 4);
    int[] choice;
    while ((choice = gen.next()) != null) {
        int i = choice[0];
        int j = choice[1];
        int k = choice[2];
        int l = choice[3];
        int ci = cluster.get(i);
        int cj = cluster.get(j);
        int ck = cluster.get(k);
        int cl = cluster.get(l);
        if (eliminated[ci] || eliminated[cj] || eliminated[ck] || eliminated[cl]) {
            continue;
        }
        countable = true;
        double p1 = tetradTest.tetradPValue(ci, cj, ck, cl);
        double p2 = tetradTest.tetradPValue(ci, cl, cj, ck);
        double p3 = tetradTest.tetradPValue(ci, ck, cj, cl);
        if (p1 < cutoff) {
            printTetrad(ci, cj, ck, cl, p1);
            pValues.add(p1);
        }
        if (p2 < cutoff) {
            printTetrad(ci, cl, cj, ck, p2);
            pValues.add(p2);
        }
        if (p3 < cutoff) {
            printTetrad(ci, ck, cj, cl, p3);
            pValues.add(p3);
        }
    }
    return countable ? pValues : null;
}
Also used : ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 67 with ChoiceGenerator

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

the class PossibleDsepFci method getCondSet.

private List<Node> getCondSet(Node node1, Node node2, int maxPathLength) {
    final Set<Node> possibleDsepSet = getPossibleDsep(node1, node2, maxPathLength);
    List<Node> possibleDsep = new ArrayList<>(possibleDsepSet);
    boolean noEdgeRequired = getKnowledge().noEdgeRequired(node1.getName(), node2.getName());
    List<Node> possParents = possibleParents(node1, possibleDsep, getKnowledge());
    int _depth = getDepth() == -1 ? 1000 : getDepth();
    for (int d = 0; d <= Math.min(_depth, possParents.size()); d++) {
        ChoiceGenerator cg = new ChoiceGenerator(possParents.size(), d);
        int[] choice;
        while ((choice = cg.next()) != null) {
            List<Node> condSet = GraphUtils.asList(choice, possParents);
            boolean independent = test.isIndependent(node1, node2, condSet);
            if (independent && noEdgeRequired) {
                return condSet;
            }
        }
    }
    return null;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 68 with ChoiceGenerator

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

the class ProbFci method setMinSepSet.

// ///////////////////////////////////////////////////////////////////////////
// set the sepSet of x and y to the minimal such subset of the given sepSet
// and remove the edge <x, y> if background knowledge allows
// ///////////////////////////////////////////////////////////////////////////
private void setMinSepSet(List<Node> sepSet, Node x, Node y) {
    // It is assumed that BK has been considered before calling this method
    // (for example, setting independent1 and independent2 in ruleR0_RFCI)
    /*
		// background knowledge requires this edge
		if (knowledge.noEdgeRequired(x.getNode(), y.getNode()))
		{
			return;
		}
		 */
    List<Node> empty = Collections.emptyList();
    boolean indep;
    try {
        indep = independenceTest.isIndependent(x, y, empty);
    } catch (Exception e) {
        indep = false;
    }
    if (indep) {
        getSepsets().set(x, y, empty);
        return;
    }
    int sepSetSize = sepSet.size();
    for (int i = 1; i <= sepSetSize; i++) {
        ChoiceGenerator cg = new ChoiceGenerator(sepSetSize, i);
        int[] combination;
        while ((combination = cg.next()) != null) {
            List<Node> condSet = GraphUtils.asList(combination, sepSet);
            try {
                indep = independenceTest.isIndependent(x, y, condSet);
            } catch (Exception e) {
                indep = false;
            }
            if (indep) {
                getSepsets().set(x, y, condSet);
                return;
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 69 with ChoiceGenerator

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

the class ProbFci 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
 */
private void ruleR6R7() {
    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) {
                // System.out.println("Single tails (tail) " + c + " *-> " + b);
                // 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 : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 70 with ChoiceGenerator

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

the class ProbFci method ruleR0.

// ===========================PRIVATE METHODS=========================//
/**
 * Orients colliders in the graph.  (FCI Step C)
 * <p>
 * Zhang's step F3, rule R0.
 */
private void ruleR0() {
    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> sepset2 = this.sepsets.get(a, c);
            if (sepset2 == null)
                continue;
            if (!sepset2.contains(b)) {
                if (!isArrowpointAllowed(a, b)) {
                    continue;
                }
                if (!isArrowpointAllowed(c, b)) {
                    continue;
                }
                graph.setEndpoint(a, b, Endpoint.ARROW);
                graph.setEndpoint(c, b, Endpoint.ARROW);
                logger.log("colliderOrientations", SearchLogUtils.colliderOrientedMsg(a, b, c));
            }
        }
    }
}
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