Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A method for calculating standard deviations with Apex.
public class AdvancedMath
{
public static Double standardDeviation(Double[] numbers)
{
//determine the sum of the range of numbers
Double sum = 0;
for(Double d : numbers)
{
sum += d;
}
//determine the mean of the range of numbers
Double mean = sum / numbers.size();
//for each number subtract the mean and square the result
Double squaredDifferencesSum = 0;
for(Double d : numbers)
{
squaredDifferencesSum += Math.pow((d-mean), 2);
}
//determine the mean for the squared differences
Double squaredDifferencesMean = squaredDifferencesSum / numbers.size();
//determine the standard deviation
Double standardDeviation = Math.sqrt(squaredDifferencesMean);
return standardDeviation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment