I have the following discrete probability distribution where $p$, $q$ and $r$ are known constants:
$P(X=0)=q$, $0<q<1$
$P(X=1)=1-p-q-r$, $0<p+q+r<1$
$P(X=2)=p$, $0<p<1$
$P(X=3)=r$, $0<r<1$
How can I sample from this distribution?
To generate $n$ independent values from this distribution, you can use:
sample(0:3, size = n, replace = TRUE, prob = c(p, 1-p-q-r, q, r))
For example, here we generate $n=10^6$ values and show the sample proportions:
#Generate a large number of values from this distribution
set.seed(1)
p <- 0.11
q <- 0.20
r <- 0.35
n <- 10^6
X <- sample(0:3, size = n, replace = TRUE, prob = c(p, 1-p-q-r, q, r))
#Show sample proportions
table(X)/n
X
0 1 2 3
0.109570 0.340265 0.200222 0.349943
There is a quite simple strategy for discrete distributions such as this with small number of elements in the support:
u <- generate a uniform RV in [0,1]
if u < q: x <- 0
elseif u < 1-p-r: x <- 1
elseif u < 1-r: x <- 2
else x <- 3
This is basically an over simplification of Inverse Transform Sampling.