Here you can find the source of variance(double[] vals)
public static double variance(double[] vals)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w . jav a 2 s.co m * |vals| is const. */ public static double variance(double[] vals) { if (vals.length == 0) { return Double.NaN; } double mean = mean(vals); double[] deviations = new double[vals.length]; for (int i = 0; i < vals.length; i++) { deviations[i] = Math.pow(vals[i] - mean, 2); } return mean(deviations); } /** * |vals| is const. */ public static double mean(double[] vals) { if (vals.length == 0) { return Double.NaN; } double sum = 0; for (double val : vals) { sum += val; } return sum / vals.length; } }