3

follow question:

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

Alex
  • 31
  • 1

4 Answers4

2

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]

enter image description here

David Keith
  • 4,340
  • 1
  • 12
  • 28
2

You may use RandomFunction and WienerProcess.

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

Mathematica graphics

ListLinePlot[paths]

Mathematica graphics

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

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143
1

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)
Alan
  • 13,686
  • 19
  • 38
1

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"}]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148