Java Variance variance(double[] v)

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

Description

Computes the (bias-corrected sample) variance.

License

Open Source License

Declaration

public static double variance(double[] v) 

Method Source Code

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

public class Main {
    /**/*  w ww . j ava 2 s . c o m*/
     * Computes the (bias-corrected sample) variance.
     */
    public static double variance(double[] v) {
        if (v.length > 1) {
            final double m = mean(v);
            double ans = 0.0;
            for (int i = 0; i < v.length; i++)
                ans += (v[i] - m) * (v[i] - m);
            return ans / (v.length - 1);
        } else
            throw new IllegalArgumentException("Array length must be of 2 or greater.");
    }

    /**
     * Computes the (bias-corrected sample) variance.
     */
    public static double variance(int[] v) {
        if (v.length > 1) {
            final double m = mean(v);
            double ans = 0.0;
            for (int i = 0; i < v.length; i++)
                ans += (v[i] - m) * (v[i] - m);
            return ans / (v.length - 1);
        } else
            throw new IllegalArgumentException("Array length must be of 2 or greater.");
    }

    /**
     * Computes the mean.
     */
    public static double mean(double[] v) {
        if (v.length == 0)
            throw new IllegalArgumentException("Nothing to compute! The array must have at least one element.");
        return (mass(v) / (double) v.length);
    }

    /**
     * Computes the mean.
     */
    public static double mean(int[] v) {
        if (v.length == 0)
            throw new IllegalArgumentException("Nothing to compute! The array must have at least one element.");
        return (mass(v) / (double) v.length);
    }

    /**
     * Returns the sum of the elements of the array.
     */
    public static double mass(double[] v) {
        double somme = 0.0;
        for (int k = 0; k < v.length; k++) {
            somme += v[k];
        }
        return (somme);
    }

    /**
     * Returns the sum of the elements of the array.
     */
    public static int mass(int[] v) {
        int somme = 0;
        for (int k = 0; k < v.length; k++) {
            somme += v[k];
        }
        return (somme);
    }
}

Related

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