LINQ Equivalent for Standard Deviation?

You can make your own extension calculating it public static class Extensions { public static double StdDev(this IEnumerable 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; } } Transformed into extension from Adding Standard Deviation to LINQ by Chris Bennett.

1 I'd make that test "values.Count() > 1", because if it's exactly 1 you'll have a divide by zero error when you calculate the return value. – duffymo Feb 12 '10 at 17:59 1 Math. Pow(d-avg, 2)?

I'd skip the function call and use (d-avg)*(d-avg) – duffymo Feb 12 '10 at 18:00 How about adding a usage example. Thanks! – subt13 Jan 7 at 21:30 1 The line ret = Math.

Sqrt((sum) / values.Count()-1); is missing parentheses around values.Count()-1, it should be ret = Math. Sqrt(sum / (values.Count()-1)); – Alex Peck Jun 22 at 8:01.

Dynami's answer (with fix for math) works but makes multiple passes through the data to get a result. This is a single pass method that provides the correct output: public static double StdDev(this IEnumerable values) { // ref: warrenseen.com/blog/2006/03/13/how-to-ca... double mean = 0.0; double sum = 0.0; double stdDev = 0.0; foreach (double val in values) { n++; double delta = val - mean; mean += delta / n; sum += delta * (val - mean); } if (1.

You may not have iterated the entire sequence more than once, but your method will still make two calls to GetEnumerator (which could be triggering a complex SQL query). Why not skip the condition and check n at the end of the loop? – Gideon Engelberth May 21 '10 at 23:47 Thanks Gideon, removes a level of nesting too.

You're correct about the SQL, it's not relevant to what I'm working on so I hadn't considered the implication. – David Clarke May 23 '10 at 22:42.

This is a comment for Dynami's answer. There is a math error in the final calculation. There should be brackets around "values.Count()-1".

When "(sum) / values.Count()" is less than zero, subtracting one from it results in a negative number and I think we all know what happens when you try to take the square root of a negative number. Line should read //Put it all together ret = Math. Sqrt(sum / (values.Count()-1)).

This is the sample standard deviation since it divides by n - 1. For the normal standard deviation you need to divide by n instead. This uses Welford's method which has higher numerical accuracy compared to the Average(x^2)-Average(x)^2 method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions