mrguy123 has asked for the wisdom of the Perl Monks concerning the following question:
For small numbers it works fine. However, for large input it is too slow. For example, I have an input set that needs to call the hypergeom function 137544 times, and it takes about 70 seconds. As this is a web based tool, it is of course too slow.#!/usr/bin/perl -w use strict; sub logfact { return gammln(shift(@_) + 1.0); } sub hypergeom { # There are m "bad" and n "good" balls in an urn. # Pick N of them. The probability of i or more successful selection +s: # (m!n!N!(m+n-N)!)/(i!(n-i)!(m+i-N)!(N-i)!(m+n)!) my ($n, $m, $N, $i) = @_; my $loghyp1 = logfact($m)+logfact($n)+logfact($N)+logfact($m+$n-$N) +; my $loghyp2 = logfact($i)+logfact($n-$i)+logfact($m+$i-$N)+logfact( +$N-$i)+logfact($m+$n); return exp($loghyp1 - $loghyp2); } sub gammln { my $xx = shift; my @cof = (76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.12086509738661e-2, -0.5395239384953e-5); my $y = my $x = $xx; my $tmp = $x + 5.5; $tmp -= ($x + .5) * log($tmp); my $ser = 1.000000000190015; for my $j (0..5) { $ser += $cof[$j]/++$y; } -$tmp + log(2.5066282746310005*$ser/$x); } print hypergeom(300,700,100,40),"\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Fast hypergeometric calculation in Perl
by BrowserUk (Patriarch) on Aug 24, 2010 at 09:16 UTC | |
Re: Fast hypergeometric calculation in Perl
by salva (Canon) on Aug 24, 2010 at 10:14 UTC | |
Re: Fast hypergeometric calculation in Perl
by Khen1950fx (Canon) on Aug 24, 2010 at 09:22 UTC | |
by tsee (Curate) on Aug 24, 2010 at 09:47 UTC | |
Re: Fast hypergeometric calculation in Perl
by roboticus (Chancellor) on Aug 24, 2010 at 11:49 UTC | |
Re: Fast hypergeometric calculation in Perl
by zentara (Cardinal) on Aug 24, 2010 at 12:30 UTC | |
Re: Fast hypergeometric calculation in Perl
by JavaFan (Canon) on Aug 24, 2010 at 09:43 UTC | |
Re: Fast hypergeometric calculation in Perl
by tospo (Hermit) on Aug 24, 2010 at 14:08 UTC | |
Re: Fast hypergeometric calculation in Perl
by pemungkah (Priest) on Aug 24, 2010 at 17:43 UTC |