dbinom_raw | R Documentation |
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,
version = c("2008", "R4.4"),
verbose = getOption("verbose"))
x |
vector with values typically in |
n |
called |
p |
called |
q |
mathemtically the same as |
log |
logical indicating if the |
version |
a |
verbose |
integer indicating the amount of verbosity of
diagnostic output, |
numeric vector of the same length as x
which may have to be
thought of recycled along n
, p
and/or q
.
R Core and Martin Maechler
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.
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) -> dbin,
dbinom (x, n, p), tolerance = 1e-13))# 1.636e-14 (Apple clang 14.0.3)
stopifnot(all.equal(dbin, dbinom_raw(x, n, p, q=1-p, version = "R4.4") -> dbin44,
tolerance = 1e-13))
cat("n = ", n, ": ", (aeq <- all.equal(dbin44, dbin, tolerance = 0)), "\n")
if(n < 3000) stopifnot(is.character(aeq)) # check that dbin44 is "better" ?!
}
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)|")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.