Vous êtes sur la page 1sur 7

THE UNIVERSITY OF NEW SOUTH WALES

SCHOOL OF MATHEMATICS AND STATISTICS

MATH3311 Mathematical Computing for Finance


MATH5335 Computational Methods for Finance
ASSIGNMENT 3 Session 2, 2017
Due: 2pm Wednesday 20 September (Week 9)
Please read the new instructions carefully:

Upload the appropriate files by uploading them using the Assignment 3 link on
the course web page on UNSW Moodle. You may leave it as draft.

@Math3311: Write a Matlab file A3.m which contains your answers to the
assignment questions. The plot Figure1.pdf should contain your plot of the
function in question a).
Upload the following files:
a) A3.m
b) A3report.txt
c) Figure1.pdf
The file A3report.txt can be generated by running the file A3marking.p. Do
not edit the file A3report.txt. The file A3marking.p can be downloaded
from the Moodle course page.
@Math5335: Write a Matlab file A3.m which contains your answers to the
assignment questions a) and b). Write Matlab functions naiveifft.m and
radix2ifft.m which contain your answers to question c). The plot Figure1.pdf
should contain your plot of the function in question a).
Upload the following files:
a) A3.m
b) Figure1.pdf
c) naiveifft.m
d) radix2ifft.m
e) A3report.txt
f) A3ifftreport.txt
The file A3report.txt can be generated by running the file A3marking.p.
The file A3ifftreport.txt can be generated by running the file testifft.m.
Do not edit the files A3report.txt and A3ifftreport.txt. The files
A3marking.p and testifft.m can be downloaded from the Moodle course page.

1
Do NOT submit any .zip, .tar, .7z or similar file. The files you upload must be
exactly as stated above. If you are asked to upload a file called A3.m, I will run
a script file which checks whether a file called A3.m exists. If your file A3.m is
in a .zip file, the script file will not find it and will award 0 marks for it.

Information on how to use the marking file:

Store your assignment script A3.m and the marking file A3marking.p in the
same folder.
Run the file A3marking.p. It will produce another file called A3report.txt
which will give you feedback on the results produced by your file A3.m.
You can run the file A3marking.p as many times as you like.
The suggested marks in the file A3report.txt only give an indication. Your
marks will depend on how you obtained the answer, which is not checked by the
marking program.

To produce the file A3ifftreport.txt, run the program testifft.m. This program
will test your Matlab functions using random numbers as input. If your program
is correct, the errors should all be smaller than 1010 .

In the beginning of each file include the following information:

% Given Name Surname


% Student Number
% Course (either Math5335 or Math3311)

The file A3report.txt gives feedback on your assignment and suggested marks. The
suggested marks are not final, the final marks depend on how you obtained the
answers to the questions.

The file A3report.txt produced by the marking file should not contain any unnec-
essary output.
For instance, if a question asks you to compute, say, a certain norm and store it in the
variable normchk, then you should NOT display the value of normchk. The marking
program will automatically check the value you computed and hence displaying it is
not necessary. You should only display a value if you are specifically asked to do so.
Marks will be subtracted if the report file contains more than 50 lines of output
generated by your program.

If you are asked to create a plot, save the plot as FigureX.pdf, where X is 1 for the
first plot, 2 for the second plot, and so on. Upload the plots on Moodle additionally
to the files above.

2
You may discuss your assignment with others. However, the program
must be written by yourself.
See https://student.unsw.edu.au/plagiarism

Late assignments will only be accepted on documented medical or compassionate


grounds.

You may upload each file separately and you may upload the files any number of
times. Only the last submission will be marked. You may leave your assignment as
draft, i.e. you do not have to click submit.
Note that once you click the submit button you cannot change the file anymore. If
you clicked submit but would still like to make changes to your submission, then
please send me an email and I can revert your submission back to draft which will
allow you to make changes.

In order to mark your assignment, I will run your Matlab scripts and Matlab
functions with file names as specified above and check whether the numerical values
your program produces are correct. Note that it is very important to store results
using the correct name.

Use the file name exactly as written above. If you are asked to create a Matlab file
called AQ1.m, then save it exactly under that name. Files saved as aq1.m, Aq1, Aq.m,
aQ1.txt and so on will not run and be marked as wrong.

Do not use any variable name which include the word test or sol, i.e., do not
use names like ctest, c test, msol, n sol and so on;
Suppress lengthy output using ;
Do not use the Matlab command clear or clc in your m-file.
Not following these instructions may yield a reduction in your marks.

Your Matlab script must compute the answers directly. Just providing the answer
(even with a description how the answer was computed) is not acceptable. For
instance, if you are asked to compute sin(2) and store it as s2, write

s2 = sin(2);

You do not get marks for writing

% s2 = sin(2);
s2 = 0.909297426825682;

even if the answer is correct.

Numerical answers must be given as floating point number, or if requested, as integer.

3
Questions
a) Motivation: In this question we use the Newton method, secant method, and the
Matlab function fzero. In lectures (Lecture 07) we have seen that these methods
can for instance be used to find the implied volatility of financial assets.

The goal is to approximate the value z > 0 for which the function

f (z) = ez 2z 2 (1)

satisfies f (z ) = 0.
i) Define an anonymous function f given by (1) and the derivative df given by
f 0 (z) = ez 4z. The function should accept vectors as input.
Hint: For instance, the function g(z) = ez can be defined as an anonymous
function by
g = @(z) exp(z);
ii) Use plot to find a starting value close to z . Store the starting value as the first
element in a vector named zn.
iii) Use the Newton method to approximate the root with the starting value deter-
mined in the previous question. Store the values in the vector zn such that the
value obtained after k iterations is zn(k+1). Produce a table of the form:
Newton Method
Iteration Approximation Function value
Start val X.XXXXe+XX X.XXXXe-XX
1 X.XXXXe+XX X.XXXXe-XX
2 X.XXXXe+XX X.XXXXe-XX
3 X.XXXXe+XX X.XXXXe+XX
(The number of rows will depend on how many iterations you used.)
iv) Use the Secant method to approximate the root. Store the approximations in a
vector called zs. Produce again a table as for the Newton method.
v) Use the Matlab function fzero to approximate z . Store the approximation
in the variable zm and the function value as fzm. Use fprintf to print the value
of the approximation and the function value.
vi) Compute the differences between the approximations:
diffns = zn(end) - zs(end)
diffnm = zn(end) - zm
diffsm = zs(end) - zm
To get full marks, the absolute values of these differences have to be smaller
than 1015 .

4
b) Motivation: Finite difference methods are an important method for approximating
solutions of differential equations. We have seen an example in Lecture 05, pp. 13
15, where the solution of a boundary value problem was approximated using finite
differences. A similar method can be used for initial value problems, as in the first
part below. Finite difference methods will be an important topic again in Lecture 11
when we discuss solving the Black Scholes PDE in pricing financial options.
The following example is chosen for practice. An approximation to the solution is
implemented in Matlab (besselj). This gives us an easy way to check the accuracy
of our solution.

Consider the initial value problem

x2 u00 (x) + xu0 (x) + (x2 2 )u (x) =0 0 < x < T, (2)


u (0) = ,
u0 (0) = .

Set = 1, 1 = 0, 1 = 1/2 and T = 10. For N N let xn = T n/N for n =


0, 1, . . . , N . The goal is to estimate the function u1 (x).
Let
Un u1 (xn ), n = 0, 1, . . . , N.

i) Define a variable N in Matlab and set it to 106 . Define a variable T in Matlab


and set it to 10.
ii) Define a row vector x such that the nth entry is (n1)T /N for n = 1, 2, . . . , N +1.
iii) Define a row vector U of length N + 1 in Matlab. Using the initial values, set
U(1) to 0 and use the approximation
1 U1 U0
u01 (0) =
2 T /N
to define U(2).

Note that because an index in Matlab cannot be 0, in the implementation we


start from U(1) rather than U(0). Thus the value Un is stored in Matlab in
U(n+1).
iv) We use a central difference method to approximate u001 , that is,
Un+1 2Un + Un1
u001 (xn ) . (3)
T 2 /N 2
We use a forward difference method to approximate u01 , that is,
Un+1 Un
u01 (xn ) . (4)
T /N

5
Substitute (3) and (4) into (2) to obtain an expression involving Un+1 , Un , Un1 .
See Lecture 05, pages 13 and 14, to give you an idea on how to do this. Note
that in the lecture example it was necessary to solve a linear system; this is not
necessary here, as you can solve for Un+1 and obtain a function in terms of Un
and Un1 . Then, using the initial conditions, compute the values of Un+1 for
n = 1, 2, . . . , N 1.
v) The solution to the above equation is the Bessel function of the first kind with
index 1. In Matlab this function can be computed using besselj(1,x). Com-
pute the infinity norm of the difference between your approximation U and
besselj(1,x) (where x is from part ii)). Store the difference in the variable
normbessel1 and use fprintf to print the value of normbessel1. To get full
marks, this error has to be smaller than 105 .
vi) We now want to approximate the Bessel function of the first kind of index 2,
i.e., we consider the same problem as in c) except we set = 2. (Choose N , T ,
and x as before.)
It is known that this Bessel function has series expansion

X (1)m  x 2m+2
u2 (x) = . (5)
m=0
m!(m + 2)! 2

As in iv), use a finite difference method together with some suitable conditions
to approximate the solution to (2). Store your finite difference approximations
in the row vector V. Use (5) to obtain initial or boundary conditions which allow
you to approximate the solution accurately (accurately is made more precise
in the next question).
The difficulty in this question is to find suitable conditions which allow you
to obtain an accurate approximation. You may have to try out a number of
different ways of computing an approximation. Be creative. (Try variations of
the method of Lecture 05, pages 1316. See also Lecture 07 where we discussed
approximations of derivatives.)
vii) In Matlab the solution for (2) can be computed using besselj(2,x). Compute
the infinity norm of besselj(2,x) - V, where x is as defined in ii), and store it
in the variable normbessel2. Use fprintf to print the value of normbessel2.
To get full marks, this error has be smaller than 106 .

Math5335 only
Motivation: In the following question we implement the fast Fourier transform. The fast
Fourier transform has led to huge advances in many areas of science, engineering and com-
putational finance. In answering the question below you should get a better understanding
of the underlying idea of the fast Fourier transform, which may be useful since similar ideas
can be employed in many applications.

6
c) For data points x0 , . . . , xN 1 R the Fourier transform is given by
N
X 1
yk = xn e2ikn/N . (6)
n=0

The inverse Fourier transform is given by


N 1
1 X
xn = yk e2ikn/N . (7)
N k=0

Download the file testift.m, store it in the same directory as your files. This
program tests your programs using a random input vector and stores the result in
the file A3ifftreport.txt.

i) Write a Matlab function called naiveifft.m which computes the inverse


Fourier transform (7) using a straight forward algorithm (using only +, ,
, /, exp, for, while and so on, but not fft, ifft or other built-in functions
from Matlab). The input is a row vector of length N and the output should
also be a row vector of the same length.
ii) Write a Matlab function called radix2ifft.m which implements the radix
2 algorithm of the fast Fourier transform. You can either modify the Mat-
lab function radix2fft.m (which implements the Fourier transform using a
recursive algorithm), or come up with your own version.
The function radix2fft.m can be downloaded from the course Moodle page
in the folder Lecture 06. An explanation of the idea of this program is in the
handwritten notes Lecture06notes.pdf, also in the folder Lecture 06. This was
discussed at the beginning of the lecture in Week 7.
You can only use +, , , /, exp, for, while and so on, but not fft, ifft or
other built-in functions from Matlab.
The input is a row vector of length 2m for some non-negative integer m. The
function should return an error message if the length of the input vector is not
of length 2m for some non-negative integer m.
iii) The point of the fast Fourier transform is the improvement in speed, your pro-
gram should run fast. The testing script testifft.m in fact checks how fast
your program runs.
Make your program faster and/or more accurate than the program myradix2ifft.p.

Vous aimerez peut-être aussi