Standard Deviation in LINQ

Does LINQ support the cumulative SQL function STDDEV() (standard deviation)?

If not, what is the easiest / best way to calculate it?

Example:

  SELECT test_id, AVERAGE(result) avg, STDDEV(result) std FROM tests GROUP BY test_id 
+76
linq standard-deviation
Feb 12 2018-10-12
source share
6 answers

You can make your own extension by calculating it

 public static class Extensions { public static double StdDev(this IEnumerable<double> values) { double ret = 0; int count = values.Count(); if (count > 1) { //Compute the Average double avg = values.Average(); //Perform the Sum of (value-avg)^2 double sum = values.Sum(d => (d - avg) * (d - avg)); //Put it all together ret = Math.Sqrt(sum / count); } return ret; } } 

If you have a sample of the population, not the entire population, you should use ret = Math.Sqrt(sum / (count - 1)); .

Converted to extension from Adding standard deviation to LINQ by Chris Bennett .

+93
Feb 12 '10 at 17:52
source share

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) { // ref: http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/ double mean = 0.0; double sum = 0.0; double stdDev = 0.0; int n = 0; foreach (double val in values) { n++; double delta = val - mean; mean += delta / n; sum += delta * (val - mean); } if (1 < n) stdDev = Math.Sqrt(sum / (n - 1)); return stdDev; } 

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.

+57
May 20 '10 at 21:29
source share

This transforms David Clark's answer into an extension that follows the same form as other LINQ aggregated functions, such as Average.

Usage: var stdev = data.StdDev(o => o.number)

 public static class Extensions { public static double StdDev<T>(this IEnumerable<T> list, Func<T, double> values) { // ref: https://stackoverflow.com/questions/2253874/linq-equivalent-for-standard-deviation // ref: http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/ var mean = 0.0; var sum = 0.0; var stdDev = 0.0; var n = 0; foreach (var value in list.Select(values)) { n++; var delta = value - mean; mean += delta / n; sum += delta * (value - mean); } if (1 < n) stdDev = Math.Sqrt(sum / (n - 1)); return stdDev; } } 
+29
Sep 14 '12 at 17:20
source share
 var stddev = Math.Sqrt(data.Average(z=>z*z)-Math.Pow(data.Average(),2)); 
+2
Mar 06 '15 at 12:03
source share
 public static double StdDev(this IEnumerable<int> values, bool as_sample = false) { var count = values.Count(); if (count > 0) // check for divide by zero // Get the mean. double mean = values.Sum() / count; // Get the sum of the squares of the differences // between the values and the mean. var squares_query = from int value in values select (value - mean) * (value - mean); double sum_of_squares = squares_query.Sum(); return Math.Sqrt(sum_of_squares / (count - (as_sample ? 1 : 0))) } 
0
Apr 25 '16 at 0:26
source share

Right to the point (and C #> 6.0), Dynamis answer becomes this:

  public static double StdDev(this IEnumerable<double> values) { var count = values?.Count() ?? 0; if (count <= 1) return 0; var avg = values.Average(); var sum = values.Sum(d => Math.Pow(d - avg, 2)); return Math.Sqrt(sum / count); } 
0
Jul 23 '19 at 9:07
source share



All Articles