You are on page 1of 5

UNIVERSITY OF NEW SOUTH WALES

SCHOOL OF MATHEMATICS AND STATISTICS


MATH5335 Computational Methods for Finance
Session 2, 2015
Matlab Computing Test
Time allowed: 90 minutes
Name Student number Signature

Instructions — Read Carefully


• Create script M-files q1.m, q2.m, q3.m, q4.m and q5.m containing your answers to
Questions 1, 2, . . . , 5, respectively.

• Question 5 also requires you to define a function M-file q5pdf.m, in addition to q5.m.

• Questions 1, 2, 3, and 4 are each worth 10 marks, while Question 5 is worth 15 marks.

• Each script must require no interactive input, or depend on any variables defined
outside the script. To obtain the output for Question 1, you must be able to type

>> clear
>> q1

Do not include the clear statement in your scripts q1.m, q2.m etc.

• Use semicolons to avoid producing unnecessary output.

• Save all files in your home folder. Do NOT create any sub-folders.

• No materials are allowed, except a pen.

• When you first start Matlab, typing

>> what

should list the M-files gauleg.m and the MAT file q3val.mat. At the end you should
also see your M-files q1.m, q2.m, ...

Login and start Matlab


Do not turn over the page until instructed to do so

1
Question 1 (10 marks): Answer in the file q1.m
Let n = 10. Consider the n × n matrix
 
2 0 1 0 3 0 0 0 0 0
 0 2 0 1 0 3 0 0 0 0 
 
 0 0 2 0 1 0 3 0 0 0 
 
 0 0 0 2 0 1 0 3 0 0 
 
 3 0 0 0 2 0 1 0 3 0 
A=  −2
.
 3 0 0 0 2 0 1 0 3 

 0 −2 3 0 0 0 2 0 1 0 
 
 0
 0 −2 3 0 0 0 2 0 1 

 0 0 0 −2 3 0 0 0 2 0 
0 0 0 0 −2 3 0 0 0 2

(a) Define the variable n = 10

(b) Construct A efficiently.

(c) Find the SV D factorization of A. (Hint: help svd)

(d) Verify your result by computing, and then printing out, the value of the matrix
norm kA − U SV T k∞ , where U, S, V are the matrices obtained from the SV D factor-
ization.

(e) Compute the largest and smallest singular value of the singular value decomposition
storing it in the variables lsv and ssv.

(f) Compute the condition number in 2-norm of the matrix A using the singular values
of A.

(g) Define b = (1, 2, 3, . . . , n − 1, n)T .

(h) Solve Ax = b using the singular value decomposition of A storing the answer in the
variable xsvd.

Question 2 (10 marks): Answer in the file q2.m


Consider the n × n covariance matrix
 
1 0 0 −φ 0 0 0 0 ... 0
 0 1 + φ2 0 0 −φ 0 0 0 ... 0 
 
2
 0
 0 1 + φ 0 0 −φ 0 0 . . . 0 

−φ 2
 0 0 1+φ 0 0 −φ 0 ... 0 
2
 0 −φ 0 0 1 + φ 0 0 −φ . . . 0 
C =  ..
 
.. .. .. .. .. .. .. .. 
 . . . . . . . . . 
2
 
 0 . . . 0 −φ 0 0 1 + φ 0 0 −φ 
2
 
 0 ... 0 0 −φ 0 0 1+φ 0 0 
1 + φ2 0 
 
 0 ... 0 0 0 −φ 0 0
0 ... 0 0 0 0 −φ 0 0 1

(a) Define n = 100 and φ = 0.99.

2
(b) Construct the n by n matrix C storing only the non-zero elements (Hint: spdiags).

(c) Compute the eigenvalues of C storing them in the vector ev.

(d) Compute the inverse matrix C −1 using inv. In Figure 1 create a spy plot of C −1
(Hint: spy).

(e) Use the Matlab command symamd to re-order the rows and columns of C. Store the
re-ordered matrix in the variable B.

(f) Compute the inverse B −1 of B and in Figure 2 create a spy plot of B −1 .

Question 3 (10 marks): Answer in the file q3.m


The file q3val.mat holds two arrays

tdata = [t1 , t2 , . . . , tn ] and ydata = [y1 , y2 , . . . , yn ].

(a) Use load to create tdata and ydata.

(b) Find the value of n and store it in ndata.

(c) Use a function of the form

y = x1 + x2 t + x3 exp(t)

to approximate the data.

(d) Print out the values of the coefficients x1 , x2 and x3 , with at least 4 significant figures.

(e) Calculate the cubic spline that interpolates the data which has slope 0 at the first
and last data points.

(f) On the same axes, plot the data, the least-squares fit and the cubic spline interpolant
for t ∈ [0, 1]. Make sure you include a legend.

Question 4 (10 marks): Answer in the file q4.m


In this question we use the fast Fourier transform to perform a matrix-vector multiplication
for circulant matrices.

Background
A matrix C = (ci,j )1≤i,j≤n is circulant if ci,j = di−j for some values d−n+1 , d−n+2 , . . . , d−1 ,
d0 , d1 , . . . , dn−1 which satisfy di+n = di for −n < i < 0. Thus a circulant matrix is of the
form  
d0 dn−1 dn−2 . . . d1
 d1 d0 dn−1 . . . d2 
 
 d2 d d . . . d
C= 3 .
1 0

 .. ... . . . . . . .. 
 . .
dn−1 dn−2 dn−3 . . . d0

3
Let y = (y0 , y1 , . . . , yn−1 )> be a vector of length n. The Fourier transform is given by
n−1
X
Fk (y) = ybk = yj e−i2πjk/n for k = 0, 1, . . . , n − 1. (1)
j=0

In Matlab, the Fourier transform (1) can be computed using fft.


Let y y0 , yb1 , . . . , ybn−1 )> . The inverse Fourier transform is given by
b = (b
n−1
1X
Fj−1 (b
y ) = yj = ybk ei2πjk/n for j = 0, 1, . . . , n − 1.
n k=0
In Matlab, the inverse Fourier transform can be computed using ifft.
It is known that the linear system Cx = b is equivalent to
Fk (b) = Fk (d)Fk (x) for k = 0, 1, . . . , n − 1,
where d = (d0 , d1 , . . . , dn−1 )> is the first column of the circulant matrix C and x =
(x0 , x1 , . . . , xn−1 )> .
Thus we can solve the linear system Cx = b by computing
Fk (b)
x
bk = for k = 0, 1, . . . , n − 1, (2)
Fk (d)
setting x
b = (b
x0 , x bn−1 )> and then computing
b1 , . . . , x
xj = Fj−1 (b
x) for j = 0, 1, . . . , n − 1.

Matlab questions
(a) Define n = 1000.
(b) Set c = [1, 2, 3, . . . , n].
(c) Use

C = gallery(’circul’,c);

to generate a circulant matrix C. The first row of the matrix C is then the vector c.
(d) Generate a column vector of length n of standard normal random variables using the
Matlab command randn. Store this vector in the variable b.
(e) Solve the linear system Cx = b using the Matlab backslash operator and store the
solution in the vector xbs.
(f) Use the Matlab command fft to compute the Fourier transform of the vector b and
store it in the vector bfft.
(g) Define d to be the first column of the matrix C. Compute the Fourier transform of
d and store it in the vector dfft.
(h) Compute x b as given by the formula (2) using coordinate-wise division and store it in
the vector xfft.
(i) Compute the inverse Fourier transform of the vector xfft using the Matlab command
ifft and store the result in the vector x.
(j) Compute the infinity norm of xbs - x and store the result in nrmchk.

4
Question 5: Answer in the files q5pdf.m and q5.m
Consider the following distribution with real parameters µ and s > 0 with probability
density function
  
2π x−µ x−µ

 + sin π for µ ≤ x ≤ µ + s;
f (x) = s(4 + π) s s (3)

0 otherwise.

(a) (5 marks): Answer in the file q5pdf.m

i) Write a function M-file q5pdf.m that defines the function


f = q5pdf(x, mu, s)
to calculate f (x), where mu and s hold the values of µ and s respectively.
ii) If the input argument x is a vector then the function should produce an array
of function values of the same size.
iii) Include comments at the beginning of your function so that
help q5pdf
explains input and output arguments and the purpose of the function.

(b) (10 marks): Answer in the file q5.m

i) Define variables for µ = 40, s = 30, N = 80 and the interval [a, b] = [µ, µ + s].
ii) Use the Matlab function gauleg to calculate N Gauss-Legendre nodes and
weights for the interval [a, b].
iii) Use your function q5pdf to calculate the probability density function f , which
is given by (3), at the Gauss-Legendre nodes.
iv) Plot the probability density function f which is given by (3).
v) The payoff function for a put option with strike K = 50 and asset value Y is

PayOff(Y ) = max(K − Y, 0).

Suppose Y has the above distribution with µ = 40 and s = 30. Estimate


Z ∞
E[PayOff(Y )] = Payoff(y)f (y) dy,
0

and print your estimate to 6 decimal places.

You might also like