## 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

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: 2598
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?

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

### 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.

startz
Non-normality and collinearity are NOT problems!
Posts: 3400
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.

Code: Select all

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

EViews Glenn
EViews Developer
Posts: 2598
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.

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

### 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 self-explanatory.
Attachments
truncated.prg
Encrypted code that generates random draws from truncated normal distribution using the rejection method.

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

### 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.

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

### 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 add-in?

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

### Re: Sampling from a truncated normal distribution

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

### 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?

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

### Re: Sampling from a truncated normal distribution

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

startz
Non-normality and collinearity are NOT problems!
Posts: 3400
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

startz
Non-normality and collinearity are NOT problems!
Posts: 3400
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.