Perfect random numbers - How can I refine an initial dataset from randn to be perfrectly normal?
Show older comments
Hi, I would like to create "perfect" random numbers. By perfect I mean a set of random values that has
a mean of 0.0000, a std of 1.0000, a skewness of 0.0000, and a kurtosis of 3.0000
while each of numbers created with randn comes very close, it is not perfect in that sense. How can I further refine the initial dataset from randn to be "perfectly" normal?
Thanks
1 Comment
Matt Kindig
on 21 May 2013
I'm not you can do this with a finite number of random values.
Answers (5)
Teja Muppirala
on 22 May 2013
If you want to generate a set of numbers that "looks random", has 0 mean, 1 variance, 0 skewness, 3 kurtosis, you can start with an initial dataset using RANDN and then kind of iteratively perturb it until you get what you want.
rng(0)
R = randn(1000,1);
R0 = R;
R_prev= 0;
opts = optimset('TolFun',0,'Display','Off');
smallNumber = 1e-12;
while norm(R-R_prev)/norm(R_prev) > smallNumber;
R_prev = R;
R = R-mean(R); %Fix Mean
R = R/std(R); %Fix Std. Dev
% Fix Skewness
x = fzero( @(x) skewness( R .* exp(x*R) ) ,0 ,opts);
R = R .* exp(x*R);
% Fix Kurtosis
x = fzero( @(x) kurtosis(sign(R) .* (abs(R).^x))-3 ,1 ,opts);
R = sign(R) .* (abs(R).^x);
end
figure, plot(R0,'b');
hold on;
plot(R,'r');
legend({'Original', 'Slightly Modified'});
format long
disp('Original:')
[mean(R0) std(R0) skewness(R0) kurtosis(R0)]
disp('Modified:')
[mean(R) std(R) skewness(R) kurtosis(R)]
5 Comments
Sven
on 22 May 2013
Teja Muppirala
on 23 May 2013
To generate a set of points with standard deviation of 1.2, a skew of 0.5, and an (excess) kurtosis of 0.4
You would change these three lines:
R = R/std(R); %Fix Std. Dev
x = fzero( @(x) skewness( R .* exp(x*R) ) ,0 ,opts);
x = fzero( @(x) kurtosis(sign(R) .* (abs(R).^x))-3 ,1 ,opts);
to
R = R/std(R)*1.2; %Fix Std. Dev % Fix Skewness
x = fzero( @(x) skewness( R .* exp(x*R) )-0.5 ,0 ,opts);
x = fzero( @(x) kurtosis(sign(R) .* (abs(R).^x))-3.4 ,1 ,opts);
Sven
on 24 May 2013
Sven
on 12 Jun 2013
süleymand demir
on 16 Nov 2020
Hi Teja,
I wanted to use this code to genarate some datas. But I could not generate data for some conditions.
For example: a mean of 0.0000, a std of 1.0000, a skewness of 1.0000, and a kurtosis of 4.0000.
Could you help me please.
Walter Roberson
on 21 May 2013
1 vote
You cannot do this without constraining two of the values to be non-random (values that fix the mean and std.)
The Normal Distribution is defined by an infinite process; if you use a finite number of values, you can only approximate the Normal Distribution.
3 Comments
Jan
on 22 May 2013
I even do not think, that any set of random numbers can have exactly the mean 0.0, most of all if the std is fixed to exactly 1.0 also. Therefore solving this problem requires to redefine "random" exactly at first.
Walter Roberson
on 22 May 2013
Roger has a FEX contribution for a "random" set of numbers with a fixed sum. That technique can be used with the fixed sum being 0. Call that T. Then T/std(T) will have a sum of 0 and a std of 1.
I have difficulties accepting that the numbers are truly "random" if they have a fixed sum, but if Roger finds it meaningful then I consider that perhaps there is something lacking in my understanding.
I think, Roger applies a specific definition of "random" here: The maximal achievable indepedence between the values might be a solid base.
Considering the total interconnectedness of the properties in the universe, random is only vaguely defined at all. Einstein, Podolsky and Rosen hesitated to accept, that the spins of two conjugated photons can be oriented randomly although the sum of the spins will vanish after the measurement.
Iain
on 22 May 2013
1 vote
It isn't possible.
Just looking at the mean & std, if you do manage to get the mean to be exactly 0 and the std to be exactly 1, how can you possibly add another sample without either changing the mean (and retaining the std), or changing the std (and retaining the mean). Skewness and kurtosis just make it even harder.
Sven
on 22 May 2013
0 votes
3 Comments
In that case, why do you say randn isn't close enough? Using a sufficiently large sample set, the sample moments I get are well within 3 or 4 decimal places,
x=randn(1,1e7);
moment1=mean(x),
moment2=std(x),
moment3=mean(x.^3),
moment4=mean(x.^4),
moment1 =
-2.2094e-04
moment2 =
0.9998
moment3 =
-0.0012
moment4 =
2.9995
Sven
on 24 May 2013
Whether the number of draws is large or small is irrelevant. The statistical mean and other moments are what they are independently of how you sample them. My test with large draws was only to measure the statistical moments better and show you that they have the values they should.
If you're saying that you want the empirical moments to deviate from the statistical moments by a limited amount for a certain sample size, then you have no control over that, I'm afraid. The empirical moments are random variables and will have a variance that depends on your sample size and the statistical variance. You cannot change the empirical moments' variances without first changing the statistical distribution of the things you're sampling.
Iain
on 14 Jun 2013
Hmmm:
Take the inverse cdf, at regular intervals.
rnds = icdf('norm',(1/samples):(1/samples):(1-1/samples), 0 , 1);
These numbers are not random, but they, as a set, have a mean of 0, std of close to 1, a skewness of 0, and a kurtosis of "close to" 3.
Generate a random order:
[~, I] = sort(randn(size(rnds)));
randoms = rnds(I);
Categories
Find more on Hypothesis Tests 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!