Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Created March 21, 2017 00:38
Show Gist options
  • Save msrivastav13/d23c499e3fa07ccc6ac45d4aa24e40cb to your computer and use it in GitHub Desktop.
Save msrivastav13/d23c499e3fa07ccc6ac45d4aa24e40cb to your computer and use it in GitHub Desktop.
Simple apex Utility to calculate Standard Deviation
public with sharing class StandardDeviationComputation {
public static decimal getStandardDeviation(Integer precision){
AggregateResult[] groupedResults = [SELECT COUNT(Id),SUM(DistanceSquare__c) FROM Survey__c GROUP BY Standard_Deviation__c ];
decimal sumOfDistanceSquare = (decimal)groupedResults[0].get('expr1');
integer count = (integer)groupedResults[0].get('expr0');
if(count > 0){
return system.Math.sqrt(sumOfDistanceSquare/count).setScale(precision);//Scale is kept at 3 currently
}else{
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment