perlquestion
Itatsumaki
<p>Howdy fellow monks,</p>
<p>I am trying to implement the hyper-geometric probability in Perl. The general form of the equation is given, for instance, in the second page of <a href="http://www.pubmedcentral.nih.gov/articlerender.fcgi?tool=pubmed&pubmedid=12431279">this article</a>, under the heading "Interpretation of Clusters".</p>
<p>Since the values can get quite large, I thought I would implement using Math::Big and Math::BigFloat, but I seem to have a misunderstanding or a bug somewhere that I cannot track down. The code is below. The symptom is that the value of <code>$delta</code> through each loop iteration is 0, and the final probability value is always one. When I do some calculations manually I get distinctly non-zero $delta for the last few loop iterations. Can anyone see what's I've done wrong?</p>
<code>
use strict;
use Math::BigFloat;
use Math::Big;
my $G = 40; #$ARGV[0];
my $C = 25; #$ARGV[1];
my $n = 15; #$ARGV[2];
my $k = 14; #$ARGV[3];
sub choose {
my $temp = Math::BigFloat->new('1');
$temp = Math::Big::factorial($_[0]) /
Math::Big::factorial ($_[0] - $_[1]) /
Math::Big::factorial($_[1]);
return $temp;
}
my $p = Math::BigFloat->new('1');
my $denom = Math::BigFloat->new(choose($G, $n));
for (my $i = 0; $i < $k; $i++) {
my $val1 = $G - $C;
my $val2 = $n - $i;
my $delta = Math::BigFloat->new();
$delta = choose($C, $i) * choose($val1, $val2) / $denom;
print "$delta\n";
$p -= $delta;
}
print "Probability estimate: $p\n";
</code>
-Tats