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

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

Introduction

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

Prototype

public double t(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
        throws NullArgumentException, NumberIsTooSmallException 

Source Link

Document

Computes a 2-sample t statistic , comparing the means of the datasets described by two StatisticalSummary instances, without the assumption of equal subpopulation variances.

Usage

From source file:carskit.alg.cars.transformation.prefiltering.splitting.ItemSplitting.java

public Table<Integer, Integer, Integer> split(SparseMatrix sm, int min) {
    Table<Integer, Integer, Integer> datatable = HashBasedTable.create();

    for (Integer j : itemRatingList.keySet()) {
        Collection<Integer> uis = itemRatingList.get(j);
        double maxt = Double.MIN_VALUE;
        int splitcond = -1;

        for (Integer cond : condContextsList.keySet()) {
            Collection<Integer> ctx = condContextsList.get(cond);
            // start to extract two rating list
            HashMultiset<Double> rate1 = HashMultiset.create();
            HashMultiset<Double> rate2 = HashMultiset.create();

            for (Integer ui : uis) {
                List<Integer> uctx = sm.getColumns(ui);
                for (Integer c : uctx) {
                    double rate = sm.get(ui, c);
                    if (ctx.contains(c))
                        rate1.add(rate);
                    else
                        rate2.add(rate);
                }//from ww w .  ja  va  2 s  .c om
            }

            double[] drate1 = Doubles.toArray(rate1);
            double[] drate2 = Doubles.toArray(rate2);

            if (drate1.length >= min && drate2.length >= min) {
                TTest tt = new TTest();
                double p = tt.tTest(drate1, drate2);
                if (p < 0.05) {
                    double t = tt.t(drate1, drate2);
                    if (t > maxt) {
                        // update the split
                        splitcond = cond;
                        maxt = t;
                    }
                }
            }
        }
        if (splitcond != -1) {
            // put u, ctx, new uid into datatable
            int newid = startId++;
            Collection<Integer> ctx = condContextsList.get(splitcond);
            for (Integer c : ctx)
                datatable.put(j, c, newid);
        }

    }
    Logs.info(datatable.rowKeySet().size() + " items have been splitted.");
    return datatable;
}

From source file:carskit.alg.cars.transformation.prefiltering.splitting.UserSplitting.java

public Table<Integer, Integer, Integer> split(SparseMatrix sm, int min) {
    Logs.debug("UserSplitting: startId = " + startId);
    Table<Integer, Integer, Integer> datatable = HashBasedTable.create();

    for (Integer u : userRatingList.keySet()) {
        Collection<Integer> uis = userRatingList.get(u);
        double maxt = Double.MIN_VALUE;
        int splitcond = -1;

        for (Integer cond : condContextsList.keySet()) {
            Collection<Integer> ctx = condContextsList.get(cond);
            // start to extract two rating list
            HashMultiset<Double> rate1 = HashMultiset.create();
            HashMultiset<Double> rate2 = HashMultiset.create();

            for (Integer ui : uis) {
                List<Integer> uctx = sm.getColumns(ui);
                for (Integer c : uctx) {
                    double rate = sm.get(ui, c);
                    if (ctx.contains(c))
                        rate1.add(rate);
                    else
                        rate2.add(rate);
                }/*from ww  w. j a  va2 s  .co m*/
            }

            double[] drate1 = Doubles.toArray(rate1);
            double[] drate2 = Doubles.toArray(rate2);

            if (drate1.length >= min && drate2.length >= min) {
                TTest tt = new TTest();
                double p = tt.tTest(drate1, drate2);
                if (p < 0.05) {
                    double t = tt.t(drate1, drate2);
                    if (t > maxt) {
                        // update the split
                        splitcond = cond;
                        maxt = t;
                    }
                }
            }
        }

        if (splitcond != -1) {
            // put u, ctx, new uid into datatable
            int newid = startId++;
            Collection<Integer> ctx = condContextsList.get(splitcond);
            for (Integer c : ctx)
                datatable.put(u, c, newid);
        }

    }
    Logs.info(datatable.rowKeySet().size() + " users have been splitted.");
    return datatable;
}

From source file:eu.betaas.taas.securitymanager.taastrustmanager.taastrustcalculator.StatisticsCalculator.java

public boolean isSimilarMean(double[] valuesA, double[] valuesB) {
    TTest studentTest = new TTest();
    boolean testResult = false;
    double error = 0;
    double tValue = 0;

    double meanA = StatUtils.mean(valuesA);
    double meanB = StatUtils.mean(valuesB);

    try {//from w w w . ja v a2  s. c  om
        testResult = studentTest.tTest(valuesA, valuesB, 0.05);
        error = studentTest.tTest(valuesA, valuesB);
        tValue = studentTest.t(valuesA, valuesB);
        logger.debug("Test result --> MA = " + meanA + " -- MB = " + meanB);
        logger.debug("Test result --> " + testResult + " with p " + error + " and tValue = " + tValue);
    } catch (Exception ex) {
        logger.error("There was an error when trying to calculate Student's t test!");
        ex.printStackTrace();
        return false;
    }

    return testResult;

}

From source file:org.apache.solr.client.solrj.io.eval.TTestEvaluator.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 Number) {
        double mean = ((Number) value1).doubleValue();

        if (value2 instanceof List) {
            List<Number> values = (List<Number>) value2;
            double[] samples = new double[values.size()];
            for (int i = 0; i < samples.length; i++) {
                samples[i] = values.get(i).doubleValue();
            }/*from  w ww  .j  av a 2  s  .c om*/

            double tstat = tTest.t(mean, samples);
            double pval = tTest.tTest(mean, samples);

            tuple.put("t-statistic", tstat);
            tuple.put("p-value", pval);
            return tuple;
        } else {
            throw new IOException("Second parameter for ttest must be a double array");
        }
    } else 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();
        }

        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.t(samples1, samples2);
            double pval = tTest.tTest(samples1, samples2);
            tuple.put("t-statistic", tstat);
            tuple.put("p-value", pval);
            return tuple;
        } else {
            throw new IOException("Second parameter for ttest must be a double array");
        }
    } else {
        throw new IOException("First parameter for ttest must be either a double our double array");
    }
}

From source file:performancestatisticsasset.GroupGroupCompare.java

void analyze(DistributionSet set1Time, DistributionSet set2Time, DistributionSet set1Perf,
        DistributionSet set2Perf, ResultSet resultTime, ResultSet resultPerf) {
    TTest tempTest = new TTest();
    Result tempRes;//  ww w .  ja v a 2 s. c  o  m

    //Time
    //For all the trials do the analysis
    int trials = set1Time.distributionSet.size();
    if (set1Time.distributionSet.size() > set2Time.distributionSet.size())
        trials = set2Time.distributionSet.size();

    for (int i = 0; i < trials; i++) {
        if ((set1Time.distributionSet.get(i).n > 1) && (set2Time.distributionSet.get(i).n > 1)) {
            tempRes = new Result();
            tempRes.t = tempTest.t(set1Time.distributionSet.get(i), set2Time.distributionSet.get(i));
            tempRes.p = tempTest.tTest(set1Time.distributionSet.get(i), set2Time.distributionSet.get(i));
            resultTime.addResult(tempRes);
        } else {
            tempRes = new Result();
            tempRes.NaN = true;
            resultTime.addResult(tempRes);
        }
    }

    //Performance
    //For all the trials do the analysis
    trials = set1Perf.distributionSet.size();
    if (set1Perf.distributionSet.size() > set2Perf.distributionSet.size())
        trials = set2Perf.distributionSet.size();

    for (int i = 0; i < trials; i++) {
        if ((set1Perf.distributionSet.get(i).n > 1) && (set2Perf.distributionSet.get(i).n > 1)) {
            tempRes = new Result();
            tempRes.t = tempTest.t(set1Perf.distributionSet.get(i), set2Perf.distributionSet.get(i));
            tempRes.p = tempTest.tTest(set1Perf.distributionSet.get(i), set2Perf.distributionSet.get(i));
            resultPerf.addResult(tempRes);
        } else {
            tempRes = new Result();
            tempRes.NaN = true;
            resultPerf.addResult(tempRes);
        }
    }
}