Skip to content

Instantly share code, notes, and snippets.

@jmoody
Created May 4, 2011 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmoody/955463 to your computer and use it in GitHub Desktop.
Save jmoody/955463 to your computer and use it in GitHub Desktop.
- (NSDecimalNumber *) standardDeviationWithPreviousPoints:(NSArray *) dataPoints
event:(CountEvent *) event {
NSDecimalNumber *result;
if ([self canComputeNonMeanStatistics:dataPoints]) {
NSDecimalNumber *accumulator = [NSDecimalNumber zero];
NSArray *allMeans = [self arrayWithDataPoints:dataPoints event:event];
NSDecimalNumber *mean = [self mean:allMeans];
NSDecimalNumber *variance;
for (NSDecimalNumber *number in allMeans) {
variance = [self varianceWithActual:number mean:mean];
accumulator = [accumulator decimalNumberByAdding:variance];
}
// we need n - 1, but thankfully, that is what we have here
// because we added the current variance before iterating over
// the points
NSDecimalNumber *n = [LjsDecimalAide dnWithInt:[self dataPointsCount:dataPoints]];
NSDecimalNumber *sigmaSquared = [accumulator decimalNumberByDividingBy:n];
result = [self squareRootWithDecimal:sigmaSquared];
} else {
result = self.uncomputableStatistic;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment