# Generating random variables from a mixture of Normal distributions and Exponential distribution using composition method

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:

1. Generate random number $$N$$ with distribution $$\left \{ \frac{a_n}{n+1} \right \}$$
2. 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.$$

• @Xi'an yes it is, I updated my post Nov 25, 2022 at 16:18
• What is $a_n$ In 1.? Nov 25, 2022 at 16:38
• The inverse transform generation is not what you wrote.(*Hint: It is called the inverse cdf transform.) Nov 25, 2022 at 17:07
• R code to generate samples from a mixture of Normals appears as the rmix 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.
– whuber
Nov 25, 2022 at 17:20

Let me rephrase the problem as follows:

Question In order to sample $$X$$ from $$0.3\,\mathcal Exp(1)+0.5\, \mathcal N(0,1)+0.2\,\mathcal N(4,1)\tag{1}$$

a. Write $$\text{Prob}(X\le x)$$ as $$0.3\,\text{Prob}(X_1\le x)+0.5\,\text{Prob}(X_2\le x)+0.2\,\,\text{Prob}(X_3\le x)$$ and specify the distributions of the three random variables $$X_1,X_2,X_3$$

b. Identify an integer-valued random variable $$Z$$ such that

1. $$Z\in\{1,2,3\}$$ with probability one
2. $$X|Z\sim\begin{cases}X_1 &\text{if }Z=1\\X_2 &\text{if }Z=2\\X_3&\text{if }Z=3\\\end{cases}$$
3. $$X$$ is marginally distributed as (1).

c. Conclude with a generation of $$X$$ based on the joint generation of $$(Z,X)$$.

• Thank you I understood it completely wrong Nov 25, 2022 at 18:17