Calculate Std Dev - CSharp System

CSharp examples for System:Math Statistics

Description

Calculate Std Dev

Demo Code


using System.Diagnostics.Contracts;
using System.Collections;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w  w w.  ja  va 2s . c om

public class Main{
        public static double CalculateStdDev(this IEnumerable<double> numbers)
        {
            Contract.Requires(numbers != null);
            double average = 0.0;
            double sumOfDerivation = 0.0;
            double count = 0.0;
            foreach (double value in numbers)
            {
                sumOfDerivation += (value) * (value);
                average += value;
                count++;
            }
            average /= count;
            double sumOfDerivationAverage = sumOfDerivation / count;
            return Math.Sqrt(sumOfDerivationAverage - (average * average));
        }
}

Related Tutorials