# dchisqApprox

##### Approximations of the (Noncentral) Chi-Squared Density

Compute the density function \(f(x, *)\) of the (noncentral) chi-squared distribution.

- Keywords
- distribution, math

##### Usage

```
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))
```

##### Arguments

- x
non-negative numeric vector.

- df
degrees of freedom (parameter), a positive number.

- ncp
non-centrality parameter \(\delta\); ....

- log
logical indicating if the result is desired on the log scale.

- eps
positive convergence tolerance for the series expansion: Terms are added while

`term * q > (1-q)*eps`

, where`q`

is the term's multiplication factor.- termSml
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 14105, e.g., for`dchisq(2000, 2, 1000)`

.- ncpLarge
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`

.- kmax
the number of terms in the sum for

`dnoncentchisq()`

.

##### Details

`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`

.

##### Value

numeric vector similar to `x`

, containing the (logged if
`log=TRUE`

) values of the density \(f(x,*)\).

##### Note

R's `dchisq()`

is typically more uniformly
accurate than the approximations nowadays, apart from `dnchisqR()`

which should behave the same.
There may occasionally exist small differences between `dnchisqR(x, *)`

and `dchisq(x, *)`

for the same parameters.

##### References

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.

##### See Also

R's own `dchisq()`

.

##### Examples

```
# NOT RUN {
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))
# }
```

*Documentation reproduced from package DPQ, version 0.4-2, License: GPL (>= 2)*