Random number generate in an interval
Mostrar comentarios más antiguos
Hi community,
I would like to generate random number in the follow distribuition: Normal, Gamma and Weibull.
Amount of data required: 5000 numbers at interval [a,b]
At the example, it was used the follow code:
A = linspace(0,1,5000)
sr = size(A)
b = normrnd(27.5,15.73,sr)
plot(b)
Its works, however, the valid interval to generate was [1,54]
Can us help me, please?
Yours faithfully
7 comentarios
William Rose
el 14 de Sept. de 2023
Editada: William Rose
el 14 de Sept. de 2023
[edit: fix spelling errors]
It is not possible to generate random numbers that follow the normal or Weibull or gamma distributions, and limit them to a finite range, because all of those distributions have non-zero probability out to infinity. The latter two go from 0 to +Inf, and the normal from -Inf to +Inf.
If you specify a mean and s.d. for normal, or a shape and a scale for the Weibull and gamma, you can generate random numbers, and then you can clip any values outside your interval of interest. Technically, once you have done this, the remaining numbers do not have the originally specified distribution. In practice, the clipping won't make a difference, if the probability of extreme values was extremely small anyway, compared to the sample size.
I hope this helps.
(27.5 - 1) / 15.73
(54 - 27.5) / 15.73
Less than 2 standard distributions -- the excluded area is non-trivial, and will make a significant difference in normality.
A = linspace(0,1,5000)
sr = size(A)
b = normrnd(27.5,15.73,sr);
[min(b), max(b)]
pd = fitdist(b.','Normal')
btrunc = max(1, min(b, 54));
[min(btrunc), max(btrunc)]
pd2 = fitdist(btrunc .', 'Normal')
The standard distribution of the truncated version is notably different.
William Rose
el 14 de Sept. de 2023
@Walter Roberson is exactly right that if
and
, then clipping to [1,54] leaves a distinctly non-normal distribution. I was not sure from your orignal post if the code
b = normrnd(27.5,15.73,sr)
was just a separate example, or if it represented the mean and SD which you actually want. You will need to calculate a bit to translate a desired mean and SD to shape and scale parameters for gamma (easy) or Weibull (less easy).
Bruno Luong
el 14 de Sept. de 2023
Editada: Bruno Luong
el 14 de Sept. de 2023
Careful
b = normrnd(27.5,15.73,sr);
btrunc = max(1, min(b, 54));
This clipping does NOT generate truncated normal distribution which is conditional probability (rejection) and NOT clipping probability
rng(100)
pd = truncate(makedist('Normal',27.5,15.73),1,54);
x = 1:.01:54;
figure
plot(x,pdf(pd,x))
hold on
b = normrnd(27.5,15.73,[1 500000]);
btrunc = max(1, min(b, 54));
histogram(btrunc,'Normalization','pdf')
figure
plot(x,pdf(pd,x),'LineWidth',5)
hold on
breject = b(b>=1 & b<=54);
histogram(breject,'Normalization','pdf')
Right, different interpretations of what "clipping" means in the context.
format long g
b = normrnd(27.5,15.73,[1 500000]);
out_of_range = (b < 1) | (b > 54);
mean(out_of_range) * 100
so over 9% of the samples are outside of the target range
Walter Roberson
el 22 de Sept. de 2023
It works! Thank you very much
Respuestas (2)
the cyclist
el 14 de Sept. de 2023
Editada: the cyclist
el 14 de Sept. de 2023
0 votos
A normal distribution, by definition, has support from negative infinity to positive infinity. You cannot have both a normal distribution and a finite range.
You can use the truncate function to create a truncated normal distribution object, and draw values from that. (See the second example on that page.) Is that what you want?
Bruno Luong
el 14 de Sept. de 2023
Editada: Bruno Luong
el 15 de Sept. de 2023
The file TruncatedGaussian.m is from this page https://www.mathworks.com/matlabcentral/fileexchange/23832-truncated-gaussian?s_tid=srchtitle
This doesn't need stat toolbox.
sigma = 15.73;
mu = 27.5;
interval = [1 54];
n = [1, 5000000];
X = TruncatedGaussian(-sigma,interval-mu,n) + mu;
histogram(X)
Categorías
Más información sobre Random Number Generation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


