Re: please check my homework




"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
> ---------------------------------------------------------------
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
>
> long double bidist(long double p, long double n, long double x);
> long double factorial(long double n);
>
> int main(int argc, char *argv[])
> {
> long double pp, nn, xx, result;
>
> if(argc != 4) exit(EXIT_FAILURE);
>
> pp = strtold(argv[1], NULL);
> nn = strtold(argv[2], NULL);
> xx = strtold(argv[3], NULL);
>
> result = bidist(pp, nn, xx);
> printf("%Lf\n", result);
>
> return 0;
> }
>
> /* 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.

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?

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?

> 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.


.



Relevant Pages

  • Re: Factoring, SF, and transforms
    ... >>probability that no head has resulted by the nth flip. ... any positive integer; this is what you were disputing. ... > ALL cases where the coin eventually lands on heads. ...
    (sci.crypt)
  • Re: Martingale in the field
    ... The chances of getting *any* specific fair coin flipping ... rolls, just much less likely than ALL the other sets of rolls put ... This same principle can be illustrated by examining the probability ... Probability of exactly 0 successes in 10 trials is 0.000976562 ...
    (rec.gambling.craps)
  • Re: Multiplicative Chernoff bound
    ... and the average number of failures and successes m per sample. ... the chernoff bound to find e such that with probability c, ... relative error of my estimation m of p is as small as I want (with ... and the confidence intervals from this will be asymptotically ...
    (sci.stat.math)
  • Re: SF: Infinity proof
    ... you can pick a positive integer at random by choosing a ... and then choosing the integer as follows: ... The normal way of handling probability concerning positive integers is ... This gives a probability distribution depending on k, ...
    (sci.crypt)
  • Nonrenormalization vs Renormalization 10: Gamma Analyzed By PI
    ... The gamma function already derives from Probable Influence/Causation ... other asymmetric nonnegative real line distributions, ... Recall that for positive integer n, ... same way that conditional probability y/x converts to 1 + y - x in PI ...
    (sci.physics)