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

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

Introduction

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

Prototype

public void addValue(double v) 

Source Link

Document

Adds the value to the dataset.

Usage

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

public Double getAverage(Double[] data, Integer windowSize) {
    // Create a DescriptiveStats instance and set the window size to 100
    DescriptiveStatistics stats = new DescriptiveStatistics();
    stats.setWindowSize(windowSize);/*from ww  w .ja  va  2  s  .co m*/

    for (int i = 0; i < data.length; i++) {
        if (data[i] != null) {
            stats.addValue(data[i]);
        } else {
            break;
        }
    }
    return stats.getMean();
}

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);//from www  .  j  a  v  a 2  s  .co  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.mot.common.math.CalculatorFactory.java

public Double getMax(Double[] data) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

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

        if (data[i] != null) {
            stats.addValue((Double) data[i]);
        } else {/*w  w w  .j  a  v  a 2  s.c  o  m*/
            break;
        }
    }

    return stats.getMax();
}

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

public Double getMin(Double[] data) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

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

        if (data[i] != null) {
            stats.addValue((Double) data[i]);
        } else {//from   w  ww .  j  a  v  a  2s.c  om
            break;
        }
    }
    return stats.getMin();
}

From source file:org.orbisgis.view.toc.actions.cui.legend.ui.PnlAbstractCategorized.java

private Thresholds computeStats(String fieldName) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    try (Connection connection = getDataSource().getConnection();
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery("select " + TableLocation.quoteIdentifier(fieldName) + " from "
                    + getTable() + " where " + TableLocation.quoteIdentifier(fieldName) + " is not null")) {
        while (rs.next()) {
            stats.addValue(rs.getDouble(1));
        }//from   w w  w.ja  va2 s  .c o  m
    } catch (SQLException e) {
        LOGGER.warn(I18N.tr("The application has ended unexpectedly"), e);
    }
    return new Thresholds(stats, fieldName);
}

From source file:org.orbisgis.view.toc.actions.cui.legends.ui.PnlAbstractCategorized.java

private Thresholds computeStats(String fieldName) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    DataSource ds = getDataSource();
    try {//from  w  w  w.  j  a  v  a2s.  c om
        int fieldIndex = ds.getMetadata().getFieldIndex(fieldName);
        long rowCount = ds.getRowCount();
        for (long i = 0; i < rowCount; i++) {
            Value val = ds.getFieldValue(i, fieldIndex);
            stats.addValue(val.getAsDouble());
        }
    } catch (DriverException e) {
        LOGGER.warn(I18N.tr("The application has ended unexpectedly"));
    }
    return new Thresholds(stats, fieldName);
}

From source file:org.pascani.dsl.lib.util.EventStats.java

/**
 * Adds the events' (numerical) values to a {@link DescriptiveStatistics}
 * instance.// w  w w.j a  va2  s  . com
 * 
 * @return an object maintaining a dataset of values of a single statistical
 *         variable to compute descriptive statistics based on the events'
 *         values
 */
public DescriptiveStatistics statistics() {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    for (Event<? extends Number> event : this.events) {
        stats.addValue(event.value().doubleValue());
    }

    return stats;
}

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

private void setValuesForTextFields() {

    // 1. Get luminance scale.
    LuminanceScale luminanceScale;/*from  w  w  w  . j  ava 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/*  ww w.j a  v  a 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.wattdepot.server.http.api.DepositoryAverageValuesServer.java

/**
 * retrieve the depository measurement list for a sensor.
 * //  w ww .ja  v a  2 s .  co  m
 * @return measurement list.
 */
public InterpolatedValueList doRetrieve() {
    getLogger().log(Level.INFO,
            "GET /wattdepot/{" + orgId + "}/" + Labels.DEPOSITORY + "/{" + depositoryId + "}/" + Labels.VALUES
                    + "/" + Labels.AVERAGE + "/?" + Labels.SENSOR + "={" + sensorId + "}&" + Labels.START + "={"
                    + start + "}&" + Labels.END + "={" + end + "}&" + Labels.INTERVAL + "={" + interval + "}&"
                    + Labels.VALUE_TYPE + "={" + dataType + "}");
    if (isInRole(orgId)) {
        if (start != null && end != null && interval != null) {
            InterpolatedValueList ret = new InterpolatedValueList();
            try {
                Depository depository = depot.getDepository(depositoryId, orgId, true);
                XMLGregorianCalendar startRange = DateConvert.parseCalString(start);
                XMLGregorianCalendar endRange = DateConvert.parseCalString(end);
                int intervalMinutes = Integer.parseInt(interval);
                List<XMLGregorianCalendar> rangeList = Tstamp.getTimestampList(startRange, endRange,
                        intervalMinutes);
                if (rangeList != null) {
                    for (int i = 1; i < rangeList.size(); i++) {
                        Date valueDate = DateConvert.convertXMLCal(rangeList.get(i));
                        XMLGregorianCalendar startInterval = rangeList.get(i - 1);
                        XMLGregorianCalendar endInterval = rangeList.get(i);
                        List<XMLGregorianCalendar> intervalList = Tstamp.getNTimestampList(24, startInterval,
                                endInterval);
                        DescriptiveStatistics stats = new DescriptiveStatistics();
                        Date previous = null;
                        for (int j = 0; j < intervalList.size(); j++) {
                            Date timestamp = DateConvert.convertXMLCal(intervalList.get(j));
                            Double value = null;
                            if ("point".equals(dataType)) {
                                value = getValue(depositoryId, orgId, sensorId, timestamp);
                            } else {
                                if (previous != null) {
                                    value = getValue(depositoryId, orgId, sensorId, previous, timestamp);
                                }
                                previous = timestamp;
                            }
                            if (value != null) {
                                stats.addValue(value);
                            }
                        }

                        if (!Double.isNaN(stats.getMean())) {
                            ret.getInterpolatedValues().add(new InterpolatedValue(sensorId, stats.getMean(),
                                    depository.getMeasurementType(), valueDate));
                        }
                    }
                }
            } catch (IdNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (DatatypeConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ret;
        } else {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Missing start and/or end times or interval.");
            return null;
        }
    } else {
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Bad credentials.");
        return null;
    }
}