path-dependent辅导、讲解C++编程、辅导C++、讲解Black-Scholes model
- 首页 >> C/C++编程Pricing path-dependent call options
C++ Programming with Applications to Finance Spring 2018
The famous Black-Scholes model from mathematical finance for a non-dividend paying stock
can be formulated as
dSt = rStdt + σStWt
,
where r is the continuously compounded risk-free interest rate, σ > 0 is the volatility of the
stock and W is a Brownian motion under the equivalent martingale measure. The Black-Scholes
model depends on two parameters, namely r and σ. The interest rate r is readily available;
however the volatility σ cannot be observed directly, and needs to be estimated from market
data.
Vanilla call option pricing
Explicit formula have been derived for many types of options. For example, the price at time
0 of a vanilla European call option with strike price K and expiry time T is given by the
Black-Scholes formula
C(S0, K, T, σ, r) = S0Φ(d+) ? Ke?rT Φ(d?), (1)
where
is the distribution function of the standard normal distribution.
Trinomial tree approximation
The It o lemma of stochastic calculus allows us to derive the stochastic differential equation of
the log-price xt = ln St as
dxt = νdt + σdWt
The linearity of this stochastic differential equation means that it is often more convenient to
model x directly rather than the stock price S itself.
Explicit formulae have been derived for many types of options (including put and call options);
however approximation methods are needed to price many other options. One such
method is the trinomial model. Let’s consider a trinomial model of the stock price in which,
over a small time interval of length ?t > 0, the log-price x can go up by ?x > 0, stay the same,
or go down by x, with probabilities qu, qm and qd respectively. This is illustrated as follows.
The features of the continuous-time process can now be captured by the parameters ?x, ?t, qu,
qm and qd. Formulae for the transition probabilities qu, qm and qd can be derived by matching
the first two moments of the continuous-time process in (2) and the trinomial process over the
time interval of length t, in other words,
νt = E(?x) = qu × x + qm × 0 + qd
Solving this system for qu and qd, and requiring that the transition probabilities add up to 1,
gives
This is a good choice for ?x because it links the size of stock price movements with the length
of the time interval over which such movements take place.
The trinomial process described above can be extended to form a trinomial tree. Starting
from a given initial stock price S0 > 0, each time step n represents “real time” t = n?t, and
the stock price at node i is
S(n, i) = S0e
ix
, (3)
where i = n, . . . , 1, 0, 1, . . . , n. The resulting tree is illustrated in the following figure.
S(0, 0) = S0 S(1, 0) = S0 S(2, 0) = S0 S(3, 0) = S0
2
The trinomial tree with N steps can be used to approximate the stock price movement over
a “real” time interval [0, T] by setting t =
and the price of an option in the trinomial
model can be used to approximate its price in the Black-Scholes model.
Trinomial method for path-independent options in the trinomial tree
If the payoff of a European option with expiry time T can be expressed as a function h that
depends on ST only, then it is called path-independent because it doesn’t depend on the path
followed by the stock price up to time T (it depends only on the final position at time T). For
example, a vanilla European call option is path-independent and its payoff function is
h(ST ) = max{ST K, 0}.
The option price of any path-independent European option with payoff h can be approximated
by the trinomial method using an N-step trinomial tree with t =
as follows:
At time step N, let
H(N, i) = h(S(N, i)) for all i = N, N + 1 . . . , N.
At every time step n = N 1, N 2, . . . , 0, assume that H(n + 1, i) is known for all
i = n 1,n, . . . , n + 1. Then define
H(n, i) = e
[quH(n + 1, i + 1) + qmH(n + 1, i) + qdH(n + 1, i 1)] ,
where qu, qm and qd are the risk-neutral probabilities given above.
The price of the option is then H(0, 0).
Monte Carlo method for path-dependent and path-independent options in the
trinomial tree
The payoff of a path-dependent European option with expiry time T depends not only on the
final stoock price ST , but also on how the stock price fluctuates during the lifetime of the
option. The payoff of a path-dependent option with expiry time T in the Black-Scholes model
can usually be approximated by a function g(S(0), S(1), . . . , S(N)) that can be used in an Nstep
trinomial tree with t =
, where the sequence S(0), S(1), . . . , S(N) represents a stock
price path over time steps 0, 1, . . . , N in the tree. The following table gives the payoff functions
of two path-dependent call options.
Option Black-Scholes payoff g(S(0), S(1), . . . , S(N))
Floating strike lookback call option
max
ST min
0≤t≤T
Fixed strike lookback call option max
max
0≤t≤T
St K, 0
max
max
n=0,...,N
S(n) K, 0
The price of a path-dependent option with payoff function g can be approximated by Monte
Carlo simulation, which involves generating M random sample paths of the trinomial tree. The
Monte Carlo method is as follows for each k = 1, . . . , M:
Generate N independent random variables v
(n = 1, . . . , N) such that
1 with probability qu,
0 with probability qm,
1 with probability qd,
where qu, qm and qd are the risk-neutral probabilities given above.
3
Generate a random stock price path (S
k
(0), Sk
(1), . . . , Sk
(N)) by setting S
k
(0) = S0 andSk
(n) = S
for all n = 1, . . . , N,
where the formula for S(·, ·) is given by (3).
The price of the path-dependent option is then approximated by
(0), . . . , Sk
(N)). (4)
The Monte Carlo method also works for a path-independent option with payoff function h;
we just write
in (4) above.
Project tasks
The aim in this project is to create an option pricing application by completing the following
tasks based on the information provided above. You should use the header file Project.h,
which must be included in your project without change.
The marks for each of the tasks below will be split into 60% for coding style, clarity, accuracy
and efficiency of computation, and 40% for documentation (including comments within the code)
and ease of use. Credit will be given for partial completion of each task.
General hints:
Read the submission instructions at the end of this document before starting work on the
project. Read through all tasks before starting work on the project. Tasks do not have to be
completed in the order that they are listed here.
It might be helpful to look at Chapter 5 of Capi′nski & Zastawniak (2012), which covers
Monte Carlo pricing of path-dependent options in the Black-Scholes model. However, the
technical details are different in this project.
You are free to recycle any code produced by yourself during the module. You are also
free to use any code provided in Moodle during the module, provided that such code is
acknowledged.
Test your project with a freshly downloaded copy of Project.h just before submission.
1. Data input and calibration (20%)
Your program should ask the user to enter values for S0 and r. It should then compute σ by
using the observed market price Cquote of a call option with strike price K and expiry time T
and solving the non-linear equation
C(S0, K, T, σ, r) = Cquote
4
for σ, where the function C(·) is the formula for a call price given in (1). Your program should
ask the user to enter Cquote, K and T, and display the value of σ once it has been calculated.
The value of σ should be accurate to at least 5 decimal places.
Hint: There is no closed formula for the function N(·) in (1). However, it can be approximated
accurately in a number of different ways. One such method is described in Section 4.7 of
Capi′nski & Zastawniak (2012).
2. Trinomial tree (10%)
The header file Project.h contains a class TriModel representing the trinomial tree described
above. Create a new source code file TriModel01.cpp which should contain the definitions of
all the member functions of the class TriModel that aren’t already defined in Project.h.
3. Monte Carlo pricing (20%)
The header file Project.h also contains a class PathDepOption representing path-dependent
options. Create a new source file PathDepOption.cpp and provide the definition of the Monte
Carlo pricing function PathDepOption::PriceByTrinomialMC() in it.
Hints:
Learn about the rand() and srand() functions from a good C++ manual. Defining
n with the desired distribution.
Take note when debugging that the number of repetitions in Monte Carlo pricing can be
very large, which means that the sum in (4) may exceed the range of the double variable
type.
4. Path-dependent European call options (10%)
Create two subclasses LookbackFloatingCall and LookbackFixedCall of PathDepOption in
a new header file PathDepCall.h to represent floating and fixed strike lookback call options.
The definitions of any member functions not defined in PathDepCall.h should appear in a new
code file PathDepCall.cpp.
5. Vanilla call option (20%)
Create a class VanillaCall to represent a vanilla European call option with expiry time T and
strike K. The class should offer the following functionality:
Can be priced with the Black-Scholes formula.
Can be priced with the trinomial method for path-independent options in a trinomial tree
with step size
Can be priced with the Monte Carlo method for path-independent options in a trinomial
tree with step size ?t =
5
The class declaration should appear in VanillaCall.h. Any member functions not defined
there should appear in VanillaCall.cpp.
The class VanillaCall should be designed in accordance with the principles of object oriented
design. In particular, it must be possible to add further path-independent options (such
as vanilla put options) with similar functionality without modifying the code in VanillaCall.h
or VanillaCall.cpp.
Hints:
? The payoff to be used with the Monte Carlo procedure is
g(S(0), S(1), . . . , S(N)) = max{S(N) ? K, 0}.
It is worth considering whether this class can be used for the calibration procedure in
Task 1.
It would be helpful to reuse existing code (for example, the code developed for Monte
Carlo pricing in Task 3). It is therefore worth considering whether VanillaCall should
be a subclass of some base class (or perhaps more than one base class). There are a
number of different options for this. Consult a good C++ manual if you encounter any
technical difficulties.
It is possible for a C++ class to have two or more member functions with the same name,
as long as the parameters are different in each case. This is called function overloading.
Consult a good C++ manual on this if needed.
6. Numerical study of call option prices (20%)
Your program should perform a numerical study of call option prices, in two parts. In this task,
your program should use the values of K and T entered by the user as part of Task 1.
Your program should produce a file VanillaCall.csv using the comma separated values
(csv) format. It should contain a table of vanilla call option prices, together with appropriate
row and column headings. Each row n = 1, 2, . . . , 10 consists of 11 column entries, as follows:
The first entry should be the option price in the trinomial tree with N = 100n, calculated
with the trinomial method.
The mth entry, where m = 2, . . . , 11, should be the option price in the trinomial tree with
N = 100n, calculated with the Monte Carlo method for path-independent options with
M = 10000(m 1) repetitions.
Your program should also produce a comma separated values file PathDependentCall.csv.
Each row n = 1, 2, . . . , 10 of the table in this file should contain the prices of a floating and fixed
strike lookback option, both computed by using the Monte Carlo method with M = 100000
repetitions in a trinomial model with N = 100n steps. You should add row and column headings
as appropriate.
Sample files VanillaCall.csv and PathDependentCall.csv have been provided to illustrate
the structure of the output. Your output should contain numerical results in place of each
of the entries marked “price”, and you may vary the heading text if you wish.
Hint: Spreadsheet applications (e.g. Microsoft Excel) can open .csv files.
6
Submission instructions
Submit your work by uploading it in Moodle by 3pm on Wednesday 25 April 2018.
Format
Submit the code as a single compressed .zip file, including all Code::Blocks project (.cbp)
files, data files (.csv), source code and header (.cpp and .h) files and the executable (.exe)
file produced by compiling and linking the project, all residing in a single parent directory.
The .zip file should preserve the subdirectory structure of the parent directory (that is, the
subdirectory structure must be automatically recreated when unzipping the file). It must be
possible to open all project files and to compile, link and run the project using the
version of Code::Blocks running on computer lab machines.
The code should be accompanied by detailed documentation, split into two parts:
Code developer documentation Presenting the structure of the code, available classes and
functions, main variables and any other information to help understand the code. This
should include information on how to extend the code by adding new options. Test runs
of your code should be reported, including tables and graphs. More detailed explanations
of the methods used should also be reported in a separate section of the developer
documentation.
End user instructions How to use the compiled .exe program, how to input data and how
the results are presented, and a brief description of the methods implemented.
The documentation files must be submitted in .pdf format (two separate .pdf files containing
developer documentation and user instructions) and uploaded in Moodle as a single .zip file
separate from the code file.
It is advisable to allow enough time (at least one hour) to upload your files to avoid possible
congestion in Moodle before the deadline. In the unlikely event of technical problems in Moodle
please email your .zip files to alet.roux@york.ac.uk before the deadline.
Code usage permissions and academic integrity
You may use and adapt any code submitted by yourself as part of this module, including your
own solutions to the exercises. You may use and adapt all C++ code provided in Moodle as
part of this module, including code from Capi′nski & Zastawniak (2012) and the solutions to
exercises. Any code not written by yourself must be acknowledged and referenced both in your
source files and developer documentation.
This is an individual project. You may not collaborate with anybody else or use code
that has not been provided in Moodle. You may not use code written by other students.
Collusion and plagiarism detection software will be used to detect academic misconduct, and
suspected offences will be reported to University authorities.
If things go wrong
Late submissions will incur penalties according to the standard University rules for assessed
work.
It must be possible to open all project files and to compile, link and run the
project using the version of Code::Blocks running on computer lab machines. It
may prove impossible to examine projects that cannot be unzipped and opened, compiled and
7
run on computer lab machines directly from the directories created by unzipping the submitted
.zip files. In such cases a mark of 0 will be recorded. It is therefore essential that all
project files and the directory structure are tested thoroughly on computer lab machines before
being submitted in Moodle. It is advisable to run all such tests starting from the .zip files
about to be submitted, and using a different lab computer to that on which the files have been
created.
All files must be submitted inside the zipped project directory, and all the files
in the project directory should be connected to the project. A common error is to
place some files on a network drive rather than in the submitted directory. Please bear in mind
that testing on a lab computer may not catch this error if the machine has access to the network
drive. However, the markers would have no access to the file (since they have no access to your
part of the network drive) and would be unable to compile the project.
As part of the marking process, the file Project.h will be replaced. If the copy of
Project.h used in a project has been modified in such a way that the code does not
compile with the original file, then a mark of 0 will be recorded.
References
Capi′nski, M. J. & Zastawniak, T. (2012), Numerical Methods in Finance with C++, Mastering
Mathematical Finance, Cambridge University Press.