generate random numbers with exact mean and std

81 views (last 30 days)
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 Comment
Oleg Komarov
Oleg Komarov on 8 Oct 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.

Sign in to comment.

Answers (4)

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

Sign in to comment.


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

Sign in to comment.


Chibuzo Nnonyelu
Chibuzo Nnonyelu on 15 Sep 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 Comment
Walter Roberson
Walter Roberson on 15 Sep 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.

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!