generate random numbers with exact mean and std

81 visualizaciones (últimos 30 días)
Seldeeno
Seldeeno el 8 de Oct. de 2014
Comentada: Dominic Martin el 11 de Feb. de 2021
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 comentario
Oleg Komarov
Oleg Komarov el 8 de Oct. de 2014
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.

Iniciar sesión para comentar.

Respuestas (4)

Michael Haderlein
Michael Haderlein el 8 de Oct. de 2014
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 comentarios
Michael Haderlein
Michael Haderlein el 9 de Oct. de 2014
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 el 11 de Feb. de 2021
I have the same needs as OP. I'm in need of a multimodal training dataset to use with Kernel Density Estimation.

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 8 de Oct. de 2014
Editada: John D'Errico el 8 de Oct. de 2014
(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 el 8 de Oct. de 2014
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 comentarios
Iain
Iain el 8 de Oct. de 2014
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 el 9 de Oct. de 2014
Editada: Michael Haderlein el 9 de Oct. de 2014
Ok, the order is random. You're right ;-)

Iniciar sesión para comentar.


Chibuzo Nnonyelu
Chibuzo Nnonyelu el 15 de Sept. de 2015
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 comentario
Walter Roberson
Walter Roberson el 15 de Sept. de 2015
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by