Permalink
Browse files

Added Robinson-Fisher

  • Loading branch information...
1 parent 7305ecd commit ba754c8b476711a2785688aefb2502b3b03cbc04 @smendes smendes committed Apr 14, 2010
Showing with 48 additions and 2 deletions.
  1. +3 −0 src/BKBayesianClassifier.h
  2. +45 −2 src/BKBayesianClassifier.m
@@ -67,6 +67,7 @@
- (void)buildProbabilityCache;
- (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities;
+- (float)robinsonFisherCombinerOnProbabilities:(NSArray*)probabilities;
- (void)trainWithFile:(NSString*)path forPoolNamed:(NSString*)poolName;
- (void)trainWithString:(NSString*)trainString forPoolNamed:(NSString*)poolName;
@@ -80,4 +81,6 @@
- (void)printInformations;
+- (float)chi2PWithChi:(float)chi andDegreeOfFreedom:(int)df;
+
@end
@@ -209,6 +209,48 @@ - (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities
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
+{
+ NSUInteger length = [probabilities count];
+ uint32_t nth = (uint32_t)length;
+ double probs[length], inverseProbs[length];
+
+ NSUInteger idx = 0;
+ for (NSNumber *probability in probabilities) {
+ probs[idx] = [probability floatValue];
+ inverseProbs[idx] = 1.0f - [probability floatValue];
+ idx++;
+ }
+
+ double inverseProbsReduced = inverseProbs[0];
+ double probsReduced = probs[0];
+ for (NSUInteger i = 1; i < length; i++) {
+ inverseProbsReduced = inverseProbsReduced * inverseProbs[i];
+ 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)];
+
+ return (1.0 + H - S) / 2.0;
+}
+
#pragma mark -
#pragma mark Trainning Methods
- (void)trainWithFile:(NSString*)path forPoolNamed:(NSString*)poolName
@@ -272,8 +314,9 @@ - (NSDictionary*)guessWithTokens:(NSArray*)tokens
NSArray *tokensProbabilities = [pool probabilitiesForTokens:tokens];
if ([tokensProbabilities count] > 0) {
- float probabilityCombined = [self robinsonCombinerOnProbabilities:tokensProbabilities];
- [result setObject:[NSNumber numberWithFloat:probabilityCombined] forKey:poolName];
+ float probabilityCombined = [self robinsonFisherCombinerOnProbabilities:tokensProbabilities];
+ [result setObject:[NSNumber numberWithFloat:probabilityCombined]
+ forKey:poolName];
}
}

0 comments on commit ba754c8

Please sign in to comment.