Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Generation of Correlated Data
Replies: 10   Last Post: May 6, 2013 9:34 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Tom Lane

Posts: 854
Registered: 12/7/04
Re: Generation of Correlated Data
Posted: Apr 30, 2013 10:01 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

> Is it possible to generalize this to data sets with any mean, variance and
> covariance?


I didn't see the original posting, but it sounds like you want to specify an
exact mean and covariance. This is not the same as generating from a
theoretical mean and covariance (which is what I believe most people ought
to want to do most of the time), because the result is less random than it
should be. But the question comes up from time to time. Here's an example:

% Desired mean and covariance
Mu = [10 15.5 9.99];
Sigma = [5 -3 0;-3 4 1;0 4 10];

% Create too-perfect sample with zero mean and identity covariance
x = randn(100,length(Mu));
z = zscore(x); % if you don't have Statistics Toolbox, subtract mean and
divide by std
c = chol(cov(z));
z = z/c;
cov(z)
mean(z)

% Apply desired mean and covariance
z = z*chol(Sigma);
z = bsxfun(@plus,Mu,z);
cov(z)
mean(z)

-- Tom




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.