
Last chance! 50% off unlimited learning
Sale ends in
A pure R implementation of R's C API (‘Mathlib’ specifically)
dbinom_raw()
function which computes binomial probabilities
and is continuous in x
, i.e., also “works” for
non-integer x
.
dbinom_raw (x, n, p, q = 1-p, log = FALSE, verbose = getOption("verbose"))
vector with values typically in 0:n
, but here allowed to
non-integer values.
called size
in R's dbinom()
.
called prob
in R's dbinom()
, the success
probability, hence in
mathemtically the same as
logical indicating if the log()
of the resulting
probability should be returned; useful notably in case the probability
itself would underflow to zero.
integer indicating the amount of verbosity of
diagnostic output, 0
means no output, 1
more, etc.
numeric vector of the same length as x
(which may have to be
thought of recycled along n
, p
and/or q
.
Note that our CRAN package Rmpfr provides
dbinom
, an mpfr-accurate function to be used
used instead of R's or this pure R version relying bd0()
and
stirlerr()
where the latter currently only provides
accurate double precision accuracy.
# NOT RUN {
<!-- %% NB: regr.tests in >>> ../tests/dnbinom-tst.R <<< -->
# }
# NOT RUN {
for(n in c(3, 10, 27, 100, 500, 2000, 5000, 1e4, 1e7, 1e10)) {
x <- if(n <= 2000) 0:n else round(seq(0, n, length.out=2000))
p <- 3/4
stopifnot(all.equal(dbinom_raw(x, n, p, q=1-p),
dbinom (x, n, p), tol = 1e-14))
}
n <- 1024 ; x <- 0:n
plot(x, dbinom_raw(x, n, p, q=1-p) - dbinom(x, n, p), type="l", main = "|db_r(x) - db(x)|")
plot(x, dbinom_raw(x, n, p, q=1-p) / dbinom(x, n, p) - 1, type="b", log="y",
main = "rel.err. |db_r(x / db(x) - 1)|")
# }
Run the code above in your browser using DataLab