0

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?

ptikobj
  • 2,690
  • 7
  • 39
  • 64
  • 1
    possible duplicate of [Weighted random numbers in MATLAB](http://stackoverflow.com/questions/2977497/weighted-random-numbers-in-matlab) – Amro Aug 18 '11 at 15:17

1 Answers1

4

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.

hughes
  • 5,595
  • 3
  • 39
  • 55
  • Yeah, basically I am not in a loop and keep a "current" sum of probabilities and once the sum is above the random "value", I chose this event. – ptikobj Aug 19 '11 at 07:51