Normal distribution for a given range of numbers.

100 vues (au cours des 30 derniers jours)
Neje
Neje le 16 Avr 2018
I have a range in which my parameter can vary for example, 0.38 to 0.5. I need to produce in between points such that the data will be normally distributed. I have thought of the followig way.
% code
xmin=0.38
xmax=0.5
n=20
x=xmin+randn(1,n)*(xmax-xmin);
But randn(1,n) gives me random numbers from more than 1 as well. Which does not serve the purpose of putting the range. Then my xmax becomes more than 0.5. How to achieve this for normal distribution and the given range? Thank you :)

Réponse acceptée

Birdman
Birdman le 16 Avr 2018

Use rand, instead of randn:

 x=xmin+rand(1,n)*(xmax-xmin);
  7 commentaires
Mostafa Nakhaei
Mostafa Nakhaei le 20 Fév 2020
If you draw you will see that x=xmin+rand(1,n)*(xmax-xmin) is far from being normal.
try this:
p = 6;
xmin=0.38;
xmax=0.5;
n=10000;
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
hist(X,50)
you will see this is closer to normal distributation as exlained by John in the next comment.
Aman Kumar
Aman Kumar le 16 Fév 2022
what is the role of p in this code.

Connectez-vous pour commenter.

Plus de réponses (2)

John D'Errico
John D'Errico le 17 Avr 2018

A normal distribution does not have limits. In theory it is possible to see generated points that lie all the way out to infinity, or at least arbitrarily close to that point.

You might consider a truncated normal distribution. I know there is at least one such utility to be found on the MATLAB Central File Exchange. You can do the search as easily as can I. A truncated normal distribution is not that difficult to sample from either. The stats toolbox would make it fairly easy.

Just as easy is to make use of the central limit theorem. If you want a fairly normal looking distribution of points, that all lie within limits of xmax and xmin, do this:

p = 6;
xmin=0.38;
xmax=0.5;
n=10000;
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
hist(X,50)

Don't go overboard and make p too large however. Large values of p will see you generate very few points near those limits. As well, large values of p will take more memory and time to generate the sample.

If p is smaller, 3 for example, the distribution will look a bit less smooth, but you will more likely get points near the endpoints.

p = 3;

If you have the stats toolbox, then betarnd will work too. So we could generete samples with a distribution that looks fairly like a truncated Normal.

A = 3;
B = 3;
X = betarnd(A,B,1,n);
X = xmin + (xmax - xmin)*betarnd(A,B,1,n);
hist(X,50)

As you make A and B larger, the distribution will start looking vaguely more normally distributed, but the number of events that occur near your limits will start to drop again.

So it all depends on exactly how you want that distribution to behave. But as I have shown, there are lots of ways to do this.

  7 commentaires
Aman Kumar
Aman Kumar le 16 Fév 2022
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
Please Define the all variable used in this line
Aman Kumar
Aman Kumar le 16 Fév 2022
How you get the value of p variable in this code

Connectez-vous pour commenter.


siranjeevi gurumani
siranjeevi gurumani le 28 Juin 2022
Modifié(e) : siranjeevi gurumani le 28 Juin 2022
Use rescaling to get a random normal distribution between the desired range if mean and standard deviation is not a concern
xmin=0.38;
xmax=0.5;
n=2000;
temp = normrnd(0,1,1,n);%generating a standard normal distribution
x=xmin+(xmax-xmin)*((temp-min(temp))/(max(temp)-min(temp)));%rescaling the range
hist(x)

Community Treasure Hunt

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

Start Hunting!

Translated by