Search in sources :

Example 21 with DepthChoiceGenerator

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

the class SemBicScoreImages2 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)

Example 22 with DepthChoiceGenerator

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

the class Peter1Score 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)

Example 23 with DepthChoiceGenerator

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

the class IndependenceFactsEditor method generateResults.

private void generateResults() {
    results = new ArrayList<>();
    List<String> dataVars = getDataVars();
    if (getVars().size() < 2) {
        tableModel.fireTableDataChanged();
        return;
    }
    // Need a choice generator over the ?'s, and a depth choice generator over the +'s. The +'s all have to come
    // at the end at index >= 2.
    int numQuestionMarksFirstTwo = 0;
    int numQuestionMarksRest = 0;
    int numPluses = 0;
    int numFixed = 0;
    for (int i = 0; i < vars.size(); i++) {
        String var = vars.get(i);
        if ("?".equals(var) && i < 2)
            numQuestionMarksFirstTwo++;
        else if ("?".equals(var))
            numQuestionMarksRest++;
        else if ("+".equals(var))
            numPluses++;
        else
            numFixed++;
    }
    int[] questionMarkFirstTwoIndices = new int[numQuestionMarksFirstTwo];
    int[] questionMarkRestIndices = new int[numQuestionMarksRest];
    int[] plusIndices = new int[numPluses];
    int[] fixedIndices = new int[numFixed];
    String[] fixedVars = new String[numFixed];
    int _i = -1;
    int _j = -1;
    int _k = -1;
    int _l = -1;
    for (int i = 0; i < vars.size(); i++) {
        if ("?".equals(vars.get(i)) && i < 2)
            questionMarkFirstTwoIndices[++_i] = i;
        else if ("?".equals(vars.get(i)))
            questionMarkRestIndices[++_j] = i;
        else if ("+".equals(vars.get(i)))
            plusIndices[++_k] = i;
        else {
            fixedIndices[++_l] = i;
            fixedVars[_l] = vars.get(i);
        }
    }
    List<String> vars1 = new ArrayList<>(dataVars);
    vars1.removeAll(vars);
    ChoiceGenerator gen1 = new ChoiceGenerator(vars1.size(), questionMarkFirstTwoIndices.length);
    int[] choice1;
    LOOP: while ((choice1 = gen1.next()) != null) {
        List<String> s2 = asList(choice1, vars1);
        List<String> vars2 = new ArrayList<>(vars1);
        vars2.removeAll(s2);
        ChoiceGenerator gen2 = new ChoiceGenerator(vars2.size(), questionMarkRestIndices.length);
        int[] choice2;
        while ((choice2 = gen2.next()) != null) {
            List<String> s3 = asList(choice2, vars2);
            List<String> vars3 = new ArrayList<>(vars2);
            vars3.removeAll(s3);
            DepthChoiceGenerator gen3 = new DepthChoiceGenerator(vars3.size(), plusIndices.length);
            int[] choice3;
            while ((choice3 = gen3.next()) != null) {
                results.add(new ArrayList<IndependenceResult>());
                for (int prod = 0; prod < indTestProducers.size(); prod++) {
                    String[] vars4 = new String[fixedIndices.length + questionMarkFirstTwoIndices.length + questionMarkRestIndices.length + choice3.length];
                    for (int i = 0; i < fixedIndices.length; i++) {
                        vars4[fixedIndices[i]] = fixedVars[i];
                    }
                    for (int i = 0; i < choice1.length; i++) {
                        vars4[questionMarkFirstTwoIndices[i]] = vars1.get(choice1[i]);
                    }
                    for (int i = 0; i < choice2.length; i++) {
                        vars4[questionMarkRestIndices[i]] = vars2.get(choice2[i]);
                    }
                    for (int i = 0; i < choice3.length; i++) {
                        vars4[plusIndices[i]] = vars3.get(choice3[i]);
                    }
                    IndependenceTest independenceTest = getIndependenceTest(prod);
                    Node x = independenceTest.getVariable(vars4[0]);
                    Node y = independenceTest.getVariable(vars4[1]);
                    List<Node> z = new ArrayList<>();
                    for (int i = 2; i < vars4.length; i++) {
                        z.add(independenceTest.getVariable(vars4[i]));
                    }
                    IndependenceResult.Type indep;
                    double pValue;
                    try {
                        indep = independenceTest.isIndependent(x, y, z) ? IndependenceResult.Type.INDEPENDENT : IndependenceResult.Type.DEPENDENT;
                        pValue = independenceTest.getPValue();
                    } catch (Exception e) {
                        indep = IndependenceResult.Type.UNDETERMINED;
                        pValue = Double.NaN;
                    }
                    results.get(results.size() - 1).add(new IndependenceResult(results.size(), factString(x, y, z), indep, pValue));
                }
                if (results.size() > getListLimit())
                    break LOOP;
            }
        }
    }
    model.setResults(results);
    tableModel.fireTableDataChanged();
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node) IndependenceTest(edu.cmu.tetrad.search.IndependenceTest) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) List(java.util.List) IndependenceResult(edu.cmu.tetradapp.model.IndependenceResult)

Example 24 with DepthChoiceGenerator

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

the class Jcpc method pathBlockingSet.

// ================================PRIVATE METHODS=======================//
private List<Node> pathBlockingSet(IndependenceTest test, Graph graph, Node x, Node y) {
    List<Node> commonAdjacents = graph.getAdjacentNodes(x);
    commonAdjacents.retainAll(graph.getAdjacentNodes(y));
    DepthChoiceGenerator generator = new DepthChoiceGenerator(commonAdjacents.size(), commonAdjacents.size());
    int[] choice;
    while ((choice = generator.next()) != null) {
        Set<Node> colliders = new HashSet<Node>(GraphUtils.asList(choice, commonAdjacents));
        List<Node> _descendants = graph.getDescendants(new ArrayList<Node>(colliders));
        Set<Node> descendants = new HashSet<Node>(_descendants);
        Set<Node> sepset = pathBlockingSetExcluding(graph, x, y, colliders, descendants);
        ArrayList<Node> _sepset = new ArrayList<Node>(sepset);
        if (test.isIndependent(x, y, _sepset)) {
            return _sepset;
        }
    }
    return null;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 25 with DepthChoiceGenerator

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

the class Nlo method fullOrient5.

public Graph fullOrient5(Graph graph) {
    graph = GraphUtils.undirectedGraph(graph);
    graph = GraphUtils.replaceNodes(graph, dataSet.getVariables());
    for (Edge edge : graph.getEdges()) {
        Node X = edge.getNode1();
        Node Y = edge.getNode2();
        Node C = null;
        double maxQ = 0.0;
        List<Node> adjX = graph.getAdjacentNodes(X);
        DepthChoiceGenerator gen = new DepthChoiceGenerator(adjX.size(), 4);
        int[] choice;
        List<Node> parents = new ArrayList<>();
        while ((choice = gen.next()) != null) {
            List<Node> W = GraphUtils.asList(choice, adjX);
            if (W.isEmpty())
                continue;
            double[] r = cci.residuals(name(X), names(W));
            boolean indep = testIndependence(W, r);
            double q = cci.getQ();
            System.out.println("\tW = " + W + " indep = " + (indep) + " q = " + q);
            if (indep && q > maxQ) {
                parents = W;
                C = X;
                maxQ = q;
            }
        }
        List<Node> adjY = graph.getAdjacentNodes(Y);
        DepthChoiceGenerator gen2 = new DepthChoiceGenerator(adjY.size(), 4);
        int[] choice2;
        while ((choice2 = gen2.next()) != null) {
            List<Node> W = GraphUtils.asList(choice2, adjY);
            if (W.isEmpty())
                continue;
            double[] r = cci.residuals(name(Y), names(W));
            boolean indep = testIndependence(W, r);
            double q = cci.getQ();
            System.out.println("\tW = " + W + " indep = " + (indep) + " q = " + q);
            if (indep && q > maxQ) {
                parents = W;
                C = Y;
                maxQ = q;
            }
        }
        if (C != null) {
            for (Node P : parents) {
                if (C == P) {
                    throw new IllegalArgumentException();
                }
                // if (!(P == C)) continue;
                System.out.println("Orienting " + P + "-->" + C);
                graph.setEndpoint(P, C, Endpoint.ARROW);
            }
        }
    }
    return graph;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

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