Performance of random number generator

조회 수: 3 (최근 30일)
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia 2015년 10월 4일
댓글: Sebastián Acevedo Mejia 2015년 10월 7일
I'm trying to generate a lognormal random number that is truncated, and the only way that I've seen that I can do that is by first fitting the distribution to the data: pd = fitdist(data,'lognormal');
then truncating it: t = truncate(pd,minw,maxw);
and then creating the random numbers with: r = random(t,1,10000);
The problem I have is that I'm doing this for 100 different values of mu-sigma-pairs, and doing 10,000 simulations, and it is taking forever. I started running the code this morning (10 am) in Matlab R2014b in a server and 8 hours later it is still not done.
Before, I was doing this without truncating the values by using R = lognrnd(mu,sigma), and it only took 1.5 to 2 hours to run the exact same code.
What can I do to make it faster? Is there a way to truncate the random number while using lognrnd?
Thanks for your help
  댓글 수: 7
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia 2015년 10월 5일
Hi Kirby,
Thank for this suggestion, I think this might work, I'm gonna give it a try. :)
Thanks
Sebastian
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia 2015년 10월 7일
Kirby,
Thanks a lot, your suggestion worked very well, I just needed to tweak it a bit for my 4-D array.
Thanks for the help
Sebastian

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 10월 5일
total_samples_needed = 10000;
have_samples = [];
while true
num_needed_now = total_samples_needed - length(have_samples);
if num_needed_now <= 0; break; end
current_samples = RANDOMGENERATOR(1,num_needed_now);
current_samples(current_samples < Lower_Bound | current_samples > Upper_bound) = [];
have_samples = horzcat(have_samples, current_samples);
end
If you want to make it more efficient, you can ask it to generate 1.1 (or as appropriate) times num_needed_now and at the end discard any unneeded ones you generated. An appropriate multiplication factor would be 1 divided by the cdf between Lower_Bound and Upper_Bound.
RANDOMGENERATOR would be replaced by the appropriate call for your purposes.
  댓글 수: 2
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia 2015년 10월 5일
Hi Walter,
Thanks for taking the time to help me. I think what you are suggesting would work in some other cases, but in my case the parameters mu and sigma of the distribution change over time so I have to adjust for that. Bu you've given me some idea of how to move forward.
Thanks
Sebastian
Walter Roberson
Walter Roberson 2015년 10월 5일
Just have the current mu and sigma passed to RANDOMGENERATOR . As long as they do not have to change within a run of total_samples_needed there is no problem.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by