Sampling from a truncated normal distribution

For questions regarding programming in the EViews programming language.

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

tconnors
Posts: 4
Joined: Mon Nov 15, 2010 2:04 am

Sampling from a truncated normal distribution

Postby tconnors » Mon Nov 15, 2010 2:29 am

Hi all,

I am trying to draw random numbers from a truncated normal distribution. This is an essential task for my master's thesis and I am really stuck. I could not find any functions or commands in EViews to generate random draws from a truncated normal distribution. Is there a way to do that in EViews (version 7.1) or should I look elsewhere for help? Any suggestions will be appreciated.

Thanks,
Taylor

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

Re: Sampling from a truncated normal distribution

Postby EViews Glenn » Mon Nov 15, 2010 11:42 am

Shouldn't you be able to sample from the normal, then use @recode to NA out values that you want to truncate?

tconnors
Posts: 4
Joined: Mon Nov 15, 2010 2:04 am

Re: Sampling from a truncated normal distribution

Postby tconnors » Tue Nov 16, 2010 3:34 am

Hi Glenn, thanks for the reply. Unfortunately, that is called censoring. Truncation is a different approach that requires the modification of density functions.

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Sampling from a truncated normal distribution

Postby startz » Tue Nov 16, 2010 8:26 am

Are you sure? I think Glenn is right because what you're doing is effectively rejection sampling. You might give it a try and draw the histogram to see what works.

Alternatively, something like the following works for drawing from a truncated normal with upper truncation point zero. Assume the series mu and sig are means and standard deviations.

Code: Select all

series u = rnd
series f = @cnorm(-mu/sig)
series z = @qnorm(f)
series truncatedDraw = mu+z*sig

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

Re: Sampling from a truncated normal distribution

Postby EViews Glenn » Tue Nov 16, 2010 10:26 am

To censor, you'd replace the NAs with the censoring value.

trubador
Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

Re: Sampling from a truncated normal distribution

Postby trubador » Tue Nov 16, 2010 2:09 pm

I agree that Glenn's suggestion is effective, since the accepted draws would have the desired distribution. However, this process will become fairly inefficient if the probability of the normal draw falling in the given interval is fairly low. The general approach is called Rejection method and is similar to what Startz has suggested. In order to generate draws from a distribution with truncated density, it is necessary to have a comparison distribution from which draws can be generated more easily and which is nonzero wherever the original distribution is nonzero.

Attached you'll find an old program that I have written long time ago for this purpose. There is still more work to be done to complete and optimize the code, so I prefer to encrypt it. However, it works efficiently and does the job using a truncated logistic as the comparison distribution. Moreover, I have added some flavors from EViews 7 and it should be pretty self-explanatory.
Attachments
truncated.prg
Encrypted code that generates random draws from truncated normal distribution using the rejection method.
(3.36 KiB) Downloaded 452 times

tconnors
Posts: 4
Joined: Mon Nov 15, 2010 2:04 am

Re: Sampling from a truncated normal distribution

Postby tconnors » Thu Nov 18, 2010 12:15 pm

I'd like to thank each and every one of you for the help you have provided. It's more than i could have hoped for. :D

tconnors
Posts: 4
Joined: Mon Nov 15, 2010 2:04 am

Re: Sampling from a truncated normal distribution

Postby tconnors » Mon Nov 29, 2010 1:53 am

the code has been very helpful. is it possible to have this as a new feature in eviews or at least as an add-in?

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

Re: Sampling from a truncated normal distribution

Postby EViews Gareth » Mon Nov 29, 2010 10:16 am

There is no reason why you cannot add that program to your Add-ins menu.
Follow us on Twitter @IHSEViews

fredmancr
Posts: 2
Joined: Thu May 22, 2014 2:11 pm

Re: Sampling from a truncated normal distribution

Postby fredmancr » Thu May 29, 2014 3:49 pm

The program placed by trubador seems to work pretty well. I would like to use it in the context of another program but I am not sure if that can be done by using the "run" comand. I such case, I asume I should provide the arguments the program uses... but, since it is encrypted I don't know the order in which they should be provided. Any help?

trubador
Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

Re: Sampling from a truncated normal distribution

Postby trubador » Tue Jun 03, 2014 7:37 am


louizabadis
Posts: 6
Joined: Mon May 30, 2016 2:17 am

Re: probability of given interval

Postby louizabadis » Mon May 30, 2016 2:32 am

hi;
i have series of observations and want to calculate the probability of a given interval, for example: [-0.025; +0.025]. thanks

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Sampling from a truncated normal distribution

Postby startz » Mon May 30, 2016 7:11 am

@mean(x>-.25 and x<.25)

louizabadis
Posts: 6
Joined: Mon May 30, 2016 2:17 am

Re: Sampling from a truncated normal distribution

Postby louizabadis » Mon May 30, 2016 2:30 pm

Hello;
thank you for ansewer, but it doesen't work. The message given by eviews is: "scalar sent to function which operates on a matrix"
Regards

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Sampling from a truncated normal distribution

Postby startz » Mon May 30, 2016 3:06 pm

Show exactly what you did, and perhaps attach an EViews workfile.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 42 guests