The R Project SVN R

Rev

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

Rev 42302 Rev 42303
Line -... Line 1...
-
 
1
/*
-
 
2
 * AUTHOR
-
 
3
 *   Catherine Loader, catherine@research.bell-labs.com.
-
 
4
 *   October 23, 2000.
-
 
5
 *
-
 
6
 *  Merge in to R:
-
 
7
 *	Copyright (C) 2000, The R Core Development Team
-
 
8
 *
-
 
9
 *  This program is free software; you can redistribute it and/or modify
-
 
10
 *  it under the terms of the GNU General Public License as published by
-
 
11
 *  the Free Software Foundation; either version 2 of the License, or
-
 
12
 *  (at your option) any later version.
-
 
13
 *
-
 
14
 *  This program is distributed in the hope that it will be useful,
-
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
-
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-
 
17
 *  GNU General Public License for more details.
-
 
18
 *
-
 
19
 *  You should have received a copy of the GNU General Public License
-
 
20
 *  along with this program; if not, a copy is available at
-
 
21
 *  http://www.r-project.org/Licenses/
-
 
22
 *
-
 
23
 *
-
 
24
 * DESCRIPTION
-
 
25
 *
-
 
26
 *   To compute the binomial probability, call dbinom(x,n,p).
-
 
27
 *   This checks for argument validity, and calls dbinom_raw().
-
 
28
 *
-
 
29
 *   dbinom_raw() does the actual computation; note this is called by
-
 
30
 *   other functions in addition to dbinom().
-
 
31
 *     (1) dbinom_raw() has both p and q arguments, when one may be represented
-
 
32
 *         more accurately than the other (in particular, in df()).
-
 
33
 *     (2) dbinom_raw() does NOT check that inputs x and n are integers. This
-
 
34
 *         should be done in the calling function, where necessary.
-
 
35
 *         -- but is not the case at all when called e.g., from df() or dbeta() !
-
 
36
 *     (3) Also does not check for 0 <= p <= 1 and 0 <= q <= 1 or NaN's.
-
 
37
 *         Do this in the calling function.
-
 
38
 */
-
 
39
 
-
 
40
#include "nmath.h"
-
 
41
#include "dpq.h"
-
 
42
 
-
 
43
double attribute_hidden
-
 
44
dbinom_raw(double x, double n, double p, double q, int give_log)
-
 
45
{
-
 
46
    double lf, lc;
-
 
47
 
-
 
48
    if (p == 0) return((x == 0) ? R_D__1 : R_D__0);
-
 
49
    if (q == 0) return((x == n) ? R_D__1 : R_D__0);
-
 
50
 
-
 
51
    if (x == 0) {
-
 
52
	if(n == 0) return R_D__1;
-
 
53
	lc = (p < 0.1) ? -bd0(n,n*q) - n*p : n*log(q);
-
 
54
	return( R_D_exp(lc) );
-
 
55
    }
-
 
56
    if (x == n) {
-
 
57
	lc = (q < 0.1) ? -bd0(n,n*p) - n*q : n*log(p);
-
 
58
	return( R_D_exp(lc) );
-
 
59
    }
-
 
60
    if (x < 0 || x > n) return( R_D__0 );
-
 
61
 
-
 
62
    /* n*p or n*q can underflow to zero if n and p or q are small.  This
-
 
63
       used to occur in dbeta, and gives NaN as from R 2.3.0.  */
-
 
64
    lc = stirlerr(n) - stirlerr(x) - stirlerr(n-x) - bd0(x,n*p) - bd0(n-x,n*q);
-
 
65
 
-
 
66
    /* f = (M_2PI*x*(n-x))/n; could overflow or underflow */
-
 
67
    lf = log(M_2PI) + log(x) + log(n-x) - log(n);
-
 
68
 
-
 
69
    return R_D_exp(lc - 0.5*lf);
-
 
70
}
-
 
71
 
-
 
72
double dbinom(double x, double n, double p, int give_log)
-
 
73
{
-
 
74
#ifdef IEEE_754
-
 
75
    /* NaNs propagated correctly */
-
 
76
    if (ISNAN(x) || ISNAN(n) || ISNAN(p)) return x + n + p;
-
 
77
#endif
-
 
78
 
-
 
79
    if (p < 0 || p > 1 || R_D_negInonint(n))
-
 
80
	ML_ERR_return_NAN;
-
 
81
    R_D_nonint_check(x);
-
 
82
    if (x < 0 || !R_FINITE(x)) return R_D__0;
-
 
83
 
-
 
84
    n = R_D_forceint(n);
-
 
85
    x = R_D_forceint(x);
-
 
86
 
-
 
87
    return dbinom_raw(x, n, p, 1-p, give_log);
-
 
88
}