more useful options PerlMonks

### Re^2: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?

by mrborisguy (Hermit)
 on Jun 14, 2005 at 00:35 UTC ( #466331=note: print w/replies, xml ) Need Help??

Also, the Math::Gsl::Sf has a gamma function, which may speed up calculations (I'm not sure if it would or not, I haven't actually studied the inner workings of the gamma function). The gamma function of any whole number is equal to that number's factorial, ie gamma(300) == 300!, which is why I think this function may be faster than a factorial, since there probably aren't 300 multiplications, I bet it's just some sort of integral, which may have quite a few calculations, but not 300 large multiplications.

-Bryan

• Comment on Re^2: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?

Replies are listed 'Best First'.
Re^3: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?
by Zaxo (Archbishop) on Jun 14, 2005 at 04:51 UTC

Good idea, but no cigar.

\$ perl -MMath::Gsl::Sf=:Gamma -e'print gamma(300)' gsl: gamma.c:1111: ERROR: overflow Default GSL error handler invoked. Aborted (core dumped) \$
The GSL doesn't handle large numbers.

There's also a math error to correct: gamma(\$n) == factorial(\$n - 1)

After Compline,
Zaxo

if your calculations over the factorial numbers are only multiplications and divisions (as I think they are), you can operate over their log values instead, transforming multiplications and divisions to additions and substractions respectively. i.e.:
log (\$n! / (\$r! * (\$n-\$r)!) = Sum(log(1)..log(\$n)) - Sum(log(1)..log(\$r)) - Sum(log(1)..log(\$n-\$r))
and as your operations will only involve a small number of integers, you can cache log(\$n) and log(\$n!) to speed up the calculations.

Really... all along I thought gamma was equal to the factorial. That's new to me, thanks!

-Bryan

Create A New User
Node Status?
node history
Node Type: note [id://466331]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2021-05-08 06:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Perl 7 will be out ...

Results (96 votes). Check out past polls.

Notices?