Week 6. Path Dependent Options
Dr P. V. Johnson
Department
of Mathematics
2022
Dr P. V. Johnson MATH60082
Last Time
Random numbers
Monte Carlo
European Options
Dr P. V. Johnson MATH60082
This Time
Vectors
Path Dependent Options
Dr P. V. Johnson MATH60082
In order to use vectors you
need to include the vector
library at the top of your
code. Open up a new
project and use the code
opposite as a template.
Click here to download full code
1 #inc lude
2 #inc lude
3 #inc lude
4 #inc lude
5 #inc lude
6 #inc lude
7 us ing namespace std ;
8
9 i n t main ( )
10 {
11 // empty program
12
13 }
Dr P. V. Johnson MATH60082
In order to use vectors you
need to include the vector
library at the top of your
code. Open up a new
project and use the code
opposite as a template.
Declare a vector of doubles
in you code. The syntax is
as follows:
vector
a(4),b,c(6,3.);
where a is a vector with 4
elements, b is an empty
vector, and c is a vector
with 6 elements each with
the value 3.
Click here to download full code
1 #inc lude
2 #inc lude
3 #inc lude
4 #inc lude
5 #inc lude
6 #inc lude
7 us ing namespace std ;
8
9 i n t main ( )
10 {
11 // empty program
12 vector a (4) ,b , c ( 6 , 3 . ) ;
13 a [ 0 ] = 1 . ;
14 cout << a [ 0 ] << endl ;
15 }
Dr P. V. Johnson MATH60082
In order to use vectors you
need to include the vector
library at the top of your
code. Open up a new
project and use the code
opposite as a template.
Set some values to your
vector and print them out
to the screen, using the
access operator [] to
reference an element of the
vector
Click here to download full code
1 #inc lude
2 #inc lude
3 #inc lude
4 #inc lude
5 #inc lude
6 #inc lude
7 us ing namespace std ;
8
9 i n t main ( )
10 {
11 // empty program
12 vector a (4) ,b , c ( 6 , 3 . ) ;
13 a [ 0 ] = 1 . ;
14 cout << a [ 0 ] << endl ;
15 }
Dr P. V. Johnson MATH60082
Now declare a 5× 4 2D
vector of doubles using the
syntax:
vector>
V(5,vector(4));
Again we use the access
operator [] to access
elements, so we write
V[1][3] = 5.;
Assign some values to the
vector and print them out.
Click here to download full code
1 {
2 // empty program
3 vector a (4) ,b , c ( 6 , 3 . ) ;
4 a [ 0 ] = 1 . ;
5 cout << a [ 0 ] << endl ;
6 // a 2D vector
7 vector> V(4 ,
vector(5) ) ;
8 }
Dr P. V. Johnson MATH60082
Now declare a 5× 4 2D
vector of doubles using the
syntax:
vector>
V(5,vector(4));
Again we use the access
operator [] to access
elements, so we write
V[1][3] = 5.;
Assign some values to the
vector and print them out.
Click here to download full code
1 // a s s i gn i t some va lues
2 f o r ( i n t i =0; i <4; i++)
3 {
4 f o r ( i n t j =0; j <5; j++)
5 {
6 V[ i ] [ j ] = 10∗ i + j ;
7 }
8 }
9 // and output them to sc r een
10 f o r ( i n t i =0; i <4; i++)
11 {
12 f o r ( i n t j =0; j <5; j++)
13 {
14 cout << V[ i ] [ j ] << ” ”
;
15 }
16 cout << endl ;
17 }
Dr P. V. Johnson MATH60082
TASKS
1 Create a 1D vector and assign it some values, then print
them out.
2 Try using the vector functions (look them up on the web),
such as:
cout << V.size(); V.clear(); V.resize(2);
3 Create a 2D vector and assign it some values, then print
them out.
4 Practice assigning values to the vector and performing
calculations on it (e.g. find the average of all the elements).
5 Try writing a function that takes a vector as an argument.
6 Find some solutions here:-
(click here to download)
Dr P. V. Johnson MATH60082
Path Dependent Options
Assume that the stock price St is observed over K + 1 equally
spaced intervals t0, t1, . . . , tK with t0 = 0 and tK = T . Now let
us solve the value of a path dependent option where we define
the underlying asset A to be the average absolute movements in
the share price over several periods such that
A =
∑K
i=1 |S(ti)− S(ti−1)|√
K
.
Assuming the Black Scholes framework under the risk neutral
measure, and we wish to price a call option with payoff
C(A,S, t = T ) = max(A−X, 0).
Dr P. V. Johnson MATH60082
Path Dependent Options
Find the value
C(S0, t = 0) = e
−rTEQ[max(A−X, 0)]
where K = 20, strike price X = 30 and a maturity T = 1 is
one year from now;
the current stock price is S0 = 104.81;
the stock price follows a GBM with σ = 0.4, the interest
rate is r = 0.03.
Dr P. V. Johnson MATH60082
Path Dependent Options
First start with an empty
solution.
Now add some parameters
from the question
including your random
number generator.
Click here to download full code
1 #inc lude
2 #inc lude
3 #inc lude
4 #inc lude
5 #inc lude
6 #inc lude
7 #inc lude
8 us ing namespace std ;
9
10 i n t main ( )
11 {
12 }
Dr P. V. Johnson MATH60082
Path Dependent Options
First start with an empty
solution.
Now add some parameters
from the question
including your random
number generator.
Click here to download full code
1 #inc lude
2 #inc lude
3 #inc lude
4 #inc lude
5 #inc lude
6 #inc lude
7 #inc lude
8 us ing namespace std ;
9
10 i n t main ( )
11 {
12 mt19937 rng ;
13 norma l d i s t r i bu t i on<> ND(0 . ,
1 . ) ;
14
15 double S0 = 104 .81 , sigma =
0 .4 , r = 0 .03 , T = 1 , X =
3 0 . ;
16 i n t K = 20 ;
17
18
19 }
Dr P. V. Johnson MATH60082
Path Dependent Options
The next step is to create a
stock path (in a vector)
and then output it to
screen and check it looks
reasonable.
Next calculate the variable
A from your path, does it
look correct?
Run a loop over your path
10 times to check you can
generate new paths and
new values of A, does it
look reasonable?
Click here to download full code
1 // now c r ea t e a path
2 double dt = T / K;
3 vector stockPath (K +
1) ;
4 stockPath [ 0 ] = S0 ;
5 f o r ( i n t i = 1 ; i <= K; i++)
6 {
7 double phi = ND( rng ) ;
8 stockPath [ i ] = stockPath [ i
− 1 ] ∗ exp ( ( r−0.5∗
sigma∗ sigma ) ∗dt
9 + phi ∗
sigma∗
sq r t (
dt ) ) ;
10 }
11 // and p lo t i t
12 f o r ( i n t i = 0 ; i <= K; i++)
13 {
14 cout << i ∗dt << ” ” <<
stockPath [ i ] << endl ;
15 }
Dr P. V. Johnson MATH60082
Path Dependent Options
The next step is to create a
stock path (in a vector)
and then output it to
screen and check it looks
reasonable.
Next calculate the variable
A from your path, does it
look correct?
Run a loop over your path
10 times to check you can
generate new paths and
new values of A, does it
look reasonable?
Click here to download full code
1 }
2 f o r ( i n t i = 0 ; i <= K; i
++)
3 {
4 cout << i ∗dt << ” ” <<
stockPath [ i ] <<
endl ;
5 }
6 A = A/ sq r t (K) ;
7 cout << ” A = ” << A <<
endl ;
8 }
9 }
Dr P. V. Johnson MATH60082
Path Dependent Options
Now do a Monte-Carlo
calculation to calculate the
option value.
Once this is working put
your code into a function
You can now perform
analysis on it to check that
the value is converging and
everything appears to be
working.
Click here to download full code
1 A = A + fabs ( stockPath
[ i ] − stockPath [ i
− 1 ] ) ;
2 }
3 A = A/ sq r t (K) ;
4 // add in the payo f f to
the sum
5 sum = sum + max(A − X, 0 . )
;
6 }
7 cout << ” Cal l Option = ” <<
sum / N∗exp(−r ∗T) << endl ;
8 }
Dr P. V. Johnson MATH60082
Path Dependent Options
Now do a Monte-Carlo
calculation to calculate the
option value.
Once this is working put
your code into a function
You can now perform
analysis on it to check that
the value is converging and
everything appears to be
working.
Click here to download full code
1
2 double ca l lOpt ion ( double S0 ,
double sigma , double r ,
double T, double X, i n t K, i n t
N)
3 {
4 s t a t i c mt19937 rng ;
5 no rma l d i s t r i bu t i on<> ND(0 . ,
1 . ) ;
6 double sum = 0 . ;
7 f o r ( i n t n = 0 ; n < N; n++)
Dr P. V. Johnson MATH60082
TASKS
REMEMBER TO CHANGE YOUR ENVIRONMENT TO
RELEASE BEFORE RUNNING INTENSIVE CODES
1 Try running this code with different values of
N = 100, 200, ..., what happens?
2 Choose a large enough value of N , what happens when you
choose different values of K?
3 For a fixed value of K, can you estimate the value and the
accuracy of your result? (Hint: look at lab class 3)
4 Try making A the average or the maximum/minimum
value – what happens to the option value?
Dr P. V. Johnson MATH60082