Difference between rng('shuffle') and NO rng for estimating with MLE

I am trying to estimate a 3-parameter weibul distribution with random numbers. But I dont understand the difference between rng('shuffle') and NO rng('shuffle'), I mean deleting the line. Both lines estimates random numbers in my code. Can someone tell me the difference?
And if you can give me any advice for my code for estimating a 3-parameter weibul estimation, I would appreciate that.
clear all
n = 1000;
t0 = 0.5;
b_A = 1:5;
T_A = 1:5;
LowerBound= [0 0 0];
rng('shuffle');
data = zeros(n,length(b_A),length(T_A));
params = zeros(length(b_A),length(T_A));
data2P = zeros(n,length(b_A),length(T_A));
params2p = zeros(length(b_A),4,length(T_A));
for k= T_A
for i= b_A
data(:,i,k) = wblrnd(i,k, [n,1]) + t0;
data2P(:,i,k) = wblrnd(b_A(i),T_A(k), [n,1]);
start = [i k t0];
custompdf = @(x,a,b,c) (x>c).*(b/a).*(((x-c)/a).^(b-1)).*exp(-((x-c)/a).^b);
opt = statset('MaxIter',1e5,'MaxFunEvals',1e5,'FunValCheck','off');
params(i,1:3,k) = mle(data(:,i,k),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',LowerBound,'UpperBound',[Inf Inf min(data(:,i,k))])
params(i,4,k) = i;
params(i,5,k) = k;
params(i,6,k) = t0;
params2p(i,1:2,k) = wblfit(data2P(:,i,k));
params2p(i,3,k) = i;
params2p(i,4,k) = k;
end
end

1 Commento

Please do not delete your question. If you feel this is a beginner question: there are many beginners, so be nice to them. They might come across your question and learn from it.
I made a capture so people can revert your edit if you attempt to vandalize your question.

Accedi per commentare.

 Risposta accettata

If you start a fresh instance of MATLAB, and generate a sequence of pseudorandom numbers, it will always be the same sequence. (Try it!)
If you do not use the
rng('shuffle')
then you will get the numbers from that sequence. If you do use it, then instead of traveling along that predetermined sequence of numbers, you will leap to a different point on that sequence, based on the time when you make the call to the function.

8 Commenti

Hello,
thank you for your answer. But didnt get it.
So when I want to estimate my given parameter (b,T,t0) with MLE with whole random numbers, what should I prefer?
Just for you, that you can understand what the weibul is and what I am doing:
In order to determine the reliability of a product, service life tests are often carried out in practice. Statistical distribution functions can be used to describe the random failure behaviour of components or systems. The unknown parameters of the lifetime distributions are estimated from the existing failure times.
The Weibul distribution is a model with which a distribution function can be described mathematically.
The distribution function can be fully represented by the 2- or 3-parametric Weibull distribution.
To fit the Weibull distribution to data and find parameter estimates you use MLE. The Weibull distribution uses these parameters:
  • T is scale parameter ( on the x-axis)
  • b is the shape parameter
  • t0 is a parameter where the function starts. ( with the 2 parameter distribution, t0=0).
  • For generating the sample data, we use a size n. Like n=10 or 100 or 1000.
What I described above is also described in the documentation here. There is also a lot more information in the documentation. In a forum like this, I don't think I can really give you a much better explanation.
I am sympathetic to your desire for simple advice, though. Here is some advice that will not give you the most insight and understanding what your are doing, but may help you practically.
Use
rng('default')
in your code when testing your code for correctness. This ensures that every time you run your code, you get the same pseudorandom numbers while testing, which eliminates a source of variability in your results, and can help you focus on debugging.
Use
rng('shuffle')
when you run your code to generate your actual results. This way, every time your run your code, you will get a different set of random number, and see the true variability that this has on your results.
But, ideally, you should get a stronger understanding of the random number generation. After all, it is your code, and your results, so you should understand all parts of what your code is doing.
Hi,
Thank you very much, I appreciate it.
I tried with and without the line rng("shuffle"). I tried it three times. Close matlab and tried again.
In both cases, with every run, it shows other estimates. So I still dont know the difference between rng("shuffle") and no rng("shuffle").
What do you mean with sequence?
I don't know if you read any of the documentation I suggested, but this behavior is explained. It is a bit painful to simply try to re-explain what I have linked to in the documentation.
You are generating random numbers. Of course the result will be different in different runs. That is the point of random numbers!
But, to try to be more helpful, I suggest the following experiment. Start a new instance of MATLAB, and then run this command:
rand(3,1)
I expect you will get this result.
ans =
0.8147
0.9058
0.1270
(It will possible you will not, if you have an older version.)
Now run
rand(3,1)
again. You will get
ans =
0.9134
0.6324
0.0975
Now, close MATLAB and open a fresh instance again. And then run
rand(3,1)
again. This time, you will get the same sequence as you did the first time you opened MATLAB.
ans =
0.8147
0.9058
0.1270
This is because MATLAB is drawing from a pseudorandom sequence of values that are generated from the same "seed" every time you start MATLAB.
But if you call
rng('shuffle')
then you break out of that sequence, and you leap into a different point in that sequence, according to whatever the current time happens to be.
You can think of it this way. MATLAB has a very, very, very, very long list of numbers that obey all the properties of random numbers. They are indistinguishable from randomly generated ones. You can either start from the beginning of that list (which is nice, especially for debugging code), or you can hop into an arbitrary point in that list, according to the clock time when you call the function (which is the "shuffle" idea).
Again, all of this is covered in the documentation.
I really read your links. But I didnt get it. My english is also not the best, so thats why I dont understand that much, I am sry. I am a student in Germany and need that for my university.
I am really sorry when you tell me the same thing twice. I understand now what you explaining.
My last question is now, you tell that shuffle depends on the clock time? How is " rng('shuffle') " in relation with the clock time?
Sorry I feel very stupid.
Sorry, I did not mean to make you feel bad.
In the Input Arguments section of the rng documentation, it explains that 'shuffle" ...
--------
"Initializes generator based on the current time, resulting in a different sequence of random numbers after each call to rng."
--------

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by