Search in sources :

Example 1 with TextTable

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

the class FactorAnalysisAction method tableString.

private String tableString(TetradMatrix matrix, NumberFormat nf, double threshold) {
    TextTable table = new TextTable(matrix.rows() + 1, matrix.columns() + 1);
    for (int i = 0; i < matrix.rows() + 1; i++) {
        for (int j = 0; j < matrix.columns() + 1; j++) {
            if (i > 0 && j == 0) {
                table.setToken(i, j, "X" + i);
            } else if (i == 0 && j > 0) {
                table.setToken(i, j, "Factor " + j);
            } else if (i > 0 && j > 0) {
                double coefficient = matrix.get(i - 1, j - 1);
                String token = !Double.isNaN(coefficient) ? nf.format(coefficient) : "Undefined";
                token += Math.abs(coefficient) > threshold ? "*" : " ";
                table.setToken(i, j, token);
            }
        }
    }
    return "\n" + table.toString();
}
Also used : TextTable(edu.cmu.tetrad.util.TextTable)

Example 2 with TextTable

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

the class HsimUtils method correctnessRatio.

public static double correctnessRatio(int[][] counts) {
    // StringBuilder builder = new StringBuilder();
    TextTable table2 = new TextTable(9, 7);
    table2.setToken(1, 0, "---");
    table2.setToken(2, 0, "o-o");
    table2.setToken(3, 0, "o->");
    table2.setToken(4, 0, "<-o");
    table2.setToken(5, 0, "-->");
    table2.setToken(6, 0, "<--");
    table2.setToken(7, 0, "<->");
    table2.setToken(8, 0, "No Edge");
    table2.setToken(0, 1, "---");
    table2.setToken(0, 2, "o-o");
    table2.setToken(0, 3, "o->");
    table2.setToken(0, 4, "-->");
    table2.setToken(0, 5, "<->");
    table2.setToken(0, 6, "No Edge");
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 6; j++) {
            if (i == 7 && j == 5)
                table2.setToken(i + 1, j + 1, "*");
            else
                table2.setToken(i + 1, j + 1, "" + counts[i][j]);
        }
    }
    // builder.append(table2.toString());
    int correctEdges = 0;
    int estimatedEdges = 0;
    for (int i = 0; i < counts.length; i++) {
        for (int j = 0; j < counts[0].length - 1; j++) {
            if ((i == 0 && j == 0) || (i == 1 && j == 1) || (i == 2 && j == 2) || (i == 4 && j == 3) || (i == 6 && j == 4)) {
                correctEdges += counts[i][j];
            }
            estimatedEdges += counts[i][j];
        }
    }
    return (correctEdges / (double) estimatedEdges);
}
Also used : TextTable(edu.cmu.tetrad.util.TextTable)

Example 3 with TextTable

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

the class TimeoutComparison method printStats.

private void printStats(double[][][] statTables, Statistics statistics, Mode mode, int[] newOrder, List<AlgorithmSimulationWrapper> algorithmSimulationWrappers, List<AlgorithmWrapper> algorithmWrappers, List<SimulationWrapper> simulationWrappers, double[] utilities, Parameters parameters) {
    if (mode == Mode.Average) {
        out.println("AVERAGE STATISTICS");
    } else if (mode == Mode.StandardDeviation) {
        out.println("STANDARD DEVIATIONS");
    } else if (mode == Mode.WorstCase) {
        out.println("WORST CASE");
    } else {
        throw new IllegalStateException();
    }
    int numTables = statTables.length;
    int numStats = statistics.size();
    NumberFormat nf = new DecimalFormat("0.00");
    NumberFormat smallNf = new DecimalFormat("0.00E0");
    out.println();
    for (int u = 0; u < numTables; u++) {
        if (!graphTypeUsed[u]) {
            continue;
        }
        int rows = algorithmSimulationWrappers.size() + 1;
        int cols = (isShowSimulationIndices() ? 1 : 0) + (isShowAlgorithmIndices() ? 1 : 0) + numStats + (isShowUtilities() ? 1 : 0);
        TextTable table = new TextTable(rows, cols);
        table.setTabDelimited(isTabDelimitedTables());
        int initialColumn = 0;
        if (isShowSimulationIndices()) {
            table.setToken(0, initialColumn, "Sim");
            for (int t = 0; t < algorithmSimulationWrappers.size(); t++) {
                Simulation simulation = algorithmSimulationWrappers.get(newOrder[t]).getSimulationWrapper();
                table.setToken(t + 1, initialColumn, "" + (simulationWrappers.indexOf(simulation) + 1));
            }
            initialColumn++;
        }
        if (isShowAlgorithmIndices()) {
            table.setToken(0, initialColumn, "Alg");
            for (int t = 0; t < algorithmSimulationWrappers.size(); t++) {
                AlgorithmWrapper algorithm = algorithmSimulationWrappers.get(newOrder[t]).getAlgorithmWrapper();
                table.setToken(t + 1, initialColumn, "" + (algorithmWrappers.indexOf(algorithm) + 1));
            }
            initialColumn++;
        }
        for (int statIndex = 0; statIndex < numStats; statIndex++) {
            String statLabel = statistics.getStatistics().get(statIndex).getAbbreviation();
            table.setToken(0, initialColumn + statIndex, statLabel);
        }
        if (isShowUtilities()) {
            table.setToken(0, initialColumn + numStats, "U");
        }
        for (int t = 0; t < algorithmSimulationWrappers.size(); t++) {
            for (int statIndex = 0; statIndex < numStats; statIndex++) {
                Statistic statistic = statistics.getStatistics().get(statIndex);
                final AlgorithmWrapper algorithmWrapper = algorithmSimulationWrappers.get(newOrder[t]).getAlgorithmWrapper();
                final SimulationWrapper simulationWrapper = algorithmSimulationWrappers.get(newOrder[t]).getSimulationWrapper();
                Algorithm algorithm = algorithmWrapper.getAlgorithm();
                Simulation simulation = simulationWrapper.getSimulation();
                if (algorithm instanceof HasParameterValues) {
                    parameters.putAll(((HasParameterValues) algorithm).getParameterValues());
                }
                if (simulation instanceof HasParameterValues) {
                    parameters.putAll(((HasParameterValues) simulation).getParameterValues());
                }
                final String abbreviation = statistic.getAbbreviation();
                Object[] o = parameters.getValues(abbreviation);
                if (o.length == 1 && o[0] instanceof String) {
                    table.setToken(t + 1, initialColumn + statIndex, (String) o[0]);
                    continue;
                }
                double stat = statTables[u][newOrder[t]][statIndex];
                if (stat == 0.0) {
                    table.setToken(t + 1, initialColumn + statIndex, "-");
                } else if (stat == Double.POSITIVE_INFINITY) {
                    table.setToken(t + 1, initialColumn + statIndex, "Yes");
                } else if (stat == Double.NEGATIVE_INFINITY) {
                    table.setToken(t + 1, initialColumn + statIndex, "No");
                } else if (Double.isNaN(stat)) {
                    table.setToken(t + 1, initialColumn + statIndex, "*");
                } else {
                    table.setToken(t + 1, initialColumn + statIndex, Math.abs(stat) < Math.pow(10, -smallNf.getMaximumFractionDigits()) && stat != 0 ? smallNf.format(stat) : nf.format(stat));
                }
            }
            if (isShowUtilities()) {
                table.setToken(t + 1, initialColumn + numStats, nf.format(utilities[newOrder[t]]));
            }
        }
        out.println(getHeader(u));
        out.println();
        out.println(table);
    }
}
Also used : HasParameterValues(edu.cmu.tetrad.algcomparison.utils.HasParameterValues) DecimalFormat(java.text.DecimalFormat) ExternalAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) MultiDataSetAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm) Simulation(edu.cmu.tetrad.algcomparison.simulation.Simulation) Statistic(edu.cmu.tetrad.algcomparison.statistic.Statistic) TextTable(edu.cmu.tetrad.util.TextTable) NumberFormat(java.text.NumberFormat)

Example 4 with TextTable

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

the class Comparison method getTextTable.

private static TextTable getTextTable(DataSet dataSet, int[] columns, NumberFormat nf) {
    TextTable table = new TextTable(dataSet.getNumRows() + 2, columns.length + 1);
    table.setToken(0, 0, "Run #");
    for (int j = 0; j < columns.length; j++) {
        table.setToken(0, j + 1, dataSet.getVariable(columns[j]).getName());
    }
    for (int i = 0; i < dataSet.getNumRows(); i++) {
        table.setToken(i + 1, 0, Integer.toString(i + 1));
    }
    for (int i = 0; i < dataSet.getNumRows(); i++) {
        for (int j = 0; j < columns.length; j++) {
            table.setToken(i + 1, j + 1, nf.format(dataSet.getDouble(i, columns[j])));
        }
    }
    NumberFormat nf2 = new DecimalFormat("0.00");
    for (int j = 0; j < columns.length; j++) {
        double sum = 0.0;
        for (int i = 0; i < dataSet.getNumRows(); i++) {
            sum += dataSet.getDouble(i, columns[j]);
        }
        double avg = sum / dataSet.getNumRows();
        table.setToken(dataSet.getNumRows() + 2 - 1, j + 1, nf2.format(avg));
    }
    table.setToken(dataSet.getNumRows() + 2 - 1, 0, "Avg");
    return table;
}
Also used : TextTable(edu.cmu.tetrad.util.TextTable) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 5 with TextTable

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

the class ComparisonScript method runFromSimulation.

private void runFromSimulation() {
    ComparisonParameters params = new ComparisonParameters();
    // Continuous or Discrete
    params.setDataType(ComparisonParameters.DataType.Continuous);
    // number of variables
    params.setNumVars(12);
    // number of edges
    params.setNumEdges(12);
    // smallest sample size to generate
    int minSample = 100;
    // largest sample size to generate
    int maxSample = 1000;
    // ramp up sample size by this increment
    int increment = 100;
    // number of data sets to run for a particular sample size, results will be averaged
    int numTrials = 2;
    /**
     * If you want to run data sets from file instead of generating random graphs *
     */
    // set this to true
    params.setDataFromFile(false);
    // how many true graphs are in your directory?
    int maxGraphs = 2;
    // how many data sets are there in your directory for each true graph?
    int dataSetsPerGraph = 3;
    // remember the path to the data directory is set in Comparison2.java
    /**
     ******************** *
     */
    /**
     * If you want to run on NO DATA, i.e., just run each algorithm directly on some random true graphs *
     */
    // set this to true
    params.setNoData(false);
    if (params.isDataFromFile() && params.isNoData()) {
        throw new IllegalArgumentException("Cannot have setDataFromFile and setNoData both be true!");
    }
    ArrayList<Comparison2.TableColumn> tableColumns = new ArrayList<>();
    tableColumns.add(Comparison2.TableColumn.AdjPrec);
    tableColumns.add(Comparison2.TableColumn.AdjRec);
    tableColumns.add(Comparison2.TableColumn.AhdPrec);
    tableColumns.add(Comparison2.TableColumn.AhdRec);
    tableColumns.add(Comparison2.TableColumn.SHD);
    tableColumns.add(Comparison2.TableColumn.Elapsed);
    List<ComparisonParameters.Algorithm> algList = new ArrayList<>();
    /**
     * add algorithm to compare to the list algList. comment out those you don't want to consider. *
     */
    // algList.add(ComparisonParameters.Algorithm.PC);
    // algList.add(ComparisonParameters.Algorithm.FGES);
    // algList.add(ComparisonParameters.Algorithm.FCI);
    algList.add(ComparisonParameters.Algorithm.TsFCI);
    if (params.isDataFromFile()) {
        System.out.println("running algorithm on data from input files");
        minSample = 1;
        maxSample = maxGraphs;
        increment = 1;
        numTrials = dataSetsPerGraph;
    }
    if (params.isNoData()) {
        System.out.println("running algorithm on NO DATA, only true graph");
        minSample = 1;
        maxSample = 1;
        increment = 1;
    }
    int count = 0;
    TextTable avgTable = new TextTable((((maxSample - minSample) / increment) + 1) * algList.size() + 2, tableColumns.size() + 1);
    for (int sampleSize = minSample; sampleSize <= maxSample; sampleSize += increment) {
        params.setSampleSize(sampleSize);
        if (params.isDataFromFile()) {
            params.setGraphNum(sampleSize);
            System.out.println("graph file number = " + sampleSize);
        } else
            System.out.println("sample size = " + sampleSize);
        for (ComparisonParameters.Algorithm alg : algList) {
            count++;
            params.setAlgorithm(alg);
            params.setIndependenceTest(ComparisonParameters.IndependenceTestType.FisherZ);
            params.setScore(ScoreType.SemBic);
            // params.setOneEdgeFaithfulnessAssumed(false);
            List<ComparisonResult> resultsTrials = new ArrayList<>();
            for (int trial = 1; trial <= numTrials; trial++) {
                params.setTrial(trial);
                resultsTrials.add(Comparison2.compare(params));
            }
            TextTable tempTable = new TextTable(numTrials + 2, tableColumns.size() + 1);
            tempTable = Comparison2.summarize(resultsTrials, tableColumns);
            System.out.println(tempTable);
            for (int k = 0; k <= tempTable.getNumColumns() - 1; k++) {
                avgTable.setToken(count, k, tempTable.getTokenAt(tempTable.getNumRows() - 1, k));
            }
            if (params.isDataFromFile()) {
                avgTable.setToken(count, 0, "G=" + sampleSize + ", alg = " + alg);
            } else if (params.isNoData()) {
                avgTable.setToken(count, 0, "N=" + 0 + ", alg = " + alg);
            } else
                avgTable.setToken(count, 0, "N=" + sampleSize + ", alg = " + alg);
        }
    // loop over algorithm in algList
    }
    // add column names, then print table
    for (int j = 0; j <= tableColumns.size() - 1; j++) {
        avgTable.setToken(0, j, tableColumns.get(j).toString());
    }
    System.out.println(avgTable);
}
Also used : TextTable(edu.cmu.tetrad.util.TextTable) ArrayList(java.util.ArrayList)

Aggregations

TextTable (edu.cmu.tetrad.util.TextTable)19 DecimalFormat (java.text.DecimalFormat)8 NumberFormat (java.text.NumberFormat)8 ArrayList (java.util.ArrayList)2 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)1 ExternalAlgorithm (edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm)1 MultiDataSetAlgorithm (edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm)1 Simulation (edu.cmu.tetrad.algcomparison.simulation.Simulation)1 Statistic (edu.cmu.tetrad.algcomparison.statistic.Statistic)1 HasParameterValues (edu.cmu.tetrad.algcomparison.utils.HasParameterValues)1 DataSet (edu.cmu.tetrad.data.DataSet)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 SemPm (edu.cmu.tetrad.sem.SemPm)1