Standard Deviation in C#

  1. Standard Deviation With the Self-Defined Method in C
  2. Standard Deviation With the Extension Function in C

This tutorial will introduce the methods to find the standard deviation of elements of a list in C#.

Standard Deviation With the Self-Defined Method in C

The standard deviation is the measure of the dispersion from the mean in statistics. It is calculated by taking the square of each element’s difference from the mean value, adding all the squared values, dividing the answer with the total number of values, and taking a square root of the resultant value.

standard deviation

Unfortunately, there is no built-in method to calculate standard deviation in C#. But, we can define some logic to calculate it. The following code example shows us how we can find the standard deviation of the elements inside a list in C#.

using System;
using System.Collections.Generic;
using System.Linq;

namespace sd_list
{
    class Program
    {
        static double standardDeviation(IEnumerable<double> sequence)
        {
            double result = 0;

            if (sequence.Any())
            {
                double average = sequence.Average();
                double sum = sequence.Sum(d => Math.Pow(d - average, 2));
                result = Math.Sqrt((sum) / (sequence.Count() - 1));
            }
            return result;
        }
        static void Main(string[] args)
        {
            List<double> intList = new List<double> { 1, 2, 3, 4, 5 };
            double standard_deviation = standardDeviation(intList);
            Console.WriteLine("Standard Deviation = {0}",standard_deviation);
        }
    }
}

Output:

Standard Deviation = 1.58113883008419

In the above code, we created the function standardDeviation() that calculates the standard deviation of the elements of a list of doubles in C#. We first calculated the mean of the values with the sequence.Average() function. We then calculated the sum of the square of the difference of the individual values from the mean and saved it in the sum variable. In the end, we divided the sum by the total number of values and calculated its square root. We saved the result of this calculation inside the result variable and returned the result variable.

Standard Deviation With the Extension Function in C

The logic discussed in the previous example can also be transformed into an extension function for the sake of ease of use. The following code example shows us how to change the previous example’s logic into an extension method in C#.

using System;
using System.Collections.Generic;
using System.Linq;

namespace sd_list
{
    public static class ExtensionClass
    {
        public static double standardDeviation(this IEnumerable<double> sequence)
        {
            double average = sequence.Average();
            double sum = sequence.Sum(d => Math.Pow(d - average, 2));
            return Math.Sqrt((sum) / (sequence.Count() - 1));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<double> intList = new List<double> { 1, 2, 3, 4, 5 };
            double standard_deviation = intList.standardDeviation();
            Console.WriteLine("Standard Deviation = {0}",standard_deviation);
        }
    }
}

Output:

Standard Deviation = 1.58113883008419

In the above code, we transformed our previous code into an extension method in C#.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp Math

  • Calculate Distance Between 2 Points in C#