http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179
MATLAB Central Newsreader  Generation of Correlated Data
Feed for thread: Generation of Correlated Data
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 12 Aug 2008 08:39:01 +0000
Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#448667
Deva MDP
Can some one tell how to generate two random data sets <br>
with known correlation, (say Corr. Coef. = 0.5)

Tue, 12 Aug 2008 15:26:01 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#448705
Roger Stafford
"Deva MDP" <devasiri@gmail.com> wrote in message <g7ri75$8hr<br>
$1@fred.mathworks.com>...<br>
> Can some one tell how to generate two random data sets <br>
> with known correlation, (say Corr. Coef. = 0.5)<br>
<br>
Generate any two mutually independent variables, x and y, whose means are <br>
0 and variances 1. For example x = randn(n,1) and y = randn(n,1). Then we <br>
have<br>
<br>
E(x) = E(y) = 0,<br>
E(x^2) = E(y^2) = 1<br>
E(x*y) = E(x)*E(y) = 0<br>
<br>
Then construct z = x + k*y where k is some constant yet to be determined. <br>
Now we have<br>
<br>
E(z) = 0,<br>
E(z^2) = E(x^2) + 2*k*E(x*y) + k^2*E(y^2) = 1 + k^2,<br>
E(x*z) = E(x^2) + k*E(x*y) = 1<br>
<br>
Hence<br>
<br>
corr(x,z) = E(x*z)/sqrt(E(x^2)*E(z^2)) = 1/sqrt(1+k^2)<br>
<br>
Therefore solve for the value of k that gives you the desired correlation <br>
coefficient. For corr = .5 it would be k = sqrt(3).<br>
<br>
Are you sure you don't have further requirements? You have left a lot of <br>
freedom in your description here.<br>
<br>
Roger Stafford

Mon, 15 Sep 2008 07:13:02 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#600331
Deva MDP
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <g88cr6$d72$1@fred.mathworks.com>...<br>
> "Deva MDP" <devasiri@gmail.com> wrote in message <g885b5$m3v<br>
> $1@fred.mathworks.com>...<br>
> > "Deva MDP" <devasiri@gmail.com> wrote in message<br>
> > <g7ri75$8hr$1@fred.mathworks.com>...<br>
> > > Can some one tell how to generate two random data sets <br>
> > > with known correlation, (say Corr. Coef. = 0.5)<br>
> > devasiri@gmail.com<br>
> > <br>
> > Dear Friend,<br>
> > <br>
> > Thank you for the support given. I undestood how to generate<br>
> > two data vectors to a required correlatin between them.But<br>
> > my problem is as follows which I couldn't clarify yet.<br>
> > <br>
> > I have generated a correlated random vector with 3 columns<br>
> > for a desired correlaton matrix. Though my work is<br>
> > successful, still I don't know the theory behind this procedure.<br>
> > The procedure adopted is as follows.<br>
> > <br>
> > (1) Generated 3 random vectors with ndependently normally<br>
> > dstributed entries. X=[x1 x2 x3]<br>
> > Corr(X)= Identity matrix approximately.<br>
> > <br>
> > (2) Then x is transformed in to Y by Y=X*c , where c=<br>
> > squreroot of G (G s the ultimate correlation matrix of Y)<br>
> > (c is +ve definte matrx)<br>
> > The form of G = (1 g g;g 1 g;g g 1], g is the correlaton<br>
> > between the formed vectors.<br>
> > <br>
> > Thankful If you can kndly let me know the theory behind this<br>
> > procedure.<br>
> > <br>
> > Best regards<br>
> > <br>
> > Devasiri<br>
> <br>
> For the sake of discussion suppose that your three independent normally <br>
> distributed random variables x1, x2, and x3 have mean 0 and variance 1, so <br>
> that correlation and covariance are one and the same. If c is the matrix <br>
> square root of the G you have defined, then the following holds true. The <br>
> covariance matrix of your n by 3 matrix Y = X*c is given by<br>
> <br>
> E{Y'*Y} = E{(X*c)'*(X*c)} = E{c'*X'*X*c}<br>
> = c'*E{X'*X}*c = c'*I*c = c'*c = c*c = G<br>
> <br>
> Here I is the identity matrix for the covariance matrix of X, and c = c' because <br>
> it is symmetric. Thus Y has the desired covariances.<br>
> <br>
> Note that the same would be true for any positive definite G. All you have to <br>
> do is find its matrix square root (using eigenvector methods presumably.)<br>
> <br>
<br>
<br>
> Roger Stafford<br>
> Dear Roger,<br>
<br>
I have already generated the required correlated data sets. <br>
using C=sqrtm(g). y=X*C, Also I undestand what you explaind to me previously. <br>
<br>
But, still I am suffering with the proof of Corr(Y)= G, (here, C'*C=G, C= symmetric, + semidefinite matrix), <br>
I can prove Corr(x)=I (Identity matrix)<br>
since Corr(X*C)= Corr(X)<br>
<br>
<br>
I do not get Corr(Y)=G.<br>
Please help me to prove this<br>
I need this theorey to include it in my methodology.<br>
<br>
<br>
Thank you very much.<br>
Devasiri

Tue, 16 Sep 2008 08:40:04 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#600524
Deva MDP
"Deva MDP" <devasiri@gmail.com> wrote in message <gal1tu$2ha$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <g88cr6$d72$1@fred.mathworks.com>...<br>
> > "Deva MDP" <devasiri@gmail.com> wrote in message <g885b5$m3v<br>
> > $1@fred.mathworks.com>...<br>
> > > "Deva MDP" <devasiri@gmail.com> wrote in message<br>
> > > <g7ri75$8hr$1@fred.mathworks.com>...<br>
> > > > Can some one tell how to generate two random data sets <br>
> > > > with known correlation, (say Corr. Coef. = 0.5)<br>
> > > devasiri@gmail.com<br>
> > > <br>
> > > Dear Friend,<br>
> > > <br>
> > > Thank you for the support given. I undestood how to generate<br>
> > > two data vectors to a required correlatin between them.But<br>
> > > my problem is as follows which I couldn't clarify yet.<br>
> > > <br>
> > > I have generated a correlated random vector with 3 columns<br>
> > > for a desired correlaton matrix. Though my work is<br>
> > > successful, still I don't know the theory behind this procedure.<br>
> > > The procedure adopted is as follows.<br>
> > > <br>
> > > (1) Generated 3 random vectors with ndependently normally<br>
> > > dstributed entries. X=[x1 x2 x3]<br>
> > > Corr(X)= Identity matrix approximately.<br>
> > > <br>
> > > (2) Then x is transformed in to Y by Y=X*c , where c=<br>
> > > squreroot of G (G s the ultimate correlation matrix of Y)<br>
> > > (c is +ve definte matrx)<br>
> > > The form of G = (1 g g;g 1 g;g g 1], g is the correlaton<br>
> > > between the formed vectors.<br>
> > > <br>
> > > Thankful If you can kndly let me know the theory behind this<br>
> > > procedure.<br>
> > > <br>
> > > Best regards<br>
> > > <br>
> > > Devasiri<br>
> > <br>
> > For the sake of discussion suppose that your three independent normally <br>
> > distributed random variables x1, x2, and x3 have mean 0 and variance 1, so <br>
> > that correlation and covariance are one and the same. If c is the matrix <br>
> > square root of the G you have defined, then the following holds true. The <br>
> > covariance matrix of your n by 3 matrix Y = X*c is given by<br>
> > <br>
> > E{Y'*Y} = E{(X*c)'*(X*c)} = E{c'*X'*X*c}<br>
> > = c'*E{X'*X}*c = c'*I*c = c'*c = c*c = G<br>
> > <br>
> > Here I is the identity matrix for the covariance matrix of X, and c = c' because <br>
> > it is symmetric. Thus Y has the desired covariances.<br>
> > <br>
> > Note that the same would be true for any positive definite G. All you have to <br>
> > do is find its matrix square root (using eigenvector methods presumably.)<br>
> > <br>
> <br>
> <br>
> > Roger Stafford<br>
> > Dear Roger,<br>
> <br>
> I have already generated the required correlated data sets. <br>
> using C=sqrtm(g). y=X*C, Also I undestand what you explaind to me previously. <br>
> <br>
> But, still I am suffering with the proof of Corr(Y)= G, (here, C'*C=G, C= symmetric, + semidefinite matrix), <br>
> I can prove Corr(x)=I (Identity matrix)<br>
> since Corr(X*C)= Corr(X)<br>
> <br>
> <br>
> I do not get Corr(Y)=G.<br>
> Please help me to prove this<br>
> I need this theorey to include it in my methodology.<br>
> <br>
> <br>
> Thank you very much.<br>
> Devasiri<br>
>

Sun, 17 Aug 2008 03:19:01 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#510946
Deva MDP
"Deva MDP" <devasiri@gmail.com> wrote in message<br>
<g7ri75$8hr$1@fred.mathworks.com>...<br>
> Can some one tell how to generate two random data sets <br>
> with known correlation, (say Corr. Coef. = 0.5)<br>
devasiri@gmail.com<br>
<br>
Dear Friend,<br>
<br>
Thank you for the support given. I undestood how to generate<br>
two data vectors to a required correlatin between them.But<br>
my problem is as follows which I couldn't clarify yet.<br>
<br>
I have generated a correlated random vector with 3 columns<br>
for a desired correlaton matrix. Though my work is<br>
successful, still I don't know the theory behind this procedure.<br>
The procedure adopted is as follows.<br>
<br>
(1) Generated 3 random vectors with ndependently normally<br>
dstributed entries. X=[x1 x2 x3]<br>
Corr(X)= Identity matrix approximately.<br>
<br>
(2) Then x is transformed in to Y by Y=X*c , where c=<br>
squreroot of G (G s the ultimate correlation matrix of Y)<br>
(c is +ve definte matrx)<br>
The form of G = (1 g g;g 1 g;g g 1], g is the correlaton<br>
between the formed vectors.<br>
<br>
Thankful If you can kndly let me know the theory behind this<br>
procedure.<br>
<br>
<br>
Best regards<br>
<br>
Devasiri

Sun, 17 Aug 2008 05:27:02 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#510952
Roger Stafford
"Deva MDP" <devasiri@gmail.com> wrote in message <g885b5$m3v<br>
$1@fred.mathworks.com>...<br>
> "Deva MDP" <devasiri@gmail.com> wrote in message<br>
> <g7ri75$8hr$1@fred.mathworks.com>...<br>
> > Can some one tell how to generate two random data sets <br>
> > with known correlation, (say Corr. Coef. = 0.5)<br>
> devasiri@gmail.com<br>
> <br>
> Dear Friend,<br>
> <br>
> Thank you for the support given. I undestood how to generate<br>
> two data vectors to a required correlatin between them.But<br>
> my problem is as follows which I couldn't clarify yet.<br>
> <br>
> I have generated a correlated random vector with 3 columns<br>
> for a desired correlaton matrix. Though my work is<br>
> successful, still I don't know the theory behind this procedure.<br>
> The procedure adopted is as follows.<br>
> <br>
> (1) Generated 3 random vectors with ndependently normally<br>
> dstributed entries. X=[x1 x2 x3]<br>
> Corr(X)= Identity matrix approximately.<br>
> <br>
> (2) Then x is transformed in to Y by Y=X*c , where c=<br>
> squreroot of G (G s the ultimate correlation matrix of Y)<br>
> (c is +ve definte matrx)<br>
> The form of G = (1 g g;g 1 g;g g 1], g is the correlaton<br>
> between the formed vectors.<br>
> <br>
> Thankful If you can kndly let me know the theory behind this<br>
> procedure.<br>
> <br>
> Best regards<br>
> <br>
> Devasiri<br>
<br>
For the sake of discussion suppose that your three independent normally <br>
distributed random variables x1, x2, and x3 have mean 0 and variance 1, so <br>
that correlation and covariance are one and the same. If c is the matrix <br>
square root of the G you have defined, then the following holds true. The <br>
covariance matrix of your n by 3 matrix Y = X*c is given by<br>
<br>
E{Y'*Y} = E{(X*c)'*(X*c)} = E{c'*X'*X*c}<br>
= c'*E{X'*X}*c = c'*I*c = c'*c = c*c = G<br>
<br>
Here I is the identity matrix for the covariance matrix of X, and c = c' because <br>
it is symmetric. Thus Y has the desired covariances.<br>
<br>
Note that the same would be true for any positive definite G. All you have to <br>
do is find its matrix square root (using eigenvector methods presumably.)<br>
<br>
Roger Stafford

Sun, 17 Aug 2008 07:26:01 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#510958
Deva MDP
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid><br>
wrote in message <g88cr6$d72$1@fred.mathworks.com>...<br>
> "Deva MDP" <devasiri@gmail.com> wrote in message <g885b5$m3v<br>
> $1@fred.mathworks.com>...<br>
> > "Deva MDP" <devasiri@gmail.com> wrote in message<br>
> > <g7ri75$8hr$1@fred.mathworks.com>...<br>
> > > Can some one tell how to generate two random data sets <br>
> > > with known correlation, (say Corr. Coef. = 0.5)<br>
> > devasiri@gmail.com<br>
> > <br>
> > Dear Friend,<br>
> > <br>
> > Thank you for the support given. I undestood how to generate<br>
> > two data vectors to a required correlatin between them.But<br>
> > my problem is as follows which I couldn't clarify yet.<br>
<br>
<br>
<br>
> > I have generated a correlated random vector with 3 columns<br>
> > for a desired correlaton matrix. Though my work is<br>
> > successful, still I don't know the theory behind this<br>
procedure.<br>
> > The procedure adopted is as follows.<br>
> > <br>
> > (1) Generated 3 random vectors with ndependently normally<br>
> > dstributed entries. X=[x1 x2 x3]<br>
> > Corr(X)= Identity matrix approximately.<br>
> > <br>
> > (2) Then x is transformed in to Y by Y=X*c , where c=<br>
> > squreroot of G (G s the ultimate correlation matrix of Y)<br>
> > (c is +ve definte matrx)<br>
> > The form of G = (1 g g;g 1 g;g g 1], g is the correlaton<br>
> > between the formed vectors.<br>
> > <br>
> > Thankful If you can kndly let me know the theory behind this<br>
> > procedure.<br>
> > <br>
> > Best regards<br>
> > <br>
> > Devasiri<br>
> <br>
> For the sake of discussion suppose that your three<br>
independent normally <br>
> distributed random variables x1, x2, and x3 have mean 0<br>
and variance 1, so <br>
> that correlation and covariance are one and the same. If<br>
c is the matrix <br>
> square root of the G you have defined, then the following<br>
holds true. The <br>
> covariance matrix of your n by 3 matrix Y = X*c is given by<br>
> <br>
> E{Y'*Y} = E{(X*c)'*(X*c)} = E{c'*X'*X*c}<br>
> = c'*E{X'*X}*c = c'*I*c = c'*c = c*c = G<br>
> <br>
> Here I is the identity matrix for the covariance matrix of<br>
X, and c = c' because <br>
> it is symmetric. Thus Y has the desired covariances.<br>
> <br>
> Note that the same would be true for any positive<br>
definite G. All you have to <br>
> do is find its matrix square root (using eigenvector<br>
methods presumably.)<br>
> <br>
> Roger Stafford<br>
> <br>
Dear Roger,<br>
<br>
Thank you very much for your support.<br>
I can understand that.But I got Corr(A*Y)=Corr(Y)<br>
As I have the above thinking n my mind, still I find dffcult<br>
to undestand the correlation relationship between X and Y<br>
data sets.<br>
<br>
Regards from<br>
<br>
Devasiri

Sun, 17 Aug 2008 17:47:31 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#510994
Greg Heath
On Aug 17, 1:27=A0am, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> "Deva MDP" <devas...@gmail.com> wrote in message <g885b5$m3v<br>
><br>
> $...@fred.mathworks.com>...<br>
><br>
><br>
><br>
><br>
><br>
> > "Deva MDP" <devas...@gmail.com> wrote in message<br>
> > <g7ri75$8h...@fred.mathworks.com>...<br>
> > > Can some one tell how to generate two random data sets<br>
> > > with known correlation, (say Corr. Coef. =3D 0.5)<br>
> > devas...@gmail.com<br>
><br>
> > Dear Friend,<br>
><br>
> > Thank you for the support given. I undestood how to generate<br>
> > two data vectors to a required correlatin between them.But<br>
> > my problem is as follows which I couldn't clarify yet.<br>
><br>
> > I have generated a correlated random vector with 3 columns<br>
> > for a desired correlaton matrix. Though my work is<br>
> > successful, still I don't know the theory behind this procedure.<br>
> > The procedure adopted is as follows.<br>
><br>
> > (1) Generated 3 random vectors with ndependently normally<br>
> > dstributed entries. X=3D[x1 x2 x3]<br>
> > Corr(X)=3D Identity matrix approximately.<br>
><br>
> > (2) Then x is transformed in to Y by Y=3DX*c , where c=3D<br>
> > squreroot of G (G s the ultimate correlation matrix of Y)<br>
> > (c is +ve definte matrx)<br>
> > The form of G =3D (1 g g;g 1 g;g g 1], g is the correlaton<br>
> > between the formed vectors.<br>
><br>
> > Thankful If you can kndly let me know the theory behind this<br>
> > procedure.<br>
><br>
> > Best regards<br>
><br>
> > Devasiri<br>
><br>
> =A0 For the sake of discussion suppose that your three independent normal=<br>
ly<br>
> distributed random variables x1, x2, and x3 have mean 0 and variance 1, s=<br>
o<br>
> that correlation and covariance are one and the same. =A0If c is the matr=<br>
ix<br>
> square root of the G you have defined, then the following holds true. =A0=<br>
The<br>
> covariance matrix of your n by 3 matrix Y =3D X*c is given by<br>
><br>
> =A0E{Y'*Y} =3D E{(X*c)'*(X*c)} =3D E{c'*X'*X*c}<br>
> =A0=3D c'*E{X'*X}*c =3D c'*I*c =3D c'*c =3D c*c =3D G<br>
><br>
> Here I is the identity matrix for the covariance matrix of X, and c =3D c=<br>
' because<br>
> it is symmetric. =A0Thus Y has the desired covariances.<br>
><br>
> =A0 Note that the same would be true for any positive definite G. =A0All =<br>
you have to<br>
> do is find its matrix square root (using eigenvector methods presumably.)<br>
<br>
The matrix square root is obviously not unique. Why do you prefer<br>
using eigenvector methods instead of SQRTM?<br>
<br>
Hope this helps.<br>
<br>
Greg

Sun, 17 Aug 2008 21:48:01 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#575322
Roger Stafford
Greg Heath <heath@alumni.brown.edu> wrote in message <br>
<193fc75736224faab8ee<br>
abf2fa117d70@p25g2000hsf.googlegroups.com>...<br>
<br>
> The matrix square root is obviously not unique.<br>
<br>
The square root of a positive definite matrix plays essentially the same role of <br>
uniqueness in matrix theory as does the square root of a positive scalar number <br>
in the real continuum. To partially quote Mathworks, it "is the principal square <br>
root of the matrix ... the unique square root for which every eigenvalue has <br>
nonnegative real part."<br>
<br>
> Why do you prefer using eigenvector methods instead of SQRTM?<br>
<br>
As you must be fully aware, Greg, the matlab function 'sqrtm' does in fact use <br>
eigenvector methods. It is an excellent way of finding a matrix principal square <br>
root. It is not a question of "instead of".<br>
<br>
Roger Stafford

Mon, 29 Apr 2013 20:12:12 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#903428
Chetan
Hi Roger,<br>
Is it possible to generalize this to data sets with any mean, variance and covariance? I did some calculations at home, but I find that the correlation(X,Z) does not change much from the original correlation of (X,Y) . Infact the correlation as a function of K declines asymptotically. I could be totally wrong of course  but if you provide me an email address, I can send you my file  if you care to look that is.<br>
Ty<br>
Chet<br>
<br>
<br>
<br>
"Roger Stafford" wrote in message <g7sa29$gnv$1@fred.mathworks.com>...<br>
> "Deva MDP" <devasiri@gmail.com> wrote in message <g7ri75$8hr<br>
> $1@fred.mathworks.com>...<br>
> > Can some one tell how to generate two random data sets <br>
> > with known correlation, (say Corr. Coef. = 0.5)<br>
> <br>
> Generate any two mutually independent variables, x and y, whose means are <br>
> 0 and variances 1. For example x = randn(n,1) and y = randn(n,1). Then we <br>
> have<br>
> <br>
> E(x) = E(y) = 0,<br>
> E(x^2) = E(y^2) = 1<br>
> E(x*y) = E(x)*E(y) = 0<br>
> <br>
> Then construct z = x + k*y where k is some constant yet to be determined. <br>
> Now we have<br>
> <br>
> E(z) = 0,<br>
> E(z^2) = E(x^2) + 2*k*E(x*y) + k^2*E(y^2) = 1 + k^2,<br>
> E(x*z) = E(x^2) + k*E(x*y) = 1<br>
> <br>
> Hence<br>
> <br>
> corr(x,z) = E(x*z)/sqrt(E(x^2)*E(z^2)) = 1/sqrt(1+k^2)<br>
> <br>
> Therefore solve for the value of k that gives you the desired correlation <br>
> coefficient. For corr = .5 it would be k = sqrt(3).<br>
> <br>
> Are you sure you don't have further requirements? You have left a lot of <br>
> freedom in your description here.<br>
> <br>
> Roger Stafford<br>
>

Tue, 30 Apr 2013 14:01:52 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#903475
Tom Lane
> Is it possible to generalize this to data sets with any mean, variance and <br>
> covariance?<br>
<br>
I didn't see the original posting, but it sounds like you want to specify an <br>
exact mean and covariance. This is not the same as generating from a <br>
theoretical mean and covariance (which is what I believe most people ought <br>
to want to do most of the time), because the result is less random than it <br>
should be. But the question comes up from time to time. Here's an example:<br>
<br>
% Desired mean and covariance<br>
Mu = [10 15.5 9.99];<br>
Sigma = [5 3 0;3 4 1;0 4 10];<br>
<br>
% Create tooperfect sample with zero mean and identity covariance<br>
x = randn(100,length(Mu));<br>
z = zscore(x); % if you don't have Statistics Toolbox, subtract mean and <br>
divide by std<br>
c = chol(cov(z));<br>
z = z/c;<br>
cov(z)<br>
mean(z)<br>
<br>
% Apply desired mean and covariance<br>
z = z*chol(Sigma);<br>
z = bsxfun(@plus,Mu,z);<br>
cov(z)<br>
mean(z)<br>
<br>
 Tom

Mon, 06 May 2013 13:34:07 +0000
Re: Generation of Correlated Data
http://www.mathworks.com/matlabcentral/newsreader/view_thread/174179#903811
Chetan
Thanks Tom,<br>
I'm going to try this and get back to you.<br>
Best<br>
Chet<br>
<br>
<br>
"Tom Lane" <tlane@mathworks.com> wrote in message <kloisg$bkc$1@newscl01ah.mathworks.com>...<br>
> > Is it possible to generalize this to data sets with any mean, variance and <br>
> > covariance?<br>
> <br>
> I didn't see the original posting, but it sounds like you want to specify an <br>
> exact mean and covariance. This is not the same as generating from a <br>
> theoretical mean and covariance (which is what I believe most people ought <br>
> to want to do most of the time), because the result is less random than it <br>
> should be. But the question comes up from time to time. Here's an example:<br>
> <br>
> % Desired mean and covariance<br>
> Mu = [10 15.5 9.99];<br>
> Sigma = [5 3 0;3 4 1;0 4 10];<br>
> <br>
> % Create tooperfect sample with zero mean and identity covariance<br>
> x = randn(100,length(Mu));<br>
> z = zscore(x); % if you don't have Statistics Toolbox, subtract mean and <br>
> divide by std<br>
> c = chol(cov(z));<br>
> z = z/c;<br>
> cov(z)<br>
> mean(z)<br>
> <br>
> % Apply desired mean and covariance<br>
> z = z*chol(Sigma);<br>
> z = bsxfun(@plus,Mu,z);<br>
> cov(z)<br>
> mean(z)<br>
> <br>
>  Tom