Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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
Table? – Sjoerd C. de Vries Oct 29 '12 at 12:36
For the "write to file" portion of your task, there's Put[]... – J. M. Oct 29 '12 at 12:59
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 and – Sjoerd C. de Vries Oct 29 '12 at 14:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.