Search in sources :

Example 1 with ChoiceGenerator

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

the class LTestSsys1 method main.

public static void main(String[] argv) {
    String fileName = argv[0];
    InputStream s;
    StringTokenizer st;
    int ngenes = 5;
    int ntimes = 400;
    int nrecords = 5;
    int nchips = 4;
    double[][] cases = new double[4][2004];
    try {
        s = new FileInputStream(fileName);
    } catch (IOException e) {
        System.out.println("Cannot open file " + fileName);
        return;
    }
    // DataInputStream in = new DataInputStream(s);
    BufferedReader in = new BufferedReader(new InputStreamReader(s));
    for (int k = 0; k < nrecords; k++) {
        try {
            st = new StringTokenizer(in.readLine());
            if (k == 0) {
                continue;
            }
            // int ichip = Integer.parseInt(st.nextToken("\t"));
            for (int j = 0; j < ntimes * ngenes; j++) {
                cases[k - 1][j] = Double.parseDouble(st.nextToken("\t"));
            }
        } catch (IOException e) {
            System.out.println("Read error in " + fileName);
            return;
        }
    }
    double[][] gene = new double[ntimes][ngenes];
    double[][] deriv = new double[ntimes][ngenes];
    double[] sum = new double[ngenes];
    double baseLevel = 15.0;
    NumberFormat nf = NumberFormatUtil.getInstance().getNumberFormat();
    double min = 1000.0;
    for (int j = 0; j < ntimes; j++) {
        for (int g = 0; g < ngenes; g++) {
            int icol = j * ngenes + g;
            sum[g] = 0.0;
            for (int c = 0; c < nchips; c++) // sum[g] += cases[c][icol]*cases[c][icol];
            {
                sum[g] += cases[c][icol];
            }
            gene[j][g] = sum[g];
            if (gene[j][g] < min) {
                min = gene[j][g];
            }
            if (j != 0) {
                deriv[j][g] = (gene[j][g] - gene[j - 1][g]) / 10.0;
            }
        }
    /*  DEBUG Prints
            System.out.println("At time " + j);
            for(int g = 0; g < ngenes; g++) {
              String expression = nf.format(gene[j][g]);
              System.out.print(expression + " ");
            }
            System.out.println();
            */
    }
    System.out.println("minimum value = " + min);
    for (int g = 0; g < ngenes; g++) {
        System.out.println("For gene " + g);
        int k = 5;
        ChoiceGenerator cg = new ChoiceGenerator(ngenes, k);
        int[] regs = new int[k];
        while ((regs = cg.next()) != null) {
            System.out.println("Sets of " + k + " regulators are:");
            System.out.println(regs[0] + " " + regs[1] + " " + regs[2] + " " + regs[3] + " " + regs[4]);
            for (int t = 1; t < ntimes; t++) {
                String g0 = nf.format(Math.log(gene[t][regs[0]] + baseLevel));
                String g1 = nf.format(Math.log(gene[t][regs[1]] + baseLevel));
                String g2 = nf.format(Math.log(gene[t][regs[2]] + baseLevel));
                String g3 = nf.format(Math.log(gene[t][regs[3]] + baseLevel));
                String g4 = nf.format(Math.log(gene[t][regs[4]] + baseLevel));
                if (deriv[t][g] > 0.35) {
                    System.out.println("lnamlnb+" + g0 + "gmh0+" + g1 + "gmh1+" + g2 + "gmh2+" + g3 + "gmh3+" + g4 + "gmh4 > 0");
                } else if (deriv[t][g] < -0.35) {
                    System.out.println("lnamlnb+" + g0 + "gmh0+" + g1 + "gmh1+" + g2 + "gmh2+" + g3 + "gmh3+" + g4 + "gmh4 < 0");
                }
            }
        }
    }
/*
        double[] p = new double[ngenes];
        for(int g = 0; g < ngenes; g++) {
          for(int j = 0; j < ntimes; j++)
            if(gene[j][g] > 0) p[g]++;
          p[g] /= ntimes;
          //System.out.println(" gene " + g + " p = " + p[g]);
        }
        */
}
Also used : StringTokenizer(java.util.StringTokenizer) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) NumberFormat(java.text.NumberFormat)

Example 2 with ChoiceGenerator

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

the class TestChoiceGenerator method testChoiceGeneratorCounts.

/**
 * Tests to make sure the ChoiceGenerator is output the correct number of choices
 * for various values of and b.
 */
@Test
public void testChoiceGeneratorCounts() {
    for (int a = 0; a <= 20; a++) {
        for (int b = 0; b <= a; b++) {
            ChoiceGenerator generator = new ChoiceGenerator(a, b);
            int n = 0;
            while ((generator.next()) != null) {
                n++;
            }
            long numerator = 1;
            long denominator = 1;
            for (int k = a; k - b > 0; k--) {
                numerator *= k;
                denominator *= k - b;
            }
            long numChoices = numerator / denominator;
            if (n != numChoices) {
                fail("a = " + a + " b = " + b + " numChoices = " + numChoices + " n = " + n);
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Test(org.junit.Test)

Example 3 with ChoiceGenerator

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

the class Fges2 method calculateArrowsForward.

// Calculates the new arrows for an a->b edge.
private void calculateArrowsForward(Node a, Node b) {
    if (mode == Mode.heuristicSpeedup && !effectEdgesGraph.isAdjacentTo(a, b))
        return;
    if (adjacencies != null && !adjacencies.isAdjacentTo(a, b))
        return;
    this.neighbors.put(b, getNeighbors(b));
    if (a == b)
        throw new IllegalArgumentException();
    if (existsKnowledge()) {
        if (getKnowledge().isForbidden(a.getName(), b.getName())) {
            return;
        }
    }
    Set<Node> naYX = getNaYX(a, b);
    if (!isClique(naYX))
        return;
    List<Node> TNeighbors = getTNeighbors(a, b);
    Set<Set<Node>> previousCliques = new HashSet<>();
    previousCliques.add(new HashSet<Node>());
    Set<Set<Node>> newCliques = new HashSet<>();
    FOR: for (int i = 0; i <= TNeighbors.size(); i++) {
        final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
        int[] choice;
        while ((choice = gen.next()) != null) {
            Set<Node> T = GraphUtils.asSet(choice, TNeighbors);
            Set<Node> union = new HashSet<>(naYX);
            union.addAll(T);
            boolean foundAPreviousClique = false;
            for (Set<Node> clique : previousCliques) {
                if (union.containsAll(clique)) {
                    foundAPreviousClique = true;
                    break;
                }
            }
            if (!foundAPreviousClique) {
                break FOR;
            }
            if (!isClique(union))
                continue;
            newCliques.add(union);
            double bump = insertEval(a, b, T, naYX, hashIndices);
            if (Double.isNaN(bump)) {
                bump = Double.MIN_VALUE;
            }
            if (bump > 0 || Double.isNaN(bump)) {
                addArrow(a, b, naYX, T, bump);
            }
        // if (mode == Mode.heuristicSpeedup && union.isEmpty() && score.isEffectEdge(bump) &&
        // !effectEdgesGraph.isAdjacentTo(a, b) && graph.getParents(b).isEmpty()) {
        // effectEdgesGraph.addUndirectedEdge(a, b);
        // }
        }
        previousCliques = newCliques;
        newCliques = new HashSet<>();
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 4 with ChoiceGenerator

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

the class FgesMb method calculateArrowsForward.

// Calculates the new arrows for an a->b edge.
private void calculateArrowsForward(Node a, Node b) {
    if (mode == Mode.heuristicSpeedup && !effectEdgesGraph.isAdjacentTo(a, b)) {
        return;
    }
    if (adjacencies != null && !adjacencies.isAdjacentTo(a, b)) {
        return;
    }
    this.neighbors.put(b, getNeighbors(b));
    if (existsKnowledge()) {
        if (getKnowledge().isForbidden(a.getName(), b.getName())) {
            return;
        }
    }
    Set<Node> naYX = getNaYX(a, b);
    if (!GraphUtils.isClique(naYX, this.graph)) {
        return;
    }
    List<Node> TNeighbors = getTNeighbors(a, b);
    int _maxIndegree = maxIndegree == -1 ? 1000 : maxIndegree;
    final int _max = Math.min(TNeighbors.size(), _maxIndegree - graph.getIndegree(b));
    Set<Set<Node>> previousCliques = new HashSet<>();
    previousCliques.add(new HashSet<Node>());
    Set<Set<Node>> newCliques = new HashSet<>();
    FOR: for (int i = 0; i <= _max; i++) {
        final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
        int[] choice;
        while ((choice = gen.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            Set<Node> T = GraphUtils.asSet(choice, TNeighbors);
            Set<Node> union = new HashSet<>(naYX);
            union.addAll(T);
            boolean foundAPreviousClique = false;
            for (Set<Node> clique : previousCliques) {
                if (union.containsAll(clique)) {
                    foundAPreviousClique = true;
                    break;
                }
            }
            if (!foundAPreviousClique) {
                break FOR;
            }
            if (!GraphUtils.isClique(union, this.graph)) {
                continue;
            }
            newCliques.add(union);
            double bump = insertEval(a, b, T, naYX, hashIndices);
            if (bump > 0.0) {
                addArrow(a, b, naYX, T, bump);
            }
        // if (mode == Mode.heuristicSpeedup && union.isEmpty() && score.isEffectEdge(bump) &&
        // !effectEdgesGraph.isAdjacentTo(a, b) && graph.getParents(b).isEmpty()) {
        // effectEdgesGraph.addUndirectedEdge(a, b);
        // }
        }
        previousCliques = newCliques;
        newCliques = new HashSet<>();
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 5 with ChoiceGenerator

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

the class GFciMax method search.

// ========================PUBLIC METHODS==========================//
public Graph search() {
    long time1 = System.currentTimeMillis();
    List<Node> nodes = getIndependenceTest().getVariables();
    logger.log("info", "Starting FCI algorithm.");
    logger.log("info", "Independence test = " + getIndependenceTest() + ".");
    this.graph = new EdgeListGraphSingleConnections(nodes);
    Fges fges = new Fges(score);
    fges.setKnowledge(getKnowledge());
    fges.setVerbose(verbose);
    fges.setNumPatternsToStore(0);
    fges.setFaithfulnessAssumed(faithfulnessAssumed);
    fges.setMaxDegree(maxDegree);
    fges.setOut(out);
    graph = fges.search();
    Graph fgesGraph = new EdgeListGraphSingleConnections(graph);
    sepsets = new SepsetsGreedy(fgesGraph, independenceTest, null, maxDegree);
    graph.reorientAllWith(Endpoint.CIRCLE);
    for (Node b : nodes) {
        List<Node> adjacentNodes = fgesGraph.getAdjacentNodes(b);
        if (adjacentNodes.size() < 2) {
            continue;
        }
        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]);
            if (graph.isAdjacentTo(a, c) && fgesGraph.isAdjacentTo(a, c)) {
                if (sepsets.getSepset(a, c) != null) {
                    graph.removeEdge(a, c);
                }
            }
        }
    }
    // modifiedR0(fgesGraph);
    sepsets = new SepsetsMinScore(fgesGraph, independenceTest, maxDegree);
    addColliders(graph, fgesGraph);
    FciOrient fciOrient = new FciOrient(sepsets);
    fciOrient.setVerbose(verbose);
    fciOrient.setOut(out);
    fciOrient.setKnowledge(getKnowledge());
    fciOrient.setCompleteRuleSetUsed(completeRuleSetUsed);
    fciOrient.setMaxPathLength(maxPathLength);
    fciOrient.doFinalOrientation(graph);
    GraphUtils.replaceNodes(graph, independenceTest.getVariables());
    long time2 = System.currentTimeMillis();
    elapsedTime = time2 - time1;
    return graph;
}
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