wasBayesSharp – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | |||
5 | namespace BayesSharp.Combiners |
||
6 | { |
||
7 | public class RobinsonFisherCombiner : ICombiner |
||
8 | { |
||
9 | /// <summary> |
||
10 | /// Computes the probability of a message being spam (Robinson-Fisher method) |
||
11 | /// H = C-1( -2.ln(prod(p)), 2*n ) |
||
12 | /// S = C-1( -2.ln(prod(1-p)), 2*n ) |
||
13 | /// I = (1 + H - S) / 2 |
||
14 | /// </summary> |
||
15 | /// <param name="numbers">List of numbers to be combined</param> |
||
16 | public double Combine(IEnumerable<double> numbers) |
||
17 | { |
||
18 | var probList = numbers.ToList(); |
||
19 | var n = probList.Count(); |
||
20 | double h; |
||
21 | double s; |
||
22 | try |
||
23 | { |
||
24 | h = Chi2P(-2.0 * Math.Log(probList.Aggregate(1.0, (x, y) => x * y)), 2 * n); |
||
25 | } |
||
26 | catch (OverflowException) |
||
27 | { |
||
28 | h = 0.0; |
||
29 | } |
||
30 | try |
||
31 | { |
||
32 | s = Chi2P(-2.0 * Math.Log(probList.Aggregate(1.0, (x, y) => x * (1 - y))), 2 * n); |
||
33 | } |
||
34 | catch (OverflowException) |
||
35 | { |
||
36 | s = 0.0; |
||
37 | } |
||
38 | return (1 + h - s) / 2; |
||
39 | } |
||
40 | |||
41 | private static double Chi2P(double chi, double df) |
||
42 | { |
||
43 | var m = chi / 2.0; |
||
44 | var term = Math.Exp(-m); |
||
45 | var sum = term; |
||
46 | for (var i = 1; i <= df / 2; i++) |
||
47 | { |
||
48 | term *= m / i; |
||
49 | sum += term; |
||
50 | } |
||
51 | return Math.Min(sum, 1.0); |
||
52 | } |
||
53 | } |
||
54 | } |