MTH773P / MTH773U Advanced Computing in Finance Class Test 1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
MSci in Financial Mathematics (2021/22)
MTH773P / MTH773U
Advanced Computing in Finance
Class Test 1
Instructions
❼ The test is available for 24 hours from 13.00 on Monday 7th March 2022.
❼ You may choose when you wish to start the test. This is your start time.
❼ Your finish time is 3 hours after your start time, or the end of the 24-hour window if this is earlier.
❼ You must complete the uploading of your solutions to QMplus before your finish time.
❼ Late submissions cannot be accepted, so please allow sufficient time for uploading your
solutions to QMplus.
When undertaking the test. . .
❼ You should attempt all the questions.
❼ This is an “open book” assessment. This means that you may consult lecture notes, textbooks
and other static resources, although you should never directly copy material from elsewhere into your solutions.
❼ We do not expect you to spend significantly longer than 2 hours on this assessment. Indeed, excessively long, highly elaborate and/or over-engineered solutions may be penalised.
❼ Generally speaking, we expect you to use C++ language features that have been covered in
this module (or in your Semester A module on C++). If you use any other C++ features then you must provide detailed explanatory comments in your code.
❼ If you believe that there may be some error in this question paper, then please type an
appropriate comment in your solution, and continue answering as best you can.
When you have completed answering the questions. . .
❼ Please upload the following files to QMplus before your finish time:
Question 1 Question 2 Question 3 |
question01 .txt main02 .cpp main03 .cpp, plus any other .cpp and .h files |
Do not upload any other files such as Visual Studio configuration or build files, object ( .obj) files or executables ( .exe).
❼ Email the same set of files to maths@qmul .ac .uk, also before your finish time.
Note: The only purpose of this email is so that we have a backup copy of your submission which we would use in the (unlikely) event of a system failure with QMplus.
Question 1 [20 marks]
Please type your answers for this question into a text file named question01 .txt using a text editor such as Notepad. (Do not use other file formats such as Microsoft Word.) Please put your name and student ID number on the first line of this file.
Note: You should answer using your own words, to demonstrate that you fully understand the concepts.
(a) Explain briefly the difference between a vector and a set (in the C++ library). 4
(b) Give a precise definition of the following terms, and also a finance-related example of each:
1. A default constructor,
2. A parametrised constructor,
3. A copy constructor.
(c) This question concerns virtual functions in C++.
1. Explain what is meant by a virtual function.
2. Write a short code sample to illustrate clearly the usage of a virtual function. Your code should be written in such a way that its behaviour will change if the keyword “virtual” is removed.
Question 2 [30 marks]
In this question, you are going to write a class to represent a 2-by-2 matrix of the form ╱ c(a) d(b) 、.
(a) Create a new Visual Studio project with a single file, main02 .cpp. You should implement everything for this question in this single file. On the first line of this file please type a C++ comment with your name and student ID number.
(b) Copy-and-paste the following code into your file. This will form the starting point for your solution.
#include <iostream> using namespace std; class Matrix { private: double m_a, m_b, m_c, m_d; }; void calculate() { } |
|
int main() { Matrix P(2 .2, 4 .6, Matrix Q(7 .0, 2 .3, Matrix R = P + Q; Matrix S = R * R + S = S * P; cout << S << endl; calculate(); return 0; } |
// Note: Do not change anything inside main() |
3 .8, 6 .1); // Line 1 -4 .2, 9); // Line 2 // Line 3 Q; // Line 4 // Line 5 // Line 6 // Line 7 |
(c) Add a constructor to the class so that a new matrix object can be created with a particular set of values for the elements a, b, c and d. (This will enable lines 1 and 2 to compile.) 4
(d) Explain why you do not need to write either a copy constructor or an assignment operator for
the Matrix class in this simple example.
(Type your answer as a C++ comment at the end of the file main02 .cpp .) 4
(e) Add two new member functions to the class so that two matrix objects can be either added or
multiplied together, to form a new matrix. (This will enable lines 3, 4 and 5 to compile.) 8
(f) Write a new function so that line 6 will display the matrix S on the screen in the single-line
format “(a, b, c, d)” . What output does your program give (for the matrix S)?
(Type your answer as another C++ comment at the end of the file main02 .cpp .) 4
(g) Replace the empty function calculate() with the following code which creates a collection of four matrices. Add any other necessary code to your file so that the program can be built
and will run. 2
void calculate() { vector<Matrix*> matrices; matrices .push_back(new Matrix(1 .2, 2 .2, -3 .5, 4 .8)); matrices .push_back(new Matrix(-9 .2, 0 .6, -0 .2, 6 .8)); matrices .push_back(new Matrix(11 .4, 6 .7, 3 .2, -8 .3)); matrices .push_back(new Matrix(4 .7, 3 .9, 2 .5, -9 .8)); // // Add code here for parts (h) and (i) // return; } |
(h) In the space indicated, write some code that will use an iterator to loop over all the matrices in the collection, in order to determine the sum of all of them. The sum should be displayed on the screen. What output does your program give for the sum?
(Type your answer as another C++ comment at the end of the file main02 .cpp .) 4
(i) Finally, write some code to “free” the memory allocated by new. 4
Question 3 [50 marks]
In this question you will be pricing so-called “power options” using the Black-Scholes model. Please read the following two pages carefully, before attempting the assignment which starts on page 8.
Power options |
Power options are vanilla European options whose payoffs at expiry time T depend on the share price ³T to some fixed power. Specifically, the payoffs for call and put power options are given by:
Call power option: 令T(³T) = max ╱ ³T(n) - 云」0、
Put power option: 抄T(³T) = max ╱云 - ³T(n)」0、
where n S 0 is some fixed number, 云 S 0 is the strike price and ³T is the price of the underlying share at expiry. Note that a power option with n = 1 is just the same as an ordinary option.
Overview of option pricing |
The price today (at time t = 0) of a vanilla European option expiring at future time T is given by the discounted expectation of the payoff VT(³T)
V0 = e_rT |0 X g(³T)VT(³T)d³T」
where g(³T) is the probability density function of the share price at time T (in the risk-neutral measure) as seen at time t = 0. We will work within the framework of the Black-Scholes model where the share price follows geometric Brownian motion, and so g(³T) is the log-normal distribution
(
) ) )
g(³T) = (
) ) )
(
³Ts 2。1^ exp ┌ - ┐
0
for ³T S 0」
otherwise」
with mean and standard deviation parameters
m = log ³0 + (r - #2 )T and s = #^T|
As usual, ³0 is the price of the share at time t = 0, # is the volatility of the share price, and r is the annualised continuously-compounded interest rate. We are assuming here that the share pays no dividends during the lifetime of the option.
The integral above can actually be performed analytically for power options, giving exact formulas for the prices today:
令0 = ³0(n) exp ┌(n - 1)(r + n#2 /2)T┐Φ(h+) - 云e_rT Φ(h_ )」 抄0 = 云e_rT Φ(-h_ ) - ³0(n) exp ┌(n - 1)(r + n#2 /2)T┐ Φ(-h+)」
where
h+ = log(³0/云1/n ) )n - 、#2、T and h_ = h+ - n#^T」
and Φ(α) is the cumulative distribution function of the standard normal distribution. You will implement these exact formulas for 令0 and 抄0 in part (b).
Numerical pricing |
In part (c) you will evaluate the same integral numerically to determine approximate values for the prices which you will then compare with the exact values. You will need to replace the upper limit of the integral (infinity) with some suitable finite value ³T,max . You can then use Simpson’s rule for the integral using 之 steps, where 之 is even:
|ab y (α) dα ≈ !(┌)y (α0 ) + 2 1 y (α2j) + 4 y (α2j _1 ) + y (αN )[(2) 」
where h = (b - a)/之 and αj = a + jh for j = 0」1」| | | 」之 .
Cumulative normal distribution function Φ(α) |
The following C++ function implements the mathematical function Φ(α). You may incorporate this into your program for part (b) if you wish.
#include <cmath>
double Phi(double x) { const double SQRT_TWO = 1 .4142135623730950488016887242097; return 0 .5 * std::erfc(-x / SQRT_TWO); } |
(a) Create a new Visual Studio project, initially with a single file main03 .cpp. You may add other files to the project, as required. On the first line of every file please type a C++ comment with your name and student ID number.
(b) Write a program to price a power option using the analytical (exact) formulas. Your program should behave as follows:
1. Ask the user which type of power option she wishes to price. She should be able to enter either “CALL” or “PUT” .
2. Then prompt the user for the option parameters K , n and T , and the market data parameters ³0 , # and T , and check that the values entered are sensible. (If she enters invalid values, then the program should simply display an appropriate error message, and terminate.)
3. Calculate today’s price of the option using the appropriate analytical formula.
4. Display the price on the screen.
Hint: For the highest marks, remember to use the techniques of object-oriented programming that we have covered in this module.
What are the exact prices of a power call option and a power put option, each with time to expiry T = 1|25 years, strike price K = 105 and n = 2, where ³0 = 10, T = 0|05 and # = 0|3?
(Type your answers as a C++ comment at the end of the file main03 .cpp.)
(c) Now extend your program so that it can price a power option numerically, using Simpson’s rule for the integral. The program should behave as follows (steps 1, 2 and 6 are the same as before):
1. Ask the user which type of power option she wishes to price. She should be able to enter either “CALL” or “PUT” .
2. Then prompt the user for the option parameters K , n and T , and the market data parameters ³0 , # and T , and check that the values entered are sensible. (If she enters invalid values, then the program should simply display an appropriate error message, and terminate.)
3. Ask the user which method she wishes to use. She should be allowed to enter either “AN” for the analytical method, or “NUM” for the numerical method.
4. If the user has chosen the numerical method, then prompt for ³T,max (the upper limit of the integral) and N (the number of integration steps).
5. Calculate today’s price of the option using the chosen method.
6. Display the price on the screen.
For the particular options that you priced in part (b), what values do you get for the prices when using the numerical method with an upper integration limit of ³T,max = 50 and N = 50 integration steps?
(Type your answers as another C++ comment at the end of the file main03 .cpp.)
(d) Keeping ³T,max = 50, for each of the two options priced in part (b), determine the smallest value of N that gives a numerical value for the price that is less than 0.001% away from the
actual (exact) value.
(Type your answer as another C++ comment at the end of the file main03 .cpp .) 5
2022-07-27