Search in sources :

Example 31 with DepthChoiceGenerator

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

the class HitonVariant method hitonMb.

private List<Node> hitonMb(Node t) {
    // MB <- {}
    Set<Node> mb = new HashSet<>();
    Map<Node, List<Node>> pcSets = new HashMap<>();
    List<Node> pc = hitonPc(t);
    pcSets.put(t, pc);
    Set<Node> _pcpc = new HashSet<>();
    for (Node node : pc) {
        List<Node> f = hitonPc(node);
        pcSets.put(node, f);
        _pcpc.addAll(f);
    }
    List<Node> pcpc = new LinkedList<>(_pcpc);
    Set<Node> currentMb = new HashSet<>(pc);
    currentMb.addAll(pcpc);
    currentMb.remove(t);
    HashSet<Node> diff = new HashSet<>(currentMb);
    diff.removeAll(pc);
    diff.remove(t);
    // for each x in PCPC \ PC
    for (Node x : diff) {
        List<Node> s = null;
        // Find an S such PC such that x _||_ t | S
        DepthChoiceGenerator generator = new DepthChoiceGenerator(pcpc.size(), depth);
        int[] choice;
        while ((choice = generator.next()) != null) {
            List<Node> _s = new LinkedList<>();
            for (int index : choice) {
                _s.add(pcpc.get(index));
            }
            if (independenceTest.isIndependent(t, x, _s)) {
                s = _s;
                break;
            }
        }
        if (s == null) {
            System.out.println("S not found.");
            // mb.add(x);
            continue;
        }
        // y_set <- {y in PC(t) : x in PC(y)}
        Set<Node> ySet = new HashSet<>();
        for (Node y : pc) {
            if (pcSets.get(y).contains(x)) {
                ySet.add(y);
            }
        }
        // For each y in y_set
        for (Node y : ySet) {
            if (x == y)
                continue;
            List<Node> _s = new LinkedList<>(s);
            _s.add(y);
            // If x NOT _||_ t | S U {y}
            if (!independenceTest.isIndependent(t, x, _s)) {
                mb.add(x);
                break;
            }
        }
    }
    mb.addAll(pc);
    return new LinkedList<>(mb);
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node)

Example 32 with DepthChoiceGenerator

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

the class MmmbGraphRestricted method minAssoc.

private List<Node> minAssoc(Node x, Node target, List<Node> pc) {
    double assoc = 1.0;
    List<Node> set = new LinkedList<>();
    if (pc.contains(x))
        throw new IllegalArgumentException();
    if (pc.contains(target))
        throw new IllegalArgumentException();
    if (x == target)
        throw new IllegalArgumentException();
    DepthChoiceGenerator generator = new DepthChoiceGenerator(pc.size(), depth);
    int[] choice;
    while ((choice = generator.next()) != null) {
        List<Node> s = new LinkedList<>();
        for (int index : choice) {
            s.add(pc.get(index));
        }
        // sets that contain the last node added to PC.
        if (pc.size() > 0 && !s.contains(pc.get(pc.size() - 1))) {
            continue;
        }
        double _assoc = association(x, target, s);
        if (_assoc < assoc) {
            assoc = _assoc;
            set = s;
        }
    }
    return set;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node)

Example 33 with DepthChoiceGenerator

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

the class Ccd method doNodeStepD.

private void doNodeStepD(Graph psi, SepsetProducer sepsets, Map<Triple, Set<Node>> supSepsets, Map<Node, List<Node>> local, Node b) {
    List<Node> adj = psi.getAdjacentNodes(b);
    if (adj.size() < 2) {
        return;
    }
    ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
    int[] choice;
    while ((choice = gen.next()) != null) {
        List<Node> _adj = GraphUtils.asList(choice, adj);
        Node a = _adj.get(0);
        Node c = _adj.get(1);
        if (!psi.isDefCollider(a, b, c))
            continue;
        List<Node> S = sepsets.getSepset(a, c);
        if (S == null)
            continue;
        ArrayList<Node> TT = new ArrayList<>(local.get(a));
        TT.removeAll(S);
        TT.remove(b);
        TT.remove(c);
        DepthChoiceGenerator gen2 = new DepthChoiceGenerator(TT.size(), -1);
        int[] choice2;
        while ((choice2 = gen2.next()) != null) {
            Set<Node> T = GraphUtils.asSet(choice2, TT);
            Set<Node> B = new HashSet<>(T);
            B.addAll(S);
            B.add(b);
            if (sepsets.isIndependent(a, c, new ArrayList<>(B))) {
                psi.addDottedUnderlineTriple(a, b, c);
                supSepsets.put(new Triple(a, b, c), B);
                break;
            }
        }
    }
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 34 with DepthChoiceGenerator

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

the class BdeuScoreImages method printMinimalLinearlyDependentSet.

// Prints a smallest subset of parents that causes a singular matrix exception.
private void 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) {
            out.println("### Linear dependence among variables: " + _sel);
        }
    }
}
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 35 with DepthChoiceGenerator

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

the class TestSearchGraph method testDSeparation2.

/**
 * Tests to see if d separation facts are symmetric.
 */
@Test
public void testDSeparation2() {
    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, 14, 30, 15, 15, true)));
    List<Node> nodes = graph.getNodes();
    int depth = -1;
    for (int i = 0; i < nodes.size(); i++) {
        for (int j = i; 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]));
                }
                boolean dConnectedTo = graph.isDConnectedTo(x, y, z);
                boolean dConnectedTo1 = graph.isDConnectedTo(y, x, z);
                if (dConnectedTo != dConnectedTo1) {
                    System.out.println(x + " d connected to " + y + " given " + z);
                    System.out.println(graph);
                    System.out.println("dconnectedto = " + dConnectedTo);
                    System.out.println("dconnecteto1 = " + dConnectedTo1);
                    fail();
                }
            }
        }
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Test(org.junit.Test)

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