I have the following discrete probability distribution where $p$, $q$ and $r$ are known constants:
How can I sample from this distribution?
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It only takes a minute to sign up.Sign up to join this community
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.