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))
Sampling From Discrete Distribution
Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13317
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Sampling From Discrete Distribution
It isn't clear what you're trying to achieve. Where does this fit in with a model?
Follow us on Twitter @IHSEViews
Re: Sampling From Discrete Distribution
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)
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)
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13317
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Sampling From Discrete Distribution
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
Re: Sampling From Discrete Distribution
I thought that might be the case. Thanks Gareth for the quick turnaround on this.
-
- EViews Developer
- Posts: 2672
- Joined: Wed Oct 15, 2008 9:17 am
Re: Sampling From Discrete Distribution
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
Looks good. Thanks Glenn.
Who is online
Users browsing this forum: No registered users and 38 guests