Adding Standard Deviation to LINQ

This article tells you how to implement a simple Standard Deviation function for LINQ (Language Integrated Query).

If you have performed much in the way of basic data analysis using LINQ (Language Integrated Query), you probably have noticed that standard deviation is not included out of the box. Why Standard Deviation is missing is a mystery as LINQ does include the other basic aggregators such as min, max, avg, sum, and count. To construct a function to perform standard deviation over a set of numbers, you need to examine the math. Figure 1 shown below is the basic standard deviation function.


Figure 1: The math behind the basic stand deviation function.

In simpler terms, standard deviation is calculated as follows:




  1. Calculate the average of the values.

  2. Subtract each value from the average.

  3. Take the result to the second power.

  4. Sum up the result of steps 1-3 over all of the values.

  5. Divides the sum by the number of values.

  6. Take the square root of the result.



Create the Standard Deviation Function



Now that you are familiar with the math, you can create a function that performs this math over a set of values of type double, as shown below.




private double CalculateStdDev(IEnumerable<double> values)
{
double ret = 0;

if (values.Count() > 0)
{
//Compute the Average
double avg = values.Average();

//Perform the Sum of (value-avg)_2_2
double sum = values.Sum(d => Math.Pow(d - avg, 2));

//Put it all together
ret = Math.Sqrt((sum) / values.Count()-1);
}
return ret;
}


Read the Rest of this Article at Developer.com

Regional Articles
- Adding Standard Deviation to LINQ Alabama
- Adding Standard Deviation to LINQ Alaska
- Adding Standard Deviation to LINQ Arizona
- Adding Standard Deviation to LINQ Arkansas
- Adding Standard Deviation to LINQ California
- Adding Standard Deviation to LINQ Colorado
- Adding Standard Deviation to LINQ Connecticut
- Adding Standard Deviation to LINQ DC
- Adding Standard Deviation to LINQ Delaware
- Adding Standard Deviation to LINQ Florida
- Adding Standard Deviation to LINQ Georgia
- Adding Standard Deviation to LINQ Hawaii
- Adding Standard Deviation to LINQ Idaho
- Adding Standard Deviation to LINQ Illinois
- Adding Standard Deviation to LINQ Indiana
- Adding Standard Deviation to LINQ Iowa
- Adding Standard Deviation to LINQ Kansas
- Adding Standard Deviation to LINQ Kentucky
- Adding Standard Deviation to LINQ Louisiana
- Adding Standard Deviation to LINQ Maine
- Adding Standard Deviation to LINQ Maryland
- Adding Standard Deviation to LINQ Massachusetts
- Adding Standard Deviation to LINQ Michigan
- Adding Standard Deviation to LINQ Minnesota
- Adding Standard Deviation to LINQ Mississippi
- Adding Standard Deviation to LINQ Missouri
- Adding Standard Deviation to LINQ Montana
- Adding Standard Deviation to LINQ Nebraska
- Adding Standard Deviation to LINQ Nevada
- Adding Standard Deviation to LINQ New Hampshire
- Adding Standard Deviation to LINQ New Jersey
- Adding Standard Deviation to LINQ New Mexico
- Adding Standard Deviation to LINQ New York
- Adding Standard Deviation to LINQ North Carolina
- Adding Standard Deviation to LINQ North Dakota
- Adding Standard Deviation to LINQ Ohio
- Adding Standard Deviation to LINQ Oklahoma
- Adding Standard Deviation to LINQ Oregon
- Adding Standard Deviation to LINQ Pennsylvania
- Adding Standard Deviation to LINQ Rhode Island
- Adding Standard Deviation to LINQ South Carolina
- Adding Standard Deviation to LINQ South Dakota
- Adding Standard Deviation to LINQ Tennessee
- Adding Standard Deviation to LINQ Texas
- Adding Standard Deviation to LINQ Utah
- Adding Standard Deviation to LINQ Vermont
- Adding Standard Deviation to LINQ Virginia
- Adding Standard Deviation to LINQ Washington
- Adding Standard Deviation to LINQ West Virginia
- Adding Standard Deviation to LINQ Wisconsin
- Adding Standard Deviation to LINQ Wyoming
Related Articles
- C# Tips and Tricks
Learn some tips and tricks with the C# 3.0 language features along with exploring some productivity gains with the Visual Studio IDE.
- SharePoint with HTTP Modules and Handlers
- Interacting with .NET WinForms, Part 2
- Extended Strings
- Devexpress ASPxGridView
- Using the Visual Studio 6.0 Driver Build Environment
- Automating Repetitive Tasks in Visual Studio

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML