Search in sources :

Example 1 with DepthChoiceGenerator

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

the class FciMax method doNode.

private void doNode(Graph graph, Map<Triple, Double> scores, Node b) {
    List<Node> adjacentNodes = graph.getAdjacentNodes(b);
    if (adjacentNodes.size() < 2) {
        return;
    }
    ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
    int[] combination;
    while ((combination = cg.next()) != null) {
        Node a = adjacentNodes.get(combination[0]);
        Node c = adjacentNodes.get(combination[1]);
        // Skip triples that are shielded.
        if (graph.isAdjacentTo(a, c)) {
            continue;
        }
        List<Node> adja = graph.getAdjacentNodes(a);
        double score = Double.POSITIVE_INFINITY;
        List<Node> S = null;
        DepthChoiceGenerator cg2 = new DepthChoiceGenerator(adja.size(), -1);
        int[] comb2;
        while ((comb2 = cg2.next()) != null) {
            List<Node> s = GraphUtils.asList(comb2, adja);
            independenceTest.isIndependent(a, c, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        List<Node> adjc = graph.getAdjacentNodes(c);
        DepthChoiceGenerator cg3 = new DepthChoiceGenerator(adjc.size(), -1);
        int[] comb3;
        while ((comb3 = cg3.next()) != null) {
            List<Node> s = GraphUtils.asList(comb3, adjc);
            independenceTest.isIndependent(c, a, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        // S actually has to be non-null here, but the compiler doesn't know that.
        if (S != null && !S.contains(b)) {
            scores.put(new Triple(a, b, c), score);
        }
    }
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 2 with DepthChoiceGenerator

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

the class MmmbGraphRestricted method mmmb.

// ===========================PRIVATE METHODS==========================//
private List<Node> mmmb(Node t) {
    // MB <- {}
    Set<Node> mb = new HashSet<>();
    Set<Node> _pcpc = new HashSet<>();
    for (Node node : getPc(t)) {
        List<Node> f = getPc(node);
        this.pc.put(node, f);
        _pcpc.addAll(f);
    }
    List<Node> pcpc = new LinkedList<>(_pcpc);
    Set<Node> currentMb = new HashSet<>(getPc(t));
    currentMb.addAll(pcpc);
    currentMb.remove(t);
    HashSet<Node> diff = new HashSet<>(currentMb);
    diff.removeAll(getPc(t));
    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));
            }
            numIndTests++;
            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 : getPc(t)) {
            if (this.pc.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}
            numIndTests++;
            if (!independenceTest.isIndependent(t, x, _s)) {
                mb.add(x);
                break;
            }
        }
    }
    mb.addAll(getPc(t));
    return new LinkedList<>(mb);
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node)

Example 3 with DepthChoiceGenerator

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

the class ShiftSearch method search.

public int[] search() {
    if (maxShift < 1) {
        throw new IllegalStateException("Max shift should be >= 1: " + maxShift);
    }
    int numVars = ((DataSet) dataSets.get(0)).getNumColumns();
    List<Node> nodes = dataSets.get(0).getVariables();
    int[] shifts;
    int[] bestshifts = new int[numVars];
    int maxNumRows = ((DataSet) dataSets.get(0)).getNumRows() - maxShift;
    double b = getAvgBic(dataSets);
    printShifts(bestshifts, b, nodes);
    DepthChoiceGenerator generator = new DepthChoiceGenerator(nodes.size(), getMaxNumShifts());
    int[] choice;
    CHOICE: while ((choice = generator.next()) != null) {
        shifts = new int[nodes.size()];
        double zSize = Math.pow(getMaxShift(), choice.length);
        int iIndex = dataSets.get(0).getVariables().indexOf(((DataSet) dataSets.get(0)).getVariable("I"));
        Z: for (int z = 0; z < zSize; z++) {
            if (scheduleStop)
                break;
            int _z = z;
            for (int i = 0; i < choice.length; i++) {
                if (choice[i] == iIndex) {
                    continue;
                }
                shifts[choice[i]] = (_z % (getMaxShift()) + 1);
                if (!forwardSearch) {
                    shifts[choice[i]] = -shifts[choice[i]];
                }
                _z /= getMaxShift();
            }
            List<DataModel> _shiftedDataSets = getShiftedDataSets(shifts, maxNumRows);
            double _b = getAvgBic(_shiftedDataSets);
            if (_b < 0.999 * b) {
                b = _b;
                printShifts(shifts, b, nodes);
                System.arraycopy(shifts, 0, bestshifts, 0, shifts.length);
            }
        }
    }
    println("\nShifts with the lowest BIC score: ");
    printShifts(bestshifts, b, nodes);
    return bestshifts;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node)

Example 4 with DepthChoiceGenerator

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

the class Mmmb method mmmb.

// ===========================PRIVATE METHODS==========================//
private List<Node> mmmb(Node t) {
    // MB <- {}
    Set<Node> mb = new HashSet<>();
    Set<Node> _pcpc = new HashSet<>();
    for (Node node : getPc(t)) {
        List<Node> f = getPc(node);
        this.pc.put(node, f);
        _pcpc.addAll(f);
    }
    List<Node> pcpc = new LinkedList<>(_pcpc);
    Set<Node> currentMb = new HashSet<>(getPc(t));
    currentMb.addAll(pcpc);
    currentMb.remove(t);
    HashSet<Node> diff = new HashSet<>(currentMb);
    diff.removeAll(getPc(t));
    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));
            }
            numIndTests++;
            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 : getPc(t)) {
            if (this.pc.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}
            numIndTests++;
            if (!independenceTest.isIndependent(t, x, _s)) {
                mb.add(x);
                break;
            }
        }
    }
    mb.addAll(getPc(t));
    return new LinkedList<>(mb);
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node)

Example 5 with DepthChoiceGenerator

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

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

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