Itatsumaki has asked for the wisdom of the Perl Monks concerning the following question:
Howdy fellow monks,
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 this article, under the heading "Interpretation of Clusters".
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 $delta 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?
-Tatsuse 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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hypergeometric Probability Calculation
by Abigail-II (Bishop) on Dec 04, 2003 at 23:22 UTC | |
by Itatsumaki (Friar) on Dec 04, 2003 at 23:48 UTC | |
by Abigail-II (Bishop) on Dec 05, 2003 at 12:32 UTC | |
by tilly (Archbishop) on Dec 05, 2003 at 16:50 UTC | |
by Itatsumaki (Friar) on Mar 22, 2004 at 05:28 UTC | |
| |
by Itatsumaki (Friar) on Dec 05, 2003 at 17:12 UTC |