We were discussing Standard Deviations in chat, so I decided to write a standard deviation calculator:
static double PopulationStandardDeviation(List<double> numberSet)
{
double mean = numberSet.Average();
return Math.Sqrt(numberSet.Sum(x => Math.Pow(x - mean, 2)) / numberSet.Count);
}
static double SampleStandardDeviation(List<double> numberSet)
{
double mean = numberSet.Sum() / numberSet.Count;
return Math.Sqrt(numberSet.Sum(x => Math.Pow(x - mean, 2)) / (numberSet.Count - 1));
}
How could these be improved? Because they are both Standard Devation calculators, should I combine them into one method with a header of static double StandardDeviation(List<double> numberSet, bool isSample)
? Are they an optimal solution for the problem?
StandardDeviation()
StandardDeviationP()
\$\endgroup\$double? stdDev = EntityFunctions.StandardDeviation(new List<double> { 1, 2, 3 });
will throw an exception. \$\endgroup\$