Compute the weighted geometric mean. - Java java.lang

Java examples for java.lang:Math Calculation

Description

Compute the weighted geometric mean.

Demo Code

// BSD License (http://lemurproject.org/galago-license)
//package com.java2s;

public class Main {
    /**/*from  w  w w .  j  a va 2  s.  co m*/
     * Compute the weighted geometric mean.
     */
    public static double weightedGeometricMean(double[] weights,
            double[] scores) {
        if (weights.length == 0)
            throw new IllegalArgumentException(
                    "Not clear what geometric mean should do with empty arguments");
        if (weights.length != scores.length)
            throw new IllegalArgumentException(
                    "weights and scores must be of the same length");

        double product = 1;
        for (int i = 0; i < weights.length; i++) {
            product *= weights[i] * scores[i];
        }

        return Math.pow(product, 1.0 / weights.length);
    }
}

Related Tutorials