2

I am triying to generate realizations of a Gaussian random process using the KL expansion. For that, I need to multiply an eigenvector for an eigenvalue and a random variable.

I have tried

realizationNumber = 500;
evecRand = 0*ConstantArray[1, {longitudeOfEigA, longitudeOfEigA}];
For[i = 1, i <= 2, i++,
  For[j = 1, j <= 4, j++;
   evecRand[[j]] = 
    eval[[j]]^0.5*RandomVariate[NormalDistribution[]]*evec[[j]]]];

My result is

evecRand[[7]]
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0}

However, if I write

eval[[j]]^0.5*RandomVariate[NormalDistribution[]]*evec[[j]] 

I get

{0.0950823, 0.0945212, 0.0924617, 0.0889367, 0.0840018, 0.0777355, \
0.0702368, 0.0616249, 0.0520361, 0.0416224, 0.030549, 0.0189913, \
0.00713256, -0.00483922, -0.0167343, -0.0283641, -0.0395443, \
-0.0500977, -0.0598569, -0.0686673, -0.0763893, -0.0829003, \
-0.0880973, -0.0918978, -0.0942416, -0.0950915, -0.0944341, \
-0.0922798, -0.0886626, -0.0836401, -0.0772917, -0.0697182, \
-0.0610395, -0.0513933, -0.0409323, -0.0298226, -0.0182401, \
-0.00636851, 0.00560405, 0.0174878, 0.0290943, 0.0402396, 0.0507471, \
0.0604502, 0.069195, 0.076843, 0.0832729, 0.0883828, 0.0920918, \
0.0943409, 0.0950946, 0.0943409, 0.0920918, 0.0883828, 0.0832729, \
0.076843, 0.069195, 0.0604502, 0.0507471, 0.0402396, 0.0290943, \
0.0174878, 0.00560405, -0.00636851, -0.0182401, -0.0298226, \
-0.0409323, -0.0513933, -0.0610395, -0.0697182, -0.0772917, \
-0.0836401, -0.0886626, -0.0922798, -0.0944341, -0.0950915, \
-0.0942416, -0.0918978, -0.0880973, -0.0829003, -0.0763893, \
-0.0686673, -0.0598569, -0.0500977, -0.0395443, -0.0283641, \
-0.0167343, -0.00483922, 0.00713256, 0.0189913, 0.030549, 0.0416224, \
0.0520361, 0.0616249, 0.0702368, 0.0777355, 0.0840018, 0.0889367, \
0.0924617, 0.0945212, 0.0950823}

Which is what I am looking for. How can I save these vectors to evecRand?

slow_learner
  • 233
  • 1
  • 9

2 Answers2

3

Here is another way to accomplish what is in the OP's self-answer:

evecRand = 
  Sqrt[eval]*RandomVariate[NormalDistribution[], Length@evec]*evec;

Appendix

Test data:

{eval, evec} = 
  Eigensystem[# . Transpose[#] &@RandomReal[1, {101, 101}]];
Michael E2
  • 235,386
  • 17
  • 334
  • 747
1

In the end, I have been able to write

evecRand = 0*ConstantArray[1, {longitudeOfEigA}];
For[i = 0, i &lt; realizationNumber, i++,
  For[j = 0, j &lt; longitudeOfEigA, j++;
   evecRand[[j]] = 
    eval[[j]]^0.5*RandomVariate[NormalDistribution[]]*evec[[j]]]];

This somehow provides me with what I want, a matrix of size 101*101.

slow_learner
  • 233
  • 1
  • 9
  • Summer reading: https://mathematica.stackexchange.com/questions/134609/why-should-i-avoid-the-for-loop-in-mathematica – Michael E2 Jun 11 '21 at 14:34
  • The body of the loop does not depend on i, so what's the point of For[i = 0, i < realizationNumber, i++,...? – Michael E2 Jun 11 '21 at 14:40
  • I am doing this to calculate one realization of a random process, now that I know how to make this I am going to save evecRand as a member of a matrix of size realizationNumber, or at least I hope I know how to do it – slow_learner Jun 11 '21 at 14:43