Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfix and optimization for Robinson-Fisher

  • Loading branch information...
commit 298e2bda998aa27caaac4a23a3cb68b55ee3ad44 1 parent 92c61da
@lok authored
Showing with 32 additions and 21 deletions.
  1. +0 −2  src/BKBayesianClassifier.h
  2. +32 −19 src/BKBayesianClassifier.m
View
2  src/BKBayesianClassifier.h
@@ -87,6 +87,4 @@ extern const NSString *BKCorpusDataPoolName;
- (void)printInformations;
-- (double)chi2PWithChi:(double)chi andDegreeOfFreedom:(int)df;
-
@end
View
51 src/BKBayesianClassifier.m
@@ -40,6 +40,11 @@
const NSString *BKCorpusDataPoolName = @"__BKCorpus__";
+@interface BKBayesianClassifier (Private)
++ (double)chiSquare:(double)chi withDegreeOfFreedom:(NSUInteger)df;
+@end
+
+
@implementation BKBayesianClassifier
@@ -234,28 +239,17 @@ - (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities userInfo:(id)us
return (1.0f + S) / 2.0f;
}
-- (double)chi2PWithChi:(double)chi andDegreeOfFreedom:(int)df
-{
- double m = chi / 2.0;
- double sum, term;
-
- if ((df & 1) == 1) return -1.0;
-
- sum = term = exp(-m);
- for (int i = 1; i < (df / 2); i++) {
- term *= m/i;
- sum += term;
- }
-
- return MIN(sum, 1.0);
-}
-
- (float)robinsonFisherCombinerOnProbabilities:(NSArray*)probabilities userInfo:(id)userInfo
{
NSUInteger length = [probabilities count];
- uint32_t nth = (uint32_t)length;
double probs[length], inverseProbs[length];
+ if (length > (NSUIntegerMax / 2)) {
+ @throw [NSException exceptionWithName:@"NSUInteger overflow"
+ reason:@"Too much probabilities to be combined"
+ userInfo:nil];
+ }
+
NSUInteger idx = 0;
for (NSNumber *probability in probabilities) {
probs[idx] = [probability doubleValue];
@@ -270,8 +264,8 @@ - (float)robinsonFisherCombinerOnProbabilities:(NSArray*)probabilities userInfo:
probsReduced = probsReduced * probs[i];
}
- double H = [self chi2PWithChi:(-2.0f * log(probsReduced)) andDegreeOfFreedom:(2 * nth)];
- double S = [self chi2PWithChi:(-2.0f * log(inverseProbsReduced)) andDegreeOfFreedom:(2 * nth)];
+ double H = [BKBayesianClassifier chiSquare:(-2.0f * log(probsReduced)) withDegreeOfFreedom:(2 * length)];
+ double S = [BKBayesianClassifier chiSquare:(-2.0f * log(inverseProbsReduced)) withDegreeOfFreedom:(2 * length)];
return (1.0 + H - S) / 2.0;
}
@@ -379,4 +373,23 @@ - (void)printInformations
}
}
+#pragma mark -
+#pragma mark Private Methods
++ (double)chiSquare:(double)chi withDegreeOfFreedom:(NSUInteger)df
+{
+ double m = chi / 2.0;
+ double sum, term;
+
+ if ((df & 1) == 1) return -1.0;
+
+ sum = term = exp(-m);
+ for (int i = 1; i < (df / 2); i++) {
+ term *= m/i;
+ sum += term;
+ }
+
+ return MIN(sum, 1.0);
+}
+
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.