ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Standard Deviation LINQ extension method (with overloads)

Typical standard deviation formula set in LINQ fluent syntax. For when Average, Min, and Max just aren't enough information. Works with int, double, float.

Source

public static class Extensions
{
    public static double StdDevP(this IEnumerable<int> source)
    {
        return StdDevLogic(source, 0);
    }

    public static double StdDevP(this IEnumerable<double> source)
    {
        return StdDevLogic(source, 0);
    }

    public static double StdDevP(this IEnumerable<float> source)
    {
        return StdDevLogic(source, 0);
    }

    public static double StdDev(this IEnumerable<int> source)
    {
        return StdDevLogic(source);
    }

    public static double StdDev(this IEnumerable<double> source)
    {
        return StdDevLogic(source);
    }

    public static float StdDev(this IEnumerable<float> source)
    {
        return StdDevLogic(source);
    }

    private static double StdDevLogic(this IEnumerable<double> source, int buffer = 1)
    {
        if (source == null)
        { throw new ArgumentNullException("source"); }

        var data = source.ToList();
        var average = data.Average();
        var differences = data.Select(u => Math.Pow(average - u, 2.0)).ToList();
        return Math.Sqrt(differences.Sum() / (differences.Count() - buffer));
    }

    private static double StdDevLogic(this IEnumerable<int> source, int buffer = 1)
    {
        return StdDevLogic(source.Select(x => (double)x));
    }

    private static float StdDevLogic(this IEnumerable<float> source, int buffer = 1)
    {
        if (source == null)
        { throw new ArgumentNullException("source"); }
        var data = source.ToList();
        var average = data.Average();
        var differences = data.Select(u => Math.Pow(average - u, 2.0)).ToList();
        return (float)Math.Sqrt(differences.Sum() / (differences.Count() - buffer));
    }
}

Example

var nums1 = new[] { 11, 12, 13, 12, 13, 15, 12, 14, 15, 15, 12, 14, 15 };
//Prints out the standard deviation of the entire data set (population)
Console.WriteLine(nums1.StdDevP());
//Prints out the standard deviation of the entire data set, but makes allowances for missing data points.
Console.WriteLine(nums1.Take(10).StdDev());
var nums2 = new[] { 11.0, 12.0, 13.0, 12.0, 13.0, 15.0, 12.0, 14.0, 15.0, 15.0, 12.0, 14.0, 15.0 };
//Prints out the standard deviation of the entire data set (population)
Console.WriteLine(nums2.StdDevP());
//Prints out the standard deviation of the entire data set, but makes allowances for missing data points.
Console.WriteLine(nums2.Take(10).StdDev());
var nums3 = new[] { 11.0f, 12.0f, 13.0f, 12.0f, 13.0f, 15.0f, 12.0f, 14.0f, 15.0f, 15.0f, 12.0f, 14.0f, 15.0f };
//Prints out the standard deviation of the entire data set (population)
Console.WriteLine(nums3.StdDevP());
//Prints out the standard deviation of the entire data set, but makes allowances for missing data points.
Console.WriteLine(nums3.Take(10).StdDev());

Author: ParrottSquawk

Submitted on: 6 aug. 2013

Language: C#

Type: System.Double, System.Float

Views: 7418