Performance of random number generator

3 Ansichten (letzte 30 Tage)
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia am 4 Okt. 2015
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 Kommentare
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia am 5 Okt. 2015
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 am 7 Okt. 2015
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 5 Okt. 2015
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 Kommentare
Sebastián Acevedo Mejia
Sebastián Acevedo Mejia am 5 Okt. 2015
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 am 5 Okt. 2015
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by