Re: please check my homework
- From: David Marsh <dmarsh@xxxxxxxx>
- Date: Wed, 26 Oct 2005 16:20:25 GMT
Michael Jørgensen wrote:
"David Marsh" <dmarsh@xxxxxxxx> wrote in message news:sAw7f.306880$tl2.244927@xxxxxxxxxxx
This is a homework assignment for extra credit. The statistical material has not been covered in class. Is my bidist() function correct? It seems to work with the examples I have tried, but I would appreciate your comments and suggestions. PLEASE don't write any code. Error checking deliberately omitted for now and I am aware of the potential for factorials to exceed LDBL_MAX.
David ---------------------------------------------------------------
/* return the probability of x successes in n events, given the probability p for success in a single event, using the formula for the binomial distribution: n! f(x) = --------- ( p^x )( (1-p)^(n-x) ) x! (n-x)! */
In the above comment you should specify that n must be a positive integer, that x must be an integer in the intercal [0..n], and that p must be a real number in the interval [0..1].
Write also a test function that calculates: f(0) + f(1) + f(2) + ... + f(n-1) + f(n). This sum should be exactly 1.0 for all valid values of n and p.
OK, that could evolve into a stats project in its own right.
Also, make an estimate of how large values of n your program allows, without running into the problem with LDBL_MAX. For instance 100! = 9.33*10^257.
I'm very concerned with the problem of overflow. You've already recognized the problem, but you should at least consider what alternatives there are. How could you deal with the problem, if it became necessary?
In a full-blown application I would restrict input for n to n! < LDBL_MAX. This was omitted for brevity in the posted program.
Performance might be a problem too. You have three calls to factorial, each involving a loop. For large values of n and x this might be very slow. If necessary, how would you deal with that?
The only alternative to calculating factorials on the fly that I can think of is a lookup table with values of n! up to the largest one that doesn't overflow. It would be a lot of tedious work to build but it might be useful in other applications.
long double bidist(long double p, long double n, long double x) { long double fn, fx, fnx, t1, t2, t3;
fn = factorial(n); fx = factorial(x); fnx = factorial(n-x); t1 = fn / (fx * fnx); t2 = (long double)pow(p, x); t3 = (long double)pow(1.0-p, n-x);
return t1 * t2 * t3; }
/* return n! */ long double factorial(long double n) { long double f;
f = 1.0; while(n > 1.0) f *= n--;
return f; }
-Michael.
Thanks, Michael, for looking at my code.
David .
- Follow-Ups:
- Re: please check my homework
- From: vishnuvyas
- Re: please check my homework
- From: Michael Jørgensen
- Re: please check my homework
- References:
- please check my homework
- From: David Marsh
- Re: please check my homework
- From: Michael Jørgensen
- please check my homework
- Prev by Date: [ANNOUNCE] EVENTS, HUMOUR, BOOKS related to SOFTWARE DEVELOPMENT
- Next by Date: Re: Serial port
- Previous by thread: Re: please check my homework
- Next by thread: Re: please check my homework
- Index(es):
Relevant Pages
|