Draw random number from a truncated distribution with upper bound

13 views (last 30 days)
Hello everyone,
how can I draw a random number from a truncated normal distribution with mean 0 and varying var (sigma^2)? The upper bound of the truncation value varies within my time series. What I did first is to specify the probability distribution for the different variance values (1xn matrix). After that, I draw a truncated normal distribution for each "row"/ new upper bound. Since I have a huge time series, this step takes forever.
for i = 1:size(pd,2);
t(:,i) = truncate(pd(size(pd,1),i),-inf,upperBound(:,i)));
end
Whenever, this step would be executed I would draw a random number out of each truncated distribution, using
r = random(t(:,1),1);
However, I am not sure if that will work, though.
Is there a way to predefine the matrix for the specification of t? A normal zero/nan etc. matrix doesnt work. And how can I speed up the process to draw from a truncated normal distribution with varying upper bound values?

Accepted Answer

Torsten
Torsten on 23 Oct 2017
https://de.mathworks.com/matlabcentral/fileexchange/53180-truncated-normal-generator
Best wishes
Torsten.
  5 Comments
Torsten
Torsten on 24 Oct 2017
u=10;
s=1;
m=3;
U=normcdf(u);
n=100;
X=rand(n,1);
Y=m+s*norminv(X*U);
produces n random numbers taken from the normal distribution N(m,s^2) truncated at the upper bound u.
Can you take it from here ?
Best wishes
Torsten.
Leo
Leo on 24 Oct 2017
Thank you for your help. I finally got trandn to work properly. I added some for loops. Your 2nd suggestion did also work fine. Thanks a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!