Sampling From Discrete Distribution
Posted: Tue Aug 09, 2016 8:47 am
In need a function that can be used in a model simulation to sample from a discrete distribution. I have written code that draws a uniform random number that determines which discrete outcome is chosen, but I don't know how to incorporate this code into a model. The code is given below. Vector yval contains the discrete outcomes and vector cump contains the associated cumulative probabilities. The last six lines need to be incorporated into the Model being simulated. Can you advise how this can be incorporated in a model.
' The following 7 statements need be executed only once for a given model
vector cump = @fill(.2,.3,.55,.72,1)
vector yval=@fill(8,10,15,17,22)
vector(1,1) z
scalar n=@rows(cump)
vector zz=@ones(n,1)
vector v4=@subextract(cump, 1, 1, n-1, 1)
vector cumlag=@vcat(z,v4)
' The statements below need to be executed for each model simulation draw
genr r=rnd
zz=zz*r
vector x1=@ceil(cump-zz)
vector x2=-@ceil(cumlag-zz)+1
vector x3=@emult(x1,x2)
scalar draw=@csum(@emult(yval,x3))
' The following 7 statements need be executed only once for a given model
vector cump = @fill(.2,.3,.55,.72,1)
vector yval=@fill(8,10,15,17,22)
vector(1,1) z
scalar n=@rows(cump)
vector zz=@ones(n,1)
vector v4=@subextract(cump, 1, 1, n-1, 1)
vector cumlag=@vcat(z,v4)
' The statements below need to be executed for each model simulation draw
genr r=rnd
zz=zz*r
vector x1=@ceil(cump-zz)
vector x2=-@ceil(cumlag-zz)+1
vector x3=@emult(x1,x2)
scalar draw=@csum(@emult(yval,x3))