The R Project SVN R

Rev

Rev 13434 | Rev 33687 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
574 ihaka 1
/*
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1998 Ross Ihaka
13434 maechler 4
 *  Copyright (C) 2000-2001 The R Development Core Team
574 ihaka 5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
5458 ripley 18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
574 ihaka 19
 *
20
 *  SYNOPSIS
21
 *
19500 hornik 22
 *    #include <Rmath.h>
574 ihaka 23
 *    double rhyper(double NR, double NB, double n);
24
 *
25
 *  DESCRIPTION
26
 *
27
 *    Random variates from the hypergeometric distribution.
28
 *    Returns the number of white balls drawn when kk balls
29
 *    are drawn at random from an urn containing nn1 white
30
 *    and nn2 black balls.
31
 *
32
 *  REFERENCE
33
 *
34
 *    V. Kachitvichyanukul and B. Schmeiser (1985).
35
 *    ``Computer generation of hypergeometric random variates,''
36
 *    Journal of Statistical Computation and Simulation 22, 127-145.
37
 */
38
 
8431 ripley 39
#include "nmath.h"
574 ihaka 40
 
1028 maechler 41
/* afc(i) :=  ln( i! )	[logarithm of the factorial i.
42
 *	   If (i > 7), use Stirling's approximation, otherwise use table lookup.
43
*/
574 ihaka 44
 
45
static double afc(int i)
46
{
13434 maechler 47
    const double al[9] =
48
    {
49
	0.0,
50
	0.0,/*ln(0!)=ln(1)*/
51
	0.0,/*ln(1!)=ln(1)*/
52
	0.69314718055994530941723212145817,/*ln(2) */
53
	1.79175946922805500081247735838070,/*ln(6) */
54
	3.17805383034794561964694160129705,/*ln(24)*/
55
	4.78749174278204599424770093452324,
56
	6.57925121201010099506017829290394,
57
	8.52516136106541430016553103634712
58
	/*, 10.60460290274525022841722740072165*/
59
    };
574 ihaka 60
    double di, value;
13434 maechler 61
 
1028 maechler 62
    if (i < 0) {
3076 pd 63
      MATHLIB_WARNING("rhyper.c: afc(i), i=%d < 0 -- SHOULD NOT HAPPEN!\n",i);
1362 maechler 64
      return -1;/* unreached (Wall) */
1028 maechler 65
    } else if (i <= 7) {
574 ihaka 66
	value = al[i + 1];
67
    } else {
68
	di = i;
69
	value = (di + 0.5) * log(di) - di + 0.08333333333333 / di
70
	    - 0.00277777777777 / di / di / di + 0.9189385332;
71
    }
72
    return value;
73
}
74
 
75
double rhyper(double nn1in, double nn2in, double kkin)
76
{
13434 maechler 77
    const double con = 57.56462733;
78
    const double deltal = 0.0078;
79
    const double deltau = 0.0034;
80
    const double scale = 1e25;
81
 
82
    /* extern double afc(int); */
83
 
574 ihaka 84
    int nn1, nn2, kk;
13434 maechler 85
    int i, ix;
86
    Rboolean reject, setup1, setup2;
87
 
88
    double e, f, g, p, r, t, u, v, y;
89
    double de, dg, dr, ds, dt, gl, gu, nk, nm, ub;
90
    double xk, xm, xn, y1, ym, yn, yk, alv;
574 ihaka 91
 
13434 maechler 92
    /* These should become `thread_local globals' : */
574 ihaka 93
    static int ks = -1;
13434 maechler 94
    static int n1s = -1, n2s = -1;
574 ihaka 95
 
13434 maechler 96
    static int k, m;
574 ihaka 97
    static int minjx, maxjx, n1, n2;
98
 
13434 maechler 99
    static double a, d, s, w;
100
    static double tn, xl, xr, kl, kr, lamdl, lamdr, p1, p2, p3;
101
 
102
 
574 ihaka 103
    /* check parameter validity */
104
 
7671 maechler 105
    if(!R_FINITE(nn1in) || !R_FINITE(nn2in) || !R_FINITE(kkin))
106
	ML_ERR_return_NAN;
574 ihaka 107
 
108
    nn1 = floor(nn1in+0.5);
109
    nn2 = floor(nn2in+0.5);
4562 pd 110
    kk	= floor(kkin +0.5);
574 ihaka 111
 
7671 maechler 112
    if (nn1 < 0 || nn2 < 0 || kk < 0 || kk > nn1 + nn2)
113
	ML_ERR_return_NAN;
114
 
574 ihaka 115
    /* if new parameter values, initialize */
10470 maechler 116
    reject = TRUE;
574 ihaka 117
    if (nn1 != n1s || nn2 != n2s) {
13434 maechler 118
	setup1 = TRUE;	setup2 = TRUE;
574 ihaka 119
    } else if (kk != ks) {
13434 maechler 120
	setup1 = FALSE;	setup2 = TRUE;
121
    } else {
122
	setup1 = FALSE;	setup2 = FALSE;
574 ihaka 123
    }
124
    if (setup1) {
125
	n1s = nn1;
126
	n2s = nn2;
127
	tn = nn1 + nn2;
128
	if (nn1 <= nn2) {
129
	    n1 = nn1;
130
	    n2 = nn2;
131
	} else {
132
	    n1 = nn2;
133
	    n2 = nn1;
134
	}
135
    }
136
    if (setup2) {
137
	ks = kk;
138
	if (kk + kk >= tn) {
139
	    k = tn - kk;
140
	} else {
141
	    k = kk;
142
	}
143
    }
144
    if (setup1 || setup2) {
145
	m = (k + 1.0) * (n1 + 1.0) / (tn + 2.0);
146
	minjx = imax2(0, k - n2);
147
	maxjx = imin2(n1, k);
148
    }
4562 pd 149
    /* generate random variate --- Three basic cases */
574 ihaka 150
 
4562 pd 151
    if (minjx == maxjx) { /* I: degenerate distribution ---------------- */
574 ihaka 152
	ix = maxjx;
1362 maechler 153
	/* return ix;
1098 thomas 154
	   No, need to unmangle <TSL>*/
155
	/* return appropriate variate */
1362 maechler 156
 
1098 thomas 157
	if (kk + kk >= tn) {
158
	  if (nn1 > nn2) {
159
	    ix = kk - nn2 + ix;
160
	  } else {
161
	    ix = nn1 - ix;
162
	  }
163
	} else {
164
	  if (nn1 > nn2)
165
	    ix = kk - ix;
166
	}
574 ihaka 167
	return ix;
1362 maechler 168
 
4562 pd 169
    } else if (m - minjx < 10) { /* II: inverse transformation ---------- */
574 ihaka 170
	if (setup1 || setup2) {
171
	    if (k < n2) {
172
		w = exp(con + afc(n2) + afc(n1 + n2 - k)
173
			- afc(n2 - k) - afc(n1 + n2));
174
	    } else {
175
		w = exp(con + afc(n1) + afc(k)
176
			- afc(k - n2) - afc(n1 + n2));
177
	    }
178
	}
179
      L10:
180
	p = w;
181
	ix = minjx;
7725 ripley 182
	u = unif_rand() * scale;
574 ihaka 183
      L20:
184
	if (u > p) {
13434 maechler 185
	    u -= p;
186
	    p *= (n1 - ix) * (k - ix);
187
	    ix++;
574 ihaka 188
	    p = p / ix / (n2 - k + ix);
189
	    if (ix > maxjx)
190
		goto L10;
191
	    goto L20;
192
	}
4562 pd 193
    } else { /* III : h2pe --------------------------------------------- */
574 ihaka 194
 
195
	if (setup1 || setup2) {
196
	    s = sqrt((tn - k) * k * n1 * n2 / (tn - 1) / tn / tn);
197
 
198
	    /* remark: d is defined in reference without int. */
199
	    /* the truncation centers the cell boundaries at 0.5 */
200
 
201
	    d = (int) (1.5 * s) + .5;
202
	    xl = m - d + .5;
203
	    xr = m + d + .5;
13434 maechler 204
	    a = afc(m) + afc(n1 - m) + afc(k - m) + afc(n2 - k + m);
574 ihaka 205
	    kl = exp(a - afc((int) (xl)) - afc((int) (n1 - xl))
206
		     - afc((int) (k - xl))
207
		     - afc((int) (n2 - k + xl)));
208
	    kr = exp(a - afc((int) (xr - 1))
209
		     - afc((int) (n1 - xr + 1))
210
		     - afc((int) (k - xr + 1))
211
		     - afc((int) (n2 - k + xr - 1)));
13434 maechler 212
	    lamdl = -log(xl * (n2 - k + xl) / (n1 - xl + 1) / (k - xl + 1));
213
	    lamdr = -log((n1 - xr + 1) * (k - xr + 1) / xr / (n2 - k + xr));
574 ihaka 214
	    p1 = d + d;
215
	    p2 = p1 + kl / lamdl;
216
	    p3 = p2 + kr / lamdr;
217
	}
218
      L30:
7725 ripley 219
	u = unif_rand() * p3;
220
	v = unif_rand();
4562 pd 221
	if (u < p1) {		/* rectangular region */
574 ihaka 222
	    ix = xl + u;
4562 pd 223
	} else if (u <= p2) {	/* left tail */
574 ihaka 224
	    ix = xl + log(v) / lamdl;
225
	    if (ix < minjx)
226
		goto L30;
227
	    v = v * (u - p1) * lamdl;
4562 pd 228
	} else {		/* right tail */
574 ihaka 229
	    ix = xr - log(v) / lamdr;
230
	    if (ix > maxjx)
231
		goto L30;
232
	    v = v * (u - p2) * lamdr;
233
	}
234
 
235
	/* acceptance/rejection test */
236
 
237
	if (m < 100 || ix <= 50) {
238
	    /* explicit evaluation */
239
	    f = 1.0;
240
	    if (m < ix) {
241
		for (i = m + 1; i <= ix; i++)
13434 maechler 242
		    f = f * (n1 - i + 1) * (k - i + 1) / (n2 - k + i) / i;
574 ihaka 243
	    } else if (m > ix) {
244
		for (i = ix + 1; i <= m; i++)
13434 maechler 245
		    f = f * i * (n2 - k + i) / (n1 - i) / (k - i);
574 ihaka 246
	    }
247
	    if (v <= f) {
10470 maechler 248
		reject = FALSE;
574 ihaka 249
	    }
250
	} else {
251
	    /* squeeze using upper and lower bounds */
252
	    y = ix;
253
	    y1 = y + 1.0;
254
	    ym = y - m;
255
	    yn = n1 - y + 1.0;
256
	    yk = k - y + 1.0;
257
	    nk = n2 - k + y1;
258
	    r = -ym / y1;
259
	    s = ym / yn;
260
	    t = ym / yk;
261
	    e = -ym / nk;
262
	    g = yn * yk / (y1 * nk) - 1.0;
263
	    dg = 1.0;
264
	    if (g < 0.0)
265
		dg = 1.0 + g;
266
	    gu = g * (1.0 + g * (-0.5 + g / 3.0));
267
	    gl = gu - .25 * (g * g * g * g) / dg;
268
	    xm = m + 0.5;
269
	    xn = n1 - m + 0.5;
270
	    xk = k - m + 0.5;
271
	    nm = n2 - k + xm;
272
	    ub = y * gu - m * gl + deltau
273
		+ xm * r * (1. + r * (-0.5 + r / 3.0))
274
		+ xn * s * (1. + s * (-0.5 + s / 3.0))
275
		+ xk * t * (1. + t * (-0.5 + t / 3.0))
276
		+ nm * e * (1. + e * (-0.5 + e / 3.0));
277
	    /* test against upper bound */
278
	    alv = log(v);
279
	    if (alv > ub) {
10470 maechler 280
		reject = TRUE;
574 ihaka 281
	    } else {
282
				/* test against lower bound */
283
		dr = xm * (r * r * r * r);
284
		if (r < 0.0)
13434 maechler 285
		    dr /= (1.0 + r);
574 ihaka 286
		ds = xn * (s * s * s * s);
287
		if (s < 0.0)
13434 maechler 288
		    ds /= (1.0 + s);
574 ihaka 289
		dt = xk * (t * t * t * t);
290
		if (t < 0.0)
13434 maechler 291
		    dt /= (1.0 + t);
574 ihaka 292
		de = nm * (e * e * e * e);
293
		if (e < 0.0)
13434 maechler 294
		    de /= (1.0 + e);
574 ihaka 295
		if (alv < ub - 0.25 * (dr + ds + dt + de)
296
		    + (y + m) * (gl - gu) - deltal) {
10470 maechler 297
		    reject = FALSE;
13434 maechler 298
		} 
299
		else {
300
		    /* * Stirling's formula to machine accuracy
574 ihaka 301
		     */
302
		    if (alv <= (a - afc(ix) - afc(n1 - ix)
303
				- afc(k - ix) - afc(n2 - k + ix))) {
10470 maechler 304
			reject = FALSE;
574 ihaka 305
		    } else {
10470 maechler 306
			reject = TRUE;
574 ihaka 307
		    }
308
		}
309
	    }
310
	}
311
	if (reject)
312
	    goto L30;
313
    }
314
 
315
    /* return appropriate variate */
316
 
317
    if (kk + kk >= tn) {
318
	if (nn1 > nn2) {
319
	    ix = kk - nn2 + ix;
320
	} else {
321
	    ix = nn1 - ix;
322
	}
323
    } else {
324
	if (nn1 > nn2)
325
	    ix = kk - ix;
326
    }
327
    return ix;
328
}