Last active
August 28, 2019 14:21
-
-
Save jbro-io/4720789 to your computer and use it in GitHub Desktop.
A method for calculating standard deviations with Apex.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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