Problem Sheet 2 4CMP PROGRAMMING IN C++


Problem Sheet 2, 4CMP Spring Term 2018/19
PROBLEM SHEET 2
4CMP, SPRING TERM, PART 2: PROGRAMMING IN C++
Lecturer: Dr Fabian Spill ([email protected])
Due Date: Saturday 9th February 2019 at 11.59 pm
Weighting: This problem sheet counts 15% to your final mark for the spring part of 4CMP, but only
the best two out of problem sheets 1-3 count.
INSTRUCTIONS FOR THE SUBMISSION:
Submit your code by the deadline on Canvas. Each problem on this sheet should be implemented in
a single file named as follows: main_X.cpp. Here, X is the number of the problem. For example,
main_P1.cpp.
This file should compile without errors or warnings on Visual Studio 2015 as installed on the clusters
in the learning centre, and the executable should run without problems.
The source code should also be well formatted. This means that opening and closing brackets should
be aligned in a readable way, and we will discuss such styles briefly in the lectures. Also, it is crucial
that you comment your code well. For this first problem sheet, this means that every single nontrivial
statement you write should be explained with a comment. Moreover, at the beginning of each
file, give some brief description of what the code in this file is doing. You can follow the style I use
(which contains author, version, file, description etc, but you can also create your own style of
comments. The critical bit is that it contains the information required to follow what is going on in
the code). The programs you write should run in a self-explanatory way. If you are asked that the
program provides some input, it should first have some output on the command line telling the user
what kind of input is required. For instance, if you are supposed to read in the price of a share, do
the following:
double price;
std::cout << “ Please enter the price of share: “ << std::endl;
std::cin >> price;
Then, note that obviously group work or copying solutions from other students or sources
constitutes plagiarism and is not allowed.
Marking: The most important criteria for marking is the correctness of the code. It needs to compile
and run correctly, and do what you were asked to do. However, style, efficiency, readability and
formatting are also part of the evaluation criteria. In particular, if the code is so unreadable that one
cannot evaluate if it is working correctly, then you risk to lose many marks for that.
PROBLEM 1: A CLASS FOR DATES
Create a class Date that stores dates. It should be able to store all dates starting from the 1/1/1900.
If dates have a fixed starting date, they can be enumerated by integers. Consequently, you may
assign the integer 1/1/1900 to be the integer 1, 2/1/1900 the integer 2 etc.
If you use Excel you can see that Excel handles dates this way by converting a cell in date-style to a
Cell with a generic number style.
The class may consequently represent each date by a single integer as member variable. The class
should have accessor functions to set/get this integer.
Moreover, the class should have two functions GetDate and SetDate that convert the single integer
representing a date to three integers that represent day, month and year of the date. E.g. if the
stored integer is 1, it is converted to three integers {1,1,1900) and vice versa. Finally write a
PrintDate function that writes the date in human readable form on the command line (e.g the
output could be: date is the 1/1/1900).
In main, create an object of the date class and represent first the
2
nd February 1900
Set the date through a function that translates the day/month/year representation into the single
integer representation;
Output that date to the command line using the function PrintDate. Output the integer that
represents this date.
Then, assign the integer 1000 to the object. Output the date with PrintDate in human readable
format.
You may ignore leap years for this exercise, i.e. assume that each year has exactly 365 days. If you
compare your result with e.g. the one from Excel do not despair – Excel has a bug! The year 1900
was not a leap year and does have 365 days.
PROBLEM 2: CLASS FOR CALL OPTION WITH RISK METRICS
Write a class EuropeanCall that contains the current share price, the current time, the expiry time,
the strike price, the volatility, the dividend and the interest rate as private member data. Then, the
class shall contain member functions (accessors) that can set or return all the member data (e.g.
SetStrike, GetPrice etc).
The class shall further contain the following functions:
InitialiseCall: This function should take current share price, the current time, the expiry time, the
strike price, the volatility, the dividend and the interest rate as input, and set all the member
variables accordingly.
PriceCallAnalytic: returns the value of the option from the Black-Scholes formulat
CallDeltaAnalytic: returns the Delta
CallGammaAnalytic: returns the Gamm
CallThetaAnalytic: returns the Theta
CallRhoAnalytic: returns the rho
Next, create a function called
PricePortfolioOfCalls
It takes as the input argument the data from an arbitrary number of calls, where each call can be
present multiple times. Think about how you could best code up such function. The function then
Problem Sheet 2, 4CMP Spring Term 2018/19
returns the value of the portfolio (i.e. the sum of the prices of each call, weighted by the number of
each call in the portfolio).
In main, create 3 objects of the EuropeanCall class. Set the member variables to the following values:
Call1: Share price: 50, strike: 50, Expiration: 1, Interest rates: 0.1, Volatility 0.5, Dividend: 0.2
Call2: Share price: 50, strike: 40, Expiration: 1.5, Interest rates: 0.1, Volatility 0.6, Dividend: 0.3
Call3: Share price: 50, strike: 60, Expiration: 2, Interest rates: 0.1, Volatility 0.2, Dividend: 0.0
Calculate the price and all the above risk metrics for each Call and output the results on the
command line.
Then, output the value of a portfolio that contains 3 Call1’s, 5 Call2’s, and 12 Call3’s with the
function PricePortfolioOfCalls on the command line.

因为专业,所以值得信赖。如有需要,请加QQ99515681 或邮箱:[email protected] 

微信:codinghelp

猜你喜欢

转载自www.cnblogs.com/pythonwriter/p/10357868.html