4

The following is a simple example of a much larger simulation that I want to run

Define some processes

norTheta[mu_, sigma_] := Random[NormalDistribution[mu, sigma]];
norPi[mu_, sigma_] := Random[NormalDistribution[mu, sigma]];

thetaNext[thetaNow_] := 
thetaNow + (-lambdaTheta*(thetaNow - thetaBar)*deltaT + 
sigmaTheta*norTheta[0, 1]*Sqrt[deltaT]);
piNext[piNow_, thetaNow_] := 
piNow + (-lambdaPi*(piNow - thetaNow)*deltaT + 
sigmaPi*norPi[0, 1]*Sqrt[deltaT]);

Then define some parameters

lambdaTheta = 0.07;
thetaBar = 0.02;     
sigmaTheta = 0.012; 
lambdaPi = 1.0;        
sigmaPi = 0.0125;

Then simulate the system

steps = 252;
T = 50; 
deltaT = 1/steps; // N 
Maturity = T*steps;

process = Transpose[NestList[{  piNext[#[[1]], #[[2]]],
thetaNext[#[[2]]]} &,{0.02, 0.02}, Maturity]];

I have two questions: (1) what is the best MMA of repeating the above 10,000 at each loop saving the output 'process' for use later; (2) what is the fastest way of doing this?

Many thanks, Paul

Luap Nalehw
  • 846
  • 5
  • 12
  • are you familiar with Do and Compile? or did I misunderstand the question? – acl Oct 29 '12 at 11:18
  • I'm not sure how Do / Compile helps here. For example, in matlab or C / java, I would loop over the above 'simulate system' block of code saving the output after T = 50 into a cell array. I would then finish by writing this cell array to a file. Is this the way one should do it in MMA ? – Luap Nalehw Oct 29 '12 at 11:49
  • 2
  • @Sjoerd , You suggest to reap/sow the simulated processes for each run, then write the resulting table to a text file? – Luap Nalehw Oct 29 '12 at 12:53
  • 1
    For the "write to file" portion of your task, there's Put[]... – J. M.'s missing motivation Oct 29 '12 at 12:59
  • @J.M. That's useful. I could write the data from each simulation run to seperate text files names systematically for latter reading. Thanks. – Luap Nalehw Oct 29 '12 at 13:47
  • Yes, but it's advisable to open the file handles only once. See http://mathematica.stackexchange.com/a/13309/745 – Ajasja Oct 29 '12 at 13:52
  • @LuapNalehw you could do that, yes. when I get time I'll write a short answer (if nobody has done anything better by then) – acl Oct 29 '12 at 14:03
  • 1
    No, not at all. I suggested to collect 10000 runs in a table generated by Table. I even linked to its reference page. Don't know where in my answer I suggested to use Reap/Sow. The table itself could be saved with DumpSave. See also http://mathematica.stackexchange.com/a/2008/57 and http://mathematica.stackexchange.com/a/209/57 – Sjoerd C. de Vries Oct 29 '12 at 14:11

0 Answers0