3
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

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 
| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ How do you do this in SPSS? You may be able to create an SPSS syntax file with the sps extension. $\endgroup$ – Ad van der Ven May 7 at 14:49
0
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ How do you do this in SPSS? You may be able to create an SPSS syntax file with the sps extension. $\endgroup$ – Ad van der Ven May 7 at 14:49
  • $\begingroup$ I don't know SPSS. But, it's so simple that you should be able to implement it in almost every language you're familiar with. $\endgroup$ – gunes May 7 at 14:51
  • $\begingroup$ Which programming language did you use? $\endgroup$ – Ad van der Ven May 7 at 15:34
  • 1
    $\begingroup$ Now I understood. With p = 0.05, q = 0.03 and r = 0.01 in SPSS the syntax is: COMPUTE U=RV.UNIFORM(0,1). RECODE U (0.0 thru 0.03=0) (0.03 thru 0.94=1) (0.94 thru 0.99=2) (ELSE=3). EXECUTE. $\endgroup$ – Ad van der Ven May 8 at 8:56
  • 1
    $\begingroup$ @ gunes Thanks a lot for all your help. $\endgroup$ – Ad van der Ven May 8 at 14:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.