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:com.hurence.logisland.sampling.AverageSampler.java

/**
 * divide the points sequence into equally sized buckets
 * and select the first point of each bucket
 *
 * @param inputRecords the iput list//w w w  .  j a  va2s .  co  m
 * @return
 */
@Override
public List<Record> sample(List<Record> inputRecords) {

    SummaryStatistics stats = new SummaryStatistics();

    // simple average to 100 data points
    final int realBucketSize = SamplingUtils.fitBucketSize(inputRecords, bucketSize);
    return SamplingUtils.grouped(inputRecords, realBucketSize).map(bucket -> {

        bucket.forEach(record -> {
            final Double recordValue = getRecordValue(record);
            if (recordValue != null)
                stats.addValue(recordValue);
        });

        final double meanValue = stats.getMean();
        final Record sampleRecord = getTimeValueRecord(bucket.get(0));
        final FieldType fieldType = bucket.get(0).getField(valueFieldName).getType();
        switch (fieldType) {
        case INT:
            sampleRecord.setField(valueFieldName, fieldType, (int) Math.round(meanValue));
            break;
        case LONG:
            sampleRecord.setField(valueFieldName, fieldType, Math.round(meanValue));
            break;
        case FLOAT:
            sampleRecord.setField(valueFieldName, fieldType, (float) meanValue);
            break;
        case DOUBLE:
            sampleRecord.setField(valueFieldName, fieldType, meanValue);
            break;
        }
        return sampleRecord;
    }).collect(Collectors.toList());
}

From source file:net.lizalab.util.RdRandRandomTest.java

/**
 * Tests RdRandRandom by verifying the average of the distribution of digits 0-9
 * over 100 million values. Also runs the test for Random and SecureRandom for
 * reference./*from   w w  w. j a  v a2  s .c  om*/
 * Based on Mean Test outlined in <i>Beautiful Testing</i> published by O'Reilly.
 * @throws GeneralSecurityException 
 * @throws SeedException 
 */
@Test
public final void testRdRandRandomMean() throws GeneralSecurityException, SeedException {
    final String methodName = "testRdRandRandom : ";

    SummaryStatistics stats = new SummaryStatistics();
    // Initialize the array
    ndigits = new int[10];
    for (int i = 0; i < 10; i++) {
        ndigits[i] = 0;
        stats.addValue(i);
    }

    // Calculate the confidence intervals to assert.
    mean = stats.getMean();
    stdDev = stats.getStandardDeviation();
    var = stats.getVariance();
    LOGGER.info("{} Normal mean: {}", methodName, mean);
    LOGGER.info("{} Normal std: {}", methodName, stdDev);
    LOGGER.info("{} Normal var: {}", methodName, var);
    // 99.7% CI is within 3 std.
    double expectedDev3SD = 3 * stdDev / Math.sqrt(values);
    smLowerRng3SD = mean - expectedDev3SD;
    smUpperRng3SD = mean + expectedDev3SD;
    // 95% CI is within 2 std.
    double expectedDev2SD = 2 * stdDev / Math.sqrt(values);
    smLowerRng2SD = mean - expectedDev2SD;
    smUpperRng2SD = mean + expectedDev2SD;
    LOGGER.info("{} Generating {} values.", methodName, values);
    LOGGER.info("{} Sample mean expected in range {} - {} 99.7% of the times.", methodName, smLowerRng3SD,
            smUpperRng3SD);
    LOGGER.info("{} Sample mean expected in range {} - {} 95% of the times.", methodName, smLowerRng2SD,
            smUpperRng2SD);

    LOGGER.info("{} Running for Random..", methodName);
    Random random = new Random();
    meanTest(random, false);

    LOGGER.info("{} Running for RdRand..", methodName);
    random = new RdRandRandom();
    meanTest(random, true);

    LOGGER.info("{} Running for SecureRandom..", methodName);
    random = new SecureRandom();
    meanTest(random, false);

    LOGGER.info("{} Running Uncommons Maths AESCounterRNG using RdRandSeedGenerator..", methodName);
    random = new AESCounterRNG(new RdRandSeedGenerator());
    meanTest(random, false);
}

From source file:net.recommenders.rival.evaluation.statistics.ConfidenceInterval.java

/**
 * Method that takes two metrics as parameters. It will compute the
 * differences between both (only considering the keys in the overlap)
 *
 * @param <V> type of keys for metrics
 * @param alpha probability of incorrectly rejecting the null hypothesis (1
 * - confidence_level)//from   w  w w .  java  2 s  .  co m
 * @param baselineMetricPerDimension baseline metric, one value for each
 * dimension
 * @param testMetricPerDimension test metric, one value for each dimension
 * @param pairedSamples flag to indicate if the comparison should be made
 * for the distribution of difference scores (when true) or for the
 * distribution of differences between means
 * @return array with the confidence interval: [mean - margin of error, mean
 * + margin of error]
 */
public <V> double[] getConfidenceInterval(final double alpha, final Map<V, Double> baselineMetricPerDimension,
        final Map<V, Double> testMetricPerDimension, final boolean pairedSamples) {
    if (pairedSamples) {
        Set<V> overlap = new HashSet<V>(baselineMetricPerDimension.keySet());
        overlap.retainAll(testMetricPerDimension.keySet());

        // paired or matched samples --> analyse distribution of difference scores
        SummaryStatistics differences = new SummaryStatistics();
        for (V key : overlap) {
            double diff = Math.abs(testMetricPerDimension.get(key) - baselineMetricPerDimension.get(key));
            differences.addValue(diff);
        }
        return getConfidenceInterval(alpha / 2, (int) differences.getN() - 1, (int) differences.getN(),
                differences.getStandardDeviation(), differences.getMean());
    } else {
        // independent samples --> analyse distribution of differences between means
        SummaryStatistics statsBaseline = new SummaryStatistics();
        for (double d : baselineMetricPerDimension.values()) {
            statsBaseline.addValue(d);
        }
        SummaryStatistics statsTest = new SummaryStatistics();
        for (double d : testMetricPerDimension.values()) {
            statsTest.addValue(d);
        }
        long dfT = statsBaseline.getN() + statsTest.getN() - 2;
        double sDif = Math.sqrt((1.0 / statsBaseline.getN() + 1.0 / statsTest.getN())
                * (statsBaseline.getVariance() * (statsBaseline.getN() - 1)
                        + statsTest.getVariance() * (statsTest.getN() - 1)));
        double mDif = Math.abs(statsTest.getMean() - statsBaseline.getMean());
        return getConfidenceInterval(alpha, (int) dfT, (int) dfT, sDif, mDif);
    }
}

From source file:ijfx.ui.explorer.DefaultFolderManagerService.java

private Integer fetchMoreStatistics(ProgressHandler progress, List<Explorable> explorableList) {
    if (progress == null)
        progress = new SilentProgressHandler();
    progress.setStatus("Completing statistics of the objects");
    Integer elementAnalyzedCount = 0;
    int elements = explorableList.size();
    int i = 0;/*from   w  w  w. ja v a2s .c om*/
    for (Explorable e : explorableList) {
        if (!e.getMetaDataSet().containsKey(MetaData.STATS_PIXEL_MIN)) {
            progress.setProgress(i, elements);
            if (e instanceof ImageRecordIconizer) {
                ImageRecordIconizer iconizer = (ImageRecordIconizer) e;
                SummaryStatistics stats = statsService.getStatistics(iconizer.getImageRecord().getFile());
                iconizer.getMetaDataSet().putGeneric(MetaData.STATS_PIXEL_MIN, stats.getMin());
                iconizer.getMetaDataSet().putGeneric(MetaData.STATS_PIXEL_MAX, stats.getMax());
                iconizer.getMetaDataSet().putGeneric(MetaData.STATS_PIXEL_MEAN, stats.getMean());
                iconizer.getMetaDataSet().putGeneric(MetaData.STATS_PIXEL_STD_DEV, stats.getMean());
                elementAnalyzedCount++;
            }
        }
        i++;
    }
    return elementAnalyzedCount;
}

From source file:model.scenario.OneLinkSupplyChainResult.java

public static OneLinkSupplyChainResult beefMonopolistFixedProductionsOneRun(long seed,
        float divideMonopolistGainsByThis, int monopolistSpeed, final boolean foodLearned,
        File csvFileToWrite) {/* www  . j  av a  2 s.  c o m*/
    final MacroII macroII = new MacroII(seed);
    final OneLinkSupplyChainScenarioCheatingBuyPriceAndForcedMonopolist scenario1 = new OneLinkSupplyChainScenarioCheatingBuyPriceAndForcedMonopolist(
            macroII, OneLinkSupplyChainScenario.INPUT_GOOD) {
        @Override
        public void buildFoodPurchasesPredictor(PurchasesDepartment department) {
            if (foodLearned)
                department.setPredictor(new FixedIncreasePurchasesPredictor(0));

        }

        @Override
        protected SalesDepartment createSalesDepartment(Firm firm, Market goodmarket) {
            SalesDepartment department = super.createSalesDepartment(firm, goodmarket); //To change body of overridden methods use File | Settings | File Templates.
            if (foodLearned && goodmarket.getGoodType().equals(OneLinkSupplyChainScenario.OUTPUT_GOOD))
                department.setPredictorStrategy(new FixedDecreaseSalesPredictor(0));
            return department;
        }

        @Override
        protected HumanResources createPlant(Blueprint blueprint, Firm firm, Market laborMarket) {
            HumanResources hr = super.createPlant(blueprint, firm, laborMarket); //To change body of overridden methods use File | Settings | File Templates.
            if (foodLearned && !blueprint.getOutputs().containsKey(OneLinkSupplyChainScenario.INPUT_GOOD))
                hr.setPredictor(new FixedIncreasePurchasesPredictor(0));
            return hr;
        }

    };
    scenario1.setControlType(MarginalMaximizer.class);
    scenario1.setSalesDepartmentType(SalesDepartmentOneAtATime.class);
    //use standard PID parameters
    scenario1.setDivideProportionalGainByThis(divideMonopolistGainsByThis);
    scenario1.setDivideIntegrativeGainByThis(divideMonopolistGainsByThis);
    //100 days delay
    scenario1.setBeefPricingSpeed(monopolistSpeed);
    //no need for filter with the cheating price
    scenario1.setBeefPriceFilterer(null);
    scenario1.setBeefTargetInventory(100);
    scenario1.setFoodTargetInventory(100);
    //add csv writer if needed
    if (csvFileToWrite != null)
        DailyStatCollector.addDailyStatCollectorToModel(csvFileToWrite, macroII);

    macroII.setScenario(scenario1);
    macroII.start();

    SummaryStatistics averageFoodPrice = new SummaryStatistics();
    SummaryStatistics averageBeefPrice = new SummaryStatistics();
    SummaryStatistics averageBeefTraded = new SummaryStatistics();
    while (macroII.schedule.getTime() < 15000) {
        macroII.schedule.step(macroII);

        if (macroII.schedule.getTime() >= 14500) {
            averageFoodPrice.addValue(macroII.getMarket(OneLinkSupplyChainScenario.OUTPUT_GOOD)
                    .getLatestObservation(MarketDataType.AVERAGE_CLOSING_PRICE));
            averageBeefPrice.addValue(macroII.getMarket(OneLinkSupplyChainScenario.INPUT_GOOD)
                    .getLatestObservation(MarketDataType.AVERAGE_CLOSING_PRICE));
            averageBeefTraded
                    .addValue(macroII.getMarket(OneLinkSupplyChainScenario.INPUT_GOOD).getYesterdayVolume());

        }
    }

    macroII.finish();

    System.out.println("done with price: " + averageBeefPrice.getMean() + ", and standard deviation : "
            + averageBeefPrice.getStandardDeviation());
    System.out.println("seed: " + macroII.seed());
    System.out.println();
    //the beef price is in the ballpark

    return new OneLinkSupplyChainResult(averageBeefPrice.getMean(), averageFoodPrice.getMean(),
            averageBeefTraded.getMean(), macroII);
}

From source file:edu.washington.gs.skyline.model.quantification.RegressionFit.java

public Double computeRSquared(CalibrationCurve curve, List<WeightedObservedPoint> points) {
    SummaryStatistics yValues = new SummaryStatistics();
    SummaryStatistics residuals = new SummaryStatistics();
    for (WeightedObservedPoint point : points) {
        Double yFitted = curve.getY(point.getX());
        if (yFitted == null) {
            continue;
        }//w w w . jav  a2s .  co  m
        yValues.addValue(point.getY());
        residuals.addValue(point.getY() - yFitted);
    }
    if (0 == residuals.getN()) {
        return null;
    }
    double yMean = yValues.getMean();
    double totalSumOfSquares = points.stream().mapToDouble(p -> (p.getY() - yMean) * (p.getY() - yMean)).sum();
    double sumOfSquaresOfResiduals = residuals.getSumsq();
    double rSquared = 1 - sumOfSquaresOfResiduals / totalSumOfSquares;
    return rSquared;
}

From source file:model.experiments.stickyprices.StickyPricesCSVPrinter.java

private static void woodMonopolistSupplyChainSweep() throws IOException {

    CSVWriter writer = new CSVWriter(
            new FileWriter(Paths.get("runs", "rawdata", "woodMonopolistStickinessesSweep.csv").toFile()));
    final String[] header = { "decisionSpeed", "stickiness", "distance", "finaldistance" };
    System.out.println(Arrays.toString(header));
    writer.writeNext(header);//ww w. j ava  2  s . c o m

    for (int speed = 1; speed < 30; speed++) {
        for (int stickiness = 0; stickiness < 50; stickiness++) {
            SummaryStatistics distance = new SummaryStatistics();
            SummaryStatistics finalDistance = new SummaryStatistics();
            for (int seed = 0; seed < 5; seed++) {

                final double[] result = beefMonopolistOneRun(seed, 1, stickiness, true, true, speed, null);
                distance.addValue(result[0]);
                finalDistance.addValue(result[1]);

            }

            final String[] nextLine = { String.valueOf(speed), String.valueOf(stickiness),
                    String.valueOf(distance.getMean()), String.valueOf(finalDistance.getMean()) };
            System.out.println(Arrays.toString(nextLine));
            writer.writeNext(nextLine);
            writer.flush();
        }
    }

    writer.close();

}

From source file:model.experiments.stickyprices.StickyPricesCSVPrinter.java

private static void oneHundredLearningCompetitive(File file) throws IOException {

    CSVWriter writer = new CSVWriter(new FileWriter(file));
    writer.writeNext(new String[] { "production", "price" });

    //this will take a looong time

    //run the test 5 times!
    for (long i = 0; i < 100; i++) {

        //create the run
        MacroII macroII = new MacroII(i);
        TripolistScenario scenario = new TripolistScenario(macroII);
        macroII.setScenario(scenario);/*from  w ww .ja va 2s .  com*/
        scenario.setAdditionalCompetitors(4);
        //set the demand
        scenario.setAskPricingStrategy(InventoryBufferSalesControl.class);
        scenario.setControlType(
                MonopolistScenario.MonopolistScenarioIntegratedControlEnum.MARGINAL_PLANT_CONTROL);

        //start it and have one step
        macroII.start();
        macroII.schedule.step(macroII);

        //now set the right parameters
        for (Firm firm : scenario.getCompetitors()) {
            final SalesDepartment salesDepartment = firm.getSalesDepartment(UndifferentiatedGoodType.GENERIC);
            //learning
            assert salesDepartment.getPredictorStrategy() instanceof RecursiveSalePredictor;
        }

        //run the model
        for (int j = 0; j < 5000; j++) {
            macroII.schedule.step(macroII);
            MarginalMaximizerPIDTuning.printProgressBar(5000, j, 100);
        }

        //average over the last 500 steps
        SummaryStatistics prices = new SummaryStatistics();
        SummaryStatistics quantities = new SummaryStatistics();
        for (int j = 0; j < 500; j++) {
            macroII.schedule.step(macroII);
            assert !Float.isNaN(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayAveragePrice());
            prices.addValue(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayAveragePrice());
            quantities.addValue(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayVolume());

        }

        String[] resultString = new String[2];
        resultString[0] = String.valueOf(quantities.getMean());
        resultString[1] = String.valueOf(prices.getMean());
        System.out.println(Arrays.toString(resultString));
        writer.writeNext(resultString);
        writer.flush();
    }

    writer.close();

}

From source file:com.milaboratory.core.alignment.KMapper.java

@Override
public String toString() {
    SummaryStatistics ss = getRecordSizeSummaryStatistics();
    return "K=" + kValue + "; Avr=" + ss.getMean() + "; SD=" + ss.getStandardDeviation();
}

From source file:model.experiments.stickyprices.StickyPricesCSVPrinter.java

private static void competitiveSweepRun(int additionalCompetitors) throws IOException {

    CSVWriter writer = new CSVWriter(new FileWriter(
            Paths.get("runs", "rawdata", "competitivePeriodSweep" + additionalCompetitors + ".csv").toFile()));
    writer.writeNext(new String[] { "speed", "distance", "finaldistance", "variance" });

    for (int speed = 1; speed < 30; speed++) {
        SummaryStatistics distance = new SummaryStatistics();
        SummaryStatistics finalDistance = new SummaryStatistics();
        SummaryStatistics variance = new SummaryStatistics();
        for (int seed = 0; seed < 50; seed++) {

            final double[] result = competitiveSweepRun(seed, 101, 1, 1, 14, .1f, .1f, speed, 58, null,
                    additionalCompetitors);
            distance.addValue(result[0]);
            finalDistance.addValue(result[1]);
            variance.addValue(result[2]);

        }//from  w w  w  . j a va 2 s.com

        final String[] nextLine = { String.valueOf(speed), String.valueOf(distance.getMean()),
                String.valueOf(finalDistance.getMean()), String.valueOf(variance.getMean()) };
        System.out.println(Arrays.toString(nextLine));
        writer.writeNext(nextLine);
        writer.flush();
    }

    writer.close();

}