Search in sources :

Example 21 with ChoiceGenerator

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

the class Vcpc method getPopulationTripleType.

public CpcTripleType getPopulationTripleType(Node x, Node y, Node z, IndependenceTest test, int depth, Graph graph, boolean verbose) {
    if (facts == null)
        throw new NullPointerException("Need independence facts as a parent");
    // JOE HERE ARE THE INDEPENDENCE FACTS
    System.out.println("NameS" + facts.getVariableNames());
    int numSepsetsContainingY = 0;
    int numSepsetsNotContainingY = 0;
    List<Node> _nodes = graph.getAdjacentNodes(x);
    _nodes.remove(z);
    TetradLogger.getInstance().log("adjacencies", "Adjacents for " + x + "--" + y + "--" + z + " = " + _nodes);
    int _depth = depth;
    if (_depth == -1) {
        _depth = 1000;
    }
    _depth = Math.min(_depth, _nodes.size());
    while (true) {
        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 (facts.isIndependent(x, z, cond)) {
                    // if (verbose) {
                    System.out.println("Indep Fact said: " + x + " _||_ " + z + " | " + cond);
                    if (cond.contains(y)) {
                        numSepsetsContainingY++;
                    } else {
                        numSepsetsNotContainingY++;
                    }
                } else {
                    System.out.println("This is not Indep by facts: " + x + " _||_ " + z + " | " + cond);
                }
                // 
                if (numSepsetsContainingY > 0 && numSepsetsNotContainingY > 0) {
                    return CpcTripleType.AMBIGUOUS;
                }
            }
        }
        _nodes = graph.getAdjacentNodes(z);
        _nodes.remove(x);
        TetradLogger.getInstance().log("adjacencies", "Adjacents for " + x + "--" + y + "--" + z + " = " + _nodes);
        _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)) {
                        numSepsetsContainingY++;
                    } else {
                        numSepsetsNotContainingY++;
                    }
                }
                if (numSepsetsContainingY > 0 && numSepsetsNotContainingY > 0) {
                    return CpcTripleType.AMBIGUOUS;
                }
            }
        }
        break;
    }
    if (numSepsetsContainingY > 0) {
        return CpcTripleType.NONCOLLIDER;
    } else {
        if (verbose) {
            System.out.println("Orienting " + x + "-->" + y + "<-" + z);
        }
        return CpcTripleType.COLLIDER;
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 22 with ChoiceGenerator

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

the class Vcpc method orientUnshieldedTriples.

// private void logTriples() {
// TetradLogger.getInstance().log("info", "\nCollider triples:");
// 
// for (Triple triple : colliderTriples) {
// TetradLogger.getInstance().log("info", "Collider: " + triple);
// }
// 
// TetradLogger.getInstance().log("info", "\nNoncollider triples:");
// 
// for (Triple triple : noncolliderTriples) {
// TetradLogger.getInstance().log("info", "Noncollider: " + triple);
// }
// 
// TetradLogger.getInstance().log("info", "\nAmbiguous triples (i.e. list of triples for which " +
// "\nthere is ambiguous data about whether they are colliders or not):");
// 
// for (Triple triple : getAmbiguousTriples()) {
// TetradLogger.getInstance().log("info", "Ambiguous: " + triple);
// }
// }
// 
// //        Original triple orientation procedure.
// private void orientUnshieldedTriples(IKnowledge knowledge,
// IndependenceTest test, int depth) {
// TetradLogger.getInstance().log("info", "Starting Collider Orientation:");
// colliderTriples = new HashSet<Triple>();
// noncolliderTriples = new HashSet<Triple>();
// ambiguousTriples = new HashSet<Triple>();
// 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);
// //                SearchGraphUtils.CpcTripleType type = SearchGraphUtils.getCpcTripleType2(x, y, z, test, depth, graph);
// 
// 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.");
// }
// // Population version.
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));
            CpcTripleType type = getPopulationTripleType(x, y, z, test, depth, graph, verbose);
            if (type == 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 == 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 23 with ChoiceGenerator

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

the class VcpcFast method getPopulationTripleType.

public CpcTripleType getPopulationTripleType(Node x, Node y, Node z, IndependenceTest test, int depth, Graph graph, boolean verbose) {
    // if ((x.getNode().equals("X5") && z.getNode().equals("X7"))
    // || (x.getNode().equals("X7") && z.getNode().equals("X5"))) {
    // System.out.println();
    // }
    // JOE HERE ARE THE FACTS.
    setFacts(facts);
    System.out.println("NameS" + facts.getVariableNames());
    IndependenceTest test1 = new IndTestDSep(graph);
    int numSepsetsContainingY = 0;
    int numSepsetsNotContainingY = 0;
    List<Node> _nodes = graph.getAdjacentNodes(x);
    _nodes.remove(z);
    TetradLogger.getInstance().log("adjacencies", "Adjacents for " + x + "--" + y + "--" + z + " = " + _nodes);
    int _depth = depth;
    if (_depth == -1) {
        _depth = 1000;
    }
    _depth = Math.min(_depth, _nodes.size());
    while (true) {
        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 (facts.isIndependent(x, z, cond)) {
                    // if (verbose) {
                    System.out.println("Indep Fact said: " + x + " _||_ " + z + " | " + cond);
                    if (cond.contains(y)) {
                        numSepsetsContainingY++;
                    } else {
                        numSepsetsNotContainingY++;
                    }
                } else {
                    System.out.println("This is not Indep by facts: " + x + " _||_ " + z + " | " + cond);
                }
                // 
                if (numSepsetsContainingY > 0 && numSepsetsNotContainingY > 0) {
                    return CpcTripleType.AMBIGUOUS;
                }
            }
        }
        _nodes = graph.getAdjacentNodes(z);
        _nodes.remove(x);
        TetradLogger.getInstance().log("adjacencies", "Adjacents for " + x + "--" + y + "--" + z + " = " + _nodes);
        _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)) {
                        numSepsetsContainingY++;
                    } else {
                        numSepsetsNotContainingY++;
                    }
                }
                if (numSepsetsContainingY > 0 && numSepsetsNotContainingY > 0) {
                    return CpcTripleType.AMBIGUOUS;
                }
            }
        }
        break;
    }
    if (numSepsetsContainingY > 0) {
        return CpcTripleType.NONCOLLIDER;
    } else {
        if (verbose) {
            System.out.println("Orienting " + x + "-->" + y + "<-" + z);
        }
        return CpcTripleType.COLLIDER;
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 24 with ChoiceGenerator

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

the class SepsetsMaxScore method getMaxScoreSet.

private List<Node> getMaxScoreSet(Node i, Node k) {
    double _p = 0.0;
    List<Node> _v = null;
    if (extraSepsets != null) {
        final List<Node> v = extraSepsets.get(i, k);
        if (v != null) {
            independenceTest.isIndependent(i, k, v);
            double p = independenceTest.getScore();
            if (p > _p) {
                _p = p;
                _v = v;
            }
        }
    }
    List<Node> adji = graph.getAdjacentNodes(i);
    List<Node> adjk = graph.getAdjacentNodes(k);
    adji.remove(k);
    adjk.remove(i);
    for (int d = 0; d <= Math.min((depth == -1 ? 1000 : depth), adji.size()); d++) {
        ChoiceGenerator gen = new ChoiceGenerator(adji.size(), d);
        int[] choice;
        while ((choice = gen.next()) != null) {
            List<Node> v = GraphUtils.asList(choice, adji);
            getIndependenceTest().isIndependent(i, k, v);
            double p = getIndependenceTest().getScore();
            if (p > _p) {
                _p = p;
                _v = v;
            }
        }
    }
    for (int d = 0; d <= Math.min((depth == -1 ? 1000 : depth), adjk.size()); d++) {
        ChoiceGenerator gen = new ChoiceGenerator(adjk.size(), d);
        int[] choice;
        while ((choice = gen.next()) != null) {
            List<Node> v = GraphUtils.asList(choice, adjk);
            getIndependenceTest().isIndependent(i, k, v);
            double p = getIndependenceTest().getScore();
            if (p > _p) {
                _p = p;
                _v = v;
            }
        }
    }
    this.p = _p;
    return _v;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 25 with ChoiceGenerator

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

the class SepsetsMinScore method getMinSepset.

private List<Node> getMinSepset(Node i, Node k) {
    double _p = Double.POSITIVE_INFINITY;
    List<Node> _v = null;
    List<Node> adji = graph.getAdjacentNodes(i);
    List<Node> adjk = graph.getAdjacentNodes(k);
    adji.remove(k);
    adjk.remove(i);
    for (int d = 0; d <= Math.min((depth == -1 ? 1000 : depth), Math.max(adji.size(), adjk.size())); d++) {
        if (d <= adji.size()) {
            ChoiceGenerator gen = new ChoiceGenerator(adji.size(), d);
            int[] choice;
            while ((choice = gen.next()) != null) {
                List<Node> v2 = GraphUtils.asList(choice, adji);
                getIndependenceTest().isIndependent(i, k, v2);
                double p2 = getIndependenceTest().getScore();
                if (returnNullWhenIndep) {
                    if (p2 < _p && p2 < 0) {
                        _p = p2;
                        _v = v2;
                    }
                } else {
                    if (p2 < _p) {
                        _p = p2;
                        _v = v2;
                    }
                }
            }
        }
        if (d <= adjk.size()) {
            ChoiceGenerator gen = new ChoiceGenerator(adjk.size(), d);
            int[] choice;
            while ((choice = gen.next()) != null) {
                List<Node> v2 = GraphUtils.asList(choice, adjk);
                getIndependenceTest().isIndependent(i, k, v2);
                double p2 = getIndependenceTest().getScore();
                if (returnNullWhenIndep) {
                    if (p2 < _p && p2 < 0) {
                        _p = p2;
                        _v = v2;
                    }
                } else {
                    if (p2 < _p) {
                        _p = p2;
                        _v = v2;
                    }
                }
            }
        }
    }
    this.p = _p;
    return _v;
}
Also used : Node(edu.cmu.tetrad.graph.Node) 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