/* Stochastic Trinomial Sampler */ new; cls; p={0.3,0.6,0.1}; nr=10; print/rz ptrisampler(p,nr); /* ** ptrisampler.txt - Stochastic Trinomial 0-1-2 Sampler. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets stochastic Trinomial 0-1-2 index numbers in a very easy way. ** ** Format: x=ptrisampler(p,nr); ** ** Input: p vector, 3 x 1 of probability vector(p[1] for 2, p[2] for 1, p[3] for 0) ** ** nr scalar, number of rows ** ** ** Output: x vector, nr x 1 of resulting 0-1 index vector ** */ proc ptrisampler(p,nr); local x; p=cumsumc(p); x=rndu(nr,1); x=2*(x.<=p[1])+1*(x.>p[1] .and x.<=p[2]); retp(x); endp;