generate random numbers with exact mean and std

76 ビュー (過去 30 日間)
Seldeeno
Seldeeno 2014 年 10 月 8 日
コメント済み: Dominic Martin 2021 年 2 月 11 日
Hello,
I want to generate training data with exact mean and standard deviation. I have some examples that i want to illustrate to students in lab. However, using basic random numbers from normal distribution does not guarantee the exact mean and std. Any help plz?
Thanks
  1 件のコメント
Oleg Komarov
Oleg Komarov 2014 年 10 月 8 日
Mean and std are exact in the limit, so just draw progressively more numbers and show how the empirical mean and std approximate that of the distribution.

サインインしてコメントする。

回答 (4 件)

Michael Haderlein
Michael Haderlein 2014 年 10 月 8 日
I agree with the two earlier posts that exact mean/std values and randomness are contradictory. However, the data seems to be meant for training purpose, so maybe something like this is actually requested:
>> x=randn(100,1);
>> [mean(x),std(x)]
ans =
-0.0706 0.9141 %random mean/std
>> x=x-mean(x);
>> [mean(x),std(x)]
ans =
0.0000 0.9141 %exact mean, random std
>> x=x/std(x);
>> [mean(x),std(x)]
ans =
0.0000 1.0000 %exact mean/std
So this is still random data, but the mean is exactly zero and the standard deviation is exactly 1 (yes, I know, after so many digits there will be nonzero values, but that's numerics).
Best regards,
Michael
  3 件のコメント
Michael Haderlein
Michael Haderlein 2014 年 10 月 9 日
I don't know the purpose of the data set and the exercise. I could imagine that there is useful application such as the students are meant to calculate the mean and the std and are then asked to go on calculating something else based on these two values. Then some smooth intermediate results of mean/std are nice to have.
I don't think anyone is going to say that mean/std/variance are similar/equal/... to the limits.
Dominic Martin
Dominic Martin 2021 年 2 月 11 日
I have the same needs as OP. I'm in need of a multimodal training dataset to use with Kernel Density Estimation.

サインインしてコメントする。


John D'Errico
John D'Errico 2014 年 10 月 8 日
編集済み: John D'Errico 2014 年 10 月 8 日
(Sounds like the teacher needs a refresher course in stats.) Those samples are not truly random IF they have the exact mean & standard deviation!
If you truly want to illustrate something, explain to your students what it means to come from a random sampling, that the exact mean and standard deviation will only be achieved with an infinite number of samples. The last time I checked, an infinite number of samples will be difficult to obtain, or at least it will take a while. So show how as the sample size increases, the expected deviation decreases.
If you are still unconvinced, think about what it would mean if you always got the exact mean and standard deviation from all samplings. Take a sample of size 1. Yep, ONE single, solitary sample. What could you infer if the sample mean was always exactly the same as the population mean? And is it possible to have a non-zero standard deviation with a sample of size 1?
So, now suppose you drew a sample of size 2. 2 points from the given distribution. Hey, at least we can get the desired mean and standard deviation. But the fact is, this would EXACTLY specify the two points if you know their mean and standard deviation in advance. So a "random" sample of size 2, at least random according to your definition, would obviously not be random at all.
My point is, a sample from a distribution will only asymptotically approach the population distribution parameters as the sample size increases towards the infinite.
So teach your students what a population mean and standard deviation are and how to compute the mean & std from a sample, AND the difference between those two animals. Teach them why in general the two sets of parameters will never be identically the same.

Iain
Iain 2014 年 10 月 8 日
A sequence of equal numbers of -1 and 1 has a mean of 0 and a population standard deviation of precisely 1.
That sequence of -1 and 1 can be predictable, or not.
John is partially correct in his assertion that you need an "infinite" sample size to get the true standard deviation & mean. However, as illustrated by the dataset [-1 1] and [-1 -1 1 1], it is most certainly not impossible for a subset of the "infinite" set of values to give the same mean and standard deviation.
  3 件のコメント
Iain
Iain 2014 年 10 月 8 日
That depends on your definition of random.
The result of flipping a coin can be either heads or tails. If you represent heads by +1 and tails by -1, then my sequence is a genuine result of a purely random process (just cherry-picked to make the point).
Michael Haderlein
Michael Haderlein 2014 年 10 月 9 日
編集済み: Michael Haderlein 2014 年 10 月 9 日
Ok, the order is random. You're right ;-)

サインインしてコメントする。


Chibuzo Nnonyelu
Chibuzo Nnonyelu 2015 年 9 月 15 日
x = mu + sigma*randn(sizeof); % for normally distributed random numbers
OR
x = random('normal', mu, sigma, row, column); % they basically do the same thing.
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 9 月 15 日
Those do not satisfy the requirements of having an exact mean and standard deviation. Instead, those have the properties of being statistically the right mean and standard deviation -- of tending to that mean and standard deviation more and more closely as the number of trials increases to infinity.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by