Search in sources :

Example 76 with ChoiceGenerator

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

the class PagUtils method graphInPagStep1.

public static boolean graphInPagStep1(Graph pag, Graph dag) {
    // If A and B are in O, there is an edge between A and B in gamma
    // iff for every W subset of O minus {A, B}, A and B are d-connected
    // given W in [every graph] in delta (dag).
    IndTestDSep test = new IndTestDSep(pag);
    List<Node> V = new ArrayList<>(dag.getNodes());
    for (Edge edge : pag.getEdges()) {
        Node A = edge.getNode1();
        Node B = edge.getNode2();
        List<Node> W = new ArrayList<>(V);
        W.remove(A);
        W.remove(B);
        ChoiceGenerator gen = new ChoiceGenerator(W.size(), W.size());
        int[] choice;
        while ((choice = gen.next()) != null) {
            List<Node> S = GraphUtils.asList(choice, W);
            if (test.isDSeparated(A, B, S)) {
                return false;
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 77 with ChoiceGenerator

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

the class SampleVcpc method orientUnshieldedTriplesConcurrent.

// Sample Version of Step 3 of VCPC.
// private CpcTripleType getSampleTripleType(Node x, Node y, Node z, IndependenceTest test,
// int depth, Graph graph, boolean verbose) {
// 
// if (verbose) {
// System.out.println("Checking " + x + " --- " + y + " --- " + z);
// }
// 
// int numSepsetsContainingY = 0;
// int numSepsetsNotContainingY = 0;
// 
// this.partialCorrs = getPartialCorrs();
// 
// 
// List<Node> _nodes = graph.getAdjacentNodes(x);
// _nodes.remove(z);
// int _depth = depth;
// if (_depth == -1) {
// _depth = 1000;
// }
// _depth = Math.min(_depth, _nodes.size());
// 
// 
// while (true) {
// for (int d = 0; d <= _depth; d++) {
// ChoiceGenerator cg1 = new ChoiceGenerator(_nodes.size(), d);
// int[] choice;
// while ((choice = cg1.next()) != null) {
// List<Node> cond = DataGraphUtils.asList(choice, _nodes);
// TetradMatrix submatrix = DataUtils.subMatrix(covMatrix, indexMap, x, z, cond);
// double r = StatUtils.partialCorrelation(submatrix);
// partialCorrs.put(cond, r);
// 
// if (test.isIndependent(x, z, cond)) {
// if (verbose) {
// System.out.println("Indep: " + x + " _||_ " + z + " | " + cond);
// }
// if (cond.contains(y)) {
// numSepsetsContainingY++;
// } else {
// numSepsetsNotContainingY++;
// }
// }
// 
// 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 cg1 = new ChoiceGenerator(_nodes.size(), d);
// int[] choice;
// while ((choice = cg1.next()) != null) {
// List<Node> cond = DataGraphUtils.asList(choice, _nodes);
// TetradMatrix submatrix = DataUtils.subMatrix(covMatrix, indexMap, x, z, cond);
// double r = StatUtils.partialCorrelation(submatrix);
// partialCorrs.put(cond, r);
// 
// if (test.isIndependent(x, z, cond)) {
// 
// if (verbose) {
// System.out.println("Indep: " + x + " _||_ " + z + " | " + cond);
// }
// 
// if (cond.contains(y)) {
// numSepsetsContainingY++;
// } else {
// numSepsetsNotContainingY++;
// }
// }
// if (numSepsetsContainingY > 0 && numSepsetsNotContainingY > 0) {
// return CpcTripleType.AMBIGUOUS;
// }
// }
// }
// break;
// }
// 
// double L = 0.01;
// //        System.out.println("L = " + L);
// 
// if (numSepsetsContainingY > 0 && numSepsetsNotContainingY == 0) {
// for (List<Node> sepset1 : partialCorrs.keySet()) {
// if (sepset1.contains(y)) {
// double r1 = partialCorrs.get(sepset1);
// for (List<Node> sepset2 : partialCorrs.keySet()) {
// if (!sepset2.contains(y)) {
// double r2 = partialCorrs.get(sepset2);
// double M = Math.abs(r1 - r2);
// 
// if (!(M >= L)) {
// return CpcTripleType.AMBIGUOUS;
// }
// }
// }
// return CpcTripleType.NONCOLLIDER;
// }
// }
// }
// 
// if (numSepsetsNotContainingY > 0 && numSepsetsContainingY == 0) {
// for (List<Node> sepset1 : partialCorrs.keySet()) {
// if (!sepset1.contains(y)) {
// double r1 = partialCorrs.get(sepset1);
// for (List<Node> sepset2 : partialCorrs.keySet()) {
// if (sepset2.contains(y)) {
// double r2 = partialCorrs.get(sepset2);
// double M = Math.abs(r1 - r2);
// if (!(M >= L)) {
// return CpcTripleType.AMBIGUOUS;
// }
// }
// }
// return CpcTripleType.COLLIDER;
// }
// }
// }
// return null;
// }
// public enum CpcTripleType {
// COLLIDER, NONCOLLIDER, AMBIGUOUS
// }
private void orientUnshieldedTriplesConcurrent(final IKnowledge knowledge, final IndependenceTest test, final int depth) {
    ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
    TetradLogger.getInstance().log("info", "Starting Collider Orientation:");
    Graph graph = new EdgeListGraph(getGraph());
    // System.out.println("orientUnshieldedTriples 1");
    colliderTriples = new HashSet<>();
    noncolliderTriples = new HashSet<>();
    ambiguousTriples = new HashSet<>();
    List<Node> nodes = graph.getNodes();
    for (Node _y : nodes) {
        final Node y = _y;
        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) {
            final Node x = adjacentNodes.get(combination[0]);
            final Node z = adjacentNodes.get(combination[1]);
            if (graph.isAdjacentTo(x, z)) {
                continue;
            }
            Runnable worker = new Runnable() {

                @Override
                public void run() {
                    getAllTriples().add(new Triple(x, y, z));
                    SearchGraphUtils.CpcTripleType type = SearchGraphUtils.getCpcTripleType(x, y, z, test, depth, getGraph(), verbose);
                    // 
                    if (type == SearchGraphUtils.CpcTripleType.COLLIDER) {
                        if (colliderAllowed(x, y, z, knowledge)) {
                            getGraph().setEndpoint(x, y, Endpoint.ARROW);
                            getGraph().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);
                        getGraph().addAmbiguousTriple(triple.getX(), triple.getY(), triple.getZ());
                    } else {
                        noncolliderTriples.add(new Triple(x, y, z));
                    }
                }
            };
            executor.execute(worker);
        }
    }
    // This will make the executor accept no new threads
    // and finish all existing threads in the queue
    executor.shutdown();
    try {
        // Wait until all threads are finish
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        System.out.println("Finished all threads");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    TetradLogger.getInstance().log("info", "Finishing Collider Orientation.");
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 78 with ChoiceGenerator

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

the class SampleVcpc 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 79 with ChoiceGenerator

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

the class Purify method listPValues.

private List<Double> listPValues(List<Integer> cluster, boolean[] eliminated, double cutoff) {
    if (cluster.size() < 4)
        return new ArrayList<>();
    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[2];
        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;
        }
        double p1 = tetradTest.tetradPValue(ci, cj, ck, cl);
        double p2 = tetradTest.tetradPValue(ci, cj, cl, ck);
        double p3 = tetradTest.tetradPValue(ci, ck, cl, cj);
        if (p1 < cutoff) {
            pValues.add(p1);
        }
        if (p2 < cutoff) {
            pValues.add(p2);
        }
        if (p3 < cutoff) {
            pValues.add(p3);
        }
    }
    return pValues;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 80 with ChoiceGenerator

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

the class PurifyTetradBased2 method listTetrads.

private Set<Tetrad> listTetrads(List<Node> cluster, Set<Node> eliminated, double cutoff) {
    if (cluster.size() < 4)
        return null;
    cluster = new ArrayList<>(cluster);
    boolean countable = false;
    Set<Tetrad> tetrads = new HashSet<>();
    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];
        Node ci = cluster.get(_i);
        Node cj = cluster.get(_j);
        Node ck = cluster.get(_k);
        Node cl = cluster.get(_l);
        if (eliminated.contains(ci) || eliminated.contains(cj) || eliminated.contains(ck) || eliminated.contains(cl)) {
            continue;
        }
        countable = true;
        double p1, p2, p3;
        p1 = tetradTest.tetradPValue(nodes.indexOf(ci), nodes.indexOf(cj), nodes.indexOf(ck), nodes.indexOf(cl));
        p2 = tetradTest.tetradPValue(nodes.indexOf(ci), nodes.indexOf(cj), nodes.indexOf(cl), nodes.indexOf(ck));
        p3 = tetradTest.tetradPValue(nodes.indexOf(ci), nodes.indexOf(ck), nodes.indexOf(cl), nodes.indexOf(cj));
        if (p1 < cutoff) {
            tetrads.add(new Tetrad(ci, cj, ck, cl, p1));
        }
        if (p2 < cutoff) {
            tetrads.add(new Tetrad(ci, cj, cl, ck, p2));
        }
        if (p3 < cutoff) {
            tetrads.add(new Tetrad(ci, ck, cl, cj, p3));
        }
    }
    return countable ? tetrads : null;
}
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