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

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

Introduction

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

Prototype

public long getN() 

Source Link

Document

Returns the number of available values

Usage

From source file:org.asoem.test.Statistics.java

/**
 * Assert that the mean of the measurements of the {@code treatment} are significantly less high than those of the
 * {@code control}.//from w  ww .j  av a2s  .  co m
 *
 * @param control   the measurements of the control
 * @param treatment the measurements of the treatment
 * @param alpha     the required significance level (like {@link org.asoem.greyfish.utils.math.SignificanceLevel#getAlpha()})
 */
public static void assertSignificantDecrease(final DescriptiveStatistics control,
        final DescriptiveStatistics treatment, final double alpha) {
    // Is it faster?
    logger.info("Treatment vs. Control: {}, {}", treatment, control);

    assertThat("The mean of the treatment " + "is not less than the mean of the control", treatment.getMean(),
            is(lessThan(control.getMean())));

    // Is it also significantly faster? Make a t-test.
    // Test assumptions for t-test: normality
    assertThat("The treatment is not normal distributed",
            StatisticalTests.shapiroWilk(treatment.getValues()).p(), is(lessThan(alpha)));
    assertThat("The control is not normal distributed", StatisticalTests.shapiroWilk(control.getValues()).p(),
            is(lessThan(alpha)));

    // Perform the t-test
    final double t = new TTest().t(treatment, control);
    final double p = new TTest().tTest(treatment, control);
    logger.info("t-test: t={}, p={}", t, p);
    double qt = new TDistribution(treatment.getN() - 1 + control.getN() - 1)
            .inverseCumulativeProbability(1 - alpha / 2);
    assertThat("The means are not significantly different", Math.abs(t), is(greaterThan(qt)));
}

From source file:org.bml.util.rt.telemetry.SecondBasedCounterTest.java

/**
 * Test of getLastMinutesTelemetry method, of class SecondBasedCounter.
 */// ww  w . java 2 s .  c om
public void testGetLastMinutesTelemetry() {
    System.out.println("getLastMinutesTelemetry");
    DescriptiveStatistics stats = counter.getLastMinutesTelemetry();

    long max = stats.getN();

    AtomicInteger[] counterArray = counter.getCounterArray();

    for (int index = 0; index < max; index++) {
        System.out.println(stats.getElement(index) + " -- " + counterArray[index].get());

    }

}

From source file:org.hawkular.client.test.metrics.openshift.CollectionRateDetailTest.java

private void getData(String metricID, String testID, long start, long end, Duration timeBucket) {
    Reporter.log("Fetching large data set... may take a couple minutes", true);
    List<DataPoint<Double>> rawData = client().metrics().gauge()
            .findGaugeDataWithId(metricID, String.valueOf(start), String.valueOf(end), null, null, null)
            .getEntity();/*from   w  ww  .j av  a 2s  . c om*/

    Assert.assertNotNull(rawData, testID);
    Reporter.log("raw datapoints: " + rawData.size(), true);

    List<Long> zeroList = findZeroValues(rawData);

    Assert.assertTrue(zeroList == null || zeroList.size() == 0, testID);

    Map<Long, Integer> hist = OpenshiftBaseTest.makeHistogram(rawData, timeBucket);

    Double[] result = hist.entrySet().stream().map(x -> new Double(x.getValue()))
            .toArray(size -> new Double[size]);

    double[] d = ArrayUtils.toPrimitive(result);

    // drop the first and last as they are usually outliers
    double[] samples = Arrays.copyOfRange(d, 1, d.length - 1);
    DescriptiveStatistics stats = new DescriptiveStatistics(samples);

    Reporter.log(hist.toString(), true);
    Reporter.log("size: " + stats.getN(), true);
    Reporter.log("min/max: " + stats.getMin() + "/" + stats.getMax(), true);
    Reporter.log("mean: " + stats.getMean(), true);
    Reporter.log("variance: " + stats.getVariance(), true);
    Reporter.log("stddev: " + stats.getStandardDeviation(), true);
}

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

/**
 * returns a statistic value//from w  w  w  .  ja va  2  s. c om
 *
 * @param p_statistic statistic object
 * @return statistic value
 */
public final double value(final DescriptiveStatistics 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 STANDARDDEVIATION:
        return p_statistic.getStandardDeviation();

    case SUM:
        return p_statistic.getSum();

    case SUMSQUARE:
        return p_statistic.getSumsq();

    case VARIANCE:
        return p_statistic.getVariance();

    case MEAN:
        return p_statistic.getMean();

    case KURTIOSIS:
        return p_statistic.getKurtosis();

    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// w w  w  .j a va2s  .c o m
 *
 * @param p_statistic statistic object
 * @return statistic value
 */
public final double value(@Nonnull final DescriptiveStatistics 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 STANDARDDEVIATION:
        return p_statistic.getStandardDeviation();

    case SUM:
        return p_statistic.getSum();

    case SUMSQUARE:
        return p_statistic.getSumsq();

    case VARIANCE:
        return p_statistic.getVariance();

    case MEAN:
        return p_statistic.getMean();

    case KURTIOSIS:
        return p_statistic.getKurtosis();

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

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

public static void analyseWaitTimes(String fileName, List<DynModeTrip> trips, int binsize_s) {
    Collections.sort(trips);/*from w  w w  . j ava2 s . co  m*/
    if (trips.size() == 0)
        return;
    int startTime = ((int) (trips.get(0).getDepartureTime() / binsize_s)) * binsize_s;
    int endTime = ((int) (trips.get(trips.size() - 1).getDepartureTime() / binsize_s) + binsize_s) * binsize_s;
    Map<Double, List<DynModeTrip>> splitTrips = splitTripsIntoBins(trips, startTime, endTime, binsize_s);

    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);
    format.setGroupingUsed(false);

    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");

    BufferedWriter bw = IOUtils.getBufferedWriter(fileName + ".csv");
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    TimeSeriesCollection datasetrequ = new TimeSeriesCollection();
    TimeSeries averageWaitC = new TimeSeries("average");
    TimeSeries medianWait = new TimeSeries("median");
    TimeSeries p_5Wait = new TimeSeries("5th percentile");
    TimeSeries p_95Wait = new TimeSeries("95th percentile");
    TimeSeries requests = new TimeSeries("Ride requests");

    try {
        bw.write("timebin;trips;average_wait;min;p_5;p_25;median;p_75;p_95;max");
        for (Entry<Double, List<DynModeTrip>> e : splitTrips.entrySet()) {
            long rides = 0;
            double averageWait = 0;
            double min = 0;
            double p_5 = 0;
            double p_25 = 0;
            double median = 0;
            double p_75 = 0;
            double p_95 = 0;
            double max = 0;
            if (!e.getValue().isEmpty()) {
                DescriptiveStatistics stats = new DescriptiveStatistics();
                for (DynModeTrip t : e.getValue()) {
                    stats.addValue(t.getWaitTime());
                }
                rides = stats.getN();
                averageWait = stats.getMean();
                min = stats.getMin();
                p_5 = stats.getPercentile(5);
                p_25 = stats.getPercentile(25);
                median = stats.getPercentile(50);
                p_75 = stats.getPercentile(75);
                p_95 = stats.getPercentile(95);
                max = stats.getMax();

            }
            Minute h = new Minute(sdf2.parse(Time.writeTime(e.getKey())));

            medianWait.addOrUpdate(h, Double.valueOf(median));
            averageWaitC.addOrUpdate(h, Double.valueOf(averageWait));
            p_5Wait.addOrUpdate(h, Double.valueOf(p_5));
            p_95Wait.addOrUpdate(h, Double.valueOf(p_95));
            requests.addOrUpdate(h, rides * 3600. / binsize_s);// normalised [req/h]
            bw.newLine();
            bw.write(Time.writeTime(e.getKey()) + ";" + rides + ";" + format.format(averageWait) + ";"
                    + format.format(min) + ";" + format.format(p_5) + ";" + format.format(p_25) + ";"
                    + format.format(median) + ";" + format.format(p_75) + ";" + format.format(p_95) + ";"
                    + format.format(max));

        }
        bw.flush();
        bw.close();
        dataset.addSeries(averageWaitC);
        dataset.addSeries(medianWait);
        dataset.addSeries(p_5Wait);
        dataset.addSeries(p_95Wait);
        datasetrequ.addSeries(requests);
        JFreeChart chart = chartProfile(splitTrips.size(), dataset, "Waiting times", "Wait time (s)");
        JFreeChart chart2 = chartProfile(splitTrips.size(), datasetrequ, "Ride requests per hour",
                "Requests per hour (req/h)");
        ChartSaveUtils.saveAsPNG(chart, fileName, 1500, 1000);
        ChartSaveUtils.saveAsPNG(chart2, fileName + "_requests", 1500, 1000);

    } catch (IOException | ParseException e) {

        e.printStackTrace();
    }

}

From source file:org.obiba.magma.math.summary.ContinuousVariableSummaryTest.java

@Test
public void test_compute_integerType() {
    Variable mockVariable = Variable.Builder.newVariable("mock", IntegerType.get(), "mock").build();
    ContinuousVariableSummary summary = computeFromTable(mockVariable,
            Values.asValues(IntegerType.get(), 1, 2, 3));
    DescriptiveStatistics descriptiveStats = summary.getDescriptiveStats();
    assertThat(descriptiveStats.getMin()).isEqualTo(1.0);
    assertThat(descriptiveStats.getMax()).isEqualTo(3.0);
    assertThat(descriptiveStats.getMean()).isEqualTo(2.0);
    assertThat(descriptiveStats.getN()).isEqualTo(3l);
}

From source file:org.obiba.magma.math.summary.ContinuousVariableSummaryTest.java

@Test
public void test_compute_integerTypeWithNull() {
    Variable mockVariable = Variable.Builder.newVariable("mock", IntegerType.get(), "mock").build();
    ContinuousVariableSummary summary = computeFromTable(mockVariable,
            Values.asValues(IntegerType.get(), 1, 2, 3, null, null));
    DescriptiveStatistics descriptiveStats = summary.getDescriptiveStats();
    assertThat(descriptiveStats.getMin()).isEqualTo(1.0);
    assertThat(descriptiveStats.getMax()).isEqualTo(3.0);
    assertThat(descriptiveStats.getMean()).isEqualTo(2.0);
    assertThat(descriptiveStats.getN()).isEqualTo(3l);
}

From source file:org.obiba.magma.math.summary.ContinuousVariableSummaryTest.java

@Test
public void test_compute_integerTypeMissingCategories() {
    Variable mockVariable = Variable.Builder.newVariable("mock", IntegerType.get(), "mock")
            .addCategory("888", "", true).addCategory("999", "", true).build();
    ContinuousVariableSummary summary = computeFromTable(mockVariable,
            Values.asValues(IntegerType.get(), 1, 2, 3, 888, 999));
    DescriptiveStatistics descriptiveStats = summary.getDescriptiveStats();
    assertThat(descriptiveStats.getMin()).isEqualTo(1.0);
    assertThat(descriptiveStats.getMax()).isEqualTo(3.0);
    assertThat(descriptiveStats.getMean()).isEqualTo(2.0);
    assertThat(descriptiveStats.getN()).isEqualTo(3l);
}

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());
    }/* w w  w .  ja va  2  s . com*/
    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);
}