Java Variance variance(double[] vals)

Here you can find the source of variance(double[] vals)

Description

|vals| is const.

License

Open Source License

Declaration

public static double variance(double[] vals) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w  w  w  .  jav  a 2 s.co m
     * |vals| is const.
     */
    public static double variance(double[] vals) {
        if (vals.length == 0) {
            return Double.NaN;
        }

        double mean = mean(vals);

        double[] deviations = new double[vals.length];

        for (int i = 0; i < vals.length; i++) {
            deviations[i] = Math.pow(vals[i] - mean, 2);
        }

        return mean(deviations);
    }

    /**
     * |vals| is const.
     */
    public static double mean(double[] vals) {
        if (vals.length == 0) {
            return Double.NaN;
        }

        double sum = 0;
        for (double val : vals) {
            sum += val;
        }
        return sum / vals.length;
    }
}

Related

  1. variance(double[] array)
  2. variance(double[] d)
  3. Variance(double[] in, double mean)
  4. variance(double[] population)
  5. variance(double[] v)
  6. variance(double[] values)
  7. variance(double[] values, boolean isUnbiased)
  8. variance(double[] vector)
  9. variance(double[] vector)