Page 1 of 1

Sampling From Discrete Distribution

Posted: Tue Aug 09, 2016 8:47 am
by Dennis
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))

Re: Sampling From Discrete Distribution

Posted: Tue Aug 09, 2016 8:54 am
by EViews Gareth
It isn't clear what you're trying to achieve. Where does this fit in with a model?

Re: Sampling From Discrete Distribution

Posted: Tue Aug 09, 2016 11:30 am
by Dennis
I have a Model in which a random variable takes on a discrete set of possible outcomes. In the example that I posted, there are five possible outcomes (8,10,15,17,22). The probabilities of these five outcomes are respectively (.2, .1, .25, .17, and .28). I'll call these outcome and probability vectors y and p respectively.

In doing a stochastic simulation of my Model, I need a function that can be called to draw from this (p,y) distribution. Also, I want the flexibility to use different (p,y) in different versions of the model, so I need to be able call the function with (p,y) as argument of the function.

Put differently, I need a function in Eviews that provides the counterpart to the function described on this link:
http://www.solver.com/risk-solver-help/ ... siDiscrete

PS In the example in my previous post, I put the five outcomes in: vector yval=@fill(8,10,15,17,22)
I put the cumulative probabilities in: vector cump = @fill(.2,.3,.55,.72,1)

Re: Sampling From Discrete Distribution

Posted: Tue Aug 09, 2016 11:51 am
by EViews Gareth
This is actually pretty tricky. You can't use an existing function that will do it. You'll have to come up with an @recode statement that generates the final value, then add that to the model as an identity.

Re: Sampling From Discrete Distribution

Posted: Tue Aug 09, 2016 12:01 pm
by Dennis
I thought that might be the case. Thanks Gareth for the quick turnaround on this.

Re: Sampling From Discrete Distribution

Posted: Tue Aug 09, 2016 3:55 pm
by EViews Glenn
I think that adding something along these lines to your model will work

Code: Select all

z = rnd @identity w = 8 * (z<.2) + 10 * (z<.3 and z>=.2) + 15 * (z<.55 and z>=.3) + 17 * (z<.72 and z>=.55) + 22 * (z>=.72)

Re: Sampling From Discrete Distribution

Posted: Thu Aug 11, 2016 11:38 am
by Dennis
Looks good. Thanks Glenn.