4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ are you familiar with Do and Compile? or did I misunderstand the question? $\endgroup$ – acl Oct 29 '12 at 11:18
  • $\begingroup$ 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 ? $\endgroup$ – Luap Nalehw Oct 29 '12 at 11:49
  • 2
    $\begingroup$ Table? $\endgroup$ – Sjoerd C. de Vries Oct 29 '12 at 12:36
  • 1
    $\begingroup$ For the "write to file" portion of your task, there's Put[]... $\endgroup$ – J. M. will be back soon Oct 29 '12 at 12:59
  • 1
    $\begingroup$ 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 mathematica.stackexchange.com/a/2008/57 and mathematica.stackexchange.com/a/209/57 $\endgroup$ – Sjoerd C. de Vries Oct 29 '12 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.