Draw random number from a truncated distribution with upper bound

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?

 Risposta accettata

https://de.mathworks.com/matlabcentral/fileexchange/53180-truncated-normal-generator
Best wishes
Torsten.

5 Commenti

Thanks you! But I already tried this function but I dont know what I am doing wrong.
Some of the drawn numbers are still greater than the upper bound. This is the normal code that I am using:
trunc = trandn((l - m) ./ s ,(u - m) ./ s) ;
where s is the varying standard deviation.
I construct a matrix
truncM = vec2mat(trunc,5);
and then "generate" the truncated random number by
r = (s .* truncM) + m ;
Do you know what I might be doing wrong?
Thank you in advance!
What are the sizes of l, u, m and s ?
Best wishes
Torsten.
Leo
Leo il 23 Ott 2017
Modificato: Leo il 23 Ott 2017
  • u = upper bound is 605000 x 5
  • l = the value of the lower bound is set to infinite and is also a 605000 x 5, to match the sizes
  • m = is a scalar and is equal to zero and s = 1x5, but to match the sizes, I use repmat to obtain a 605000x5 matrix
Thanks!
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.
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!

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

Leo
il 23 Ott 2017

Commentato:

Leo
il 24 Ott 2017

Community Treasure Hunt

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

Start Hunting!

Translated by