Learn R Programming

simEd (version 2.0.1)

vchisq: Variate Generation for Chi-Squared Distribution

Description

Variate Generation for Chi-Squared Distribution

Usage

vchisq(n, df, ncp = 0, stream = NULL, antithetic = FALSE, asList = FALSE)

Value

If asList is FALSE (default), return a vector of random variates.

Otherwise, return a list with components suitable for visualizing inversion, specifically:

u

A vector of generated U(0,1) variates

x

A vector of chi-squared random variates

quantile

Parameterized quantile function

text

Parameterized title of distribution

Arguments

n

number of observations

df

Degrees of freedom (non-negative, but can be non-integer)

ncp

Non-centrality parameter (non-negative)

stream

if NULL (default), uses stats::runif to generate uniform variates to invert via stats::qchisq; otherwise, an integer in 1:25 indicates the rstream stream from which to generate uniform variates to invert via stats::qchisq;

antithetic

if FALSE (default), inverts \(u\) = uniform(0,1) variate(s) generated via either stats::runif or rstream::rstream.sample; otherwise, uses \(1 - u\)

asList

if FALSE (default), output only the generated random variates; otherwise, return a list with components suitable for visualizing inversion. See return for details

Author

Barry Lawson (blawson@bates.edu),
Larry Leemis (leemis@math.wm.edu),
Vadim Kudlay (vkudlay@nvidia.com)

Details

Generates random variates from the chi-squared distribution.

Chi-Squared variates are generated by inverting uniform(0,1) variates produced either by stats::runif (if stream is NULL) or by rstream::rstream.sample (if stream is not NULL). In either case, stats::qchisq is used to invert the uniform(0,1) variate(s). In this way, using vchisq provides a monotone and synchronized binomial variate generator, although not particularly fast.

The stream indicated must be an integer between 1 and 25 inclusive.

The chi-squared distribution with df = \(n \geq 0\) degrees of freedom has density

\deqn{f_n(x) = \frac{1}{2^{n/2} \ \Gamma(n/2)} x^{n/2-1} e^{-x/2}}{
          f_n(x) = 1 / (2^(n/2) \Gamma(n/2)) x^(n/2-1) e^(-x/2)}

for \(x > 0\). The mean and variance are \(n\) and \(2n\).

The non-central chi-squared distribution with df = n degrees of freedom and non-centrality parameter ncp \(= \lambda\) has density

\deqn{f(x) = e^{-\lambda/2} \sum_{r=0}^\infty \frac{(\lambda/2)^r}{r!} f_{n + 2r}(x)}{
          f(x) = exp(-\lambda/2) SUM_{r=0}^\infty ((\lambda/2)^r / r!) dchisq(x, df + 2r)}

for \(x \geq 0\).

See Also

Examples

Run this code
 set.seed(8675309)
 # NOTE: following inverts rstream::rstream.sample using stats::qchisq
 vchisq(3, df = 3, ncp = 2)

 set.seed(8675309)
 # NOTE: following inverts rstream::rstream.sample using stats::qchisq
 vchisq(3, 3, stream = 1)
 vchisq(3, 3, stream = 2)

 set.seed(8675309)
 # NOTE: following inverts rstream::rstream.sample using stats::qchisq
 vchisq(1, 3, stream = 1)
 vchisq(1, 3, stream = 2)
 vchisq(1, 3, stream = 1)
 vchisq(1, 3, stream = 2)
 vchisq(1, 3, stream = 1)
 vchisq(1, 3, stream = 2)

 set.seed(8675309)
 variates <- vchisq(100, 3, stream = 1)
 set.seed(8675309)
 variates <- vchisq(100, 3, stream = 1, antithetic = TRUE)

Run the code above in your browser using DataLab