Compute the density function \(f(x, *)\) of the (noncentral) chi-squared distribution.
dnchisqR (x, df, ncp, log = FALSE,
eps = 5e-15, termSml = 1e-10, ncpLarge = 1000)
dnchisqBessel(x, df, ncp, log = FALSE)
dchisqAsym (x, df, ncp, log = FALSE)
dnoncentchisq(x, df, ncp, kmax = floor(ncp/2 + 5 * (ncp/2)^0.5))
numeric vector similar to x
, containing the (logged if
log=TRUE
) values of the density \(f(x,*)\).
non-negative numeric vector.
degrees of freedom (parameter), a positive number.
non-centrality parameter \(\delta\); ....
logical indicating if the result is desired on the log scale.
positive convergence tolerance for the series expansion: Terms
are added while term * q > (1-q)*eps
, where q
is the term's
multiplication factor.
positive tolerance: in the series expansion, terms are
added to the sum as long as they are not smaller than termSml *
sum
even when convergence according to eps
had occured. This
was not part of the original C code, but was added later for
safeguarding against infinite loops, from tools:::Rd_expr_PR(14105), e.g., for
dchisq(2000, 2, 1000)
.
in the case where mid
underflows to 0
, when
log
is true, or ncp >= ncpLarge
, use a central
approximation. In theory, an optimal choice of ncpLarge
would
not be arbitrarily set at 1000
(hardwired in R's
dchisq()
here), but possibly also depend on x
or
df
.
the number of terms in the sum for dnoncentchisq()
.
Martin Maechler, April 2008
dnchisqR()
is a pure R implementation of R's own C implementation
in the sources, R/src/nmath/dnchisq.c
, additionally exposing the
three “tuning parameters” eps
, termSml
, and ncpLarge
.
dnchisqBessel()
implements Fisher(1928)'s exact closed form formula
based on the Bessel function \(I_{nu}\), i.e., R's
besselI()
function;
specifically formula (29.4) in Johnson et al. (1995).
dchisqAsym()
is the simple asymptotic approximation from
Abramowitz and Stegun's formula 26.4.27
, p. 942.
dnoncentchisq()
uses the (typically defining) infinite series expansion
directly, with truncation at kmax
, and terms \(t_k\) which
are products of a Poisson probability and a central chi-square density, i.e.,
terms t.k := dpois(k, lambda = ncp/2) * dchisq(x, df = 2*k + df)
for k = 0, 1, ..., kmax
.
Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover. https://en.wikipedia.org/wiki/Abramowitz_and_Stegun provides links to the full text which is in public domain.
Johnson, N.L., Kotz, S. and Balakrishnan, N. (1995)
Continuous Univariate Distributions Vol~2, 2nd ed.; Wiley.
Chapter 29, Section 3 Distribution, (29.4), p. 436.
R's own dchisq()
.
x <- sort(outer(c(1,2,5), 2^(-4:5)))
fRR <- dchisq (x, 10, 2)
f.R <- dnchisqR(x, 10, 2)
all.equal(fRR, f.R, tol = 0) # 64bit Lnx (F 30): 1.723897e-16
stopifnot(all.equal(fRR, f.R, tol = 4e-15))
Run the code above in your browser using DataLab