Java Variance variance(double[] values)

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

Description

Computes the sample variance of the elements of the vector.

License

Open Source License

Parameter

Parameter Description
values the vector

Return

the sample variance of the elements of the vector

Declaration

public static double variance(double[] values) 

Method Source Code

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

public class Main {
    /**//from   w  w w  . j a  v a  2s . c o  m
     * Computes the sample variance of the elements of the vector.
     * @param values the vector
     * @return the sample variance of the elements of the vector
     */
    public static double variance(double[] values) {
        if (values.length < 2) {
            return 0;
        }
        double mean = mean(values);
        double var = 0;
        for (double d : values) {
            var += (d - mean) * (d - mean);
        }
        return var / (values.length - 1);
    }

    /**
     * Computes average of the elements of the vector.
     * @param values the vector
     * @return the average of the elements of the vector
     */
    public static double mean(double values[]) {
        double sum = 0;
        for (double value : values)
            sum += value;
        return sum / values.length;
    }

    /**
     * Computes average of the elements of the vector.
     * @param values the vector
     * @return the average of the elements of the vector
     */
    public static double mean(int values[]) {
        double sum = 0;
        for (int value : values)
            sum += value;
        return sum / values.length;
    }
}

Related

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