Search in sources :

Example 16 with DepthChoiceGenerator

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

the class Nlo method fullOrient.

public Graph fullOrient(Graph graph) {
    graph = GraphUtils.undirectedGraph(graph);
    graph = GraphUtils.replaceNodes(graph, dataSet.getVariables());
    for (Node node : graph.getNodes()) {
        List<Node> adjNodes = graph.getAdjacentNodes(node);
        DepthChoiceGenerator gen = new DepthChoiceGenerator(adjNodes.size(), -1);
        int[] choice;
        List<Node> bestParents = null;
        double bestAvgP = -1;
        while ((choice = gen.next()) != null) {
            if (choice.length < 1)
                continue;
            List<Node> parents = GraphUtils.asList(choice, adjNodes);
            double[] r = cci.residuals(name(node), names(parents));
            boolean allIndep = true;
            double sumP = 0.0;
            for (Node parent : parents) {
                double[] p = column(parent);
                boolean indep = cci.independent(r, p);
                if (!indep) {
                    allIndep = false;
                    break;
                }
                double _p = cci.getQ();
                sumP += _p;
            }
            if (allIndep) {
                bestParents = parents;
                break;
            }
        // double avgP = sumP / parents.size();
        // 
        // if (avgP > bestAvgP) {
        // bestAvgP = avgP;
        // bestParents = parents;
        // }
        }
        if (bestParents != null) {
            for (Node _node : bestParents) {
                graph.setEndpoint(_node, node, Endpoint.ARROW);
            }
        }
    }
    return graph;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 17 with DepthChoiceGenerator

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

the class Nlo method fullOrient3.

public Graph fullOrient3(Graph graph) {
    graph = GraphUtils.undirectedGraph(graph);
    graph = GraphUtils.replaceNodes(graph, dataSet.getVariables());
    for (Node Y : graph.getNodes()) {
        System.out.println("Y = " + Y);
        List<Node> adjNodes = graph.getAdjacentNodes(Y);
        DepthChoiceGenerator gen = new DepthChoiceGenerator(adjNodes.size(), 4);
        int[] choice;
        List<Node> bestParents = null;
        while ((choice = gen.next()) != null) {
            List<Node> W = GraphUtils.asList(choice, adjNodes);
            double[] r = cci.residuals(name(Y), names(W));
            boolean indep = testIndependence(W, r);
            System.out.println("W = " + W + " indep = " + indep + " p = " + q);
            if (indep) {
                bestParents = W;
            }
        }
        for (Node X : bestParents) {
            List<Node> _Y = Collections.singletonList(Y);
            double[] r = cci.residuals(name(X), names(_Y));
            boolean indep = testIndependence(_Y, r);
            if (!indep) {
                graph.setEndpoint(X, Y, Endpoint.ARROW);
            }
        }
    }
    return graph;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 18 with DepthChoiceGenerator

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

the class PcLocal method reorientNode2.

private void reorientNode2(Node y) {
    List<Node> adjy = graph.getAdjacentNodes(y);
    adjy.removeAll(graph.getChildren(y));
    DepthChoiceGenerator gen = new DepthChoiceGenerator(adjy.size(), adjy.size());
    int[] choice;
    double maxScore = Double.NEGATIVE_INFINITY;
    List<Node> maxParents = new ArrayList<>();
    unorientAdjacents(y);
    while ((choice = gen.next()) != null) {
        List<Node> parents = GraphUtils.asList(choice, adjy);
        Iterator<Node> pi = parents.iterator();
        int[] parentIndices = new int[parents.size()];
        int count = 0;
        while (pi.hasNext()) {
            Node nextParent = pi.next();
            parentIndices[count++] = hashIndices.get(nextParent);
        }
        int yIndex = hashIndices.get(y);
        double _score = score.localScore(yIndex, parentIndices);
        if (_score > maxScore) {
            maxScore = _score;
            maxParents = parents;
        }
    }
    for (Node v : maxParents) {
        graph.removeEdge(v, y);
        graph.addDirectedEdge(v, y);
    }
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 19 with DepthChoiceGenerator

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

the class SemBicScoreDeterministic method printMinimalLinearlyDependentSet.

// Prints a smallest subset of parents that causes a singular matrix exception.
private boolean printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
    List<Node> _parents = new ArrayList<>();
    for (int p : parents) _parents.add(variables.get(p));
    DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
    int[] choice;
    while ((choice = gen.next()) != null) {
        int[] sel = new int[choice.length];
        List<Node> _sel = new ArrayList<>();
        for (int m = 0; m < choice.length; m++) {
            sel[m] = parents[m];
            _sel.add(variables.get(sel[m]));
        }
        TetradMatrix m = cov.getSelection(sel, sel);
        try {
            m.inverse();
        } catch (Exception e2) {
            forbidden.add(sel[0]);
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return true;
        }
    }
    return false;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 20 with DepthChoiceGenerator

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

the class SemBicScoreDeterministic method getMaximalLinearlyDependentSet.

private int[] getMaximalLinearlyDependentSet(int i, int[] parents, ICovarianceMatrix cov) {
    double small = getDeterminismThreshold();
    List<Node> _parents = new ArrayList<>();
    for (int p : parents) _parents.add(variables.get(p));
    DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
    int[] choice;
    while ((choice = gen.next()) != null) {
        int[] sel0 = new int[choice.length];
        List<Integer> all = new ArrayList<>();
        for (int w = 0; w < parents.length; w++) all.add(parents[w]);
        for (int w = 0; w < sel0.length; w++) all.remove(sel0[w]);
        int[] sel = new int[all.size()];
        for (int w = 0; w < all.size(); w++) sel[w] = all.get(w);
        List<Node> _sel = new ArrayList<>();
        for (int m = 0; m < choice.length; m++) {
            sel[m] = parents[m];
            _sel.add(variables.get(sel[m]));
        }
        TetradMatrix m = cov.getSelection(sel, sel);
        double s2 = getCovariances().getValue(i, i);
        TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
        TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
        s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
        if (s2 <= small) {
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return sel;
        }
        try {
            m.inverse();
        } catch (Exception e2) {
            // forbidden.add(sel[0]);
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return sel;
        }
    }
    return new int[0];
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

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