Monte Carlo or how to calculate the model several times

How can I calculate this model several times automatically.

Clear[P];
P[t_] := P[t] = P[t - 1] + RandomVariate[NormalDistribution[0, RP]];
P[0] = 1;
ListLinePlot[Table[P[t], {t, 1, 20}]]


I used Table[Table[P[t], {t, 1, 10}], {i, 1, 10}] to calculate this 10 times, but everytime I had the same result.

Thanks

• Do you require the use of Table? Note that once you memoize it, you'll of course keep getting the same result.
– Alan
Jun 9, 2017 at 18:05
– Kuba
Jun 12, 2017 at 18:21

As Alan mentions, the fundamental problem is that the memoization assignments assign values to all but p[0] in the first run. When this is eliminated, the OP's method works:

Clear[p];

rp = 1;

p[t_] := p[t - 1] + RandomVariate[NormalDistribution[0, rp]];

p[0] = 1;

out = Table[p[t], {10}, {t, 1, 10}];

ListLinePlot[out]


You may use RandomFunction and WienerProcess.

rp = 1;
paths = RandomFunction[WienerProcess[0, rp], {1, 20, 1}, 10]


ListLinePlot[paths]


You may find the Random Processes guide in the documentation useful.

Hope this helps.

Here is one way, closest to your approach:

rp = 1;
dist = NormalDistribution[0, rp];
Table[
RecurrenceTable[{a[t + 1] == a[t] + RandomVariate[dist], a[0] == 1},
a, {t, 1, 20}],
10]


Here is another way:

shocks = RandomVariate[dist, {10, 20}];
1 + (Accumulate /@ shocks)


You can use NestList

f[n_, rp_] := NestList[# + RandomVariate[NormalDistribution[0, rp]] &, 1, n]


For example:

Manipulate[
ListPlot[Table[f[10, rp], n], Joined -> True,
PlotRange -> {-10, 10}], {n, {10, 20}}, {rp, 1, 2,
Appearance -> "Labeled"}]