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

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

Introduction

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

Prototype

public double getMean() 

Source Link

Document

Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm"> arithmetic mean </a> of the available values

Usage

From source file:com.github.jessemull.microflexbiginteger.stat.MeanWeightsTest.java

/**
 * Tests the aggregated plate statistics method using the values between the indices of
 * the collection./*from   www . j  a  va2s  .  co m*/
 */
@Test
public void testAggregatedPlateCollectionIndices() {

    int begin = random.nextInt(arrayIndices[0].first().size() - 4);
    int end = begin + random.nextInt(3) + 3;

    List<Plate> collection = Arrays.asList(arrayIndices);
    Map<Plate, BigDecimal> aggregatedReturnedMap = mean.platesAggregated(collection, weightsIndices, begin,
            end - begin, mc);

    Map<Plate, BigDecimal> aggregatedResultMap = new TreeMap<Plate, BigDecimal>();

    for (Plate plate : collection) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (Well well : plate) {

            List<BigDecimal> input = well.toBigDecimal().subList(begin, end);

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weightsIndices[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);
        aggregatedResultMap.put(plate, aggregatedResult);
    }

    for (Plate plate : collection) {

        BigDecimal result = aggregatedResultMap.get(plate);
        BigDecimal returned = aggregatedReturnedMap.get(plate);
        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflexbiginteger.stat.MeanWeightsTest.java

/**
 * Tests set calculation using indices.// w  w w .j av  a  2s  .c  om
 */
@Test
public void testSetIndices() {

    for (Plate plate : arrayIndices) {

        int begin = random.nextInt(plate.first().size() - 4);
        int end = begin + random.nextInt(3) + 3;

        Map<Well, BigDecimal> resultMap = new TreeMap<Well, BigDecimal>();
        Map<Well, BigDecimal> returnedMap = mean.set(plate.dataSet(),
                ArrayUtils.subarray(weightsIndices, begin, end), begin, end - begin, mc);

        for (Well well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigInteger bi : well) {
                input[index] = bi.doubleValue() * weightsIndices[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(ArrayUtils.subarray(input, begin, end));
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (Well well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }
}

From source file:com.github.jessemull.microflex.stat.statbigdecimal.MeanBigDecimalWeightsTest.java

/**
 * Tests the aggregated plate statistics method using the values between the indices of
 * the array.//  www  .j ava 2s .co  m
 */
@Test
public void testAggregatedPlateArrayIndices() {

    int begin = random.nextInt(arrayIndices[0].first().size() - 4);
    int end = begin + random.nextInt(3) + 3;

    Map<PlateBigDecimal, BigDecimal> aggregatedReturnedMap = mean.platesAggregated(arrayIndices, weightsIndices,
            begin, end - begin, mc);
    Map<PlateBigDecimal, BigDecimal> aggregatedResultMap = new TreeMap<PlateBigDecimal, BigDecimal>();

    for (PlateBigDecimal plate : arrayIndices) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (WellBigDecimal well : plate) {

            List<BigDecimal> input = well.data().subList(begin, end);

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weightsIndices[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);
        aggregatedResultMap.put(plate, aggregatedResult);

    }

    for (PlateBigDecimal plate : arrayIndices) {

        BigDecimal result = aggregatedResultMap.get(plate);
        BigDecimal returned = aggregatedReturnedMap.get(plate);
        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflex.stat.statbiginteger.MeanBigIntegerWeightsTest.java

/**
 * Tests the aggregated plate statistics method using a collection.
 *//*from w  w  w  . jav  a2s  .  c o  m*/
@Test
public void testAggregatedSetCollection() {

    List<WellSetBigInteger> collection = new ArrayList<WellSetBigInteger>();

    for (PlateBigInteger plate : array) {
        collection.add(plate.dataSet());
    }

    Map<WellSetBigInteger, BigDecimal> aggregatedReturnedMap = mean.setsAggregated(collection, weights, mc);
    Map<WellSetBigInteger, BigDecimal> aggregatedResultMap = new TreeMap<WellSetBigInteger, BigDecimal>();

    for (WellSetBigInteger set : collection) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (WellBigInteger well : set) {

            List<BigDecimal> input = well.toBigDecimal();

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weights[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);
        aggregatedResultMap.put(set, aggregatedResult);
    }

    for (WellSetBigInteger set : collection) {

        BigDecimal result = aggregatedResultMap.get(set);
        BigDecimal returned = aggregatedReturnedMap.get(set);

        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflexbigdecimal.stat.MeanWeightsTest.java

/**
 * Tests the aggregated plate statistics method using the values between the indices of
 * the array./*from  w  w  w.  j a va2 s .  c om*/
 */
@Test
public void testAggregatedPlateArrayIndices() {

    int begin = random.nextInt(arrayIndices[0].first().size() - 4);
    int end = begin + random.nextInt(3) + 3;

    Map<Plate, BigDecimal> aggregatedReturnedMap = mean.platesAggregated(arrayIndices, weightsIndices, begin,
            end - begin, mc);
    Map<Plate, BigDecimal> aggregatedResultMap = new TreeMap<Plate, BigDecimal>();

    for (Plate plate : arrayIndices) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (Well well : plate) {

            List<BigDecimal> input = well.data().subList(begin, end);

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weightsIndices[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);
        aggregatedResultMap.put(plate, aggregatedResult);

    }

    for (Plate plate : arrayIndices) {

        BigDecimal result = aggregatedResultMap.get(plate);
        BigDecimal returned = aggregatedReturnedMap.get(plate);
        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflexbiginteger.stat.MeanWeightsTest.java

/**
 * Tests the aggregated plate statistics method using a collection.
 *//* ww w  .  j  a v  a  2 s .  c  o  m*/
@Test
public void testAggregatedSetCollection() {

    List<WellSet> collection = new ArrayList<WellSet>();

    for (Plate plate : array) {
        collection.add(plate.dataSet());
    }

    Map<WellSet, BigDecimal> aggregatedReturnedMap = mean.setsAggregated(collection, weights, mc);
    Map<WellSet, BigDecimal> aggregatedResultMap = new TreeMap<WellSet, BigDecimal>();

    for (WellSet set : collection) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (Well well : set) {

            List<BigDecimal> input = well.toBigDecimal();

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weights[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);
        aggregatedResultMap.put(set, aggregatedResult);
    }

    for (WellSet set : collection) {

        BigDecimal result = aggregatedResultMap.get(set);
        BigDecimal returned = aggregatedReturnedMap.get(set);

        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:fr.inria.eventcloud.benchmarks.load_balancing.LoadBalancingBenchmark.java

public StatsRecorder execute() {
    this.logParameterValues();

    // creates and runs micro benchmark
    MicroBenchmark microBenchmark = new MicroBenchmark(this.nbRuns, new MicroBenchmarkService() {

        private EventCloudsRegistry registry;

        private BenchmarkStatsCollector collector;

        private String collectorURL;

        private NodeProvider nodeProvider;

        private EventCloudDeployer deployer;

        private CustomPublishProxy publishProxies;

        private Event[] events;

        @Override//w w w  .  j  a  v  a 2s  .com
        public void setup() throws Exception {
            LOG.info("Loading events from {}", LoadBalancingBenchmark.this.inputFile);
            this.events = LoadBalancingBenchmark.this.loadEvents(LoadBalancingBenchmark.this.inputFile);

            LOG.info("{} compound events loaded", this.events.length);

            this.collector = PAActiveObject.newActive(BenchmarkStatsCollector.class,
                    new Object[] { LoadBalancingBenchmark.this.nbPeers - 1 });
            this.collectorURL = PAActiveObject.registerByName(this.collector, BENCHMARK_STATS_COLLECTOR_NAME);

            this.nodeProvider = LoadBalancingBenchmark.this.createNodeProvider();

            LoadBalancingBenchmark.this.componentsManager = EventCloudComponentsManagerFactory
                    .newComponentsManager(this.nodeProvider, 1, LoadBalancingBenchmark.this.nbPeers, 1, 1, 0);

            LoadBalancingBenchmark.this.componentsManager.start();

            EventCloudDeploymentDescriptor descriptor = this.createDeploymentDescriptor(this.nodeProvider,
                    this.collectorURL);

            this.deployer = new EventCloudDeployer(new EventCloudDescription(), descriptor,
                    LoadBalancingBenchmark.this.componentsManager);
            this.deployer.deploy(1, 1);

            this.registry = LoadBalancingBenchmark.this.deployRegistry(this.deployer, this.nodeProvider);

            String registryURL = null;
            try {
                registryURL = this.registry.register("registry");
            } catch (ProActiveException e) {
                throw new IllegalStateException(e);
            }

            EventCloudId eventCloudId = this.deployer.getEventCloudDescription().getId();

            this.publishProxies = LoadBalancingBenchmark.this.createPublishProxies(this.nodeProvider,
                    registryURL, eventCloudId);
        }

        private EventCloudDeploymentDescriptor createDeploymentDescriptor(NodeProvider nodeProvider,
                String benchmarkStatsCollectorURL) {
            Criterion[] criteria = new Criterion[1];
            criteria[0] = new QuadrupleCountCriterion();

            LoadBalancingConfiguration configuration = null;

            if (LoadBalancingBenchmark.this.strategy != null) {
                configuration = new LoadBalancingConfiguration(criteria,
                        LoadBalancingBenchmark.this.componentsManager, LoadBalancingBenchmark.this.strategy);
            }

            EventCloudDeploymentDescriptor descriptor = new EventCloudDeploymentDescriptor(
                    new CustomSemanticOverlayProvider(configuration, this.collectorURL,
                            LoadBalancingBenchmark.this.nbQuadruplesPublished,
                            LoadBalancingBenchmark.this.inMemoryDatastore));

            return descriptor;
        }

        @Override
        public void run(StatsRecorder recorder) throws TimeoutException {
            LOG.info("Assigning events");
            this.publishProxies.assignEvents(this.events);

            LOG.info("Publishing events to trigger load balancing");
            this.publishProxies.publish();

            this.collector.waitCondition(3600000);

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            LOG.info("Distribution on peers is:");

            Map<OverlayId, Integer> results = new HashMap<OverlayId, Integer>();

            for (Peer peer : this.collector.getPeers()) {
                @SuppressWarnings("unchecked")
                GenericResponseOperation<Integer> result = (GenericResponseOperation<Integer>) PAFuture
                        .getFutureValue(peer.receive(new CountQuadruplesOperation(false)));

                results.put(peer.getId(), result.getValue());
            }

            // Proxy proxy =
            // org.objectweb.proactive.extensions.p2p.structured.factories.ProxyFactory.newProxy(this.deployer.getTrackers());
            // CountQuadrupleResponse response =
            // (CountQuadrupleResponse)
            // PAFuture.getFutureValue(proxy.send(new
            // CountQuadrupleRequest()));
            //
            // Map<OverlayId, Long> results = response.getResult();

            DescriptiveStatistics stats = new DescriptiveStatistics();

            int count = 0;
            for (Entry<OverlayId, Integer> entry : results.entrySet()) {
                LOG.info("{}  {}", entry.getKey(), entry.getValue());
                count += entry.getValue();
                stats.addValue(entry.getValue());
            }

            LOG.info(
                    "{} peers manage a total of {} quadruples, standard deviation is {}, variability (stddev/average * 100) is {}%",
                    results.size(), count, stats.getStandardDeviation(),
                    (stats.getStandardDeviation() / stats.getMean()) * 100);

            System.exit(1);
        }

        @Override
        public void clear() throws Exception {
            LOG.info("Clearing previously recorded information before to start benchmark");

            List<ResponseOperation> futures = new ArrayList<ResponseOperation>();

            for (Peer p : this.deployer.getRandomTracker().getPeers()) {
                futures.add(p.receive(new ClearOperation()));
            }

            PAFuture.waitForAll(futures);

            this.collector.clear();
        }

        @Override
        public void teardown() throws Exception {
            LoadBalancingBenchmark.this.undeploy(this.nodeProvider, this.deployer, this.registry,
                    this.collectorURL);
        }

    });
    microBenchmark.discardFirstRuns(this.discardFirstRuns);
    microBenchmark.showProgress();
    microBenchmark.execute();

    return microBenchmark.getStatsRecorder();
}

From source file:com.github.jessemull.microflex.stat.statbigdecimal.MeanBigDecimalWeightsTest.java

/**
 * Tests the plate statistics method using the values between the indices.
 *///www . j a  va2 s . c o  m
@Test
public void testPlateIndices() {

    for (PlateBigDecimal plate : arrayIndices) {

        int begin = random.nextInt(plate.first().size() - 4);
        int end = begin + random.nextInt(3) + 3;

        Map<WellBigDecimal, BigDecimal> resultMap = new TreeMap<WellBigDecimal, BigDecimal>();
        Map<WellBigDecimal, BigDecimal> returnedMap = mean.plate(plate,
                ArrayUtils.subarray(weightsIndices, begin, end), begin, end - begin, mc);

        for (WellBigDecimal well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigDecimal bd : well) {
                input[index] = bd.doubleValue() * weightsIndices[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(ArrayUtils.subarray(input, begin, end));
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (WellBigDecimal well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }
}

From source file:com.github.jessemull.microflex.stat.statbigdecimal.MeanBigDecimalWeightsTest.java

/**
 * Tests the aggregated plate statistics method using the values between the indices of
 * the collection./*w  w w.j a v  a2  s .co m*/
 */
@Test
public void testAggregatedPlateCollectionIndices() {

    int begin = random.nextInt(arrayIndices[0].first().size() - 4);
    int end = begin + random.nextInt(3) + 3;

    List<PlateBigDecimal> collection = Arrays.asList(arrayIndices);
    Map<PlateBigDecimal, BigDecimal> aggregatedReturnedMap = mean.platesAggregated(collection, weightsIndices,
            begin, end - begin, mc);

    Map<PlateBigDecimal, BigDecimal> aggregatedResultMap = new TreeMap<PlateBigDecimal, BigDecimal>();

    for (PlateBigDecimal plate : collection) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (WellBigDecimal well : plate) {

            List<BigDecimal> input = well.data().subList(begin, end);

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weightsIndices[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);
        aggregatedResultMap.put(plate, aggregatedResult);
    }

    for (PlateBigDecimal plate : collection) {

        BigDecimal result = aggregatedResultMap.get(plate);
        BigDecimal returned = aggregatedReturnedMap.get(plate);
        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflex.stat.statbigdecimal.MeanBigDecimalWeightsTest.java

/**
 * Tests set calculation using indices.//from  www .  j ava2  s  . co  m
 */
@Test
public void testSetIndices() {

    for (PlateBigDecimal plate : arrayIndices) {

        int begin = random.nextInt(plate.first().size() - 4);
        int end = begin + random.nextInt(3) + 3;

        Map<WellBigDecimal, BigDecimal> resultMap = new TreeMap<WellBigDecimal, BigDecimal>();
        Map<WellBigDecimal, BigDecimal> returnedMap = mean.set(plate.dataSet(),
                ArrayUtils.subarray(weightsIndices, begin, end), begin, end - begin, mc);

        for (WellBigDecimal well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigDecimal bd : well) {
                input[index] = bd.doubleValue() * weightsIndices[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(ArrayUtils.subarray(input, begin, end));
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (WellBigDecimal well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }
}