Search in sources :

Example 36 with DepthChoiceGenerator

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

the class Fask method bidirected.

private boolean bidirected(double[] x, double[] y, Graph G0, Node X, Node Y) {
    Set<Node> adjSet = new HashSet<Node>(G0.getAdjacentNodes(X));
    adjSet.addAll(G0.getAdjacentNodes(Y));
    List<Node> adj = new ArrayList<>(adjSet);
    adj.remove(X);
    adj.remove(Y);
    DepthChoiceGenerator gen = new DepthChoiceGenerator(adj.size(), Math.min(depth, adj.size()));
    int[] choice;
    while ((choice = gen.next()) != null) {
        List<Node> _adj = GraphUtils.asList(choice, adj);
        double[][] _Z = new double[_adj.size()][];
        for (int f = 0; f < _adj.size(); f++) {
            Node _z = _adj.get(f);
            int column = dataSet.getColumn(_z);
            _Z[f] = data[column];
        }
        double pc = partialCorrelation(x, y, _Z, x, Double.NEGATIVE_INFINITY, +1);
        double pc1 = partialCorrelation(x, y, _Z, x, 0, +1);
        double pc2 = partialCorrelation(x, y, _Z, y, 0, +1);
        int nc = StatUtils.getRows(x, x, Double.NEGATIVE_INFINITY, +1).size();
        int nc1 = StatUtils.getRows(x, x, 0, +1).size();
        int nc2 = StatUtils.getRows(y, y, 0, +1).size();
        double z = 0.5 * (log(1.0 + pc) - log(1.0 - pc));
        double z1 = 0.5 * (log(1.0 + pc1) - log(1.0 - pc1));
        double z2 = 0.5 * (log(1.0 + pc2) - log(1.0 - pc2));
        double zv1 = (z - z1) / sqrt((1.0 / ((double) nc - 3) + 1.0 / ((double) nc1 - 3)));
        double zv2 = (z - z2) / sqrt((1.0 / ((double) nc - 3) + 1.0 / ((double) nc2 - 3)));
        boolean rejected1 = abs(zv1) > cutoff;
        boolean rejected2 = abs(zv2) > cutoff;
        boolean possibleTwoCycle = false;
        if (zv1 < 0 && zv2 > 0 && rejected1) {
            possibleTwoCycle = true;
        } else if (zv1 > 0 && zv2 < 0 && rejected2) {
            possibleTwoCycle = true;
        } else if (rejected1 && rejected2) {
            possibleTwoCycle = true;
        }
        if (!possibleTwoCycle) {
            return false;
        }
    }
    return true;
}
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