C++代写-7CCMFM13
时间:2021-04-29
7CCMFM13
King’s College London
University Of London
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 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.
2020 c©King’s College London
7CCMFM13
1. (i) Give an explicit code example of a useful function that takes double
valued parameters by reference.
[20%]
(ii) What is wrong with the following code?
double* MyFunction ()
{
double ret[]={-1.5,4.2,3.0,1.0,-5.2};
return ret;
}
[10%]
(iii) Write a function sum which takes as input a vector of doubles (using
) and which returns the sum of the elements of the vector.
[20%]
(iv) The following code contains some bugs. Fix them.
# include
# include
using namespace std;
int main(){
cout<<"Type 0 for stone, ";
cout<<"1 for scissors, 2 for paper \n";
cout<<"Enter player 1’s move \n";
cin>>player1;
cout<<"Enter player 2’s move \n";
cin>>player2;
if (player1=player2) {
cout<<"Its a draw \n";
} else {
diff=player1-player2;
if (diff==-2 || diff==1){
cout<<"Player 1 won \n";
} else {
cout<<"Player 2 won \n";
- 2 - See Next Page
7CCMFM13
}
}
}
[10%]
(v) Give an example of a class that needs a virtual destructor and show the
syntax used to define a virtual destructor.
[20%]
(vi) Write three functions which compute the factorial of a natural number n
(i.e. n!) using: a for loop, a while loop and a do-while loop.
[20%]
- 3 - See Next Page
7CCMFM13
2. Recall Simpson’s rule for estimating the integral of a smooth function
f : [a, b] −→ R.
Given an even number n, the Simpson’s rule estimate for∫ b
a
f(x) dx
is:
h
3
(f(x0) + 4f(x1) + 2f(x2) + 4f(x3) + 2f(x4) + . . .
+2f(xn−2) + 4f(xn−1) + f(xn))
where h = (b− a)/n and xj = a+ jh.
(i) Write a C++ function to compute the Simpson’s rule estimate for∫ b
a
e
−x2
2 dx
in n steps.
[50%]
(ii) How does your code ensure that the parameters a, b and n are valid?
[10%]
(iii) How would you test your code?
[10%]
(iv) Describe how you would modify your code so that it can integrate any
real valued function f .
[30%]
- 4 - See Next Page
7CCMFM13
3. Give an implementation of the class MyMatrix. It should have a member variable
NRows, a member variable NCols, a member variable Data containing a pointer
to a memory location containing an array of doubles and a member variable
EndPointer containing a pointer to one after the end of data (all private). The
cell (i, j) will be stored at the location Data+j*NRows+i. Show how you would
implement the following:
(i) A constructor which takes as parameters a given number of rows and
columns, as well as a default value with which all the elements of the
matrix should be initialised; [20%]
(ii) A destructor; [20%]
(iii) The other functions required by the ”rule of three”; [20%]
(iv) A subscript operator ”()” which returns a modifiable value and which can
be used with a non-const MyMatrix instance; [20%]
(v) A ”+” operator to add two MyMatrix instances with the same number of
rows and columns. [20%]
Note: If you need to use in the implementation of the above functions some
other functions which have not been declared or defined, you are only asked to
declare them without providing any implementation for them.
- 5 - See Next Page
7CCMFM13
4.(i) (a) Write a function randuniform which generates a uniformly distributed
random number in the range (0, 1) (we assume that we have declared an
object mt19937 MersenneTwister).
[20%]
(b) The Box-Muller algorithm is a method of generating normally distributed
random numbers. If U1 and U2 are uniformly distributed random numbers
on the interval (0, 1) then define
X1 =

−2 log(U1) cos(2piU2)
X2 =

−2 log(U1) sin(2piU2).
X1 and X2 will now be independent normally distributed random numbers
of mean 0 and standard deviation 1.
Write a function that uses the Box-Muller algorithm to generate two inde-
pendent normally distributed random numbers. You should assume that
we have a global variable pi containing the value of pi.
[20%]
(ii) A trader is interested in computing the probability P(ST > K), where ST
is the value at time T of an asset that follows the Black-Scholes model, and
K ∈ R+. You may assume you are given the class BlackScholesModel . Com-
plete the definition of the function to evaluate this probability by Monte Carlo
simulation double ProbByMC(BlackScholesModel & m , double K, double
T). You may assume you are given a member function of the class BlackSc-
holesModel , called double generateFinalValue(double T ) which generates
independent simulations of the final value ST at time T .
[30%]
(iii) Explain what changes you would make in order to generalize the code from the
previous question to permit you to compute P(f(ST ) > K) for any function
f : R→ R and K ∈ R+.
[30%]
- 6 - See Next Page
7CCMFM13
5. We consider the following class defining the Black&Scholes model:
class BlackAndScholes{
public:
double S0;
double sigma;
double interest_rate;
double date;
BlackAndScholes();
vector generateRiskNeutralPricePath(double T,
int NSteps) const;
...
};
The following function prices a Put option by Monte-Carlo in the Black&Scholes
model.
double PriceByMonteCarlo(const BlackAndScholes& model, const
PutOption& option, int NSimulations, int NSteps)
{
vector path(NSteps, 0.0);
double total=0.0;
double payoff;
for (int i=0;i{
path=model.generateRiskNeutralPricePath(option.GetMaturity(),
NSteps);
payoff=option.payoff(path.back());
}
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;};
- 7 - See Next Page
7CCMFM13
};
double PutOption::payoff(double x) const
{
if (K>=x) return K-x;
return 0;
}
In the above code, the function generateRiskNeutralPricePath generates a
price path under the risk neutral probability measure in the Black&Scholes
model.
(i) Explain how you would redesign the code in order to include the ability to price
any derivative whose payoff only depends on the price at maturity using only
one function PriceByMonteCarlo. Do not completely rewrite the code, just
describe the key changes.
[25%]
(ii) (a) Write a function DeltaByMonteCarlo which computes the delta, ∆, of a
Put Option by Monte Carlo.
Hint: We recall here the following algorithm which might be used in order
to compute the delta
• Choose a small value for h, say h = S0∗10−6, where S0 is the initial
stock price.
• Generate N stock price paths.
• Use the Monte Carlo method to compute the price of the option
when the initial stock price is taken to be S0 + h.
• Use the Monte Carlo method with the same price paths to compute
the price of the option when the initial stock price is taken to be
S0 − h.
• Estimate the delta using the central difference estimate for the deriva-
tive:
f ′(x) ≈ f(x+ h)− f(x− h)
2h
.
- 8 - See Next Page
7CCMFM13
[40%]
(b) Briefly explain how you would test the function you write in (a).
[20%]
(iii) What design changes would you make so the function PriceByMonteCarlo
can be used for pricing with models other than the BlackScholesModel? (just
describe the key changes, without writing the code).
[15%]
Note: If you need to use in the implementation of the above functions some
other functions which have not been declared or defined, you are only asked to
declare them without providing any implementation for them.
- 9 - Final Page


essay、essay代写
essay、essay代写