Solved – Generate random number from a specific probability mass in R

How can I generate sample from a distribution with probability mass $P(X=x)$ in R? I know that probability mass, but it is not from a known distribution, also it is not linear, instead it has a complicated form.
Can I use the inverse cdf method on the density, by working out the cdf and inverting it $X=F^{-1}(U)$?

Yes, you can use the inverse CDF method. Let $x_1, x_2, dots$ be the (possibly infinite) sequence of elements which have positive probability masses $p_1,p_2,dots>0$. Let $Iin mathbb{N}$ have distribution $mathbb{P}(I=i)=p_i.$ Generate a uniform random variable $U$ and then let $J$ be such that $$mathbb{P}(I<J)=F_I(J) le U < F_I(J+1).$$ Take $X=x_J$. Then $$mathbb{P}(X=x_j)=mathbb{P}(F_I(j)le U < F_I(j+1))=F_I(j+1)-F_I(j)=p_j.$$

Similar Posts:

Rate this post

Leave a Comment