Dynami's answer works, but takes a few passes through the data to get the result. This is a one-pass method that calculates the standard deviation of a sample :
public static double StdDev(this IEnumerable<double> values) {
This is the standard deviation of the sample, since it divides by n - 1 . For normal standard deviation, you need to divide by n .
This uses the Welford method, which has higher numerical accuracy compared to the Average(x^2)-Average(x)^2 method.
David Clarke May 20 '10 at 21:29 2010-05-20 21:29
source share