Re: please check my homework



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
.



Relevant Pages

  • Re: how to solve this limit?
    ... so what do you think if m is a positive integer, ... David C. Ullrich wrote: ... correct by the binomial theorem, ...
    (sci.math)
  • Re: Coprime Numbers
    ... to a positive integer n, ... David C. Ullrich ... question in terms of the multiplicative group of Z/nZ. ...
    (sci.math)
  • Re: Coprime Numbers
    ... let U_n be the set of all the integers that are coprime ... to a positive integer n, ... for what value of n U_n is a cylic group? ... Honestly, David, my words to you were not appropriate at all. ...
    (sci.math)
  • Re: series and squares
    ... This follows from the fact that if N is a large positive integer then ... suppose that n^2 is the largest square with ... David C. Ullrich ...
    (sci.math)