Java Variance variance(double[] vector)

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

Description

variance

License

Open Source License

Declaration

public staticdouble variance(double[] vector) 

Method Source Code

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

public class Main {
    public static/* @pure@ */double variance(double[] vector) {
        double sum = 0, sumSquared = 0;

        if (vector.length <= 1) {
            return 0;
        }/*from ww  w .j a  va 2  s . c  o  m*/
        for (int i = 0; i < vector.length; i++) {
            sum += vector[i];
            sumSquared += (vector[i] * vector[i]);
        }
        double result = (sumSquared - (sum * sum / (double) vector.length)) / (double) (vector.length - 1);

        // We don't like negative variance
        if (result < 0) {
            return 0;
        } else {
            return result;
        }
    }
}

Related

  1. variance(double[] v)
  2. variance(double[] vals)
  3. variance(double[] values)
  4. variance(double[] values, boolean isUnbiased)
  5. variance(double[] vector)
  6. variance(double[] vector)
  7. variance(final double[] values)
  8. variance(final double[] values, final double mean)
  9. variance(final int[] scores)