Skip to content

Instantly share code, notes, and snippets.

@jbro-io
Last active August 28, 2019 14:21
Show Gist options
  • Save jbro-io/4720789 to your computer and use it in GitHub Desktop.
Save jbro-io/4720789 to your computer and use it in GitHub Desktop.
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