How to obtain a random sample from a categorical distribution using Matlab's rand()?
I want to generate samples 开发者_运维技巧according to a simple categorical probability distribution, e.g.
p(A) = 0.1
p(B) = 0.5
p(C) = 0.25
p(D) = 0.15
Using rand(), which uniformly generates samples in (0,1] what is the best way to achieve this?
You could just check if the random number is less than the probability of each category, in order of increasing probability:
value = rand()
if value < p(A)
return A
if value < p(A)+p(B)
return B
if value < p(A)+p(B)+P(C)
return C
else
return D
I can't really tell you the best way to get them in order without knowing more about your code. If you have only a small number of cases that won't be changing, it might be easiest just to hard-code it once by hand as I've done above.
Edit: now that I think about it, since we're accumulating the probabilities, it doesn't really matter what order they're in. I've adjusted my code accordingly.
Edit edit: I think this is essentially how randsample works.
精彩评论