0
$\begingroup$

I am doing some work with black-scholes and am trying to find random interest rates based on some given information. This is my original code:

dt = .0001;
mu = 0;
gamma = .005;
t = .0833;
random = RandomVariate[NormalDistribution[0, dt^.5], 10^2];
Z = Table[0, 10^2 + 1];
Z[[1]] = .0158;
i = 2;
While[i <= 834,
Z[[i]] = Z[[i - 1]] + mu*dt + (gamma)*random[[i - 1]];
i++
];
(*have to make this loop 100 times*) 
ListPlot[Z, DataRange -> {0, 100*dt}, PlotRange -> All];
Print[Z];

My main goal is just to get the list of values for interest rates that should be seen as 100X834 in table form. I've received the following revision to my code, from stack exchange, however it isn't outputting the correct table form that I need, I am not sure if I need to put Prepend into my while loop to get it to iterate correctly, or how I can control the dimensions of the table to give me the amount of values I need.

dt = .0001;
mu = 0;
gamma = .005;
t = .0833;
rndm := RandomVariate[NormalDistribution[0, dt^.5], 10^2];
Prepend[Accumulate[rndm] gamma + mu dt + .0158, .0158];
k = 100;
SeedRandom[1]
W = Table[Prepend[Accumulate[rndm] gamma + mu dt + .0158, .0158], 
k];
$\endgroup$
3
  • 1
    $\begingroup$ If it results in a more readable approach to you, your While loop could also be replaced by a FoldList call, with your value of Z[[1]] as a starting point, and rndm as the list to be folded in. $\endgroup$
    – MarcoB
    Dec 22, 2019 at 4:54
  • $\begingroup$ replace 10^2 with 834 in rndm := RandomVariate[NormalDistribution[0, dt^.5], 10^2];? $\endgroup$
    – kglr
    Dec 22, 2019 at 5:00
  • $\begingroup$ ahh completely forgot that the table was defined to that width, it worked thanks! $\endgroup$ Dec 22, 2019 at 15:11

1 Answer 1

2
$\begingroup$

You may use GeometricBrownianMotionProcess with RandomFunction. You may also find the Stochastic Differential Equation Processes guide useful.

mu = 0;
gamma = .005;
z0 = .0158;
dt = .0001;
steps = 834;
SeedRandom[9128]
walk = RandomFunction[
  GeometricBrownianMotionProcess[mu, gamma, z0], {0, steps dt, dt}]

Mathematica graphics

This returns a TemporalData object that can be plotted with ListLinePlot and other functions.

ListLinePlot[walk]

Mathematica graphics

n paths can be generated with

n = 5;
SeedRandom[7456]
walks = RandomFunction[
  GeometricBrownianMotionProcess[mu, gamma, z0], {0, steps dt, dt}, n]
ListLinePlot[walks]

Mathematica graphics

Mathematica graphics

Time steps and values can be extracted by the "Paths", "Values", and "Components" properties of TemporalData; see TemporalData's documentation.

Hope this helps.

$\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.