How can I sample from a mixture distribution in particular a mixture of Normal distributions and Exponential distribution in R using composition method?
For instance if I want to sample from: $0.3\textrm{Exp}(1)+0.5\textrm N(0,1)+0.2\textrm N(4,1)$.
The algorithm should be following:
- Generate random number $N$ with distribution $\left \{ \frac{a_n}{n+1} \right \}$
- Generate random number $X$ with probability distribution $g_N(x)$. Using inverse transformation we get: $Y\sim U(0,1) \Rightarrow Y^{\frac{1}{N+1}}\sim g_N$.
I do not know understand how to get $N.$
R
code to generate samples from a mixture of Normals appears as thermix
function in my post at stats.stackexchange.com/a/428083/919. Here it is in its entirety:rmix <- function(n, mu, sigma, p) { matrix(rnorm(length(mu)*n, mu, sigma), ncol=n)[ cbind(sample.int(length(mu), n, replace=TRUE, prob=p), 1:n)] }
It is readily modified to generate from a mixture of any distributions. $\endgroup$