/*
* deal -- compute combination probabilities for card draws
*
- * ($Id$)
+ * ($Id: deal.c,v 1.18 1995/04/14 01:50:14 esr Exp esr $)
*
* Based on the `cardprobs' program written by Jeremy C. York
* <jeremy@stat.cmu.edu> in September 1993; cleanup, comments,
#define TRUE 1
#define FALSE 0
-static int sx, sk, sM, sN;
+static int si, sk, sM, sN;
static int sa, sb;
static int verbose = FALSE;
{
putchar('\n');
if (sig == SIGFPE)
- (void) printf("Died while computing hyper(%d, %d, %d, %d)\n", sx,sk,sM,sN);
+ (void) printf("Died while computing hyper(i=%d, k=%d, M=%d, N=%d)\n", si,sk,sM,sN);
else
(void) printf("Caught signal %d during hyper computation\n", sig);
}
int base = BASE_DEFAULT;
int turns = TURNS_DEFAULT;
int gammatest = FALSE;
+ int hypertest = FALSE;
+ int choosetest = FALSE;
int headers = TRUE;
- while ((status = getopt(argc, argv, "b:c:ght:v")) != EOF)
+ while ((status = getopt(argc, argv, "b:c:ht:vBGH")) != EOF)
{
switch (status)
{
columns = atoi(optarg);
break;
- case 'g':
+ case 'G':
gammatest = TRUE;
break;
+ case 'H':
+ hypertest = TRUE;
+ break;
+
case 'h':
headers = FALSE;
break;
+ case 'B':
+ choosetest = TRUE;
+ break;
+
case 't':
turns = atoi(optarg);
break;
}
}
+ if (gammatest) {
+ double p = lgamma(atoi(argv[optind]));
+
+ (void) printf("lgamma(%d) = %f\n", atoi(argv[optind]), p);
+ exit(0);
+ }
+
+ if (hypertest) {
+ double p;
+
+ si = atoi(argv[optind]);
+ sk = atoi(argv[optind + 1]);
+ sM = atoi(argv[optind + 2]);
+ sN = atoi(argv[optind + 3]);
+
+ p = hyper_prob(si, sk, sM, sN);
+
+ (void) printf("hyper(%d, %d, %d, %d) = %f\n", si, sk, sM, sN, p);
+ exit(0);
+ }
+
if (optind + 2 > argc)
usage();
special = atoi(argv[optind]);
total = atoi(argv[optind + 1]);
- if (gammatest) {
+ if (choosetest) {
double p = log_a_choose_b(special, total);
(void) printf("log_a_choose_b(%d, %d) = %f\n", special, total, p);
* giving the final answer.
*/
-static double hyper_prob(x, k, M, N)
- int x, k, M, N;
+static double hyper_prob(i, k, M, N)
+ int i, k, M, N;
{
double exp();
/* for the signal handler */
- sx = x;
+ si = i;
sk = k;
sM = M;
sN = N;
- return(exp(log_a_choose_b(M,x) + log_a_choose_b(N-M,k-x) -
- log_a_choose_b(N, k)));
+ return(exp(log_a_choose_b(M,i)
+ + log_a_choose_b(N-M,k-i)
+ - log_a_choose_b(N, k)));
}
static double log_a_choose_b(a,b)
coefficient to standard error. Use this for debugging if you are
a trained numerical analyst (kids, don't try this yourselves at home!).
.TP 10
--g
+-B
+Binomial coefficient test. Compute and print the binomial coefficient
+B(n, k) or `n choose k'.
+.TP 10
+-G
Gamma function test. Compute and display the log of the gamma
-function of the two arguments. If -v tells you that log_a_choose_b()
-is blowing up, this will enable you to probe the local lgamma function.
+function of the argument. If -v tells you that the computation of
+B(n, k) is blowing up, this will enable you to probe the local lgamma
+function.
+.TP
+-H
+Compute the hypergeometric coefficient H(i, k, M, N) with arguments
+supplied from the command line. For debugging.
.PP
.SH EXAMPLES
To compute the probabilities for drawing your 3 Hurloon