Compute the weighted arithmetic mean - Java java.lang

Java examples for java.lang:Math Calculation

Description

Compute the weighted arithmetic mean

Demo Code

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

public class Main {
    /** Compute the weighted arithmetic mean */
    public static double weightedArithmeticMean(double[] weights,
            double[] scores) {
        if (weights.length == 0)
            return 0;
        if (weights.length != scores.length)
            throw new IllegalArgumentException(
                    "weights and scores must be of the same length");

        double sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += weights[i] * scores[i];
        }/*from w  ww  .j ava  2 s .c o  m*/
        return sum / ((double) weights.length);
    }
}

Related Tutorials