The R Project SVN R

Rev

Rev 33687 | Rev 36820 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33687 Rev 33718
Line 1... Line 1...
1
/*
1
/*
2
 *  Mathlib : A C Library of Special Functions
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1998 Ross Ihaka
3
 *  Copyright (C) 1998 Ross Ihaka
4
 *  Copyright (C) 2000-2001 The R Development Core Team
4
 *  Copyright (C) 2000-2001 The R Development Core Team
-
 
5
 *  Copyright (C) 2005	The R Foundation
5
 *
6
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  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
 *  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
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *  (at your option) any later version.
Line 11... Line 12...
11
 *  This program is distributed in the hope that it will be useful,
12
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *  GNU General Public License for more details.
15
 *
16
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  A copy of the GNU General Public License is available via WWW at
-
 
18
 *  http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
17
 *  along with this program; if not, write to the Free Software
19
 *  writing to the Free Software Foundation, Inc., 59 Temple Place,
18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
20
 *  Suite 330, Boston, MA  02111-1307  USA.
19
 *
21
 *
20
 *  SYNOPSIS
22
 *  SYNOPSIS
21
 *
23
 *
22
 *    #include <Rmath.h>
24
 *    #include <Rmath.h>
23
 *    double rhyper(double NR, double NB, double n);
25
 *    double rhyper(double NR, double NB, double n);
Line 32... Line 34...
32
 *  REFERENCE
34
 *  REFERENCE
33
 *
35
 *
34
 *    V. Kachitvichyanukul and B. Schmeiser (1985).
36
 *    V. Kachitvichyanukul and B. Schmeiser (1985).
35
 *    ``Computer generation of hypergeometric random variates,''
37
 *    ``Computer generation of hypergeometric random variates,''
36
 *    Journal of Statistical Computation and Simulation 22, 127-145.
38
 *    Journal of Statistical Computation and Simulation 22, 127-145.
-
 
39
 *
-
 
40
 *    The original algorithm had a bug -- R bug report PR#7314 --
-
 
41
 *    giving numbers slightly too small in case III h2pe
-
 
42
 *    where (m < 100 || ix <= 50) , see below.
37
 */
43
 */
38
 
44
 
39
#include "nmath.h"
45
#include "nmath.h"
40
 
46
 
41
/* afc(i) :=  ln( i! )	[logarithm of the factorial i.
47
/* afc(i) :=  ln( i! )	[logarithm of the factorial i.
Line 83... Line 89...
83
    /* extern double afc(int); */
89
    /* extern double afc(int); */
84
 
90
 
85
    int nn1, nn2, kk;
91
    int nn1, nn2, kk;
86
    int i, ix;
92
    int i, ix;
87
    Rboolean reject, setup1, setup2;
93
    Rboolean reject, setup1, setup2;
88
    
94
 
89
    double e, f, g, p, r, t, u, v, y;
95
    double e, f, g, p, r, t, u, v, y;
90
    double de, dg, dr, ds, dt, gl, gu, nk, nm, ub;
96
    double de, dg, dr, ds, dt, gl, gu, nk, nm, ub;
91
    double xk, xm, xn, y1, ym, yn, yk, alv;
97
    double xk, xm, xn, y1, ym, yn, yk, alv;
92
 
98
 
93
    /* These should become `thread_local globals' : */
99
    /* These should become `thread_local globals' : */
Line 235... Line 241...
235
 
241
 
236
	/* acceptance/rejection test */
242
	/* acceptance/rejection test */
237
 
243
 
238
	if (m < 100 || ix <= 50) {
244
	if (m < 100 || ix <= 50) {
239
	    /* explicit evaluation */
245
	    /* explicit evaluation */
240
            /* The original algorithm (and TOMS 668) have 
246
	    /* The original algorithm (and TOMS 668) have
241
                   f = f * i * (n2 - k + i) / (n1 - i) / (k - i);
247
		   f = f * i * (n2 - k + i) / (n1 - i) / (k - i);
242
	       in the (m > ix) case, but the definition of the 
248
	       in the (m > ix) case, but the definition of the
243
               recurrence relation on p134 shows that the +1 is 
249
	       recurrence relation on p134 shows that the +1 is
244
               needed. */
250
	       needed. */
245
	    f = 1.0;
251
	    f = 1.0;
246
	    if (m < ix) {
252
	    if (m < ix) {
247
		for (i = m + 1; i <= ix; i++)
253
		for (i = m + 1; i <= ix; i++)
248
		    f = f * (n1 - i + 1) * (k - i + 1) / (n2 - k + i) / i;
254
		    f = f * (n1 - i + 1) * (k - i + 1) / (n2 - k + i) / i;
249
	    } else if (m > ix) {
255
	    } else if (m > ix) {
Line 299... Line 305...
299
		if (e < 0.0)
305
		if (e < 0.0)
300
		    de /= (1.0 + e);
306
		    de /= (1.0 + e);
301
		if (alv < ub - 0.25 * (dr + ds + dt + de)
307
		if (alv < ub - 0.25 * (dr + ds + dt + de)
302
		    + (y + m) * (gl - gu) - deltal) {
308
		    + (y + m) * (gl - gu) - deltal) {
303
		    reject = FALSE;
309
		    reject = FALSE;
304
		} 
310
		}
305
		else {
311
		else {
306
		    /* * Stirling's formula to machine accuracy
312
		    /* * Stirling's formula to machine accuracy
307
		     */
313
		     */
308
		    if (alv <= (a - afc(ix) - afc(n1 - ix)
314
		    if (alv <= (a - afc(ix) - afc(n1 - ix)
309
				- afc(k - ix) - afc(n2 - k + ix))) {
315
				- afc(k - ix) - afc(n2 - k + ix))) {