Example usage for org.apache.commons.math.stat.descriptive DescriptiveStatistics getVariance

List of usage examples for org.apache.commons.math.stat.descriptive DescriptiveStatistics getVariance

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.descriptive DescriptiveStatistics getVariance.

Prototype

public double getVariance() 

Source Link

Document

Returns the variance of the available values.

Usage

From source file:guineu.modules.dataanalysis.anova.AnovaTestTask.java

private double anova(List<String> groups, PeakListRow row) {
    try {/*from ww  w .  j  a v  a  2  s. c om*/
        DescriptiveStatistics stats1 = new DescriptiveStatistics();
        List<double[]> classes = new ArrayList<double[]>();
        for (String group : groups) {
            List<Double> values = new ArrayList<Double>();
            for (String name : dataset.getAllColumnNames()) {
                if (dataset.getParametersValue(name, parameter) != null
                        && dataset.getParametersValue(name, parameter).equals(group)) {
                    try {
                        values.add((Double) row.getPeak(name));
                        stats1.addValue((Double) row.getPeak(name));
                    } catch (Exception e) {
                        System.out.println(row.getPeak(name));
                    }

                }
            }
            double[] valuesArray = new double[values.size()];
            for (int i = 0; i < values.size(); i++) {
                valuesArray[i] = values.get(i).doubleValue();
            }

            classes.add(valuesArray);
        }

        if (stats1.getVariance() == 0)
            return -1;
        return TestUtils.oneWayAnovaPValue(classes);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
        return -1;
    } catch (MathException ex) {
        ex.printStackTrace();
        return -1;
    }
}

From source file:org.eclipse.scout.rt.client.ui.basic.table.TableLoadingPerfTest.java

private void assertMean(Long[] durations, int expectedMean) {
    Arrays.sort(durations);/*  w w w .  j a  v a  2s . c  o  m*/
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 1; i < durations.length - 1; i++) {
        stats.addValue(durations[i]);
    }
    double avgDuration = stats.getMean();
    assertTrue(String.format("Expected Mean<100 Mean:%s Variance:%s", avgDuration, stats.getVariance()),
            avgDuration < expectedMean);
}

From source file:org.fusesource.eca.processor.StatisticsCalculator.java

protected void process(StatisticsType type, Number value, ObjectNode statsNode) throws Exception {
    EventCache<Number> cache = this.eventCache;
    if (value != null && cache != null) {
        cache.add(value);/*from   w w  w.  ja  v a2s  . com*/
        if (type.equals(StatisticsType.RATE)) {
            calculateRate(statsNode);
        } else {
            List<Number> list = this.eventCache.getWindow();
            DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
            if (list != null && !list.isEmpty()) {
                for (Number number : list) {
                    descriptiveStatistics.addValue(number.doubleValue());
                }
                switch (type) {
                case MEAN:
                    statsNode.put("mean", descriptiveStatistics.getMean());
                    break;
                case GEOMETRIC_MEAN:
                    statsNode.put("gemetric mean", descriptiveStatistics.getGeometricMean());
                    break;
                case STDDEV:
                    statsNode.put("std-dev", descriptiveStatistics.getStandardDeviation());
                    break;
                case MIN:
                    statsNode.put("minimum", descriptiveStatistics.getMin());
                    break;
                case MAX:
                    statsNode.put("maximum", descriptiveStatistics.getMax());
                    break;
                case SKEWNESS:
                    statsNode.put("skewness", descriptiveStatistics.getSkewness());
                    break;
                case KUTOSIS:
                    statsNode.put("kurtosis", descriptiveStatistics.getKurtosis());
                    break;
                case VARIANCE:
                    statsNode.put("variance", descriptiveStatistics.getVariance());
                    break;
                case COUNT:
                    statsNode.put("count", list.size());
                default:
                    statsNode.put("number", descriptiveStatistics.getN());
                    statsNode.put("mean", descriptiveStatistics.getMean());
                    statsNode.put("gemetric mean", descriptiveStatistics.getGeometricMean());
                    statsNode.put("minimum", descriptiveStatistics.getMin());
                    statsNode.put("maximum", descriptiveStatistics.getMax());
                    statsNode.put("std-dev", descriptiveStatistics.getStandardDeviation());
                    statsNode.put("median", descriptiveStatistics.getPercentile(50));
                    statsNode.put("skewness", descriptiveStatistics.getSkewness());
                    statsNode.put("kurtosis", descriptiveStatistics.getKurtosis());
                    statsNode.put("variance", descriptiveStatistics.getVariance());
                    calculateRate(statsNode);
                    statsNode.put("count", list.size());
                }
            }
        }

    }
}

From source file:org.jgap.gp.function.statistics.Variance.java

@Override
public double execute_double(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_double(n, i, args));
    }/*w  w w  . j  av a 2s  . c o m*/
    return stats.getVariance();
}

From source file:org.jgap.gp.function.statistics.Variance.java

@Override
public float execute_float(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_float(n, i, args));
    }//from w w w. jav a 2 s . co  m
    return (float) stats.getVariance();
}

From source file:org.matsim.contrib.socnetgen.sna.graph.analysis.AnalyzerTask.java

protected void printStats(DescriptiveStatistics stats, String key) {
    logger.info(String.format(/* w ww . j a  v  a2  s . com*/
            "Statistics for property %1$s:\n\tmean = %2$.4f, min = %3$.4f, max = %4$.4f, N = %5$s, Var = %6$.4f",
            key, stats.getMean(), stats.getMin(), stats.getMax(), stats.getN(), stats.getVariance()));
}

From source file:org.openehealth.ipf.commons.test.performance.processingtime.ProcessingTimeDescriptiveStatistics.java

/**
 * Returns statistical summary of all the data.
 * //ww  w. j av a2s .c  om
 * @return a <code>StatisticalSummary</code> object
 */
@Override
public StatisticalSummary getStatisticalSummaryByName(String name) {
    DescriptiveStatistics stats = statisticsByMeasurementName.get(name);

    return new StatisticalSummaryValues(stats.getMean(), stats.getVariance(), stats.getN(), stats.getMax(),
            stats.getMin(), stats.getSum());
}

From source file:playground.johannes.gsv.analysis.CountsCompareAnalyzer.java

@Override
public void notifyAfterMobsim(AfterMobsimEvent event) {
    Network network = event.getControler().getScenario().getNetwork();
    DescriptiveStatistics error = new DescriptiveStatistics();
    DescriptiveStatistics errorAbs = new DescriptiveStatistics();
    DescriptivePiStatistics errorWeighted = new WSMStatsFactory().newInstance();

    TDoubleArrayList errorVals = new TDoubleArrayList();
    TDoubleArrayList caps = new TDoubleArrayList();
    TDoubleArrayList speeds = new TDoubleArrayList();

    for (Count count : counts.getCounts().values()) {
        if (!count.getLocId().toString().startsWith(ODCalibrator.VIRTUAL_ID_PREFIX)) {
            double obsVal = 0;
            for (int i = 1; i < 25; i++) {
                obsVal += count.getVolume(i).getValue();
            }/*from   w  w w  . j ava  2 s . c  o m*/

            if (obsVal > 0) {
                double simVal = calculator.getOccupancy(count.getLocId());
                simVal *= factor;

                double err = (simVal - obsVal) / obsVal;

                error.addValue(err);
                errorAbs.addValue(Math.abs(err));
                errorWeighted.addValue(Math.abs(err), 1 / obsVal);

                Link link = network.getLinks().get(count.getLocId());
                errorVals.add(Math.abs(err));
                caps.add(link.getCapacity());
                speeds.add(link.getFreespeed());
            }
        }
    }

    logger.info(String.format("Relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            error.getMean(), error.getVariance(), error.getStandardDeviation(), error.getMin(),
            error.getMax()));
    logger.info(String.format(
            "Absolute relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            errorAbs.getMean(), errorAbs.getVariance(), errorAbs.getStandardDeviation(), errorAbs.getMin(),
            errorAbs.getMax()));
    logger.info(String.format(
            "Absolute weigthed relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            errorWeighted.getMean(), errorWeighted.getVariance(), errorWeighted.getStandardDeviation(),
            errorWeighted.getMin(), errorWeighted.getMax()));

    String outdir = event.getControler().getControlerIO().getIterationPath(event.getIteration());

    try {
        TDoubleDoubleHashMap map = Correlations.mean(caps.toNativeArray(), errorVals.toNativeArray());
        TXTWriter.writeMap(map, "capacity", "counts", String.format("%s/countsError.capacity.txt", outdir));

        map = Correlations.mean(speeds.toNativeArray(), errorVals.toNativeArray());
        TXTWriter.writeMap(map, "speed", "counts", String.format("%s/countsError.speed.txt", outdir));

        TXTWriter.writeMap(Histogram.createHistogram(error, new LinearDiscretizer(0.1), false), "Error",
                "Frequency", String.format("%s/countsError.hist.txt", outdir));
        TXTWriter.writeMap(Histogram.createHistogram(errorAbs, new LinearDiscretizer(0.1), false),
                "Error (absolute)", "Frequency", String.format("%s/countsErrorAbs.hist.txt", outdir));
        TXTWriter.writeMap(Histogram.createHistogram(errorWeighted, new LinearDiscretizer(0.1), true),
                "Error (weighted)", "Frequency", String.format("%s/countsErrorWeighted.hist.txt", outdir));

        CountsCompare2GeoJSON.write(calculator, counts, factor, network, outdir);
        NetworkLoad2GeoJSON.write(event.getControler().getScenario().getNetwork(), calculator, factor,
                outdir + "/network.json");
    } catch (IOException e) {
        e.printStackTrace();
    }

    String rootOutDir = event.getControler().getControlerIO().getOutputPath();
    boolean append = false;
    if (event.getIteration() > 0) {
        append = true;
    }
    writeErrorFile(error, String.format("%s/countsError.txt", rootOutDir), append);
    writeErrorFile(errorAbs, String.format("%s/countsAbsError.txt", rootOutDir), append);
}

From source file:playground.johannes.gsv.analysis.CountsCompareAnalyzer.java

private void writeErrorFile(DescriptiveStatistics error, String file, boolean append) {
    try {/*from ww  w  .jav  a2 s. co  m*/
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, append));
        if (!append) {
            // write header
            writer.write("mean\tvar\tstderr\tmin\tmax");
            writer.newLine();
        }

        writer.write(String.valueOf(error.getMean()));
        writer.write("\t");
        writer.write(String.valueOf(error.getVariance()));
        writer.write("\t");
        writer.write(String.valueOf(error.getStandardDeviation()));
        writer.write("\t");
        writer.write(String.valueOf(error.getMin()));
        writer.write("\t");
        writer.write(String.valueOf(error.getMax()));
        writer.newLine();

        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.johannes.gsv.matrices.analysis.NUTSCompare.java

private static void printStats(DescriptiveStatistics stats, String name) {
    logger.info(String.format("%s : mean = %.2f, median = %.2f, var = %.2f, min = %.2f, max = %.2f", name,
            stats.getMean(), stats.getPercentile(50), stats.getVariance(), stats.getMin(), stats.getMax()));
}