I'm trying to understand about Bayes based spam detection, and have difficulty understanding how to code it. To understand it, I'm reading code of SpamAssassin like below. http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Bayes/CombineChi.pm?view=markup
But, I could not understand how the chi2q function.
# Chi-squared function (API changed; see comment above)
107 sub chi2q {
108 my ($x2, $halfv) = @_;
109
110 my $m = $x2 / 2.0;
111 my ($sum, $term);
112 $sum = $term = exp(0 - $m);
113
114 # replace 'for my $i (1 .. (($v/2)-1))' idiom, which creates a temp
115 # array, with a plain C-style for loop
116 my $i;
117 for ($i = 1; $i < $halfv; $i++) {
118 $term *= $m / $i;
119 $sum += $term;
120 }
121 return $sum < 1.0 ? $sum : 1.0;
122 }
I tried to google or read book, but cannot find full explanation including from theory to code.
Can you explain why it works?