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:org.sakaiproject.gradebookng.tool.panels.SettingsGradingSchemaPanel.java

/**
 * Calculates the mean grade for the course
 * //from w w w  .  ja v  a2 s .c  om
 * @return String mean grade
 */
private String getMean(DescriptiveStatistics stats) {
    return this.total > 0 ? String.format("%.2f", stats.getMean()) : "-";
}

From source file:io.prestosql.operator.aggregation.AbstractTestApproximateCountDistinct.java

@Test(dataProvider = "provideStandardErrors")
public void testMultiplePositions(double maxStandardError) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    for (int i = 0; i < 500; ++i) {
        int uniques = ThreadLocalRandom.current().nextInt(getUniqueValuesCount()) + 1;

        List<Object> values = createRandomSample(uniques, (int) (uniques * 1.5));

        long actual = estimateGroupByCount(values, maxStandardError);
        double error = (actual - uniques) * 1.0 / uniques;

        stats.addValue(error);//from w  w w .ja v a  2 s  .  c  o m
    }

    assertLessThan(stats.getMean(), 1.0e-2);
    assertLessThan(stats.getStandardDeviation(), 1.0e-2 + maxStandardError);
}

From source file:edu.nyu.vida.data_polygamy.standard_techniques.CorrelationTechniquesReducer.java

private double[] normalize(double[] array) {
    DescriptiveStatistics stats = new DescriptiveStatistics(array);
    double mean = stats.getMean();
    double stdDev = stats.getStandardDeviation();
    for (int i = 0; i < array.length; i++) {
        array[i] = (array[i] - mean) / stdDev;
    }//w  ww  . java  2  s.  c  o  m
    return array;
}

From source file:main.java.metric.Metric.java

public static void getServerStatistic(Cluster cluster) {
    DescriptiveStatistics _data = new DescriptiveStatistics();
    DescriptiveStatistics _inflow = new DescriptiveStatistics();
    DescriptiveStatistics _outflow = new DescriptiveStatistics();
    DescriptiveStatistics _meDMV = new DescriptiveStatistics();

    for (Server server : cluster.getServers()) {
        _data.addValue(server.getServer_total_data());
        _inflow.addValue(server.getServer_inflow());
        _outflow.addValue(server.getServer_outflow());
    }/*  ww w  .  j a  v a 2 s.c om*/

    // New - mutually exclusive server sets
    ArrayList<Integer> estimated_dmgr = new ArrayList<Integer>();
    for (Entry<Pair<Integer, Integer>, Integer> entry : mutually_exclusive_serverSets.entrySet()) {
        _meDMV.addValue(entry.getValue());
        estimated_dmgr.add(entry.getValue());
    }

    // Sort the list containing data migration counts within individual server pairs
    Collections.sort(estimated_dmgr);

    // Sum up every floor(S/2) index values to get the estimated data migration time
    int estimated_mgr = 0;
    int j = 0;
    for (int i = 0; i < estimated_dmgr.size(); ++i) {
        j = (int) (i + Math.floor(Global.servers / 2));
        estimated_mgr += estimated_dmgr.get(i);
        i = j;
    }

    mean_server_inflow.add(_inflow.getMean());
    mean_server_outflow.add(_outflow.getMean());
    mean_server_data.add(_data.getMean());
    sd_server_data.add(_data.getStandardDeviation());
    total_data.add((long) Global.global_dataCount);

    mean_pairwise_inter_server_data_mgr.add(_meDMV.getMean());
    sd_pairwise_inter_server_data_mgr.add(_meDMV.getStandardDeviation());
    estimated_data_mgr.add(estimated_mgr);
}

From source file:com.linuxbox.enkive.statistics.consolidation.AbstractConsolidator.java

/**
 * Builds a map that cooresponds to the consolidation methods
 * //from   www  . j  a  va  2  s.  c  o  m
 * @param method
 *            - the method to use
 * @param exampleData
 *            - an example data object (for type consistancy after
 *            consolidation)
 * @param statsMaker
 *            - the pre-populated DescriptiveStatstistics object to pull
 *            stats from
 * @param statData
 *            - the map to populate with consolidated data
 */
public void methodMapBuilder(String method, DescriptiveStatistics statsMaker, Map<String, Object> statData) {
    if (method.equals(CONSOLIDATION_SUM)) {
        statData.put(method, statsMaker.getSum());
    } else if (method.equals(CONSOLIDATION_MAX)) {
        statData.put(method, statsMaker.getMax());
    } else if (method.equals(CONSOLIDATION_MIN)) {
        statData.put(method, statsMaker.getMin());
    } else if (method.equals(CONSOLIDATION_AVG)) {
        statData.put(method, statsMaker.getMean());
    }
}

From source file:com.caseystella.analytics.distribution.DistributionTest.java

@Test
public void testQuantiles() {
    Random r = new Random(0);
    List<DataPoint> points = new ArrayList<>();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    Distribution distribution = null;/*from   w w  w .j a  v  a2 s  .c o  m*/
    for (int i = 0; i < 100; ++i) {
        double val = r.nextDouble() * 1000;
        DataPoint dp = (new DataPoint(i, val, null, "foo"));
        points.add(dp);
        stats.addValue(val);
        if (distribution == null) {
            distribution = new Distribution(dp, ScalingFunctions.NONE, new GlobalStatistics());
        } else {
            distribution.addDataPoint(dp, ScalingFunctions.NONE);
        }
    }
    double realMedian = stats.getPercentile(50);
    double approxMedian = distribution.getMedian();
    System.out.println("mean and std dev: " + stats.getMean() + ", " + Math.sqrt(stats.getVariance()));
    System.out.println("Real : " + realMedian + ", approx: " + approxMedian);
    Assert.assertTrue(Math.abs(realMedian - approxMedian) < 5);
}

From source file:com.caseystella.analytics.outlier.batch.rpca.RPCAOutlierAlgorithm.java

public double outlierScore(List<DataPoint> dataPoints, DataPoint value) {
    double[] inputData = new double[dataPoints.size() + 1];
    int numNonZero = 0;
    if (scaling != ScalingFunctions.NONE) {
        int i = 0;
        final DescriptiveStatistics stats = new DescriptiveStatistics();
        for (DataPoint dp : dataPoints) {
            inputData[i++] = dp.getValue();

            stats.addValue(dp.getValue());
            numNonZero += dp.getValue() > EPSILON ? 1 : 0;
        }//from   w w w  . j av  a  2  s.  c  om
        inputData[i] = value.getValue();
        GlobalStatistics globalStats = new GlobalStatistics() {
            {
                setMax(stats.getMax());
                setMin(stats.getMin());
                setMax(stats.getMean());
                setStddev(stats.getStandardDeviation());
            }
        };
        for (i = 0; i < inputData.length; ++i) {
            inputData[i] = scaling.scale(inputData[i], globalStats);
        }
    } else {
        int i = 0;
        for (DataPoint dp : dataPoints) {
            inputData[i++] = dp.getValue();
            numNonZero += dp.getValue() > EPSILON ? 1 : 0;
        }
        inputData[i] = value.getValue();
    }
    int nCols = 1;
    int nRows = inputData.length;
    if (numNonZero > minRecords) {
        AugmentedDickeyFuller dickeyFullerTest = new AugmentedDickeyFuller(inputData);
        double[] inputArrayTransformed = inputData;
        if (!this.isForceDiff && dickeyFullerTest.isNeedsDiff()) {
            // Auto Diff
            inputArrayTransformed = dickeyFullerTest.getZeroPaddedDiff();
        } else if (this.isForceDiff) {
            // Force Diff
            inputArrayTransformed = dickeyFullerTest.getZeroPaddedDiff();
        }

        if (this.spenalty == null) {
            this.lpenalty = this.LPENALTY_DEFAULT;
            this.spenalty = this.SPENALTY_DEFAULT / Math.sqrt(Math.max(nCols, nRows));
        }

        // Calc Mean
        double mean = 0;
        for (int n = 0; n < inputArrayTransformed.length; n++) {
            mean += inputArrayTransformed[n];
        }
        mean /= inputArrayTransformed.length;

        // Calc STDEV
        double stdev = 0;
        for (int n = 0; n < inputArrayTransformed.length; n++) {
            stdev += Math.pow(inputArrayTransformed[n] - mean, 2);
        }
        stdev = Math.sqrt(stdev / (inputArrayTransformed.length - 1));

        // Transformation: Zero Mean, Unit Variance
        for (int n = 0; n < inputArrayTransformed.length; n++) {
            inputArrayTransformed[n] = (inputArrayTransformed[n] - mean) / stdev;
        }

        // Read Input Data into Array
        // Read Input Data into Array
        double[][] input2DArray = new double[nRows][nCols];
        input2DArray = VectorToMatrix(inputArrayTransformed, nRows, nCols);

        RPCA rSVD = new RPCA(input2DArray, this.lpenalty, this.spenalty);

        double[][] outputE = rSVD.getE().getData();
        double[][] outputS = rSVD.getS().getData();
        double[][] outputL = rSVD.getL().getData();
        return outputS[nRows - 1][0];
    } else {
        return Double.NaN;
    }
}

From source file:algorithm.NQueens.java

public void doMain(String[] args) throws InterruptedException, IOException {
    CmdLineParser parser = new CmdLineParser(this);

    try {// ww  w . ja v  a2  s.  co m
        /* Parse the arguments */
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        System.err.println("java NQueens [options...] arguments...");
        parser.printUsage(System.err);

        /* Print program sample showing all of the options */
        System.err.println("\n Example: java NQueens" + parser.printExample(ALL));
        System.exit(1);
    }

    try {
        String resultantPath = "/" + numQueens + "_q" + "/";
        if (mutation == null) {
            resultantPath += "variable/";
        } else {
            resultantPath += mutation.toString() + "/";
        }

        outputDir += resultantPath;

        File dir = new File(outputDir);
        File figureDir = new File(outputDir + "/figures/");
        /* 
         * Returns true if all the directories are created
         * Returns false and the directories may have been made
         * (so far returns false every other time and it works every time)
         */
        dir.mkdirs();
        figureDir.mkdirs();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }

    for (int count = 0; count < numRuns; ++count) {
        /* Create an initial population of uniformly random chromosomes */
        initPopulation();

        /* Initialize the Breed operation */
        if (mutation != null) {
            Breed.init(new Random(), mutation);
        } else {
            Breed.init(new Random());
        }

        /* Iterate until all of the solutions for the N queens problem has been found */
        while (solutions.size() < distinctSolutions[numQueens - 1] && numGenerations <= maxGenerations) {
            /* If the percentage of similar chromosomes due to in-breeding exceeds
             * the minimum threshold value, increase the amount of mutation
             */
            curSimilarity = similarChromosomes(population);
            if (mutation == null) {
                if (curSimilarity >= inbreedingThreshold) {
                    Breed.inBreeding(true);
                } else {
                    Breed.inBreeding(false);
                }
            }

            /* Calculate the fitness distribution of the current population */
            HashMap<Chromosome, Double> fitness = Fitness.calculate(population);

            /* Instantiate the selection iterator using the fitness distribution,
             * the selection iterator uses roulette wheel selection to select
             * each chromosome.
             */
            Selection selection = new Selection(new Random());
            selection.init(fitness);

            /* Generate the next population by selecting chromosomes from the current
             * population using selection iterator and applying the cloning, crossover,
             * and mutation operations.
             */
            ArrayList<Chromosome> nextPopulation = new ArrayList<Chromosome>(populationSize);
            ArrayList<Chromosome> chromosomes = new ArrayList<Chromosome>(2);

            while (nextPopulation.size() < populationSize) {
                /* Select a random number and apply the breeding operation */
                Integer randomNum = random.nextInt(100);

                /* Pair of parent chromosomes continue on to the next generation.*/
                if (Breed.CLONING.contains(randomNum)) {
                    chromosomes.addAll(Breed.cloning(selection));
                }
                /* Pair of parent chromosomes are cross-overed to create new pair */
                else if (Breed.CROSSOVER.contains(randomNum)) {
                    chromosomes.addAll(Breed.crossover(selection));
                }

                /* Apply the background mutation operator to the chromosomes */
                for (Chromosome chromosome : chromosomes) {
                    randomNum = random.nextInt(100);

                    if (Breed.MUTATION.contains(randomNum)) {
                        nextPopulation.add(Breed.mutation(chromosome));
                    } else {
                        nextPopulation.add(chromosome);
                    }
                }
                chromosomes.clear();
            }

            /* If there are any solutions (fitness of 1) that are unique save them */
            for (Chromosome chromosome : fitness.keySet()) {
                if (fitness.get(chromosome) == 1.0) {
                    if (uniqueSolution(chromosome)) {
                        /* Save a copy of the chromosome */
                        Chromosome solution = new Chromosome(new ArrayList<Integer>(chromosome.get()),
                                chromosome.size());
                        solutions.add(solution);
                        solutionGeneration.put(solutions.size(), numGenerations);

                        /* Perform three rotations then a reflection followed by three more rotations */
                        for (int i = 0; i < 6; ++i) {
                            rotation = Transformation.rotate(solutions.get(solutions.size() - 1));

                            if (uniqueSolution(rotation)) {
                                solutions.add(rotation);
                                solutionGeneration.put(solutions.size(), numGenerations);
                            } else {
                                if (rotationMiss.containsKey(numGenerations)) {
                                    rotationMiss.put(numGenerations, rotationMiss.get(numGenerations) + 1);
                                } else {
                                    rotationMiss.put(numGenerations, 1);
                                }
                            }

                            if (i == 2) {
                                reflection = Transformation.reflect(solution);

                                if (uniqueSolution(reflection)) {
                                    solutions.add(reflection);
                                    solutionGeneration.put(solutions.size(), numGenerations);
                                } else {
                                    if (reflectionMiss.containsKey(numGenerations)) {
                                        reflectionMiss.put(numGenerations,
                                                reflectionMiss.get(numGenerations) + 1);
                                    } else {
                                        reflectionMiss.put(numGenerations, 1);
                                    }
                                }
                            }
                        }
                    } else {
                        if (duplicateBuffer.containsKey(numGenerations)) {
                            duplicateBuffer.put(numGenerations, duplicateBuffer.get(numGenerations) + 1);
                        } else {
                            duplicateBuffer.put(numGenerations, 1);
                        }
                    }
                }
            }

            /* Save average fitness for the current generation */
            DescriptiveStatistics descStats = new DescriptiveStatistics(Doubles.toArray(fitness.values()));
            fitnessBuffer.add(descStats.getMean());

            /* Save chromosome similarity and mutation rate for current generation */
            similarityBuffer.add(curSimilarity);

            /* Save the variable mutation rate */
            if (mutation == null) {
                mutationBuffer.add((Breed.MUTATION.upperEndpoint() - Breed.MUTATION.lowerEndpoint()) / 100.0);
            }

            /* Calculate statistics for the fitness, similarity, and mutation buffer every 1000, generations */
            if ((numGenerations % 1000) == 0) {
                calcStatistics(1000);
            }

            /* Write the current results to file every 10,000 generations */
            if ((numGenerations % 10000) == 0) {
                writeResults();
            }

            /* Set the current population as the NEXT population */
            fitness.clear();
            population = nextPopulation;

            ++numGenerations;
        }

        /* Calculate statistics and write any remaining results */
        if (fitnessBuffer.size() > 0) {
            calcStatistics(1000);
        }
        writeResults();

        /* Display random solutions for the number of solutions specified */
        for (int j = 0; j < numDisplay; ++j) {
            /* Display a random solution */
            Chromosome solution = solutions.get(random.nextInt(solutions.size()));

            try {
                QueenGame myGame = new QueenGame(new QueenBoard(Ints.toArray(solution.get()), numQueens));
                myGame.playGame(
                        outputDir + "/figures/" + "figure_run_" + String.valueOf(runNumber) + "_" + j + ".png");
            } catch (Exception e) {
                System.out.println("Bad set of Queens");
            }
        }

        /* Reset the current state for the next run and increment run number */
        reset();
        ++runNumber;
    }
}

From source file:io.yields.math.framework.data.DataProvidersTest.java

@Explore(name = "check distributional properties of random numbers", dataProvider = DataProviders.FixedMersenneTwisterDataProvider.class, nrOfRuns = 10000)
@Exploration(name = "2D uniform samples", context = FunctionExplorerWithoutProperties.class, group = "data providers")
public void testRandomDistribution(Explorer<Pair> explorer) {
    KolmogorovSmirnovTest ksTest = new KolmogorovSmirnovTest();
    DescriptiveStatistics xStats = new DescriptiveStatistics();
    DescriptiveStatistics yStats = new DescriptiveStatistics();
    explorer.all().forEach(result -> {
        Pair pair = result.getFunctionOutcome().orElse(new Pair());
        xStats.addValue(pair.getX1());/*from ww w.  java 2 s  . c  o  m*/
        yStats.addValue(pair.getX2());
    });
    DescriptiveStatistics cross = new DescriptiveStatistics();
    for (int i = 0; i < xStats.getN(); i++) {
        cross.addValue((xStats.getValues()[i] - .5) * (yStats.getValues()[i] - .5));
    }
    /**
     * x and y should be uniformly distributed
     */
    assertThat(ksTest.kolmogorovSmirnovStatistic(new UniformRealDistribution(0, 1), xStats.getValues()))
            .isEqualTo(0, Delta.delta(.015));
    assertThat(ksTest.kolmogorovSmirnovStatistic(new UniformRealDistribution(0, 1), yStats.getValues()))
            .isEqualTo(0, Delta.delta(.015));
    /**
     * and have zero correlation
     */
    assertThat(cross.getMean()).isEqualTo(0, Delta.delta(.05));
}

From source file:classifiers.ComplexClassifier.java

/**
 *
 * @throws Exception/*from w ww .  ja v  a2  s.c  om*/
 */
@Override
public void BewertunginProzent() throws Exception {

    double count = 0;
    double[][] bestergeb = new double[1][2];

    double[][] hilf;
    double[][] hilf2;
    for (int i = 0; i < anzahldurchlauf; i++) {
        Bootstrap(Modelmenge);
        train(this.traindaten);

        hilf = new double[1][2];
        hilf2 = new double[1][2];

        hilf = test(traindaten);

        this.trainergebnisse[0][0] = hilf[0][0];
        this.trainergebnisse[0][1] = hilf[0][1];
        //System.out.println("Fehlerquote Training:" + (double) (int) (this.trainergebnisse[0][0] * 100) / 100 + "%" + "  " + "Dauer:" + " " + (int) (this.trainergebnisse[0][1]) + "ms");

        hilf2 = test(testdaten);

        this.testergebnisse[0][0] = hilf2[0][0];
        this.testergebnisse[0][1] = hilf2[0][1];
        /* if(testergebnisse[i][0]<=max)
         {
         max=testergebnisse[i][0];
         bestemodel=Model;
         bestergeb[0][0]=testergebnisse[i][0];
         bestergeb[0][1]=testergebnisse[i][1];
                
         }*/

        //System.out.println("Validierung:");
        //System.out.println("Validierungsngsdaten:");

        //  System.out.println("Fehlerquote Validierungs:" + " " + (double) (int) (this.testergebnisse[0][0] * 100) / 100 + "%" + "  " + "Dauer:" + " " + (int) (this.testergebnisse[0][1]) + "ms");
        // System.out.println("----------------------------------------------------------------------------------------");
    }
    DescriptiveStatistics stats1 = new DescriptiveStatistics();
    DescriptiveStatistics stat1 = new DescriptiveStatistics();
    for (int i = 0; i < trainergebnisse.length; i++) {
        stats1.addValue(trainergebnisse[0][0]);
        stat1.addValue(trainergebnisse[0][1]);
    }

    double mean1 = stats1.getMean();
    double std1 = stats1.getStandardDeviation();
    double meanzeit1 = stat1.getMean();
    double stdzeit1 = stat1.getStandardDeviation();

    // System.out.println("Mittlere Felehrquote des Tainings:" + " " + (double) (int) (mean1 * 100) / 100 + "%" + "(" + (double) (int) ((std1 / Math.sqrt(anzahldurchlauf)) * 100) / 100 + "%)");
    //System.out.println("Mittlere Dauer des trainings:" + " " + (int) (meanzeit1) + " " + "ms" + "(" + (int) ((stdzeit1 / Math.sqrt(anzahldurchlauf))) + "ms)");
    //System.out.println("--------------------------------------------------------------------------------------");

    DescriptiveStatistics stats = new DescriptiveStatistics();
    DescriptiveStatistics stat = new DescriptiveStatistics();

    // Add the data from the array
    for (int i = 0; i < testergebnisse.length; i++) {

        stats.addValue(testergebnisse[i][0]);
        stat.addValue(testergebnisse[i][1]);
    }

    this.Mittlerevalidierungsquote = stats.getMean();

    this.stadartdeviationvalidierung = (int) (stats.getStandardDeviation() / Math.sqrt(anzahldurchlauf));
    this.Mittlerezeit = stat.getMean();
    this.standartdeviationtime = (int) (stat.getStandardDeviation() / Math.sqrt(anzahldurchlauf));

    // System.out.println("Mittlere Fehlerquote der Validierungsmengen:" + " " + (double) (int) (Mittlerevalidierungsquote * 100) / 100 + "%" + "(" + (double) (int) ((stadartdeviationvalidierung / Math.sqrt(anzahldurchlauf)) * 100) / 100 + "%)");
    // System.out.println("Mittlere Dauer der Validierung :" + " " + (int) (Mittlerezeit) + " " + "ms" + "(" + (int) ((standartdeviationtime / Math.sqrt(anzahldurchlauf))) + "ms)");
    erg[1] = (double) (int) (Mittlerevalidierungsquote * 100) / 100;
    erg[2] = Mittlerezeit;
    erg[3] = (double) (int) ((stadartdeviationvalidierung / Math.sqrt(anzahldurchlauf)) * 100) / 100;
    erg[4] = (int) ((standartdeviationtime / Math.sqrt(anzahldurchlauf)));

    struct.setErgebnisse(erg);
    train(this.Modelmenge);
    hilf = test(Modelmenge);
    this.Modelergebnisse[0][0] = hilf[0][0];
    this.Modelergebnisse[0][1] = hilf[0][1];
    hilf = test(validierungsmenge);
    validierungsergebnisse[0][0] = hilf[0][0];
    validierungsergebnisse[0][1] = hilf[0][1];
    /* System.out.println("---------------------------------------------------------------------------------------");*/

    // System.out.println("Fehlerquote der  training auf dem Datensatz:" + "  " + (double) (int) (Modelergebnisse[0][0] * 100) / 100 + "%");
    //System.out.println("Zeit des trainings (Datensatz):" + " " + (int) (Modelergebnisse[0][1]) + " " + "ms");
    //System.out.println("---------------------------------------------------------------------------------------");
    //System.out.println("Fehlerquote der Test:" + "  " + (double) (int) (validierungsergebnisse[0][0] * 100) / 100 + "%");
    //System.out.println("Zeit der Test:" + " " + (int) (validierungsergebnisse[0][1]) + " " + "ms");
    //System.out.println();
    /* System.out.println("Beste Struktur:"+"  "+"Validierung:"+" "+(int)bestergeb[0][0]+"%"+"  "+"Zeit:"+" "+(int)bestergeb[0][1]+"ms");
     System.out.println("---------------------------------------------------------------------------------------");
     if(bestemodel!=null) bestemodel.ToString();*/

}