Sampling from a truncated normal distribution
Moderators: EViews Gareth, EViews Jason, EViews Moderator
Sampling from a truncated normal distribution
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
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 Developer
 Posts: 2567
 Joined: Wed Oct 15, 2008 9:17 am
Re: Sampling from a truncated normal distribution
Shouldn't you be able to sample from the normal, then use @recode to NA out values that you want to truncate?
Re: Sampling from a truncated normal distribution
Hi Glenn, thanks for the reply. Unfortunately, that is called censoring. Truncation is a different approach that requires the modification of density functions.

 Nonnormality and collinearity are NOT problems!
 Posts: 3224
 Joined: Wed Sep 17, 2008 2:25 pm
Re: Sampling from a truncated normal distribution
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.
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 Developer
 Posts: 2567
 Joined: Wed Oct 15, 2008 9:17 am
Re: Sampling from a truncated normal distribution
To censor, you'd replace the NAs with the censoring value.
Re: Sampling from a truncated normal distribution
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 selfexplanatory.
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 selfexplanatory.
 Attachments

 truncated.prg
 Encrypted code that generates random draws from truncated normal distribution using the rejection method.
 (3.36 KiB) Downloaded 202 times
Re: Sampling from a truncated normal distribution
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.
Re: Sampling from a truncated normal distribution
the code has been very helpful. is it possible to have this as a new feature in eviews or at least as an addin?

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11587
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Sampling from a truncated normal distribution
There is no reason why you cannot add that program to your Addins menu.
Follow us on Twitter @IHSEViews
Re: Sampling from a truncated normal distribution
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?

 Posts: 6
 Joined: Mon May 30, 2016 2:17 am
Re: probability of given interval
hi;
i have series of observations and want to calculate the probability of a given interval, for example: [0.025; +0.025]. thanks
i have series of observations and want to calculate the probability of a given interval, for example: [0.025; +0.025]. thanks

 Nonnormality and collinearity are NOT problems!
 Posts: 3224
 Joined: Wed Sep 17, 2008 2:25 pm
Re: Sampling from a truncated normal distribution
@mean(x>.25 and x<.25)

 Posts: 6
 Joined: Mon May 30, 2016 2:17 am
Re: Sampling from a truncated normal distribution
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
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

 Nonnormality and collinearity are NOT problems!
 Posts: 3224
 Joined: Wed Sep 17, 2008 2:25 pm
Re: Sampling from a truncated normal distribution
Show exactly what you did, and perhaps attach an EViews workfile.
Who is online
Users browsing this forum: No registered users and 5 guests