Here you can find the source of meanAndVariance(Collection
public staticdouble[] meanAndVariance(Collection<Number> vector)
//package com.java2s; //License from project: Open Source License import java.util.Collection; public class Main { public static/* @pure@ */double[] meanAndVariance(Collection<Number> vector) { return meanAndVariance(vector.toArray(new Number[vector.size()]), true); }/* w ww . j a v a2 s. c om*/ public static/* @pure@ */double[] meanAndVariance(Collection<Number> vector, boolean biased) { return meanAndVariance(vector.toArray(new Number[vector.size()]), biased); } public static/* @pure@ */double[] meanAndVariance(Number[] vector, boolean biased) { double sum = 0.0d, ssum = 0.0d; if (vector.length == 0) { return new double[] { 0.0d, 0.0d }; } for (int i = 0; i < vector.length; i++) { sum += vector[i].doubleValue(); ssum += vector[i].doubleValue() * vector[i].doubleValue(); } double mean = sum / (double) vector.length; double variance = (ssum / (double) vector.length) - (mean * mean); if (!biased) { variance *= vector.length / (double) (vector.length - 1); } return new double[] { mean, variance }; } }