Skip to contents

The srchisq_custom() function generates random samples from a Chi-squared Distribution using the STORS algorithm. It employs an optimized proposal distribution around the mode and Adaptive Rejection Sampling (ARS) for the tails.

Usage

srchisq_custom(n = 1, x = NULL)

Arguments

n

Integer, length 1. Number of samples to draw.

x

(optional) Numeric vector of length \(n\). If provided, this vector is over written in place to avoid any memory allocation.

Value

A numeric vector of length n containing random samples from the Chi-squared distribution. The degrees of freedom (df) for the distribution are specified during the optimization process using srchisq_optimize(). NOTE: When the x parameter is specified, it is updated in-place with the simulation for performance reasons.

Details

The Chi-squared Distribution

The Chi-squared distribution has the probability density function (PDF): $$f(x | k) = \frac{1}{2^{k/2} \Gamma(k/2)} x^{(k/2) - 1} \exp(-x/2), \quad x \geq 0,$$ where:

\(k\)

is the degrees of freedom (\(k > 0\)), which determines the shape of the distribution.

The Chi-squared distribution is widely used in hypothesis testing and constructing confidence intervals, particularly in the context of variance estimation.

this function is sampling from proposal that has been constructed using srchisq_optimize, using the STORS algorithm.

By default, srchisq_custom() samples from Chi-squared Distribution df = 2. The proposal distribution is pre-optimized at package load time using srchisq_optimize() with steps = 4091, creating a scalable proposal centred around the mode.

Note

This function is not scalable. Therefore, only the srchisq_custom() version is available, which requires the proposal to be pre-optimized using srchisq_optimize() before calling this function.

See also

srchisq_optimize to optimize the custom proposal.

Examples


# Genedf 10 samples from Chi-squared Distribution
samples <- srchisq_custom(10)
print(samples)
#>  [1] 0.8260040 1.3468393 0.4094885 4.8354159 3.1830207 0.7934257 1.0476060
#>  [8] 0.2932254 0.1023306 2.5595152

# Genedf 10 samples using a pre-allocated vector
x <- numeric(10)
srchisq_custom(10, x = x)
#>  [1] 0.911071012 1.503853878 0.896156524 0.927710635 3.530669047 1.451648044
#>  [7] 0.007292812 1.434762129 1.142459953 4.094099386
print(x)
#>  [1] 0.911071012 1.503853878 0.896156524 0.927710635 3.530669047 1.451648044
#>  [7] 0.007292812 1.434762129 1.142459953 4.094099386