Search in sources :

Example 6 with DepthChoiceGenerator

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

the class TestSearchGraph method testDSeparation.

/**
 * Tests to see if d separation facts are symmetric.
 */
@Test
public void testDSeparation() {
    List<Node> nodes1 = new ArrayList<>();
    for (int i1 = 0; i1 < 7; i1++) {
        nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    EdgeListGraphSingleConnections graph = new EdgeListGraphSingleConnections(new Dag(GraphUtils.randomGraph(nodes1, 0, 7, 30, 15, 15, true)));
    List<Node> nodes = graph.getNodes();
    int depth = -1;
    for (int i = 0; i < nodes.size(); i++) {
        for (int j = i + 1; j < nodes.size(); j++) {
            Node x = nodes.get(i);
            Node y = nodes.get(j);
            List<Node> theRest = new ArrayList<>(nodes);
            theRest.remove(x);
            theRest.remove(y);
            DepthChoiceGenerator gen = new DepthChoiceGenerator(theRest.size(), depth);
            int[] choice;
            while ((choice = gen.next()) != null) {
                List<Node> z = new LinkedList<>();
                for (int k = 0; k < choice.length; k++) {
                    z.add(theRest.get(choice[k]));
                }
                if (graph.isDSeparatedFrom(x, y, z) != graph.isDSeparatedFrom(y, x, z)) {
                    fail(SearchLogUtils.independenceFact(x, y, z) + " should have same d-sep result as " + SearchLogUtils.independenceFact(y, x, z));
                }
            }
        }
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Test(org.junit.Test)

Example 7 with DepthChoiceGenerator

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

the class CpcLocal method sepset.

// ================================PRIVATE METHODS=======================//
private List<Node> sepset(Node x, Node y) {
    List<Node> adj = graph.getAdjacentNodes(x);
    adj.remove(y);
    DepthChoiceGenerator gen = new DepthChoiceGenerator(adj.size(), adj.size());
    int[] choice;
    while ((choice = gen.next()) != null) {
        List<Node> cond = GraphUtils.asList(choice, adj);
        if (getIndependenceTest().isIndependent(x, y, cond)) {
            return cond;
        }
    }
    adj = graph.getAdjacentNodes(y);
    adj.remove(x);
    gen = new DepthChoiceGenerator(adj.size(), adj.size());
    while ((choice = gen.next()) != null) {
        List<Node> cond = GraphUtils.asList(choice, adj);
        if (getIndependenceTest().isIndependent(x, y, cond)) {
            return cond;
        }
    }
    return null;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 8 with DepthChoiceGenerator

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

the class Ccd method doNodeCollider.

private void doNodeCollider(Graph graph, Map<Triple, Double> colliders, Map<Triple, Double> noncolliders, Node b) {
    List<Node> adjacentNodes = graph.getAdjacentNodes(b);
    if (adjacentNodes.size() < 2) {
        return;
    }
    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> adja = graph.getAdjacentNodes(a);
        double score = Double.POSITIVE_INFINITY;
        List<Node> S = null;
        DepthChoiceGenerator cg2 = new DepthChoiceGenerator(adja.size(), -1);
        int[] comb2;
        while ((comb2 = cg2.next()) != null) {
            List<Node> s = GraphUtils.asList(comb2, adja);
            independenceTest.isIndependent(a, c, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        List<Node> adjc = graph.getAdjacentNodes(c);
        DepthChoiceGenerator cg3 = new DepthChoiceGenerator(adjc.size(), -1);
        int[] comb3;
        while ((comb3 = cg3.next()) != null) {
            List<Node> s = GraphUtils.asList(comb3, adjc);
            independenceTest.isIndependent(c, a, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        // This could happen if there are undefined values and such.
        if (S == null) {
            continue;
        }
        if (S.contains(b)) {
            noncolliders.put(new Triple(a, b, c), score);
        } else {
            colliders.put(new Triple(a, b, c), score);
        }
    }
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 9 with DepthChoiceGenerator

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

the class Jcpc method pathBlockingSet2.

private List<Node> pathBlockingSet2(IndependenceTest test, Graph graph, Node x, Node y) {
    Set<Node> boundary = markovBoundaryWithoutXY(graph, x, y);
    ArrayList<Node> _boundary = new ArrayList<Node>(boundary);
    if (!_boundary.contains(y)) {
        if (test.isIndependent(x, y, _boundary)) {
            return _boundary;
        }
    } else {
        _boundary.remove(y);
        DepthChoiceGenerator gen = new DepthChoiceGenerator(_boundary.size(), 2);
        int[] choice;
        while ((choice = gen.next()) != null) {
            List<Node> cond = GraphUtils.asList(choice, _boundary);
            if (test.isIndependent(x, y, cond)) {
                return cond;
            }
        }
    }
    return null;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ArrayList(java.util.ArrayList)

Example 10 with DepthChoiceGenerator

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

the class Jcpc method pathBlockingSetSmall.

private List<Node> pathBlockingSetSmall(IndependenceTest test, Graph graph, Node x, Node y) {
    List<Node> adjX = graph.getAdjacentNodes(x);
    adjX.removeAll(graph.getParents(x));
    adjX.removeAll(graph.getChildren(x));
    DepthChoiceGenerator gen = new DepthChoiceGenerator(adjX.size(), -1);
    int[] choice;
    while ((choice = gen.next()) != null) {
        List<Node> selection = GraphUtils.asList(choice, adjX);
        Set<Node> sepset = new HashSet<Node>(selection);
        sepset.addAll(graph.getParents(x));
        sepset.remove(x);
        sepset.remove(y);
        ArrayList<Node> sepsetList = new ArrayList<Node>(sepset);
        if (test.isIndependent(x, y, sepsetList)) {
            return sepsetList;
        }
    }
    return null;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)36 Node (edu.cmu.tetrad.graph.Node)17 ArrayList (java.util.ArrayList)11 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)9 ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)4 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)4 HashSet (java.util.HashSet)3 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)2 TetradVector (edu.cmu.tetrad.util.TetradVector)2 Test (org.junit.Test)2 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)1 IndependenceResult (edu.cmu.tetradapp.model.IndependenceResult)1 List (java.util.List)1