I try to sample from the following mixture models of gamma distribution:
The R code is as follows:
The algorithm can be translated into a vectorized approach.
Step 1: Generate a random sample k_1,...,k_n
of integers in a vector k
,where P(k)=θ_k
, k=1,...,5.
Step 2: Set rate=1/k.
n <- 5000
k <- sample(1:5, size=n, replace=TRUE, prob=(1:5)/15)
rate <- 1/k
x <- rgamma(n, shape=3, rate=rate)
My question is why x
is now the mixture of these five gamma distributions? In the expression of the mixture model, it seems that we also need coefficient theta_k
?