# st: RE: RE: How to sample from a Multinomial distribution

 From "Nick Cox" <[email protected]> To <[email protected]> Subject st: RE: RE: How to sample from a Multinomial distribution Date Wed, 27 Feb 2008 22:15:19 -0000

```The last line of code should be

gen y = cond(uniform() < 0.5, 1, cond(uniform() < 0.8, 2, 3))

but as said it's not a solution to the problem stated.

Nick Cox

I don't see that you need any program to do this.

Suppose the probabilities are 0.5 0.3 0.2.

gen random  = uniform()
gen y = cond(random < 0.5, 1, cond(random < 0.8, 2, 3))

What -cond()- is doing may be clearer laid out like this:

cond(random < 0.5, 1,
cond(random < 0.8, 2,
3))

If the random number is less than 0.5, assign 1;
Else if it is less than 0.8, assign 2;
Else assign 3.

A pitfall that I have fallen into myself is to tempted to
rewrite this as

gen y = cond(uniform() < 0.5, cond(uniform() < 0.8, 2, 3))

That is not equivalent, as each call of -uniform()- is distinct.

Nick
[email protected]

[email protected]

I have to generate data for a sample, which has three groups (G1, G2 and
G3). In this respect,  the probability distribution for the G1, G2 and
G3
follows a multinomial distribution with parameter vector P = (pG1 ,pG2
,pG3 ).

I have used -genbinomial- to generate the first two groups, and then the
last one is calculate by subtraction. Since this is not an elegant way
to
do that, is the any routine that does in Stata the task above mentioned
?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```

• References: