Calculates the standard deviation - CSharp System

CSharp examples for System:Math Statistics

Description

Calculates the standard deviation

Demo Code

// Permission is hereby granted, free of charge, to any person obtaining a copy
using System.Linq;
using System.Collections.Generic;
using System;/*from  w ww .  ja  v  a 2 s .  c  o m*/

public class Main{
        /// <summary>
        /// Calculates the standard deviation
        /// </summary>
        /// <param name="numbers">Double-values</param>
        /// <returns>Standard deviation</returns>
        public static double StandardDeviation(List<double> numbers)
        {
            if (numbers != null)
            {
                var average = Average(numbers);

                return Math.Sqrt(numbers.Sum(_ => _ * _) / numbers.Count() - (average * average));
            }

            throw new ArgumentException("Numbers are null or 0", "numbers");
        }
}

Related Tutorials