Normal distribution for a given range of numbers.

129 ビュー (過去 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 Center および File ExchangeExploration and Visualization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by