Normal distribution for a given range of numbers.

조회 수: 122 (최근 30일)
Neje
Neje 2018년 4월 16일
편집: siranjeevi gurumani 2022년 6월 28일
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 :)

채택된 답변

Birdman
Birdman 2018년 4월 16일

Use rand, instead of randn:

 x=xmin+rand(1,n)*(xmax-xmin);
  댓글 수: 7
Mostafa Nakhaei
Mostafa Nakhaei 2020년 2월 20일
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 2022년 2월 16일
what is the role of p in this code.

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

추가 답변 (2개)

John D'Errico
John D'Errico 2018년 4월 17일

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
Aman Kumar
Aman Kumar 2022년 2월 16일
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
Please Define the all variable used in this line
Aman Kumar
Aman Kumar 2022년 2월 16일
How you get the value of p variable in this code

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


siranjeevi gurumani
siranjeevi gurumani 2022년 6월 28일
편집: siranjeevi gurumani 2022년 6월 28일
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)

카테고리

Help CenterFile Exchange에서 Exploration and Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by