Search in sources :

Example 36 with ChoiceGenerator

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

the class IndependenceFactsAction method generateResults.

private void generateResults() {
    this.getResults().clear();
    List<String> dataVars = getDataVars();
    if (getVars().size() < 2) {
        tableModel.fireTableDataChanged();
        return;
    }
    int minQuestionMark = 0, minPlus = 0, maxPlus = 0;
    for (int i = 2; i < getVars().size(); i++) {
        String var = getVars().get(i);
        if ("?".equals(var)) {
            minPlus++;
            maxPlus++;
        } else if ("+".equals(var)) {
            maxPlus++;
        } else {
            minQuestionMark++;
            minPlus++;
            maxPlus++;
        }
    }
    int resultIndex = -1;
    Set<Set<String>> alreadySeen = new HashSet<>();
    int xIndex = dataVars.indexOf(vars.get(0));
    int yIndex = dataVars.indexOf(vars.get(1));
    if (xIndex == -1 || yIndex == -1) {
        xIndex = 0;
        yIndex = 1;
    }
    for (int _i = 0; _i < dataVars.size(); _i++) {
        for (int _j = _i + 1; _j < dataVars.size(); _j++) {
            String _x, _y;
            if (xIndex < yIndex) {
                _x = dataVars.get(_i);
                _y = dataVars.get(_j);
            } else {
                _y = dataVars.get(_i);
                _x = dataVars.get(_j);
            }
            if (!(vars.get(0).equals("?")) && !(vars.get(0).equals("+")) && !(vars.get(0).equals(_x))) {
                continue;
            }
            if (!(vars.get(1).equals("?")) && !(vars.get(1).equals("+")) && !(vars.get(1).equals(_y))) {
                continue;
            }
            Set<String> seen = new HashSet<>();
            seen.add(_x);
            seen.add(_y);
            if (alreadySeen.contains(seen))
                continue;
            alreadySeen.add(seen);
            if (!wildcard(1) && !getVars().get(1).equals(_y)) {
                continue;
            }
            List<String> unspecifiedVars = new ArrayList<>(dataVars);
            unspecifiedVars.remove(_x);
            unspecifiedVars.remove(_y);
            Node x = getIndependenceTest().getVariable(_x);
            Node y = getIndependenceTest().getVariable(_y);
            for (int j = 2; j < getVars().size(); j++) {
                if (!wildcard(j)) {
                    unspecifiedVars.remove(getVars().get(j));
                }
            }
            for (int n = minPlus; n <= maxPlus; n++) {
                ChoiceGenerator gen2 = new ChoiceGenerator(unspecifiedVars.size(), n - minQuestionMark);
                int[] choice2;
                while ((choice2 = gen2.next()) != null) {
                    List<Node> z = new ArrayList<>();
                    for (int i = 0; i < minQuestionMark; i++) {
                        String _z = getVars().get(i + 2);
                        z.add(getIndependenceTest().getVariable(_z));
                    }
                    for (int choice : choice2) {
                        String _z = unspecifiedVars.get(choice);
                        z.add(getIndependenceTest().getVariable(_z));
                    }
                    Result.Type indep;
                    double pValue;
                    try {
                        indep = getIndependenceTest().isIndependent(x, y, z) ? Result.Type.INDEPENDENT : Result.Type.DEPENDENT;
                    } catch (Exception e) {
                        indep = Result.Type.UNDETERMINED;
                    }
                    try {
                        pValue = getIndependenceTest().getPValue();
                    } catch (Exception e) {
                        pValue = Double.NaN;
                    }
                    if (usesDSeparation()) {
                        getResults().add(new Result(++resultIndex, dsepFactString(x, y, z), indep, pValue));
                    } else {
                        getResults().add(new Result(++resultIndex, independenceFactString(x, y, z), indep, pValue));
                    }
                }
            }
        }
    }
    tableModel.fireTableDataChanged();
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 37 with ChoiceGenerator

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

the class GraphUtils method listColliderTriples.

public static LinkedList<Triple> listColliderTriples(Graph graph) {
    LinkedList<Triple> colliders = new LinkedList<>();
    for (Node node : graph.getNodes()) {
        List<Node> adj = graph.getAdjacentNodes(node);
        if (adj.size() < 2) {
            continue;
        }
        ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
        int[] choice;
        while ((choice = gen.next()) != null) {
            List<Node> others = asList(choice, adj);
            if (graph.isDefCollider(others.get(0), node, others.get(1))) {
                colliders.add(new Triple(others.get(0), node, others.get(1)));
            }
        }
    }
    return colliders;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) LinkedList(java.util.LinkedList)

Example 38 with ChoiceGenerator

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

the class GraphUtils method getUnderlinedTriplesFromGraph.

/**
 * @return A list of triples of the form <X, Y, Z>, where <X, Y, Z> is a
 * definite noncollider in the given graph.
 */
public static List<Triple> getUnderlinedTriplesFromGraph(Node node, Graph graph) {
    List<Triple> underlinedTriples = new ArrayList<>();
    Set<Triple> allUnderlinedTriples = graph.getUnderLines();
    List<Node> adj = graph.getAdjacentNodes(node);
    if (adj.size() < 2) {
        return new LinkedList<>();
    }
    ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
    int[] choice;
    while ((choice = gen.next()) != null) {
        Node x = adj.get(choice[0]);
        Node z = adj.get(choice[1]);
        if (allUnderlinedTriples.contains(new Triple(x, node, z))) {
            underlinedTriples.add(new Triple(x, node, z));
        }
    }
    return underlinedTriples;
}
Also used : ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) LinkedList(java.util.LinkedList)

Example 39 with ChoiceGenerator

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

the class GraphUtils method getDottedUnderlinedTriplesFromGraph.

/**
 * @return A list of triples of the form <X, Y, Z>, where <X, Y, Z> is a
 * definite noncollider in the given graph.
 */
public static List<Triple> getDottedUnderlinedTriplesFromGraph(Node node, Graph graph) {
    List<Triple> dottedUnderlinedTriples = new ArrayList<>();
    Set<Triple> allDottedUnderlinedTriples = graph.getDottedUnderlines();
    List<Node> adj = graph.getAdjacentNodes(node);
    if (adj.size() < 2) {
        return new LinkedList<>();
    }
    ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
    int[] choice;
    while ((choice = gen.next()) != null) {
        Node x = adj.get(choice[0]);
        Node z = adj.get(choice[1]);
        if (allDottedUnderlinedTriples.contains(new Triple(x, node, z))) {
            dottedUnderlinedTriples.add(new Triple(x, node, z));
        }
    }
    return dottedUnderlinedTriples;
}
Also used : ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) LinkedList(java.util.LinkedList)

Example 40 with ChoiceGenerator

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

the class CcdMax method sepset.

// Returns a sepset containing the nodes in 'containing' but not the nodes in 'notContaining', or
// null if there is no such sepset.
private List<Node> sepset(Graph graph, Node a, Node c, Set<Node> containing, Set<Node> notContaining) {
    List<Node> adj = graph.getAdjacentNodes(a);
    adj.addAll(graph.getAdjacentNodes(c));
    adj.remove(c);
    adj.remove(a);
    for (int d = 0; d <= Math.min((depth == -1 ? 1000 : depth), Math.max(adj.size(), adj.size())); d++) {
        if (d <= adj.size()) {
            ChoiceGenerator gen = new ChoiceGenerator(adj.size(), d);
            int[] choice;
            while ((choice = gen.next()) != null) {
                Set<Node> v2 = GraphUtils.asSet(choice, adj);
                v2.addAll(containing);
                v2.removeAll(notContaining);
                v2.remove(a);
                v2.remove(c);
                if (!isForbidden(a, c, new ArrayList<>(v2))) {
                    getIndependenceTest().isIndependent(a, c, new ArrayList<>(v2));
                    double p2 = getIndependenceTest().getScore();
                    if (p2 < 0) {
                        return new ArrayList<>(v2);
                    }
                }
            }
        }
    }
    return null;
}
Also used : 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