Example usage for org.apache.commons.math3.stat.inference TTest pairedTTest

List of usage examples for org.apache.commons.math3.stat.inference TTest pairedTTest

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.inference TTest pairedTTest.

Prototype

public double pairedTTest(final double[] sample1, final double[] sample2) throws NullArgumentException,
        NoDataException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException 

Source Link

Document

Returns the observed significance level, or p-value, associated with a paired, two-sample, two-tailed t-test based on the data in the input arrays.

Usage

From source file:coolmap.utils.statistics.test.CTest.java

public static void ttest(CoolMapObject obj, Dimension direction, VNode leafNode1, VNode leafNode2) {
    try {//from  w ww . j a v a 2 s .com
        ArrayList<VNode> leafNodes = new ArrayList<VNode>();
        if (direction == Dimension.ROW) {
            leafNodes.addAll(obj.getViewNodesRow());
        } else if (direction == Dimension.COLUMN) {
            leafNodes.addAll(obj.getViewNodesColumn());
        } else {
            return;
        }

        if (leafNodes.contains(leafNode1) && leafNodes.contains(leafNode2)) {

            double[] data1 = null;
            double[] data2 = null;

            if (direction == Dimension.ROW) {
                data1 = extractRow(obj, leafNode1);
                data2 = extractRow(obj, leafNode2);
            } else if (direction == Dimension.COLUMN) {
                data1 = extractColumn(obj, leafNode1);
                data2 = extractColumn(obj, leafNode2);
            }

            TTest test = new TTest();
            double pValue = test.pairedTTest(data1, data2);

            TestResult result = new TestResult("Student Paired T-Test",
                    "Two tailed paired t-test with alpha = 0.05.\nData: " + obj.getName() + "\nDirection: "
                            + direction + "\nGroups: " + leafNode1 + ", " + leafNode2,
                    pValue);
            CMConsole.log(result.toString());

        } else {
            CMConsole.logError(
                    "T-test error: group dimension mismatch, must both be row ontology group or column ontology groups.");
        }

    } catch (Exception e) {
        CMConsole.logError("T-test error: " + " Dataset:" + obj + " Direction:" + direction + " Group1:"
                + leafNode1 + " Group2:" + leafNode2);
    }
}

From source file:carskit.data.processor.DataDAO.java

/**
 * print out specifications of the dataset
 *//*from www.  j  av a 2  s . co m*/
public void printSpecs() throws Exception {
    if (rateMatrix == null)
        readData(-1);

    List<String> sps = new ArrayList<>();

    int users = numUsers();
    int items = numItems();

    int numRates = rateMatrix.size();
    int dims = numContextDims();
    int conds = numConditions();
    int numctx = numContexts();
    int cdims = 1;

    StringBuilder condcount = new StringBuilder();
    StringBuilder sdims = new StringBuilder();
    for (int dim : dimConditionsList.keySet()) {
        String sdim = this.getContextDimensionId(dim);
        int counter = dimConditionsList.get(dim).size();
        if (condcount.length() > 0)
            condcount.append(", ");
        condcount.append(sdim + ": " + counter);
        cdims *= counter;
        if (sdims.length() > 0)
            sdims.append(", ");
        sdims.append(sdim);
    }

    sps.add(String.format("Dataset: %s", dataPath));
    sps.add("");
    sps.add("Statistics of U-I-C Matrix:");
    sps.add("User amount: " + users);
    sps.add("Item amount: " + items);
    sps.add("Rate amount: " + numRatings());
    sps.add("Context dimensions: " + dims + " (" + sdims.toString() + ")");
    sps.add("Context conditions: " + conds + " (" + condcount.toString() + ")");
    sps.add("Context situations: " + numctx);
    sps.add(String.format("Data density: %.4f%%", (numRates + 0.0) / users / items / cdims * 100));
    sps.add("Scale distribution: " + scaleDist.toString());

    // user/item mean
    double[] data = rateMatrix.getData();
    float mean = (float) (Stats.sum(data) / numRates);
    float std = (float) Stats.sd(data);
    float mode = (float) Stats.mode(data);
    float median = (float) Stats.median(data);
    sps.add(String.format("Average value of all ratings: %f", mean));
    sps.add(String.format("Standard deviation of all ratings: %f", std));
    sps.add(String.format("Mode of all rating values: %f", mode));
    sps.add(String.format("Median of all rating values: %f", median));

    if (fullStat) {

        sps.add("");
        Collection<Double> ratingDist_c = rates_c_count.values();
        Collection<Double> ratingDist_u = rates_u_count.values();
        Collection<Double> ratingDist_i = rates_i_count.values();
        sps.add("Distribution of rate counts per user: mean = " + Stats.mean(ratingDist_u) + ", median = "
                + Stats.median(ratingDist_u) + ", sd = " + Stats.sd(ratingDist_u));
        sps.add("Distribution of rate counts per item: mean = " + Stats.mean(ratingDist_i) + ", median = "
                + Stats.median(ratingDist_i) + ", sd = " + Stats.sd(ratingDist_i));
        sps.add("Distribution of rate counts per context condition: mean = " + Stats.mean(ratingDist_c)
                + ", median = " + Stats.median(ratingDist_c) + ", sd = " + Stats.sd(ratingDist_c));
        sps.add("");
        sps.add("Average rating in each context condition: (Average, Counts)");
        Lists.sortMap(rates_c, false);
        for (int c : rates_c.keySet()) {
            sps.add(this.getContextConditionId(c) + " - "
                    + String.format("%.6f", rates_c.get(c) / rates_c_count.get(c)) + ", "
                    + rates_c_count.get(c).intValue());
        }

        data = rateMatrix_UI.getData();
        double numRates_M = data.length;
        sps.add("");
        sps.add("Statistics of UI Matrix:");
        sps.add("User amount: " + users);
        sps.add("Item amount: " + items);
        sps.add("Rate amount: " + numRates_M);
        sps.add(String.format("Data density: %.4f%%", (numRates_M + 0.0) / users / items * 100));
        sps.add(String.format("Data density (unique pairs): %.4f%%", density_unique_ui / numRates_M * 100));
        mean = (float) (Stats.sum(data) / numRates_M);
        std = (float) Stats.sd(data);
        mode = (float) Stats.mode(data);
        median = (float) Stats.median(data);
        sps.add(String.format("Average value of all ratings: %f", mean));
        sps.add(String.format("Standard deviation of all ratings: %f", std));
        sps.add(String.format("Mode of all rating values: %f", mode));
        sps.add(String.format("Median of all rating values: %f", median));
        sps.add("Distribution of rate counts per UI pair: mean = " + Stats.mean(ratingDist_ui) + ", median = "
                + Stats.median(ratingDist_ui) + ", sd = " + Stats.sd(ratingDist_ui));

        data = rateMatrix_UC.getData();
        numRates_M = data.length;
        sps.add("");
        sps.add("Statistics of UC Matrix:");
        sps.add("User amount: " + users);
        sps.add("Condition amount: " + conds);
        sps.add("Rate amount: " + numRates_M);
        sps.add(String.format("Data density: %.4f%%", (numRates_M + 0.0) / users / numConditions() * 100));
        sps.add(String.format("Data density (unique pairs): %.4f%%", density_unique_uc / numRates_M * 100));
        mean = (float) (Stats.sum(data) / numRates_M);
        std = (float) Stats.sd(data);
        mode = (float) Stats.mode(data);
        median = (float) Stats.median(data);
        sps.add(String.format("Average value of all ratings: %f", mean));
        sps.add(String.format("Standard deviation of all ratings: %f", std));
        sps.add(String.format("Mode of all rating values: %f", mode));
        sps.add(String.format("Median of all rating values: %f", median));
        sps.add("Distribution of rate counts per UC pair: mean = " + Stats.mean(ratingDist_uc) + ", median = "
                + Stats.median(ratingDist_uc) + ", sd = " + Stats.sd(ratingDist_uc));

        data = rateMatrix_IC.getData();
        numRates_M = data.length;
        sps.add("");
        sps.add("Statistics of IC Matrix:");
        sps.add("Item amount: " + items);
        sps.add("Condition amount: " + conds);
        sps.add("Rate amount: " + numRates_M);
        sps.add(String.format("Data density: %.4f%%", (numRates_M + 0.0) / items / numConditions() * 100));
        sps.add(String.format("Data density (unique pairs): %.4f%%", density_unique_ic / numRates_M * 100));
        mean = (float) (Stats.sum(data) / numRates_M);
        std = (float) Stats.sd(data);
        mode = (float) Stats.mode(data);
        median = (float) Stats.median(data);
        sps.add(String.format("Average value of all ratings: %f", mean));
        sps.add(String.format("Standard deviation of all ratings: %f", std));
        sps.add(String.format("Mode of all rating values: %f", mode));
        sps.add(String.format("Median of all rating values: %f", median));
        sps.add("Distribution of rate counts per IC pair: mean = " + Stats.mean(ratingDist_ic) + ", median = "
                + Stats.median(ratingDist_ic) + ", sd = " + Stats.sd(ratingDist_ic));

        sps.add("");
        ArrayList<Double> urates = new ArrayList<>();
        ArrayList<Double> urates_c = new ArrayList<>();
        ArrayList<Double> irates = new ArrayList<>();
        ArrayList<Double> irates_c = new ArrayList<>();

        for (int u : rateMatrix_UI.rows()) {
            if (rateMatrix_UC.rows().contains(u)) {
                SparseVector sv = rateMatrix_UI.row(u);
                SparseVector svc = rateMatrix_UC.row(u);
                if (sv.size() != 0 && svc.size() != 0) {
                    urates.add(sv.mean());
                    urates_c.add(svc.mean());
                }
            }
        }

        for (int i : rateMatrix_UI.columns()) {
            if (rateMatrix_IC.rows().contains(i)) {
                SparseVector sv = rateMatrix_UI.column(i);
                SparseVector svc = rateMatrix_IC.row(i);
                if (sv.size() != 0 && svc.size() != 0) {
                    irates.add(sv.mean());
                    irates_c.add(svc.mean());
                }
            }
        }

        TTest tt = new TTest();
        sps.add("Paired t-test on user's average rating between UI and UC matrix: absolute mean diff = "
                + Math.abs(Stats.mean(urates) - Stats.mean(urates_c)) + ", p-value = "
                + tt.pairedTTest(Doubles.toArray(urates), Doubles.toArray(urates_c)));
        sps.add("Paired t-test on item's average rating between UI and IC matrix: absolute mean diff = "
                + Math.abs(Stats.mean(irates) - Stats.mean(irates_c)) + ", p-value = "
                + tt.pairedTTest(Doubles.toArray(irates), Doubles.toArray(irates_c)));
    }

    Logs.info(Strings.toSection(sps));
}

From source file:org.apache.solr.client.solrj.io.eval.PairedTTestEvaluator.java

@Override
public Object doWork(Object value1, Object value2) throws IOException {

    TTest tTest = new TTest();
    Map map = new HashMap();
    Tuple tuple = new Tuple(map);
    if (value1 instanceof List) {
        List<Number> values1 = (List<Number>) value1;
        double[] samples1 = new double[values1.size()];

        for (int i = 0; i < samples1.length; i++) {
            samples1[i] = values1.get(i).doubleValue();
        }//from  w  w w  . j  a  v a  2s  . com

        if (value2 instanceof List) {
            List<Number> values2 = (List<Number>) value2;
            double[] samples2 = new double[values2.size()];

            for (int i = 0; i < samples2.length; i++) {
                samples2[i] = values2.get(i).doubleValue();
            }

            double tstat = tTest.pairedT(samples1, samples2);
            double pval = tTest.pairedTTest(samples1, samples2);
            tuple.put("t-statistic", tstat);
            tuple.put("p-value", pval);
            return tuple;
        } else {
            throw new IOException("Second parameter for pairedTtest must be a double array");
        }
    } else {
        throw new IOException("First parameter for pairedTtest must be a double array");
    }
}