473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to compute binomial distribution without overflow?

I need to compute the value of binomial(n, k)=n!/k!(n-k)! * (1-p)^n *
p^k.

When n and k is very big (e.g. n 160), the step to compute n!/k!(n-
k)! is always overflow even when I used unsigned long long.

Can anyone help me?

Thank you
Gracia
Mar 19 '08 #1
5 4556
On Mar 19, 11:38 pm, gracia <graciaw...@gmail.comwrote:
I need to compute the value of binomial(n, k)=n!/k!(n-k)! * (1-p)^n *
p^k.

When n and k is very big (e.g. n 160), the step to compute n!/k!(n-
k)! is always overflow even when I used unsigned long long.

Can anyone help me?

Thank you

Gracia
Did you try using double or long double?

I am no expert in this field, but it seems you might be able to cancel
some of the terms:

e.g.

n = 10
k = 5

1*2*3*4*5*6*7*8*9*10
--------------------- = 6*7*8*9*10
1*2*3*4*5

T
Mar 20 '08 #2
gracia <gr********@gmail.comwrites:
I need to compute the value of binomial(n, k)=n!/k!(n-k)! * (1-p)^n *
p^k.

When n and k is very big (e.g. n 160), the step to compute n!/k!(n-
k)! is always overflow even when I used unsigned long long
You can use a recurrence relation which is equivalent to the definition you
cite:

long bino(int n, int k)
{
if (k==0 || n == k) return 1;
return bino(n-1,k-1) + bino(n-1,k);
}

You can speed this up considerably by caching.

Mar 20 '08 #3
"gracia" writes:
>I need to compute the value of binomial(n, k)=n!/k!(n-k)! * (1-p)^n *
p^k.

When n and k is very big (e.g. n 160), the step to compute n!/k!(n-
k)! is always overflow even when I used unsigned long long.
The easiest, but probably not the best in terms of accuracy and speed, way
would be to use logarithms.
Mar 20 '08 #4
On Mar 20, 12:38 am, gracia <graciaw...@gmail.comwrote:
I need to compute the value of binomial(n, k)=n!/k!(n-k)! *
(1-p)^n * p^k.
When n and k is very big (e.g. n 160), the step to compute
n!/k!(n- k)! is always overflow even when I used unsigned long
long.
Can anyone help me?
If you have access to the C99 math functions (which will be part
of the next version of C++ as well), you can use something like:

double
fact(
double i )
{
return round( exp( gamma( (double)( i + 1 ) ) ) ) ;
}

long
binom(
long i,
long j )
{
return round( fact( i ) / ( fact( j ) * fact( i - j ) ) ) ;
}

On the other hand, I'm not sure that it will be really correct
for very large values. Typically, a double will not be able to
represent all of the needed values exactly.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Mar 20 '08 #5
On 19 Mar, 23:38, gracia <graciaw...@gmail.comwrote:
I need to compute the value ofbinomial(n, k)=n!/k!(n-k)! * (1-p)^n *
p^k.

When n and k is very big (e.g. n 160), the step to compute n!/k!(n-
k)! is always overflow even when I used unsigned long long.

Can anyone help me?

Thank you

Gracia
See
http://groups.google.co.uk/group/com...=en&lr=&rnum=8
for details of how to perform accurate calculations for the binomial
distribution. VBA code for these calculations can be found in
http://members.aol.com/iandjmsmith/Examples.xls

A Javascript binomial distribution calculator can be found at
http://members.aol.com/iandjmsmith/EXAMPLES.HTM
Ian Smith
Mar 20 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Peuser | last post by:
Hi, I should like to make a distribution (using Tkinter), with standard DLLs removed. pythonXX.dll is no problem. tcl und tk, which make the mass of mega bytes, cannot be removed because...
1
by: Stefan Waizmann | last post by:
Hello, I would like the distutils are creating a binary distribution only - means create the distribution file with *.pyc files WITHOUT the *.py files. Any ideas? Or are the distutils the wrong...
2
by: PengYu.UT | last post by:
Hi, I know they are simple to implement, although I have to take care of overflow. But I just do not want to reinvent the wheel. Do you know if there is any library for them? Thanks! Best...
2
by: joshsackett | last post by:
Hi all, Is it possible to move the distribution database to a new folder/drive without removing replication? I am attempting to do it the same way you would move tempdb: ALTER DATABASE...
7
by: Nena | last post by:
Hi there, I'm trying to include the Numerical Recipes for C++ for the Dev-C++ Editor Version. But so far I've failed. Therefore I've copied all head-files (for example nr.h) into the folder...
4
by: hans.kindberg | last post by:
This is how my page looks like in IE6: http://www.freewebs.com/hasodaki/default.txt (without advertising banner, browsable with IE6 and to see the code with the other browsers)...
27
by: csledge | last post by:
Hi, I am trying to compute a 64 bit result from 2 32 bit registers, How do I get the carry into the higher word ? Also is %lld correct ? #include<stdio.h> long long int64( long x, int y);...
0
by: eGenix Team: M.-A. Lemburg | last post by:
________________________________________________________________________ ANNOUNCING eGenix.com mx Base Distribution Version 3.1.1 for Python 2.6 Open Source Python extensions providing...
0
by: M.-A. Lemburg | last post by:
Just to let you know: we also provide binaries and support for Mac OS X Intel and PPC. Thanks to Joe Strout for pinging us about this. On 2008-10-15 17:41, eGenix Team: M.-A. Lemburg wrote: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.