This question is really a specific problem and a methodological one concerning MMA best practices. I want to simulate a system of stochastic processes. If this were a geometric Brownian motion or random walk then the recursive (univariate) nature of the problem means FoldList
or Nestlist
is easy and neat to implement.
However, what if I wanted to simulate an mean-reverting process which itself had a mean-reverting long run mean? Now, I cannot work out the MMA efficient way of doing it other than within a For
loop.
For example, the following code simulates an OU process for inflation with a stochastic central tendency :
Clear["Global`*"];
norθ[mu_, sigma_] := Random[NormalDistribution[mu, sigma]];
norπ[mu_, sigma_] := Random[NormalDistribution[mu, sigma]];
deltaθt[θnow_] :=-λθ*(θnow-θbar)*deltaT + σθ*norθ[0, 1]*Sqrt[deltaT]
deltaπt[πnow_] := -λπ*(πnow - θnow)*deltaT + σπ*norπ[0, 1]*Sqrt[deltaT]
λθ = 0.07;
σθ = 1.2;
θbar = 2;
θnow = 2;
λπ = 1.;
σπ = 1.25;
πnow = 2;
deltaT = 1/12;
noYear = 100*12;
Process = Reap[For[i = 1, i < noYear, i++,
Sow[{i, πnow, θnow}];
πnow = deltaπt[πnow] + πnow;
θnow = deltaθt[θnow] + θnow;
]][[2, 1]];
inflation = Process[[All, {1, 2}]];
target = Process[[All, {1, 3}]];
ListLinePlot[{inflation, target}]
FoldList
? Also note that you can useRandomVariate
to get a list of numbers distributed according to a desired distribution. $\endgroup$