Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


COMP0043 Numerical Methods for Finance

Assignment 1, to be done after Section 2 Fundamental probability distributions

1. You have been provided scripts that plot the probability density function (PDF) and the cumulative distribution function (CDF) of the exponential, normal, lognormal and non-central χ2  distributions. While the CDF can be computed analytically for some distributions like the exponential one, this is not possible for others like the normal.

Modify the script normal.m so that the CDF is found besides MATLAB’s built-in command cdf also with the approximation

(a) In the simplest case where all integration weights wi  = 1, this can be done with

cumsum. Show that this reproduces approximately the result of cdf. (b) Repeat (b) using a for loop and sum rather than cumsum.

(c) If the first and the last integration weights are  1/2 while the others are  1 (trapezoidal rule), this can be done substituting sum with trapz.  Show that this reproduces better the result of cdf.

(d) Repeat (c) using a for loop and neither sum or trapz.

(e) Repeat (c) without a for loop, applying a correction vector to cumsum.          (f)  Output the first 10 and the central 10 elements of the result vectors of (a)–(e). (g) Plot the CDF obtained with cdf, cumsum and trapz.

Numerical integration is also called quadrature.  For further reading, see Chapter 4 of Numerical Recipes, especially Sections 4.0–4.2 which include the trapezoidal rule, and Chapter 11 of Tools from Stochastic Analysis for Mathematical Finance: A Gentle Introduction.

 

2.  Generate two n x n random matrices A and B using the function rand(); start with n = 3.

(a)  Compute the matrix product C = AB using MATLAB’s built-in matrix product operator * (or the function mtimes).

(b)  Compute C = AB from the definition

using three for loops.


(c) Repeat (b) swapping the two outer loops over i and j .

(d) Repeat (b) with two for loops over i and j and sum() in place of the for loop over k .

(e) Repeat (d) transposing A to increase the data locality and thus the cache hits with sum().

(f) Repeat (a–e) for n = 10, 100, 1000 suppressing the output and monitoring the CPU time with tic and toc. Be patient with for loops when n = 1000.

(g) Find how the CPU time of matrix multiplication scales with the matrix size n: produce a double logarithmic plot for Matlab’s built-in product operator (use n = 10, 20, 50,  100, 200, 500,  1000, 2000, 5000) and for the triple loop (use n = 10,  20,  50,  100,  200,  500,  1000); fit the data with fit.

For further reading, see Section 2.11 of Numerical Recipes, which briefly introduces Strassen’s algorithm for matrix multiplication.