Example usage for org.apache.commons.math3.stat.descriptive SummaryStatistics getMean

List of usage examples for org.apache.commons.math3.stat.descriptive SummaryStatistics getMean

Introduction

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

Prototype

public double getMean() 

Source Link

Document

Returns the mean of the values that have been added.

Usage

From source file:org.eclipse.dataset.AbstractDataset.java

@Override
public Number rootMeanSquare() {
    final SummaryStatistics stats = getStatistics(false);
    final double mean = stats.getMean();
    return Math.sqrt(stats.getVariance() + mean * mean);
}

From source file:org.eclipse.dataset.ComplexDoubleDataset.java

@Override
public Object mean(boolean... switches) {
    final String n = storeName(false, STORE_STATS_ITEM_NAME);
    if (storedValues == null || storedValues.isEmpty()) {
        calculateSummaryStats(false, false, n);
    }/*from   w  ww .  j  a  v  a 2s .  c om*/

    final SummaryStatistics rstats = (SummaryStatistics) storedValues.get(n + "0");
    final SummaryStatistics istats = (SummaryStatistics) storedValues.get(n + "1");
    return new Complex(rstats.getMean(), istats.getMean());
}

From source file:org.eclipse.january.dataset.AbstractDataset.java

/**
 * Calculate summary statistics for a dataset along an axis
 * @param ignoreNaNs if true, ignore NaNs
 * @param ignoreInfs if true, ignore infinities
 * @param axis/*from   w w w  .j av  a 2s  . co  m*/
 */
protected void calculateSummaryStats(final boolean ignoreNaNs, final boolean ignoreInfs, final int axis) {
    int rank = getRank();

    int[] oshape = getShape();
    int alen = oshape[axis];
    oshape[axis] = 1;

    int[] nshape = new int[rank - 1];
    for (int i = 0; i < axis; i++) {
        nshape[i] = oshape[i];
    }
    for (int i = axis + 1; i < rank; i++) {
        nshape[i - 1] = oshape[i];
    }

    final int dtype = getDType();
    IntegerDataset count = new IntegerDataset(nshape);
    Dataset max = DatasetFactory.zeros(nshape, dtype);
    Dataset min = DatasetFactory.zeros(nshape, dtype);
    IntegerDataset maxIndex = new IntegerDataset(nshape);
    IntegerDataset minIndex = new IntegerDataset(nshape);
    Dataset sum = DatasetFactory.zeros(nshape, DTypeUtils.getLargestDType(dtype));
    DoubleDataset mean = new DoubleDataset(nshape);
    DoubleDataset var = new DoubleDataset(nshape);

    IndexIterator qiter = max.getIterator(true);
    int[] qpos = qiter.getPos();
    int[] spos = oshape.clone();

    while (qiter.hasNext()) {
        int i = 0;
        for (; i < axis; i++) {
            spos[i] = qpos[i];
        }
        spos[i++] = 0;
        for (; i < rank; i++) {
            spos[i] = qpos[i - 1];
        }

        final SummaryStatistics stats = new SummaryStatistics();
        //sum of logs is slow and we dont use it, so blocking its calculation here
        stats.setSumLogImpl(new NullStorelessUnivariateStatistic());

        double amax = Double.NEGATIVE_INFINITY;
        double amin = Double.POSITIVE_INFINITY;
        boolean hasNaNs = false;
        if (ignoreNaNs) {
            for (int j = 0; j < alen; j++) {
                spos[axis] = j;
                final double val = getDouble(spos);

                if (Double.isNaN(val)) {
                    hasNaNs = true;
                    continue;
                } else if (ignoreInfs && Double.isInfinite(val)) {
                    continue;
                }

                if (val > amax) {
                    amax = val;
                }
                if (val < amin) {
                    amin = val;
                }

                stats.addValue(val);
            }
        } else {
            for (int j = 0; j < alen; j++) {
                spos[axis] = j;
                final double val = getDouble(spos);

                if (hasNaNs) {
                    if (!Double.isNaN(val))
                        stats.addValue(0);
                    continue;
                }

                if (Double.isNaN(val)) {
                    amax = Double.NaN;
                    amin = Double.NaN;
                    hasNaNs = true;
                } else if (ignoreInfs && Double.isInfinite(val)) {
                    continue;
                } else {
                    if (val > amax) {
                        amax = val;
                    }
                    if (val < amin) {
                        amin = val;
                    }
                }
                stats.addValue(val);
            }
        }

        count.setAbs(qiter.index, (int) stats.getN());

        max.setObjectAbs(qiter.index, amax);
        min.setObjectAbs(qiter.index, amin);
        boolean fmax = false;
        boolean fmin = false;
        if (hasNaNs) {
            if (ignoreNaNs) {
                for (int j = 0; j < alen; j++) {
                    spos[axis] = j;
                    final double val = getDouble(spos);
                    if (Double.isNaN(val))
                        continue;

                    if (!fmax && val == amax) {
                        maxIndex.setAbs(qiter.index, j);
                        fmax = true;
                        if (fmin)
                            break;
                    }
                    if (!fmin && val == amin) {
                        minIndex.setAbs(qiter.index, j);
                        fmin = true;
                        if (fmax)
                            break;
                    }
                }
            } else {
                for (int j = 0; j < alen; j++) {
                    spos[axis] = j;
                    final double val = getDouble(spos);
                    if (Double.isNaN(val)) {
                        maxIndex.setAbs(qiter.index, j);
                        minIndex.setAbs(qiter.index, j);
                        break;
                    }
                }
            }
        } else {
            for (int j = 0; j < alen; j++) {
                spos[axis] = j;
                final double val = getDouble(spos);
                if (!fmax && val == amax) {
                    maxIndex.setAbs(qiter.index, j);
                    fmax = true;
                    if (fmin)
                        break;
                }
                if (!fmin && val == amin) {
                    minIndex.setAbs(qiter.index, j);
                    fmin = true;
                    if (fmax)
                        break;
                }
            }
        }
        sum.setObjectAbs(qiter.index, stats.getSum());
        mean.setAbs(qiter.index, stats.getMean());
        var.setAbs(qiter.index, stats.getVariance());
    }
    setStoredValue(storeName(ignoreNaNs, ignoreInfs, STORE_COUNT + "-" + axis), count);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_MAX + "-" + axis), max);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_MIN + "-" + axis), min);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_SUM + "-" + axis), sum);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_MEAN + "-" + axis), mean);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_VAR + "-" + axis), var);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_MAX + STORE_INDEX + "-" + axis), maxIndex);
    storedValues.put(storeName(ignoreNaNs, ignoreInfs, STORE_MIN + STORE_INDEX + "-" + axis), minIndex);
}

From source file:org.eclipse.january.metadata.internal.StatisticsMetadataImpl.java

/**
 * Calculate summary statistics for a dataset along an axis
 * @param ignoreNaNs if true, ignore NaNs
 * @param ignoreInfs if true, ignore infinities
 * @param axis/*  w  w w  . j av  a  2 s.c om*/
 */
@SuppressWarnings("deprecation")
private Dataset[] createAxisStats(final int axis, final boolean ignoreNaNs, final boolean ignoreInfs) {
    int rank = dataset.getRank();

    int[] oshape = dataset.getShape();
    int alen = oshape[axis];
    oshape[axis] = 1;

    int[] nshape = new int[rank - 1];
    for (int i = 0; i < axis; i++) {
        nshape[i] = oshape[i];
    }
    for (int i = axis + 1; i < rank; i++) {
        nshape[i - 1] = oshape[i];
    }

    Dataset max;
    Dataset min;
    IntegerDataset maxIndex;
    IntegerDataset minIndex;
    LongDataset count = DatasetFactory.zeros(LongDataset.class, nshape);
    Dataset sum;
    Dataset mean;
    Dataset var;

    if (isize == 1) {
        max = DatasetFactory.zeros(nshape, dtype);
        min = DatasetFactory.zeros(nshape, dtype);
        maxIndex = DatasetFactory.zeros(IntegerDataset.class, nshape);
        minIndex = DatasetFactory.zeros(IntegerDataset.class, nshape);
        sum = DatasetFactory.zeros(nshape, DTypeUtils.getLargestDType(dtype));
        mean = DatasetFactory.zeros(DoubleDataset.class, nshape);
        var = DatasetFactory.zeros(DoubleDataset.class, nshape);
    } else {
        max = null;
        min = null;
        maxIndex = null;
        minIndex = null;
        sum = DatasetFactory.zeros(isize, nshape, DTypeUtils.getLargestDType(dtype));
        mean = DatasetFactory.zeros(isize, CompoundDoubleDataset.class, nshape);
        var = DatasetFactory.zeros(isize, CompoundDoubleDataset.class, nshape);
    }

    IndexIterator qiter = count.getIterator(true);
    int[] qpos = qiter.getPos();
    int[] spos = oshape.clone();

    if (isize == 1) {
        DoubleDataset lmean = (DoubleDataset) mean;
        DoubleDataset lvar = (DoubleDataset) var;

        final SummaryStatistics stats = new SummaryStatistics();
        while (qiter.hasNext()) {
            int i = 0;
            for (; i < axis; i++) {
                spos[i] = qpos[i];
            }
            spos[i++] = 0;
            for (; i < rank; i++) {
                spos[i] = qpos[i - 1];
            }

            stats.clear();
            //sum of logs is slow and we dont use it, so blocking its calculation here
            stats.setSumLogImpl(new NullStorelessUnivariateStatistic());

            double amax = Double.NEGATIVE_INFINITY;
            double amin = Double.POSITIVE_INFINITY;
            boolean hasNaNs = false;
            if (ignoreNaNs) {
                for (int j = 0; j < alen; j++) {
                    spos[axis] = j;
                    final double val = dataset.getDouble(spos);

                    if (Double.isNaN(val)) {
                        hasNaNs = true;
                        continue;
                    } else if (ignoreInfs && Double.isInfinite(val)) {
                        continue;
                    }

                    if (val > amax) {
                        amax = val;
                    }
                    if (val < amin) {
                        amin = val;
                    }

                    stats.addValue(val);
                }
            } else {
                for (int j = 0; j < alen; j++) {
                    spos[axis] = j;
                    final double val = dataset.getDouble(spos);

                    if (hasNaNs) {
                        if (!Double.isNaN(val))
                            stats.addValue(0);
                        continue;
                    }

                    if (Double.isNaN(val)) {
                        amax = Double.NaN;
                        amin = Double.NaN;
                        hasNaNs = true;
                    } else if (ignoreInfs && Double.isInfinite(val)) {
                        continue;
                    } else {
                        if (val > amax) {
                            amax = val;
                        }
                        if (val < amin) {
                            amin = val;
                        }
                    }
                    stats.addValue(val);
                }
            }

            count.setAbs(qiter.index, stats.getN());

            max.set(amax, qpos);
            min.set(amin, qpos);
            boolean fmax = false;
            boolean fmin = false;
            if (hasNaNs) {
                if (ignoreNaNs) {
                    for (int j = 0; j < alen; j++) {
                        spos[axis] = j;
                        final double val = dataset.getDouble(spos);
                        if (Double.isNaN(val))
                            continue;

                        if (!fmax && val == amax) { // FIXME qiter.index is wrong!!!
                            maxIndex.setAbs(qiter.index, j);
                            fmax = true;
                            if (fmin)
                                break;
                        }
                        if (!fmin && val == amin) {
                            minIndex.setAbs(qiter.index, j);
                            fmin = true;
                            if (fmax)
                                break;
                        }
                    }
                } else {
                    for (int j = 0; j < alen; j++) {
                        spos[axis] = j;
                        final double val = dataset.getDouble(spos);
                        if (Double.isNaN(val)) {
                            maxIndex.setAbs(qiter.index, j);
                            minIndex.setAbs(qiter.index, j);
                            break;
                        }
                    }
                }
            } else {
                for (int j = 0; j < alen; j++) {
                    spos[axis] = j;
                    final double val = dataset.getDouble(spos);
                    if (!fmax && val == amax) {
                        maxIndex.setAbs(qiter.index, j);
                        fmax = true;
                        if (fmin)
                            break;
                    }
                    if (!fmin && val == amin) {
                        minIndex.setAbs(qiter.index, j);
                        fmin = true;
                        if (fmax)
                            break;
                    }
                }
            }
            sum.setObjectAbs(qiter.index, stats.getSum());
            lmean.setAbs(qiter.index, stats.getMean());
            lvar.setAbs(qiter.index, stats.getVariance());
        }
    } else {
        CompoundDataset ldataset = (CompoundDataset) dataset;
        CompoundDoubleDataset lmean = (CompoundDoubleDataset) mean;
        CompoundDoubleDataset lvar = (CompoundDoubleDataset) var;
        double[] darray = new double[isize];

        while (qiter.hasNext()) {
            int i = 0;
            for (; i < axis; i++) {
                spos[i] = qpos[i];
            }
            spos[i++] = 0;
            for (; i < rank; i++) {
                spos[i] = qpos[i - 1];
            }

            final SummaryStatistics[] stats = new SummaryStatistics[isize];
            for (int k = 0; k < isize; k++) {
                stats[k] = new SummaryStatistics();
            }
            for (int j = 0; j < alen; j++) {
                spos[axis] = j;
                ldataset.getDoubleArray(darray, spos);
                boolean skip = false;
                for (int k = 0; k < isize; k++) {
                    double v = darray[k];
                    if (ignoreNaNs && Double.isNaN(v)) {
                        skip = true;
                        break;
                    }
                    if (ignoreInfs && Double.isInfinite(v)) {
                        skip = true;
                        break;
                    }
                }
                if (!skip)
                    for (int k = 0; k < isize; k++) {
                        stats[k].addValue(darray[k]);
                    }
            }

            count.setAbs(qiter.index, (int) stats[0].getN());

            for (int k = 0; k < isize; k++) {
                darray[k] = stats[k].getSum();
            }
            sum.set(darray, qpos);
            for (int k = 0; k < isize; k++) {
                darray[k] = stats[k].getMean();
            }
            lmean.setItem(darray, qpos);
            for (int k = 0; k < isize; k++) {
                darray[k] = stats[k].getVariance();
            }
            lvar.setItem(darray, qpos);
        }
    }

    return new Dataset[] { max, min, maxIndex, minIndex, count, mean, sum, var };
}

From source file:org.hoidla.window.WindowUtils.java

/**
 * Identifies outliers/*from   ww  w.  java2 s . co  m*/
 * @param data
 * @param outlierThresholdFactor
 * @param pattern
 * @return indexes outlying data points
 */
public static List<Integer> removeOutliers(double[] data, int outlierThresholdFactor, double[] pattern) {
    if (null != pattern && data.length != pattern.length) {
        throw new IllegalArgumentException("data and pattern need to be of same size");
    }

    //stats
    SummaryStatistics stats = new SummaryStatistics();
    for (double value : data) {
        stats.addValue(value);
    }
    double mean = stats.getMean();
    double stdDev = stats.getStandardDeviation();

    //thresholds
    double upThreshold = mean + outlierThresholdFactor * stdDev;
    double loThreshold = mean - outlierThresholdFactor * stdDev;

    //detect outliers
    List<Integer> outliers = new ArrayList<Integer>();
    int i = 0;
    for (double value : data) {
        if (value > upThreshold || value < loThreshold) {
            //replace with pattern value so that there is no net effect
            if (null != pattern) {
                data[i] = pattern[i];
            }
            outliers.add(i);
        }
        ++i;
    }

    return outliers;
}

From source file:org.lightjason.agentspeak.action.buildin.math.statistic.EStatisticValue.java

/**
 * returns a statistic value/* ww w .  ja va 2s.co  m*/
 *
 * @param p_statistic statistic object
 * @return statistic value
 */
public final double value(final SummaryStatistics p_statistic) {
    switch (this) {
    case GEOMETRICMEAN:
        return p_statistic.getGeometricMean();

    case MAX:
        return p_statistic.getMax();

    case MIN:
        return p_statistic.getMin();

    case COUNT:
        return p_statistic.getN();

    case POPULATIONVARIANCE:
        return p_statistic.getPopulationVariance();

    case QUADRATICMEAN:
        return p_statistic.getQuadraticMean();

    case SECONDMOMENT:
        return p_statistic.getSecondMoment();

    case STANDARDDEVIATION:
        return p_statistic.getStandardDeviation();

    case SUM:
        return p_statistic.getSum();

    case SUMLOG:
        return p_statistic.getSumOfLogs();

    case SUMSQUARE:
        return p_statistic.getSumsq();

    case VARIANCE:
        return p_statistic.getVariance();

    case MEAN:
        return p_statistic.getMean();

    default:
        throw new CIllegalStateException(
                org.lightjason.agentspeak.common.CCommon.languagestring(this, "unknown", this));
    }
}

From source file:org.lightjason.agentspeak.action.builtin.math.statistic.EStatisticValue.java

/**
 * returns a statistic value//from  ww  w  .j a va2  s .  com
 *
 * @param p_statistic statistic object
 * @return statistic value
 */
public final double value(@Nonnull final SummaryStatistics p_statistic) {
    switch (this) {
    case GEOMETRICMEAN:
        return p_statistic.getGeometricMean();

    case MAX:
        return p_statistic.getMax();

    case MIN:
        return p_statistic.getMin();

    case COUNT:
        return p_statistic.getN();

    case POPULATIONVARIANCE:
        return p_statistic.getPopulationVariance();

    case QUADRATICMEAN:
        return p_statistic.getQuadraticMean();

    case SECONDMOMENT:
        return p_statistic.getSecondMoment();

    case STANDARDDEVIATION:
        return p_statistic.getStandardDeviation();

    case SUM:
        return p_statistic.getSum();

    case SUMLOG:
        return p_statistic.getSumOfLogs();

    case SUMSQUARE:
        return p_statistic.getSumsq();

    case VARIANCE:
        return p_statistic.getVariance();

    case MEAN:
        return p_statistic.getMean();

    default:
        throw new CIllegalStateException(
                org.lightjason.agentspeak.common.CCommon.languagestring(this, "unknown", this));
    }
}

From source file:org.nmdp.ngs.reads.coverage.MeanCoverageStrategy.java

@Override
protected boolean evaluate(final Sequence reference, final SymbolList coverage) {
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    for (int i = 1, size = coverage.length() + 1; i < size; i++) {
        int c = ((IntegerAlphabet.IntegerSymbol) coverage.symbolAt(i)).intValue();
        summaryStatistics.addValue((double) c);
    }/*  w  ww.jav a  2s .c o m*/
    return summaryStatistics.getMean() >= meanCoverage;
}

From source file:org.orbisgis.corejdbc.ReadTable.java

/**
 * Compute numeric stats of the specified table column using a limited input rows. Stats are not done in the sql side.
 * @param connection Available connection
 * @param tableName Table name//from  w  w w.  j av  a  2  s  . c  o m
 * @param columnName Column name
 * @param rowNum Row id
 * @param pm Progress monitor
 * @return An array of attributes {@link STATS}
 * @throws SQLException
 */
public static String[] computeStatsLocal(Connection connection, String tableName, String columnName,
        SortedSet<Integer> rowNum, ProgressMonitor pm) throws SQLException {
    String[] res = new String[STATS.values().length];
    SummaryStatistics stats = new SummaryStatistics();
    try (Statement st = connection.createStatement()) {
        // Cancel select
        PropertyChangeListener listener = EventHandler.create(PropertyChangeListener.class, st, "cancel");
        pm.addPropertyChangeListener(ProgressMonitor.PROP_CANCEL, listener);
        try (ResultSet rs = st.executeQuery(String.format("SELECT %s FROM %s", columnName, tableName))) {
            ProgressMonitor fetchProgress = pm.startTask(rowNum.size());
            while (rs.next() && !pm.isCancelled()) {
                if (rowNum.contains(rs.getRow())) {
                    stats.addValue(rs.getDouble(columnName));
                    fetchProgress.endTask();
                }
            }
        } finally {
            pm.removePropertyChangeListener(listener);
        }
    }
    res[STATS.SUM.ordinal()] = Double.toString(stats.getSum());
    res[STATS.AVG.ordinal()] = Double.toString(stats.getMean());
    res[STATS.COUNT.ordinal()] = Long.toString(stats.getN());
    res[STATS.MIN.ordinal()] = Double.toString(stats.getMin());
    res[STATS.MAX.ordinal()] = Double.toString(stats.getMax());
    res[STATS.STDDEV_SAMP.ordinal()] = Double.toString(stats.getStandardDeviation());
    return res;
}

From source file:org.orekit.forces.gravity.SolidTidesFieldTest.java

@Test
public void testInterpolationAccuracy() throws OrekitException {

    // The shortest periods are slightly below one half day for the tidal waves
    // considered here. This implies the sampling rate should be fast enough.
    // The tuning parameters we have finally settled correspond to a two hours
    // sample containing 12 points (i.e. one new point is computed every 10 minutes).
    // The observed relative interpolation error with these settings are essentially
    // due to Runge phenomenon at points sampling rate. Plotting the errors shows
    // singular peaks pointing out of merely numerical noise.
    final IERSConventions conventions = IERSConventions.IERS_2010;
    Frame itrf = FramesFactory.getITRF(conventions, true);
    TimeScale utc = TimeScalesFactory.getUTC();
    UT1Scale ut1 = TimeScalesFactory.getUT1(conventions, true);
    NormalizedSphericalHarmonicsProvider gravityField = GravityFieldFactory.getConstantNormalizedProvider(5, 5);

    SolidTidesField raw = new SolidTidesField(conventions.getLoveNumbers(),
            conventions.getTideFrequencyDependenceFunction(ut1), conventions.getPermanentTide(),
            conventions.getSolidPoleTide(ut1.getEOPHistory()), itrf, gravityField.getAe(), gravityField.getMu(),
            gravityField.getTideSystem(), CelestialBodyFactory.getSun(), CelestialBodyFactory.getMoon());
    int step = 600;
    int nbPoints = 12;
    CachedNormalizedSphericalHarmonicsProvider interpolated = new CachedNormalizedSphericalHarmonicsProvider(
            raw, step, nbPoints, OrekitConfiguration.getCacheSlotsNumber(), 7 * Constants.JULIAN_DAY,
            0.5 * Constants.JULIAN_DAY);

    // the following time range is located around the maximal observed error
    AbsoluteDate start = new AbsoluteDate(2003, 6, 12, utc);
    AbsoluteDate end = start.shiftedBy(3 * Constants.JULIAN_DAY);
    SummaryStatistics stat = new SummaryStatistics();
    for (AbsoluteDate date = start; date.compareTo(end) < 0; date = date.shiftedBy(60)) {
        NormalizedSphericalHarmonics rawHarmonics = raw.onDate(date);
        NormalizedSphericalHarmonics interpolatedHarmonics = interpolated.onDate(date);

        for (int n = 2; n < 5; ++n) {
            for (int m = 0; m <= n; ++m) {

                if (n < 4 || m < 3) {
                    double cnmRaw = rawHarmonics.getNormalizedCnm(n, m);
                    double cnmInterp = interpolatedHarmonics.getNormalizedCnm(n, m);
                    double errorC = (cnmInterp - cnmRaw) / FastMath.abs(cnmRaw);
                    stat.addValue(errorC);

                    if (m > 0) {
                        double snmRaw = rawHarmonics.getNormalizedSnm(n, m);
                        double snmInterp = interpolatedHarmonics.getNormalizedSnm(n, m);
                        double errorS = (snmInterp - snmRaw) / FastMath.abs(snmRaw);
                        stat.addValue(errorS);
                    }/*  w  w  w .j  a va2 s  .  c  o  m*/
                }
            }
        }
    }
    Assert.assertEquals(0.0, stat.getMean(), 2.0e-12);
    Assert.assertTrue(stat.getStandardDeviation() < 2.0e-9);
    Assert.assertTrue(stat.getMin() > -9.0e-8);
    Assert.assertTrue(stat.getMax() < 8.0e-8);

}