Java Geometric Mean geometricMean(double[] values)

Here you can find the source of geometricMean(double[] values)

Description

Calculates the geometric mean The geometric mean is the product of all values in the array to the Nth root, where N is the total number of values in the array.

License

Apache License

Parameter

Parameter Description
values source of data for calculation

Return

geometric mean

Declaration

public static double geometricMean(double[] values) 

Method Source Code

//package com.java2s;
/*//from ww  w .  jav a  2s .c o  m
 *    Copyright 2016 Roche NimbleGen Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

public class Main {
    /**
     * Calculates the geometric mean
     * 
     * The geometric mean is the product of all values in the array to the Nth root, where N is the total number of values in the array.
     * 
     * reference--http://en.wikipedia.org/wiki/Geometric_mean
     * 
     * @param values
     *            source of data for calculation
     * @return geometric mean
     */
    public static double geometricMean(double[] values) {
        double geometricMean;

        // convert (all the values to log form and
        // send them to geometricMeanFromLog which
        // is a more efficient way of calculating
        // the geometric mean since it uses addition of small log values opposed
        // to multiplication of large non-log values
        int size = values.length;

        double[] logValues = new double[size];
        for (int i = 0; i < size; i++) {
            logValues[i] = Math.log(values[i]);
        }

        geometricMean = geometricMeanFromLog(logValues);

        return geometricMean;
    }

    /**
     * Calculates the geometric mean of log values.
     * 
     * The geometric mean of logarithmic values is simply the arithmethic mean converted to non-logarithmic values (exponentiated)
     * 
     * 
     * @param logValues
     *            array of values in logarithmic form
     * @return geometric mean
     */
    public static double geometricMeanFromLog(double[] logValues) {
        double logArithmeticMean = arithmeticMean(logValues);
        double geometricMean = Math.exp(logArithmeticMean);
        return geometricMean;
    }

    /**
     * calculate the arithmetic mean
     * 
     * The arithmetic mean is the sum of all values in the array divided by the total number of values in the array.
     * 
     * @param values
     *            source of data for mean calculation
     * @return arithmetic mean
     */
    public static double arithmeticMean(double[] values) {
        double arithmeticMean;

        int size = values.length;

        double sum = summation(values);

        arithmeticMean = sum / size;

        return arithmeticMean;
    }

    /**
     * @param values
     *            source of data for summation calculation
     * @return the sum of all values within the array
     */
    public static double summation(double[] values) {
        double sum = 0.0;
        int size = values.length;

        for (int i = 0; i < size; i++) {
            sum += values[i];
        }

        return sum;
    }
}

Related

  1. geometricMean(double a, double b)
  2. geometricMean(double... values)
  3. geometricMean(double[] nums)
  4. geometricMean(final double[] values)
  5. geometricMean(float[] xs)
  6. geometricMeanFromLog(double[] logValues)