generate random numbers with exact mean and std

조회 수: 75 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by