Here you can find the source of variance(double[] vector)
public staticdouble variance(double[] vector)
//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; } } }