Sampling a random normal to create a vector of truncated normals

2 views (last 30 days)
Is there a way in matlab as there is in Fortran to draw samples from normrnd with specified mean and sd and reject any that are less than a specified lower bound or greater than an upper bound. In Fortran you can simply set a zero flag for each random that is outside the limits, draw another random and if it passes the limits test add it to a vector. How can I do this in Matlab?

Accepted Answer

the cyclist
the cyclist on 5 Sep 2018
Less elegant than truncate, but can be used in core MATLAB ...
% Desired number of samples
N = 1000;
% Draw some extra, because of the rejection. Need to adjust this to be sure to get enough extra. Will depend on how severe the truncation is.
N_extra = 100;
% Draw from normal
x = randn(N+N_extra,1);
% Reject values that lie outside truncated region
x(x<-2 | x>2) = [];
% Trim to the desired number of samples
x = x(1:N);
  1 Comment
Nick
Nick on 5 Sep 2018
Thank you. I will give this a try. I have programmed something similar. But this may be more efficient.

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 5 Sep 2018
If you have the Statistics and Machine Learning Toolbox, check out the truncate function.
  2 Comments
Nick
Nick on 5 Sep 2018
I appreciate the suggestion. Unfortunately I only have Matlab 2010 and the Statistics toolbox, which does not have the truncate function. But it seems that it should be easy to simply reject random samples that fail a criterion and draw another candidate random to evaluate and accept or reject as appropriate. But it's not obvious to me how to do this in Matlab.

Sign in to comment.

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!