1
$\begingroup$

I'm completely new to Mathematica and can't seem to find an answer elsewhere so hopefully I can get some help here. I'm trying to plot a recurrence relation, where a random variate is generated at each iteration of the recurrence. From looking at the output, it appears as though the RandomVariate evaluates to a constant at each iteration. I have this inside a 'manipulate' block:

ListLinePlot[
   RecurrenceTable[{S[i] == 
   S[i - 1]*
   Exp[(r - (vol^2/2.0))*T/N + 
     vol*RandomVariate[NormalDistribution[μ, σ]]*
      Sqrt[T/N]], S[1] == S0}, S, {i, 2, N}]]}]

Any help with what I'm trying to accomplish would be appreciated.

Edit: here's the full version

 Manipulate[
 Column[{
 Plot[{PDF[ NormalDistribution[μ, σ], x],        
       PDF[ JohnsonDistribution["SU", γ, δ, μ, σ], x]}, {x, -6, 6}, Filling -> Axis],
ListLinePlot[
RecurrenceTable[{S[i] == 
   S[i - 1]*
    Exp[(r - (vol^2/2.0))*T/N + 
      vol*RandomVariate[NormalDistribution[μ, σ]]*
       Sqrt[T/N]], S[1] == S0}, S, {i, 2, N}]]}],
Style["Distribution Parameters", 12, Bold],
 {{μ, 0}, -5,   5},
 {{σ, 1}, 0.1,  5},
 {{γ, 1},    1, 10},
 {{δ, 1},    1, 10} ,
 Delimiter,
 Style["Option Parameters", 12 , Bold],
 {{N, 10}, 10, 1000},
 {{T, 1/12}, 1/12, 1},
 {{vol, 0.2}, 0, 1.0},
 {{r, 0}, 0, 0.3},
 {{q, 0}, 0, 0.3},
 {{S0, 100}, 0, 1000},
 ControlPlacement -> Left]
$\endgroup$
3
  • 1
    $\begingroup$ As your code stands it doesn't run: there are several undefined varables (mu, sigma, etc) and the brackets are misaligned. Also, you cannot use N as a variable (it is reserved for the function N). Try to use small letters to avoid such conflicts. $\endgroup$
    – bill s
    Jun 6, 2013 at 14:57
  • $\begingroup$ I intended to imply that, being inside a manipulate block, the undefined variables were defined elsewhere. I wanted to omit the other parts to draw attention to the important bit, but I'll post the full version. $\endgroup$
    – Ryan
    Jun 6, 2013 at 14:59
  • $\begingroup$ I can confirm that RecurrenceTable only calculates the parameters of the equations only once. It expects the parameters to be, well, just parameters, i.e., fixed. $\endgroup$ Jun 6, 2013 at 21:26

2 Answers 2

4
$\begingroup$

RecurrenceTable can work not only with fixed variables, just add triple Unevaluated. For example:

RecurrenceTable[{a[n + 1] == a[n] + Unevaluated@Unevaluated@Unevaluated@RandomReal[], 
  a[1] == 0}, a, {n, 1, 10}]
{0, 0.421764, 0.931848, 1.84073, 2.50044, 2.82655, 3.76122, 4.34796, 4.66102, 5.31278}
% // Differences
 {0.421764, 0.510084, 0.908884, 0.659712, 0.326103, 0.934675, 0.586739, 0.31306, 0.65175}
$\endgroup$
2
  • 2
    $\begingroup$ You can also use ξ[n_Integer] := RandomReal[] for unevaluated random numbers $\endgroup$
    – ybeltukov
    Nov 19, 2015 at 1:54
  • 1
    $\begingroup$ What kind of black magic is that triple Unevaluated?! $\endgroup$
    – Chris K
    Feb 1, 2020 at 23:21
1
$\begingroup$

Here's how I would approach this problem:

vol = 1; t = 1; n = 20; S0 = 1; r = 1;
f[x_, a_] = x*Exp[(r - (vol^2/2.0))*t/n + vol*a*Sqrt[t/n]];
randN = RandomVariate[NormalDistribution[0, 1], {n}];
out = FoldList[f, S0, randN];
ListLinePlot[out]

enter image description here

(with constants defined arbitrarily). I have replaced RecurrenceTable with FoldList and I think this gives you what you wish: unlike the code in the RecurrenceTable, the values go up and down (as any good stochastic process should).

$\endgroup$
2
  • $\begingroup$ Thanks for this. I've tried it this way and the output still doesn't look like geometric brownian motion to me. Would you mind trying to run the full version I posted above? $\endgroup$
    – Ryan
    Jun 6, 2013 at 15:18
  • $\begingroup$ I agree that the code you posted does not work -- it always looks like an exponential decay or an exponential growth. I've tried to fix the heart of the matter above. $\endgroup$
    – bill s
    Jun 6, 2013 at 15:53

Your Answer

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

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