There are two areas for optimization that I see here.
The first, if possible, is to generate all your random data in advance and then access it with an incrementing index, e.g. list[[i++]].
The second is to partially evaluate the definitions of thetaNext and piNext for a given set of parameters.
A note: Random has been deprecated for some time now and may produce inferior results. You should be using RandomReal/RandomInteger in version 7 or RandomVariate in version 8.
Update
Here is a cleaner implementation of my recommendations. Should this be inapplicable my original code is visible in the edit history of this post.
Given your definitions and parameters:
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]);
lambdaTheta = 0.07; sigmaTheta = 1.2; thetaBar = 2; lambdaPi = 1.0;
sigmaPi = 1.25; deltaT = 1/12;
steps = 15000;
T = 5;
deltaT = 1/steps; // N
Maturity = T*steps;
We can reduce the core of your NestList function (for these specific parameters) as follows:
func =
Block[{norTheta, norPi, Part},
Function @@ FullSimplify @ {
{piNext[#[[1]], #[[2]]], thetaNext[#[[2]]]} /.
{norPi[0, 1] -> #2[[1]], norTheta[0, 1] -> #2[[2]]}
}
]
{0.916667 #1[[1]] + 0.0833333 #1[[2]] + 0.360844 #2[[1]], 0.0116667 + 0.994167 #1[[2]] + 0.34641 #2[[2]]} &
We will use the second argument of this function to insert the random data, which we create with:
piList = RandomReal[NormalDistribution[0, 1], Maturity];
thetaList = RandomReal[NormalDistribution[0, 1], Maturity];
rands = {piList, thetaList}\[Transpose];
(A shorter form exists for this specific data but I am trying to retain some generality.)
And which we use FoldList to provide:
FoldList[func, {2, 2}, rands]\[Transpose] // Timing // First
0.219
By comparison your code takes 4.368 seconds on my machine with the same parameters.
simulateRun[[1, 3]]after evaluating the above code. There is something wrong in your function callthetaNext[#[[1]], #[[2]]]. You have defined it as a single argument function before hand. – PlatoManiac Nov 14 '12 at 11:18list[[i++]]) that should be somewhat faster, for what it's worth. – Mr.Wizard♦ Nov 14 '12 at 12:45