# 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 = 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 '17 at 18:05
• Thanks to all of you Your advice is realy helpful – user49420 Jun 12 '17 at 18:01
• @Alex please do not use answer for comments. Take a tour to learn how to express your gratitude. – Kuba Jun 12 '17 at 18:21

As Alan mentions, the fundamental problem is that the memoization assignments assign values to all but p 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 = 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 == 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"}] 