1

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]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Ryan
  • 11
  • 2
  • 1
    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. – bill s Jun 06 '13 at 14:57
  • 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. – Ryan Jun 06 '13 at 14:59
  • 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. – Sjoerd C. de Vries Jun 06 '13 at 21:26

2 Answers2

4

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}
ybeltukov
  • 43,673
  • 5
  • 108
  • 212
1

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).

bill s
  • 68,936
  • 4
  • 101
  • 191
  • 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? – Ryan Jun 06 '13 at 15:18
  • 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. – bill s Jun 06 '13 at 15:53