Reputation: 179
I know for a random variable x that P(x=i) for each i=1,2,...,100. Then how may I sample x by a multinomial distribution, based on the given P(x=i) in Matlab?
I am allowed to use the Matlab built-in commands rand
and randi
, but not mnrnd
.
Upvotes: 0
Views: 1206
Reputation: 4519
In general, you can sample numbers from any 1 dimensional probability distribution X using a uniform random number generator and the inverse cumulative distribution function of X. This is known as inverse transform sampling.
random_x = xcdf_inverse(rand())
How does this apply here? If you have your vector p
of probabilities defining your multinomial distribution, F = cumsum(p)
gives you a vector that defines the CDF. You can then generate a uniform random number on [0,1] using temp = rand()
and then find the first row in F
greater than temp
. This is basically using the inverse CDF of the multinomial distribution.
Be aware though that for some distributions (eg. gamma distribution), this turns out to be an inefficient way to generate random draws because evaluating the inverse CDF is so slow (if the CDF cannot expressed analytically, slower numerical methods must be used).
Upvotes: 1