Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Getting Binomial Distribution under Math::Pari (log) and Combinatorial Method

by tilly (Archbishop)
on Mar 24, 2005 at 03:08 UTC ( [id://441945]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Getting Binomial Distribution under Math::Pari (log) and Combinatorial Method
in thread Getting Binomial Distribution under Math::Pari (log) and Combinatorial Method

If you just want an approximation, then performance may be better still if you use the fact that from Stirling's formula the log of n! is approximately
log(n**n * exp(-n) * sqrt(2*PI*n)) = n*log(n) - n + (log(2 + log(PI) + log(n))/2
Plug that into the fact that n choose m is n!/(m!*(n-m)!) and you can come up with a good approximation that uses Perl's native arithmetic.

This approximation may well turn out to be the fastest approach for large numbers.

Update: From Mathworld I just found out that a significantly better approximation of n! is sqrt(2*n*PI + 1/3)*n**n/exp(n). Algebraically this is less convenient, but the improved accuracy may matter for whatever you're trying to do.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://441945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-19 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found