Java Variance variance(double[] a)

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

Description

Returns the variance.

License

Apache License

Parameter

Parameter Description
a the array.

Return

the variance.

Declaration

public static double variance(double[] a) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*w w  w.  java 2s  . c o m*/
     * Returns the variance.
     * 
     * @param a the array.
     * @return the variance.
     */
    public static double variance(double[] a) {
        return variance(a, a.length);
    }

    /**
     * Returns the variance.
     * 
     * @param a the array.
     * @param n the total number of elements.
     * @return the variance.
     */
    public static double variance(double[] a, int n) {
        double avg = mean(a, n);
        double sq = 0.0;
        for (double v : a) {
            double d = v - avg;
            sq += d * d;
        }
        return sq / (n - 1.0);
    }

    /**
     * Returns the mean.
     * 
     * @param a the array.
     * @return the mean.
     */
    public static double mean(double[] a) {
        return mean(a, a.length);
    }

    /**
     * Returns the mean.
     * 
     * @param a the array.
     * @param n the total number of elements.
     * @return the mean.
     */
    public static double mean(double[] a, int n) {
        double avg = 0.0;
        for (double v : a) {
            avg += v;
        }
        return avg / n;
    }
}

Related

  1. variance(double... array)
  2. variance(double[] a, int from, int to)
  3. variance(double[] arr)
  4. variance(double[] arr)
  5. variance(double[] array)