Skip to contents

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

Usage

srbeta_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 overwritten in place to avoid any memory allocation.

Value

A numeric vector of length n containing random samples from the Beta distribution. The shape1 and shape2 parameters are specified during the optimization process using srbeta_optimize().

NOTE: When the x parameter is specified, it is updated in-place with the simulation for performance reasons.

Details

The Beta Distribution

The Beta distribution has the probability density function (PDF): $$f(x | \alpha, \beta) = \frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha)\Gamma(\beta)} x^{\alpha - 1} (1 - x)^{\beta - 1}, \quad 0 \leq x \leq 1,$$ where:

\(\alpha\)

is the first shape parameter (\(\alpha > 0\)).

\(\beta\)

is the second shape parameter (\(\beta > 0\)).

The Beta distribution is widely used in Bayesian statistics and in modelling probabilities and proportions.

Note

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

TODO : This density instead of this function.

This function samples from a proposal constructed using srbeta_optimize, employing the STORS algorithm.

By default, srbeta_custom() samples from the standard Beta distribution with shape1 = 1 and shape2 = 1. The proposal distribution is pre-optimized at package load time using srbeta_optimize() with steps = 4091, creating a scalable proposal centred around the mode.

See also

srbeta_optimize to optimize the custom proposal.

Examples

# Generate 10 samples from Beta Distribution
samples <- srbeta_custom(10)
print(samples)
#>  [1] 0.75877365 0.46540625 0.63261582 0.65141625 0.17607349 0.08627441
#>  [7] 0.50061956 0.79581749 0.51414438 0.61036993

# Generate 10 samples using a pre-allocated vector
x <- numeric(10)
srbeta_custom(10, x = x)
#>  [1] 0.5902930 0.7003434 0.3445182 0.4578834 0.6197412 0.7940716 0.5658722
#>  [8] 0.3566920 0.3328912 0.8368528
print(x)
#>  [1] 0.5902930 0.7003434 0.3445182 0.4578834 0.6197412 0.7940716 0.5658722
#>  [8] 0.3566920 0.3328912 0.8368528