public static double StandardDeviation(IEnumerable‹double› sequence)
{
// Step 1: Compute the Mean:
var mean = sequence.Average();
// Step 2: Compute the square of the differences between each number
// and the mean:
var squaredMeanDifferences = from n in sequence
select (n - mean) * (n - mean);
// Step 3: Find the mean of those squared differences:
var meanOfSquaredDifferences = squaredMeanDifferences.Average();
// Step 4: Standard Deviation is the square root of that mean:
var standardDeviation = Math.Sqrt(meanOfSquaredDifferences);
return standardDeviation;
}