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)$?
Best Answer
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:
- Solved – Sampling from a continuous 2 dimensional probability distribution function for importance sampling
- Solved – Inverse transformation sampling for mixture distribution of two normal distributions
- Solved – What are some distributions over the probability simplex
- Solved – Inverse Transformation Sampling with Gaussian
- Solved – How does the inverse transform method work in discrete r.v.