Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a standard deviation [array] [duplicate]

Tags:

c#

.net

algorithm

double[] someDoubles = { 34.6, 45.1, 55.5, 78.5, 84.66, **1400.32**, 99.04, 103.99 }; 

This code above is a short-handed sample of an unexpected behavior of an cumulative algorithm (see the bold value). In real, this is a class which also holds a date with each value.

C# Calculate a deviation? Algorithm that sort out the rows who breaks the cumulative chain?

Advices are of help,

[INSERT]

To clarify, this is about three things
Performance is really important on this topic.

First: Fast-Scan if the values follows a cumulative pattern.
Second: Check if all values goes into a reasonable deviation.
Third: Point out and do error handling.

This question is about the first and second.

like image 376
Independent Avatar asked Mar 17 '11 08:03

Independent


1 Answers

Using LINQ:

double average = someDoubles.Average(); double sumOfSquaresOfDifferences = someDoubles.Select(val => (val - average) * (val - average)).Sum(); double sd = Math.Sqrt(sumOfSquaresOfDifferences / someDoubles.Length);  

The sd variable will have the standard deviation.

If you have a List<double>, then use someDoubles.Count in the last line for code instead of someDoubles.Length.

like image 88
Sanjeevakumar Hiremath Avatar answered Oct 06 '22 01:10

Sanjeevakumar Hiremath