3
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ Do you require the use of Table? Note that once you memoize it, you'll of course keep getting the same result. $\endgroup$
    – Alan
    Jun 9, 2017 at 18:05
  • $\begingroup$ Thanks to all of you Your advice is realy helpful $\endgroup$
    – user49420
    Jun 12, 2017 at 18:01
  • 1
    $\begingroup$ @Alex please do not use answer for comments. Take a tour to learn how to express your gratitude. $\endgroup$
    – Kuba
    Jun 12, 2017 at 18:21

4 Answers 4

2
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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)
$\endgroup$
1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.