For a security feature in a Silverlight project I needed to compute the standard deviation of a series. Googling for it I quickly found a code sample, but it dates from the days before LINQ, which makes it 70 lines-long.
Thanks to LINQ, you can write a method that computes a standard deviation with only 3 lines. It works with Silverlight 2 (and 3) and with whatever .NET 3.5 application (WPF, ASP.NET, ...). Here’s my code:
using System;
using System.Data.Linq;public static class SecurityMaths
{
public static double StandardDeviation(this IEnumerable<double> data)
{
double average = data.Average();
var individualDeviations = data.Select(num => Math.Pow(num - average, 2.0));
return Math.Sqrt(individualDeviations.Average());
}
}
Note that I defined my method as an extension method, so that it can be used just like the Average LINQ method. Which means you can use it that way:
double[] numbers = new double[] { 2,4,4,4,5,5,7,9 };
double average = numbers.Average();
double standardDev = numbers.StandardDeviation();
What a clean code: it’s almost as obvious as the mathematical definition of the standard deviation. Thanks a ton, LINQ!
When you install Visual Studio 2008 on your machine, you don't get the .NET Framework Configuration tool. You can still use caspol.exe and other command-line configuration tools, but in order to visualize Code Access Security trees I believe a GUI tool is better...
One of the most interesting features of ASP.NET 4.0 (Visual Studio 2010) is the Chart control, enabling us to create useful and stunning... charts. What's more, Microsoft just released this control for ASP.NET 3.5. For free.
