Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

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


in reply to Re: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?
in thread Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?

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)?
  • Download Code

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 00: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

Log In?
Username:
Password:

What's my password?
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 contemplating the Monastery: (5)
As of 2020-01-20 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?