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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could just check if the random number is less than the probability of each category,
in order of increasing probability: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.