Here you can find the source of stddev(Collection a)
Parameter | Description |
---|---|
a | a collection of numeric objects |
public static double stddev(Collection a)
//package com.java2s; //License from project: Open Source License import java.util.Collection; public class Main { /**//w ww . j a v a 2 s .c o m * Compute the sample standard deviation for a Collection of numeric types * @param a a collection of numeric objects * @return the average of a */ public static double stddev(Collection a) { return Math.sqrt(variance(a)); } /** * Compute the sample variance for a Collection of numeric types * @param a a collection of numeric objects * @return the average of a */ public static double variance(Collection a) { double mean = average(a); double squarediffsum = 0; for (Object x : a) { if (x instanceof Double) { squarediffsum += Math.pow((Double) x - mean, 2); } else if (x instanceof Long) { squarediffsum += Math.pow((Long) x - mean, 2); } } if (a.size() > 1) { return squarediffsum / (a.size() - 1); } else { return 0; } } /** * Compute the average for a Collection of numeric types * @param a a collection of numeric objects * @return the average of a */ public static double average(Collection a) { double sum = 0; for (Object x : a) { if (x instanceof Double) { sum += (Double) x; } else if (x instanceof Long) { sum += (Long) x; } else if (x instanceof Integer) { sum += (Integer) x; } else if (x instanceof Float) { sum += (Float) x; } } if (a.size() > 0) { return sum / a.size(); } else { return 0; } } }