Matematica per l’Economia e la Finanza Part I: Matlab Programming and Optimization Methods in Portfolio Management. M. Dolfin Univ. of Messina - Italy https://www.marinadolfin.it UNIME, April 2017 Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Outline Part I Matlab programming and optimization methods in portfolio management. Part II Monte Carlo simulations - Option Pricing with Binomial lattices and Black-Scholes formula Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Motivation Why Matlab Both basic and sophisticated functions; built-in functions or personally designed M-files; rich set of graphical capabilities; useful toolboxes; widely diffused; efficiently used in computational finance. Many functionalities within a single integrated framework. www.mathworks.com Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Motivation Need for theory Developing your own solutions; choose the most appropriate algorithm; use methods properly; understand what is going wrong. Computational mathematics may be quite different from ’pencil-and-paper’ mathematics. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Readings P. Brandimarte, Numerical Methods in Finance and Economics. A Matlab-based introduction, Wiley-Interscience (2006). W. J. Palm III, Introduction to Matlab for Engineers (3rd ed.), McGraw-Hill (2011). www.mathworks.com Further readings M. J. Miranda, P.L. Fackler, Applied computational Economics and Finance, MIT Press, Cambridge (2002) Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Start Matlab Standard desktop configuration Current Directory (top left). Command Window (center). Workspace (top right). Detail (bottom left). Command History (bottom right). Symbols of basic operations: + - / * ˆ \ = to register a value into a specific variable. == Equality operator. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Exercises Compute 6 10 13 + 18 + 5(92 ) 5(7) (ans = 410.1297) 6(351/4 ) + 140.35 (ans = 17.1123) Ex.1 A cylinder tank has height h = 15m and radius r = 8m. Compute the radius of a tank with the same height and a volume 20% greater (V = πr 2 h). (ans = 8.7636) Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Numerical arrays Ordered collection of numbers x = [0, 4, 3, 6] y = [2, 7, 1, 6] z = x + y to sum two arrays Equally spaced numbers: an example u = 0 : 0.1 : 10; w = 5 ∗ sin(u); plot(u, w) Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Ex.2 Plot the function s = 2 sin(3t + 2) + √ 5t + 1 over the interval 0 ≤ t ≤ 5, in the same window of the plot of the previous function. Put a title and properly label the axis. Edit the plot and change the thickness of the line and the fonts. Save the figure in the desktop. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Matrices You may think matrices as a collection of numbers z columns }| { (1 3 5 2 rows 8 4 2 0 7 0 2 8 in a square format Example: index the rows by companies (stocks) and the columns by dates. Sensible data: stock price on a day. dates z }| { (400 500 5000 companies 200 100 4500 100 100 5500 Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Matrix dimension Number of rows first followed by the number of columns. 1 3 5 2 A = 8 4 2 0 7 0 2 8 A3×4 ; A(3 × 4) One may use the notation (days × #assets) to identify not only the size of the matrix but also the nature of the information. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Tables Information are appended on the left and top borders Mon Tue Wed Master in Economia Bancaria e Finanziaria Bond 54 55 56 Stock 21 18 27 M. Dolfin - Univ. of Messina - ITALY Example: Prices of two assets (a bond and a stock) One bond costs $54 and one stock costs $21; a two-element row vector describes the data p = 54 21 whose entries are the prices (expressed in an appropriate unit of measure). Numbers of shares of the two securities Two-element column vector 1 n= , D(days × #securities) 2 One can evaluate the portfolio with the matrix product. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Rules of multiplication In symbols P = p1 p2 ; q1 Q= q2 V = p1 · q1 + p2 · q2 Matrix multiplication - Rule The numbers of columns in the first matrix must be equal to the number of rows in the second → the inner dimensions of the two matrix must be equal. (m × p) · (p × n) → (m × n) Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Rules of multiplication In symbols p11 q1 + p12 q2 p11 p12 p21 p22 q1 = p21 q1 + p22 q2 q2 p31 q1 + p32 q2 p31 p32 (m × p) · (p × n) → (m × n) (3 × 2) (2 × 1) → (3 × 1) Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Ex.3 Portfolio evaluation: multiply the number of shares of each security by the corresponding price of shares of each security and then sum the results Matrix of Prices Matrix of Quantities P(1 × #assets), Q(#assets × 1), Value = product of price and quantity Master in Economia Bancaria e Finanziaria P = 54 21 . 1 Q= . 2 V =P∗Q. M. Dolfin - Univ. of Messina - ITALY Ex.3 (with different values) - Value of a portfolio on 3 different days Prices Mon Tue Wed Bond 54 55 56 Stock 21 18 27 Quantities Bond Stock 1 2 Table of results Master in Economia Bancaria e Finanziaria Mon Tue 96 91 M. Dolfin - Univ. of Messina - ITALY Ex.3 - Value of two portfolios on 3 different days. Prices Mon Tue Wed Bond 54 55 56 Stock 21 18 27 Bond Stock PortA PortB 1 5 2 2 Mon Tue Wed PortA PortB 96 312 91 311 110 334 Quantities Table of results Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Addition Two matrices with the same dimension may be added together. Result: a new matrix in which each element is the sum of the corresponding elements of the two matrices. Exercise n.5 port A = Bond Stock 1 ; 2 port all = port A + port B; port B = Bond Stock port all = Bond Stock 5 2 6 4 One may also add a constant to every element in a matrix. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Subtraction Each element of the second matrix is subtracted from the corresponding element of the first one. Addition and and subtraction operate on an element-by element basis. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Element-by-element operations The relevant operator is preceded with a dot. The dimensions must be the same. It’s also called array operation. port A = port A.∗port B = Bond Stock Bond Stock Master in Economia Bancaria e Finanziaria 1 ; 2 5 ; 4 port B = Bond Stock port A./port B = 5 2 Bond Stock 0.2 1 M. Dolfin - Univ. of Messina - ITALY Element-by-element operations - Ex.4 (Yields) Values of stock dividends Dividends = 1.90 0.40 1.56 4.50 Closing prices Prices = 25.626 17.75 26.125 60.50 The array division of dividends by prices, gives the yield Yields = Dividends./Prices Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Transpose A matrix can be mirrored with respect to the diagonal; i.e. the rows becomes columns and vice versa. A= Master in Economia Bancaria e Finanziaria 1 4 9 ; 6 8 10 1 6 A0 = 4 8 9 10 M. Dolfin - Univ. of Messina - ITALY Multiple operations Can be performed by using an expression indicating the desired computations which may include multiple matrix and/or array operations (the dimensions must be appropriate for the operations). Example B = inv (A) ∗ (b ∗ c) Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Matrix division Primarily used for solving equations. For instance, given a set of (solvable) linear equations A X = B, An×n , Xn×1 , Bn×1 Matlab uses two matrix divisions symbols: left division (backslash \) → solves for A X = B. right division (slash /) → X A = B. A must be a nonsingular square matrix. Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Ex.5: left division operator Linear algebraic system of equations 6x + 12y + 4z = 70 7x − 2y + 3z = 5 2x + 8y − 9z = 64 Create a bidimensional array (matrix) 6 12 4 A = 7 −2 3 2 8 −9 70 B=5 64 solution = A\B Master in Economia Bancaria e Finanziaria M. Dolfin - Univ. of Messina - ITALY Inversion ( nonsingular square matrices) Inverse of a matrix When multiplied by the matrix, gives an identity matrix. A−1 ; in Matlab inv (A) A−1 A = I The inverse of a scalar, i.e. a 1 × 1 matrix, is the reciprocal. M = (4); I = (1); Master in Economia Bancaria e Finanziaria M−1 = 1 ; 4 1 4=1 4 M. Dolfin - Univ. of Messina - ITALY