Page 1 of 2
Sampling from a truncated normal distribution
Posted: Mon Nov 15, 2010 2:29 am
by tconnors
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
Re: Sampling from a truncated normal distribution
Posted: Mon Nov 15, 2010 11:42 am
by EViews Glenn
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
Posted: Tue Nov 16, 2010 3:34 am
by tconnors
Hi Glenn, thanks for the reply. Unfortunately, that is called censoring. Truncation is a different approach that requires the modification of density functions.
Re: Sampling from a truncated normal distribution
Posted: Tue Nov 16, 2010 8:26 am
by startz
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
Re: Sampling from a truncated normal distribution
Posted: Tue Nov 16, 2010 10:26 am
by EViews Glenn
To censor, you'd replace the NAs with the censoring value.
Re: Sampling from a truncated normal distribution
Posted: Tue Nov 16, 2010 2:09 pm
by trubador
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.
Re: Sampling from a truncated normal distribution
Posted: Thu Nov 18, 2010 12:15 pm
by tconnors
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
Re: Sampling from a truncated normal distribution
Posted: Mon Nov 29, 2010 1:53 am
by tconnors
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?
Re: Sampling from a truncated normal distribution
Posted: Mon Nov 29, 2010 10:16 am
by EViews Gareth
There is no reason why you cannot add that program to your Add-ins menu.
Re: Sampling from a truncated normal distribution
Posted: Thu May 29, 2014 3:49 pm
by fredmancr
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?
Re: Sampling from a truncated normal distribution
Posted: Tue Jun 03, 2014 7:37 am
by trubador
Re: probability of given interval
Posted: Mon May 30, 2016 2:32 am
by louizabadis
hi;
i have series of observations and want to calculate the probability of a given interval, for example: [-0.025; +0.025]. thanks
Re: Sampling from a truncated normal distribution
Posted: Mon May 30, 2016 7:11 am
by startz
@mean(x>-.25 and x<.25)
Re: Sampling from a truncated normal distribution
Posted: Mon May 30, 2016 2:30 pm
by louizabadis
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
Re: Sampling from a truncated normal distribution
Posted: Mon May 30, 2016 3:06 pm
by startz
Show exactly what you did, and perhaps attach an EViews workfile.