Example usage for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getStandardDeviation

List of usage examples for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getStandardDeviation

Introduction

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

Prototype

public double getStandardDeviation() 

Source Link

Document

Returns the standard deviation of the available values.

Usage

From source file:org.matsim.contrib.taxi.util.stats.TaxiStatsWriter.java

private void addStats(CSVLineBuilder lineBuilder, String format1, String format2, DescriptiveStatistics stats) {
    lineBuilder.addf(format1, stats.getMean()).//
            addf(format1, stats.getStandardDeviation()).//
            addEmpty().//
            addf(format2, stats.getMin()). //
            addf(format2, stats.getPercentile(2)). //
            addf(format2, stats.getPercentile(5)). //
            addf(format2, stats.getPercentile(25)). //
            addf(format2, stats.getPercentile(50)). //
            addf(format2, stats.getPercentile(75)). //
            addf(format2, stats.getPercentile(95)). //
            addf(format2, stats.getPercentile(98)). //
            addf(format2, stats.getMax());
}

From source file:org.mot.common.math.CalculatorFactory.java

public Double getTimeWindowDeviation(Number[] data, Integer windowSize) {
    // Create a DescriptiveStats instance and set the window size to 100
    DescriptiveStatistics stats = new DescriptiveStatistics();
    stats.setWindowSize(windowSize);/* w  ww . j a va2  s . c  o  m*/

    if (windowSize == 0) {
        windowSize = data.length;
    }

    for (int i = 0; i < data.length; i++) {

        if (data[i] != null) {
            stats.addValue((Double) data[i]);
        } else {
            break;
        }
    }

    return stats.getStandardDeviation();
}

From source file:org.mskcc.cbio.portal.scripts.ImportProteinArrayData.java

private double[] convertToZscores(String[] strs) {
    double[] data = new double[strs.length - 1];
    boolean nan = false;
    for (int i = 1; i < strs.length; i++) { // ignore the first column
        try {/*from   w w  w . j  ava2s  .  c om*/
            data[i - 1] = Double.parseDouble(strs[i]);
        } catch (Exception e) {
            data[i - 1] = Double.NaN;
            nan = true;
        }
    }

    DescriptiveStatistics ds = new DescriptiveStatistics(nan ? copyWithNoNaN(data) : data);
    double mean = ds.getMean();
    double std = ds.getStandardDeviation();

    for (int i = 0; i < data.length; i++) {
        if (!Double.isNaN(data[i])) {
            data[i] = (data[i] - mean) / std;
        }
    }
    return data;
}

From source file:org.obiba.opal.web.magma.Dtos.java

@SuppressWarnings({ "OverlyLongMethod", "PMD.NcssMethodCount" })
public static Math.ContinuousSummaryDto.Builder asDto(ContinuousVariableSummary summary) {
    DescriptiveStatistics descriptiveStats = summary.getDescriptiveStats();

    Math.DescriptiveStatsDto.Builder descriptiveBuilder = Math.DescriptiveStatsDto.newBuilder()
            .setN(descriptiveStats.getN()).addAllPercentiles(summary.getPercentiles());

    if (isNumeric(descriptiveStats.getMin()))
        descriptiveBuilder.setMin(descriptiveStats.getMin());
    if (isNumeric(descriptiveStats.getMax()))
        descriptiveBuilder.setMax(descriptiveStats.getMax());
    if (isNumeric(descriptiveStats.getMean()))
        descriptiveBuilder.setMean(descriptiveStats.getMean());
    if (isNumeric(descriptiveStats.getSum()))
        descriptiveBuilder.setSum(descriptiveStats.getSum());
    if (isNumeric(descriptiveStats.getSumsq()))
        descriptiveBuilder.setSumsq(descriptiveStats.getSumsq());
    if (isNumeric(descriptiveStats.getStandardDeviation())) {
        descriptiveBuilder.setStdDev(descriptiveStats.getStandardDeviation());
    }//from   w  w  w  .  j  ava2  s.c  o m
    if (isNumeric(descriptiveStats.getVariance()))
        descriptiveBuilder.setVariance(descriptiveStats.getVariance());
    if (isNumeric(descriptiveStats.getSkewness()))
        descriptiveBuilder.setSkewness(descriptiveStats.getSkewness());
    if (isNumeric(descriptiveStats.getGeometricMean())) {
        descriptiveBuilder.setGeometricMean(descriptiveStats.getGeometricMean());
    }
    if (isNumeric(descriptiveStats.getKurtosis()))
        descriptiveBuilder.setKurtosis(descriptiveStats.getKurtosis());
    double median = descriptiveStats.apply(new Median());
    if (isNumeric(median))
        descriptiveBuilder.setMedian(median);
    if (isNumeric(descriptiveStats.getVariance()))
        descriptiveBuilder.setVariance(descriptiveStats.getVariance());

    Math.ContinuousSummaryDto.Builder continuousBuilder = Math.ContinuousSummaryDto.newBuilder()
            .addAllDistributionPercentiles(summary.getDistributionPercentiles());
    for (IntervalFrequency.Interval interval : summary.getIntervalFrequencies()) {
        Math.IntervalFrequencyDto.Builder freqBuilder = Math.IntervalFrequencyDto.newBuilder()
                .setFreq(interval.getFreq());
        if (isNumeric(interval.getLower()))
            freqBuilder.setLower(interval.getLower());
        if (isNumeric(interval.getUpper()))
            freqBuilder.setUpper(interval.getUpper());
        if (isNumeric(interval.getDensity()))
            freqBuilder.setDensity(interval.getDensity());
        if (isNumeric(interval.getDensityPct()))
            freqBuilder.setDensityPct(interval.getDensityPct());
        continuousBuilder.addIntervalFrequency(freqBuilder);
    }

    for (ContinuousVariableSummary.Frequency frequency : summary.getFrequencies()) {
        Math.FrequencyDto.Builder freqBuilder = Math.FrequencyDto.newBuilder() //
                .setValue(frequency.getValue()) //
                .setFreq(frequency.getFreq())//
                .setMissing(frequency.isMissing());
        if (isNumeric(frequency.getPct()))
            freqBuilder.setPct(frequency.getPct());
        continuousBuilder.addFrequencies(freqBuilder);
    }

    return continuousBuilder.setSummary(descriptiveBuilder);
}

From source file:org.specvis.view.screenandlumscale.ViewFitLumScaleController.java

private void setValuesForTextFields() {

    // 1. Get luminance scale.
    LuminanceScale luminanceScale;//  w w w. ja v  a 2 s .c o  m
    if (StartApplication.getSpecvisData().getUiSettingsScreenAndLuminanceScale()
            .isThisWindowOpenedForStimulus()) {
        luminanceScale = StartApplication.getSpecvisData().getUiSettingsScreenAndLuminanceScale()
                .getStimulusLuminanceScale();
    } else {
        luminanceScale = StartApplication.getSpecvisData().getUiSettingsScreenAndLuminanceScale()
                .getBackgroundLuminanceScale();
    }

    // 2. Get measured and fitted luminance values.
    double[] measuredLuminances = new double[] { luminanceScale.getLuminanceForBrightness0(),
            luminanceScale.getLuminanceForBrightness20(), luminanceScale.getLuminanceForBrightness40(),
            luminanceScale.getLuminanceForBrightness60(), luminanceScale.getLuminanceForBrightness80(),
            luminanceScale.getLuminanceForBrightness100() };

    double[] fittedLuminances = new double[] { luminanceScale.getFittedLuminanceForEachBrightnessValue()[0],
            luminanceScale.getFittedLuminanceForEachBrightnessValue()[20],
            luminanceScale.getFittedLuminanceForEachBrightnessValue()[40],
            luminanceScale.getFittedLuminanceForEachBrightnessValue()[60],
            luminanceScale.getFittedLuminanceForEachBrightnessValue()[80],
            luminanceScale.getFittedLuminanceForEachBrightnessValue()[100] };

    // 2.1. Set negative fitted values to 0.1. Chi-squared analysis don't "take" negative expected values.
    for (int i = 0; i < fittedLuminances.length; i++) {
        if (fittedLuminances[i] < 0.0) {
            fittedLuminances[i] = 0.1;
        }
    }

    // 3. Calculate difference between measured and fitted values.
    double[] differenceBetweenMeasuredAndFittedLuminances = new double[measuredLuminances.length];
    for (int i = 0; i < measuredLuminances.length; i++) {
        differenceBetweenMeasuredAndFittedLuminances[i] = functions
                .round(measuredLuminances[i] - fittedLuminances[i], 2);
    }

    // 4. Calculate standard deviation between measured and fitted values.
    DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
    for (double differenceBetweenMeasuredAndFittedLuminance : differenceBetweenMeasuredAndFittedLuminances) {
        descriptiveStatistics.addValue(differenceBetweenMeasuredAndFittedLuminance);
    }

    double standardDeviation = functions.round(descriptiveStatistics.getStandardDeviation(), 2);

    textFieldSTD.setText(String.valueOf(standardDeviation));

    // 4. Calculate Chi-squared and P-value for fitted luminance values.
    double[] expectedValues = fittedLuminances;
    double[] observedValues = measuredLuminances;
    int degreesOfFreedom = observedValues.length - 1;

    ChiSquared chiSquared = new ChiSquared();
    double criticalValue = functions.round(chiSquared.calculateCriticalValue(expectedValues, observedValues),
            4);
    double pValue = functions.round(chiSquared.calculateChiSquaredPValue(degreesOfFreedom, criticalValue), 4);

    textFieldChiSquared.setText(String.valueOf(criticalValue));
    textFieldPValue.setText(String.valueOf(pValue));
}

From source file:org.uma.jmetal.util.experiment.component.GenerateLatexTablesWithStatistics.java

/**
 * Computes the statistical values//www . java  2 s .  c om
 * @param values
 * @return
 */
private Map<String, Double> computeStatistics(List<Double> values) {
    Map<String, Double> results = new HashMap<>();

    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (Double value : values) {
        stats.addValue(value);
    }

    results.put("mean", stats.getMean());
    results.put("median", stats.getPercentile(50.0));
    results.put("stdDeviation", stats.getStandardDeviation());
    results.put("iqr", stats.getPercentile(75) - stats.getPercentile(25));
    results.put("max", stats.getMax());
    results.put("min", stats.getMean());
    results.put("numberOfElements", (double) values.size());

    return results;
}

From source file:org.wildfly.swarm.proc.CSVCollector.java

public void onFinish(String id) {

    List<Object> record = new ArrayList<>();
    record.add(id);/*from   ww  w.  j a  v a  2 s  .c o m*/
    record.add(Paths.get(id).getFileName());
    for (Measure m : Measure.values()) {
        if (!results.containsKey(m)) {
            throw new RuntimeException("Measurement is missing " + m);
        }

        DescriptiveStatistics stats = results.get(m);
        record.add(stats.getN());
        record.add(stats.getMin());
        record.add(stats.getMax());
        record.add(stats.getMean());
        record.add(stats.getStandardDeviation());
        record.add(stats.getPercentile(50));
        record.add(stats.getPercentile(75));
    }

    try {
        csvOutput.printRecord(record);
        csvOutput.flush();
    } catch (IOException e) {
        throw new RuntimeException("Failed to write data", e);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Create the JSON string with summary statistics for a column.
 *
 * @param type Data-type of the column/*from w  w w . ja v a  2 s.com*/
 * @param graphFrequencies Bin frequencies of the column
 * @param missing Number of missing values in the column
 * @param unique Number of unique values in the column
 * @param descriptiveStats DescriptiveStats object of the column
 * @return JSON representation of the summary statistics of the column
 */
private JSONArray createJson(String type, SortedMap<?, Integer> graphFrequencies, int missing, int unique,
        DescriptiveStatistics descriptiveStats) throws JSONException {

    JSONObject json = new JSONObject();
    JSONArray freqs = new JSONArray();
    Object[] categoryNames = graphFrequencies.keySet().toArray();
    // Create an array with intervals/categories and their frequencies.
    for (int i = 0; i < graphFrequencies.size(); i++) {
        JSONArray temp = new JSONArray();
        temp.put(categoryNames[i].toString());
        temp.put(graphFrequencies.get(categoryNames[i]));
        freqs.put(temp);
    }
    // Put the statistics to a json object
    json.put("unique", unique);
    json.put("missing", missing);

    DecimalFormat decimalFormat = new DecimalFormat("#.###");
    if (descriptiveStats.getN() != 0) {
        json.put("mean", decimalFormat.format(descriptiveStats.getMean()));
        json.put("min", decimalFormat.format(descriptiveStats.getMin()));
        json.put("max", decimalFormat.format(descriptiveStats.getMax()));
        json.put("median", decimalFormat.format(descriptiveStats.getPercentile(50)));
        json.put("std", decimalFormat.format(descriptiveStats.getStandardDeviation()));
        if (type.equalsIgnoreCase(FeatureType.NUMERICAL)) {
            json.put("skewness", decimalFormat.format(descriptiveStats.getSkewness()));
        }
    }
    json.put("values", freqs);
    json.put("bar", true);
    json.put("key", "Frequency");
    JSONArray summaryStatArray = new JSONArray();
    summaryStatArray.put(json);
    return summaryStatArray;
}

From source file:org.wso2.carbon.ml.dataset.internal.DatabaseHandler.java

/**
 * Create the JSON string with summary statistics for a column.
 *
 * @param type              Data-type of the column
 * @param graphFrequencies  Bin frequencies of the column
 * @param missing           Number of missing values in the column
 * @param unique            Number of unique values in the column
 * @param descriptiveStats  DescriptiveStats object of the column
 * @return                  JSON representation of the summary statistics of the column
 *///  www.j a v  a2  s. co  m
private JSONArray createJson(String type, SortedMap<?, Integer> graphFrequencies, int missing, int unique,
        DescriptiveStatistics descriptiveStats) {
    JSONObject json = new JSONObject();
    JSONArray freqs = new JSONArray();
    Object[] categoryNames = graphFrequencies.keySet().toArray();
    // Create an array with intervals/categories and their frequencies.
    for (int i = 0; i < graphFrequencies.size(); i++) {
        JSONArray temp = new JSONArray();
        temp.put(categoryNames[i].toString());
        temp.put(graphFrequencies.get(categoryNames[i]));
        freqs.put(temp);
    }
    // Put the statistics to a json object
    json.put("unique", unique);
    json.put("missing", missing);

    DecimalFormat decimalFormat = new DecimalFormat("#.###");
    if (descriptiveStats.getN() != 0) {
        json.put("mean", decimalFormat.format(descriptiveStats.getMean()));
        json.put("median", decimalFormat.format(descriptiveStats.getPercentile(50)));
        json.put("std", decimalFormat.format(descriptiveStats.getStandardDeviation()));
        if (type.equalsIgnoreCase(FeatureType.NUMERICAL)) {
            json.put("skewness", decimalFormat.format(descriptiveStats.getSkewness()));
        }
    }
    json.put("values", freqs);
    json.put("bar", true);
    json.put("key", "Frequency");
    JSONArray summaryStatArray = new JSONArray();
    summaryStatArray.put(json);
    return summaryStatArray;
}

From source file:performancestatisticsasset.DistributionSet.java

public void setDistributionSet(String targetGroup, String targetTask, String targetMeasure,
        RecordList dataSet) {/*from   w w  w.  j ava 2 s  .  c  o m*/
    //Determine the number of trials selected by group and by task
    int trials = 0;
    int len = dataSet.records.size();
    for (int i = 0; i < len; i++) {
        if (dataSet.records.get(i).getTrialNumber() > trials)
            trials = dataSet.records.get(i).getTrialNumber();
    }

    Distribution tempDist;
    DescriptiveStatistics tempStat;
    //For each trial of the set do
    for (int i = 0; i < trials; i++) {
        tempDist = new Distribution();
        tempStat = new DescriptiveStatistics();
        //Select data
        for (int j = 0; j < len; j++) {
            //If the current record is of the correct trial
            if ((dataSet.records.get(j).getTrialNumber() == i + 1)
                    && (targetGroup.equals(dataSet.records.get(j).getGroupID()))
                    && (targetTask.equals(dataSet.records.get(j).getTaskID()))) {
                //Fill distribution
                switch (targetMeasure) {
                case "time":
                    tempStat.addValue(dataSet.records.get(j).getTimeToComplete());
                    break;
                case "perf":
                    tempStat.addValue(dataSet.records.get(j).getPerformance());
                    break;
                }
            }
        }
        //Transfer the computed statistics to tempDist
        tempDist.max = tempStat.getMax();
        tempDist.min = tempStat.getMin();
        tempDist.sum = tempStat.getSum();
        tempDist.variance = tempStat.getVariance();
        tempDist.mean = tempStat.getMean();
        tempDist.stdDev = tempStat.getStandardDeviation();
        tempDist.skewness = tempStat.getSkewness();
        tempDist.kurtosis = tempStat.getKurtosis();
        tempDist.n = tempStat.getN();

        //Add tempDist to distributionSet
        distributionSet.add(tempDist);
    }
}