Learn R Programming

LCPA (version 1.0.0)

rdirichlet: Generate Random Samples from the Dirichlet Distribution

Description

rdirichlet generates n random observations from a Dirichlet distribution with a specified concentration parameter vector alpha.

Usage

rdirichlet(n, alpha)

Value

A matrix with n rows and length(alpha) columns. Each row sums to 1, representing a single sample from the Dirichlet distribution.

Arguments

n

Integer. The number of random vectors to generate.

alpha

Numeric vector. The concentration parameters (must be positive). The length of this vector determines the number of dimensions \(K\).

Details

The Dirichlet distribution is a family of continuous multivariate probability distributions parameterized by a vector \(\alpha\) of positive reals. It is the multivariate generalization of the beta distribution and is commonly used as a conjugate prior to the multinomial distribution in Bayesian statistics.

Probability Density Function:

For a vector \(x = (x_1, \dots, x_K)\) on the unit simplex (where \(\sum x_i = 1\) and \(x_i \ge 0\)), the density is given by:

$$f(x_1, \dots, x_K; \alpha_1, \dots, \alpha_K) = \frac{1}{B(\alpha)} \prod_{i=1}^{K} x_i^{\alpha_i - 1}$$

where the normalizing constant \(B(\alpha)\) is the multivariate beta function:

$$B(\alpha) = \frac{\prod_{i=1}^{K} \Gamma(\alpha_i)}{\Gamma(\sum_{i=1}^{K} \alpha_i)}$$

Simulation Method:

The function utilizes the property that if \(Y_1, \dots, Y_K\) are independent Gamma random variables such that \(Y_i \sim Gamma(shape = \alpha_i, rate = 1)\), then:

$$X_i = \frac{Y_i}{\sum_{j=1}^{K} Y_j}$$

The resulting vector \((X_1, \dots, X_K)\) follows a Dirichlet distribution with parameters \(\alpha\).

Examples

Run this code
# Generate 5 samples from a 3-dimensional Dirichlet distribution
set.seed(123)
alpha_params <- c(1, 2, 5)
result <- rdirichlet(n = 5, alpha = alpha_params)
print(result)

# Check that rows sum to 1
rowSums(result)

Run the code above in your browser using DataLab