Sampling From Discrete Distribution

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

Dennis
Posts: 16
Joined: Fri Dec 16, 2011 8:11 am

Sampling From Discrete Distribution

Postby Dennis » 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))

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13318
Joined: Tue Sep 16, 2008 5:38 pm

Re: Sampling From Discrete Distribution

Postby EViews Gareth » Tue Aug 09, 2016 8:54 am

It isn't clear what you're trying to achieve. Where does this fit in with a model?
Follow us on Twitter @IHSEViews

Dennis
Posts: 16
Joined: Fri Dec 16, 2011 8:11 am

Re: Sampling From Discrete Distribution

Postby Dennis » Tue Aug 09, 2016 11:30 am

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)

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13318
Joined: Tue Sep 16, 2008 5:38 pm

Re: Sampling From Discrete Distribution

Postby EViews Gareth » Tue Aug 09, 2016 11:51 am

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.
Follow us on Twitter @IHSEViews

Dennis
Posts: 16
Joined: Fri Dec 16, 2011 8:11 am

Re: Sampling From Discrete Distribution

Postby Dennis » Tue Aug 09, 2016 12:01 pm

I thought that might be the case. Thanks Gareth for the quick turnaround on this.

EViews Glenn
EViews Developer
Posts: 2672
Joined: Wed Oct 15, 2008 9:17 am

Re: Sampling From Discrete Distribution

Postby EViews Glenn » Tue Aug 09, 2016 3:55 pm

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)

Dennis
Posts: 16
Joined: Fri Dec 16, 2011 8:11 am

Re: Sampling From Discrete Distribution

Postby Dennis » Thu Aug 11, 2016 11:38 am

Looks good. Thanks Glenn.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 41 guests