Reputation: 55
How do I generate a new list from random numbers that have the same mean and standard deviation from the original list?
I tried newlist = mean(list) + std(list)*randn(100,1); which I found on the Matlab website but it was generating slightly different std and mean from the original since the new mean is always bigger.
Upvotes: 3
Views: 592
Reputation: 86
Sorry to answer a question with a question, but I have to ask...
Why do you need to ensure that the mean of your vector of random numbers is precisely same as your original vector? In a similar vein, why does the standard deviation need to be precisely the same?
If I run a monte carlo simulation or some such, I'm trying to discover what might happen. If you adjust your random numbers so that the mean is precisely X and the standard deviation is exactly Y you're decreasing the chance that your simulation will contain an extreme event. In turn, this mean's that its less likely that something is going to go wrong.
These types of transforms are fine as an academic exercise, however, I'd have grave concerns about employing this type of method in the real world.
Upvotes: 0
Reputation: 121127
The trick is to generate random numbers with mean 0 and std dev 1. We do this by generating any old random numbers, then fixing the mean and standard deviation afterwards.
% generate your random numbers
r = randn(100, 1);
% scale the variance
r2 = r / std(r);
% shift the mean
r3 = r2 - mean(r2);
%check your answer
abs(mean(r3)) < sqrt(eps)
abs(std(r3) - 1) < sqrt(eps)
Now newlist = mean(list) + std(list) * r3
should give you what you need.
Upvotes: 2