6
$\begingroup$

Does someone know of a random number generation algorithm for a non-central chi-squared distribution with a non-integer dimension?

PS By algorithm, I am interested in the detailed procedure or methodology underlying the algorithm.

$\endgroup$
5
  • 4
    $\begingroup$ Hi! What do you mean by "non-integer dimension"? The function rchisq in R can generate random values from the $\chi^2$ distribution with a real positive non-centrality parameter (option ncp). $\endgroup$
    – utobi
    Commented Jan 28, 2023 at 16:32
  • 1
    $\begingroup$ @utobi - ... and with noninteger shape (degrees of freedom) parameter. $\endgroup$
    – jbowman
    Commented Jan 28, 2023 at 16:58
  • $\begingroup$ rchisq can also deal with non-integer degrees of freedom. $\endgroup$
    – utobi
    Commented Jan 28, 2023 at 17:01
  • 4
    $\begingroup$ Because the non-central chi-squared distribution is a Poisson mixture of central chi-squared distributions, you can easily generate random variables in a two-stage process if you can generate central chi-squared variates. $\endgroup$
    – whuber
    Commented Jan 28, 2023 at 18:12
  • 2
    $\begingroup$ Welcome to CV, user378619. In case these are useful resources, see Devroye, L. (1986). Non-Uniform Random Variate Generation. Springer Verlag. (Free pdf from the author!) And perhaps, Knuth's The Art Of Computer Programming Vol.2 Chapter 3 which has a wealth of commonly used approaches to generating random numbers, including from specific distributions. $\endgroup$
    – Alexis
    Commented Jan 28, 2023 at 20:38

1 Answer 1

7
$\begingroup$

I am assuming you know how to generate random draws from a central chi-squared distribution, or from its equivalent gamma version; see below for the details. I also suggest possible readings on algorithms for the generation of random variates from a gamma distribution, in case this premise does not hold; see also the references provided in the comments of your post.

Solution 1. As per comments, the function rchisq of R does implement what you are looking for. From the help page of this function:

Usage

rchisq(n, df, ncp = 0)

Arguments

n: number of observations. If length(n) > 1, the length is taken to be the number required.

df: degrees of freedom (non-negative, but can be non-integer).

ncp: non-centrality parameter (non-negative).

Here is a simple R code for generating $10^4$ samples from the chi-square with $\nu = 2.56$ degrees of freedom and non-centrality parameter ncp=6.52.

hist(rchisq(10^4, df=2.56, ncp=6.52))

enter image description here Solution 2. Expanding over whuber's comment, you can generate numbers from this distribution by noting the fact that its density is an infinite mixture of central chi-squared distributions with Poisson weights. That is, a non-central chi-squared random variable with non-centrality parameter $\lambda$, i.e. $\chi_\nu^2(\lambda)$ has density function

$$ f(x;\nu, \lambda) = \sum_{r=0}^{\infty} \frac{e^{-\lambda/2} (\lambda/2)^r}{r!}f_{\chi_{\nu+2r}^2}(x) = E\left(f_{\chi_{\nu+2R}^2}\right), $$ where $R\sim \text{Poisson}(\lambda/2)$ and $\chi_\nu^2$ denotes the central chi-squared distribution.

To generate a random variate $x$ from the $\chi_\nu^2(\lambda)$ distribution then do:

  • draw $r$ from $\text{Poisson}(\lambda/2)$
  • draw $x$ from $\chi^2_{\nu +2r}$

Here is a simple R code for all this.

# implement a simple function 
my_rchisq <- function(df, ncp) {
  if(ncp==0)
    stop("Please use rchisq.")
  n = rpois(1, lambda = ncp/2)
  out = rchisq(1, df= df + 2*n, ncp = 0)

  return(out)
}

# call the function N times
gen2 <- sapply(1:N, function(x) my_rchisq(dof,ncp))

# draw a histogram and compare it with true pdf
hist(gen2,breaks = 30, probability = TRUE)
plot(function(x) dchisq(x, df=dof, ncp=ncp), 
n=200, 
add=TRUE, 
lwd=2, xlim = c(0,50))

enter image description here

Comments The code presumes you know how to generate random variates from $\chi_{\nu}^2$, for any real $\nu>0$ (again, see rchisq in R). However, since this distribution is a special case of a gamma distribution, i.e. if $Y \sim \chi_{nu}^2$ then $Y$ has a gamma distribution with shape parameter $\nu/2$ and scale parameter equal to 2, it boils down to generating random draws from the gamma distribution (rgamma in R).

Since you are interested in $0<\text{ncp}<1$, this means that you have to draw from the gamma distribution with a shape parameter between zero and one. It turns out that the generation of random draws, in this case, may be troublesome due to close-to-zero values. The documentation of rgamma reads

Note that for smallish values of shape (and moderate scale) a large parts of the mass of the Gamma distribution is on values of xx so near zero that they will be represented as zero in computer arithmetic. So rgamma may well return values which will be represented as zero. (This will also happen for very large values of scale since the actual generation is done for scale = 1.)

Now, if you also wish to know how to generate random draws from a gamma distribution, I suggest looking at the references on the help page of rgamma, which are specific to this issue. In particular, for the problem of shape between 0 and 1 check

Ahrens, J. H. and Dieter, U. (1974). Computer methods for sampling from gamma, beta, Poisson and binomial distributions. Computing, 12, 223–246.

$\endgroup$
4
  • $\begingroup$ Thanks. I am aware of this particular R function. I am interested in knowing what methodology they use. In your example, is it some sort of interpolation between a 2 and 3 df random number? $\endgroup$
    – user378619
    Commented Jan 28, 2023 at 20:06
  • $\begingroup$ Many thanks. In my particular quest, I would be interested in knowing more about the case 0 < df < 1. $\endgroup$
    – user378619
    Commented Jan 28, 2023 at 20:11
  • 3
    $\begingroup$ The methodology is explained in the help page. After typing ?rchisq, you can read that "The non-central dchisq and rchisq are computed as a Poisson mixture of central chi-squares (Johnson et al, 1995, p.436)." For the case with small df, that page refers you to the Gamma distribution. The df is half the shape parameter. ?rgamma tells you "rgamma ... for 0 < shape < 1 uses Ahrens, J. H. and Dieter, U. (1974). Computer methods for sampling from gamma, beta, Poisson and binomial distributions. Computing, 12, 223–246." $\endgroup$
    – whuber
    Commented Jan 29, 2023 at 14:51
  • $\begingroup$ Thank you whuber and utobi. $\endgroup$
    – user378619
    Commented Jan 29, 2023 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.