7CCMFM13程序 写作、C++程序设计程序

7CCMFM13作业 写作、C++程序设计作业调试、Mathematics课程作业 辅导、c++语言作业 辅导
7CCMFM13
This paper is part of an examination of the College counting towards the award of a degree.
Examinations are governed by the College Regulations under the authority of the Academic
Board.
FOLLOW the instructions you have been given on how to upload your solutions
MSc Examination
7CCMFM13 C++ for Financial Mathematics (MSc)
Summer Mock Exam 2020
Time Allowed: Two Hours
All questions carry equal marks.
Full marks will be awarded for complete answers to FOUR questions.
If more than four questions are attempted, then only the best FOUR will
count.
You may consult lecture notes.
Mock Exam 2020
c Kings College London
7CCMFM13
1. (i) Write a function max(double, double) which takes two doubles and
returns the maximum of them. [20%]
(ii) Write a function mean which takes as input a vector of doubles (using
vector) and which returns the mean of the elements of the vector.
[20%]
(iii) Whats wrong with the following code?
int Setx(){
int x=3;
return x;
}
[20%]
(iv) What does the following code print out and why?
# include iostream
using namespace std;
void MyFunction(double a){
a*=10;
}
int main()
{
double a=1;
MyFunction(a);
couta\n;
return 0;
}
[20%]
(v) What does static mean in a class definition? Give an example of a class
with a static member variable. [20%]
– 2 – See Next Page
7CCMFM13
2. (i) We aim to define an interface class RNG with a function rand() to
generate random numbers according to a given distribution. What are
the two things which are missing from the code below?
class RNG{
public:
double rand() const=0;
};
[30%]
(ii) Write a class RUNG which provides an implementation of the function
rand() to generate a number in the interval (0, 1) from the uniform distribution
(we assume that we have declared an object mt19937
MersenneTwister).
[40%]
(iii) Write a class RENG which provides an implementation of the function
rand() to generate a random number from the exponential distribution
with mean parameter . [30%]
Hint: We recall the Inverse transform technique to simulate random numbers
according to a given distribution: let U be a uniform random variable
in the range (0, 1). If X = F
1
(U), then X is a random variable with cumulative
distribution function FX(x) = F. The inverse of the exponential
cumulative distribution function is ln(1 u).
– 3 – See Next Page
7CCMFM13
3. (i) Give an implementation of the class MyVector. It should have a member
variable size and a member variable ptr containing a pointer to a memory
location containing an array of ints. Show how would you implement the
following:
(a) A constructor which takes as parameter a given size; [15%]
(b) A destructor; [15%]
(c) The other functions required by the rule of three; [15%]
(d) A subscript operator [] which returns a modifiable value; [15%]
(e) A + operator to add two MyVector instances with the same size.
[15%]
(ii) Redesign the class MyVector by introducing a new member variable capacity
which stores the size of the storage space currently allocated for
MyVector, expressed in terms of elements (dont write the implementations
of the functions). Note that this capacity is not necessarily equal to
the vector size. It can be equal or greater, with the extra space allowing to
accommodate for growth without the need to reallocate on each insertion.
Write a function push back(int) that inserts a new int in MyVector.
[25%]
– 4 – See Next Page
7CCMFM13
4. We consider the following class defining the BlackSholes model:
class BlackAndSholes{
public:
double S0;
double sigma;
double interest_rate;
double date;
BlackAndSholes();
vectordouble generateRiskNeutralPricePath(double T,
int NSteps) const;

};
The following function prices a put option by Monte-Carlo in the BlackSholes
model.
double PriceByMonteCarlo(const BlackAndSholes model, const
PutOption option, int NSimulations, int NSteps)
{
vectordouble path(NSteps, 0.0);
double total=0.0;
double payoff;
for (int i=0;iNSimulations; i++)
{
path=model.generateRiskNeutralPricePath(option.GetMaturity(),
NSteps);
payoff=option.payoff(path.back());
total+=payoff;
}
double mean=total/NSimulations;
double r=model.interest_rate;
double T=option.GetMaturity()-Model.date;
return exp(-r*T)*mean;
}
class PutOption{
private: double T, K;
public:
double payoff(double x) const;
double GetMaturity() const{return T;};- 5 – See Next Page
7CCMFM13};
double PutOption::payoff(double x) const
{if (xK) return K-x;
return 0;}
In the above code, the function generateRiskNeutralPricePath generates
a price path under the risk neutral probability measure in the BlackSholes
model.
(i) How would you redesign your code in order to include the ability to price
a Call Option using only one function PriceByMonteCarlo? Provide
an implementation only for the specific functions to the Call Option.
[20%]
(ii) Illustrate your code by showing how to price a Digital Put Option.
Provide an implementation only for the specific functions to the Digital
Put Option. [20%]
(iii) We now aim to include the ability to price an Arithmetic Asian Call,
with minimum changes on the already existing code and classes. What
design changes would you make to the function PriceByMonteCarlo
and to the hierarchy of classes ? Provide an implementation only for the
specific functions to the Arithmetic Asian Call.
Hint: The payoff of an Arithmetic Asian Call is defined as follows:
max(A(0, T) K, 0),
with A(0, T) = 1n+1Pn
i=0 S(ti), where ti = iTn, 0 i n. [20%]
(iv) Illustrate your code by showing how to price a up-and-out knock-out
call option. Provide an implementation only for the specific functions
to the up-and-out knock-out call option.
– 6 – See Next Page
7CCMFM13
Hint: We recall that an up-and-out knock-out call option with maturity
T and strike K and barrier B has a payoff given by:

max{ST K, 0} if St B for all t [0, T]
0 otherwise.
[20%]
(v) How would you test the function PriceByMonteCarlo? [20%]
– 7 – See Next Page
7CCMFM13
5. We consider the following two classes: BinModel and DiscreteTimePut. The
class BlackAndSholes used here is the one defined in the previous exercise.
class BinModel{
private: double S0,R,D,U;
public: …
void SetStock(double);
void SetU(double);
void SetD(double);
void SetR(double);
};
class DiscreteTimePut{
private:
int N;
double K;
public: …
double payoff(double x) const;
void SetN(int);
void SetK(double);
double PriceBySnell(const BinModel ) const;
};
double DiscreteTimePut::payoff(double x) const
{if (xK) return K-x;
return 0;}
In the above code, the function PriceBySnell computes the price at time 0 of
an American Put in the Binomial Model.
(i) Write a function BlackSholesToBinomial(const BlackAndSholes
Model, double T, int N) which returns the corresponding object of
type BinModel.
Hint: Recall that you have to divide the time interval [0, T] into N steps
of length h =TN, and set the parameters of the binomial model to be
– 8 – See Next Page
where is the volatility and r is the continously compounded interest rate
in the Black-Sholes model. [20%]
(ii) Rewrite the function BlackSholesToBinomial that we rename ToBinomial
as a member function of the class BlackAndSholes. [20%]
(iii) Write a function AmericanPutBlackSholes(const BlackAndSholes
Model, double T, double K, const DiscreteTimePut option)
which returns an approximation of the price of the American put in the
Black-Sholes model by means of the binomial approximation (the number
of steps N should be defined in this function). [20%]
(iv) Write a new class DiscreteTimeCall and a new function AmericanCallBlackSholes(const
BlackAndSholes Model, double T, double
K, const DiscreteTimeCall option) which returns an approximation
of the price of the American Call in the Black-Sholes model by
means of the binomial approximation. [20%]
(v) Which aspects of the design do you find unsatisfactory? What solution
do you propose? [20%]
– 9 – Final Page

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导