Calculate Mean And Std Dev - CSharp System

CSharp examples for System:Math Statistics

Description

Calculate Mean And Std Dev

Demo Code


using System.Diagnostics.Contracts;
using System.Collections;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from   ww w  . java2s .co m*/

public class Main{
        public static void CalculateMeanAndStdDev(this IEnumerable<double> numbers, out double mean, out double stdDev)
        {
            Contract.Requires(numbers != null);
            mean = 0.0;
            double sumOfDerivation = 0.0;
            double count = 0.0;
            foreach (double value in numbers)
            {
                sumOfDerivation += (value) * (value);
                mean += value;
                count++;
            }
            mean /= count;
            double sumOfDerivationAverage = sumOfDerivation / count;
            stdDev = Math.Sqrt(sumOfDerivationAverage - (mean * mean));
        }
}

Related Tutorials