Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Computational Finance Zvi Wiener 02-588-3049 http://pluto.mscc.huji.ac.il/~mswiener/zvi.html CF-1 Bank Hapoalim Jun-2001 Plan 1. Introduction, deterministic methods. 2. Stochastic methods. 3. Monte Carlo I. 4. Monte Carlo II. 5. Advanced methods for derivatives. Other topics: queuing theory, floaters, binomial trees, numeraire, ESPP, convertible bond, DAC, ML-CHKP. Zvi Wiener CF1 slide 2 Linear Algebra 1 -2 Vectors rows or columns Zvi Wiener 1 {1, 1}, {-2, 1} 1 -2 1 1 CF1 slide 3 Linear Algebra Zvi Wiener CF1 slide 4 Basic Operations 1 2 -2 1 3 2 2 Zvi Wiener + 2 -1 1 = 3 6 6 = CF1 3 1 -1 slide 5 Linear Algebra x1 vector x x 2 x 3 Vectors form a linear space. x1 x1 y1 x x 2 x y x2 y 2 x y x 3 3 3 Zero vector Scalar multiplication Zvi Wiener CF1 slide 6 Linear Algebra matrix a11 A a 21 a 31 Zero matrix a12 a 22 a32 a13 Matrices also a 23 form a linear a33 space. 0 0 0 0 0 0 0 0 0 1 0 0 Unit matrix 0 1 0 0 0 1 Zvi Wiener CF1 slide 7 Linear Algebra Matrix can operate on a vector a11 Ax a 21 a 31 a12 a 22 a32 a13 x1 a11 x1 a12 x2 a13 x3 a 23 x2 a 21 x1 a 22 x 2 a 23 x3 a33 x3 a31 x1 a32 x 2 a33 x3 How does zero matrix operate? How does unit matrix operate? Zvi Wiener CF1 slide 8 Linear Algebra Transposition of a matrix a11 A a 21 a 31 a12 a 22 a32 a11 T A a12 a 13 a13 a 23 a33 a21 a 22 a 23 a31 a32 a33 A symmetric matrix is A=AT for example a variance-covariance matrix. Zvi Wiener CF1 slide 9 Linear Algebra Matrix multiplication a11 a12 a13 b11 b12 b13 AB a 21 a 22 a 23 b21 b22 b23 a b a a b b 32 33 31 32 33 31 a11b11 a12b21 a13b31 a11b12 a12b22 a13b32 a 21b11 a 22b21 a 23b31 a 21b12 a 22b22 a 23b32 a b a b a b 31 11 32 21 33 31 a31b12 a32b22 a33b32 Zvi Wiener CF1 a11b13 a12b23 a13b33 a 21b13 a 22b23 a 23b33 a31b13 a32b23 a33b33 slide 10 Scalar Product a b ai bi i b1 a1 , a2 , a3 b2 a1b1 a2b2 a3b3 b 3 a is orthogonal to b if ab = 0 Zvi Wiener CF1 slide 11 Linear Algebra Scalar product of two vectors y1 T x y x1 x2 x3 y 2 x1 y1 x2 y 2 x3 y3 y 3 Euclidean norm x Zvi Wiener 2 x xx x x T 2 1 CF1 2 2 2 3 slide 12 Determinant a11 a12 Det a21 a22 a11a22 a12a21 Determinant is 0 if the operator maps some vectors to zero (and can not be inverted). Zvi Wiener CF1 slide 13 Linear Algebra • Matrix multiplication corresponds to a consecutive application of each operator. • Note that it is not commutative! ABBA. • Unit matrix does not change a vector. • An inverse matrix is such that AA-1=I. Zvi Wiener CF1 slide 14 Linear Algebra • Determinant of a matrix ... • A matrix can be inverted if det(A)0 • Rank of a matrix • Matrix as a system of linear equations Ax=b. • Uniqueness and existence of a solution. • Trace tr(A) – sum of diagonal elements. Zvi Wiener CF1 slide 15 Linear Algebra • Change of coordinates C-1AC. • Jordan decomposition. • Matrix power Ak. • Matrix as a quadratic form (metric) xTAx. • Markov process. • Eigenvectors, eigenvalues Ax=x, optimization. Zvi Wiener CF1 slide 16 Problems Check how the following matrices act on vectors: 1 0 0 1 0 1 1 0 Zvi Wiener 1 0 0 1 0 1 1 0 1 0 0 0 cos sin sin cos CF1 slide 17 Simple Exercises • Show an example of ABBA. • Construct a matrix that inverts each vector. • Construct a matrix that rotates a two dimensional vector by an angle . • Construct a covariance matrix, show that it is symmetric. • What is mean and variance of a portfolio in matrix terms? Zvi Wiener CF1 slide 18 Examples • Credit rating and credit dynamics. • Variance-covariance model of VaR. • Can the var-covar matrix be inverted • VaR isolines (the ovals model). • Prepayment model based on types of clients. • Finding a minimum of a function. Zvi Wiener CF1 slide 19 Calculus • Function of one and many variables. • Continuity in one and many directions. • Derivative and partial derivative. • Gradient and Hessian. • Singularities, optimization, ODE, PDE. Zvi Wiener CF1 slide 20 Linear and quadratic terms $ x Zvi Wiener CF1 slide 21 Taylor series x 2 f ( x x) f ( x) f ' ( x)x f " ( x) o x 2 2 g ( x x, y y ) g ( x, y ) g x x g y y g xx x 2 y 2 g xy xy g yy o x 2 y 2 2 2 1 T F ( x x) F ( x) F ' ( x)x x F " ( x)x o x 2 Zvi Wiener CF1 2 slide 22 Variance-Covariance V ( x) V ( x1 , x2 ,..., xn ) 11 12 1n 1 21 22 , n nn n1 Zvi Wiener CF1 slide 23 Variance-Covariance Gradient vector: Zvi Wiener CF1 V x1 V ' ( x) V x n slide 24 Variance-Covariance 1 2 V ( x x) V ( x) V ' ( x)x V " ( x)x 2 1 2 V ( x) V ' ( x)x V " ( x)x 2 Zvi Wiener CF1 slide 25 Variance-Covariance V " ( x) 2 E V E x V ' ( x) E x 2 1 E V V ' trV " 2 T V V ' V ' T Zvi Wiener CF1 slide 26 Variance-Covariance For a short time period , the changes in the value are distributed approximately normal with the following mean and variance: 1 T E V V ' trV " 2 2 T V V ' V ' Zvi Wiener CF1 slide 27 Variance-Covariance Then VaR can be found as: VaR 1%, V (V ) 2.33 (V ) T 1 T V ' trV " 2.33 V ' V ' 2 Zvi Wiener CF1 slide 28 Weighted Variance covariance Volatility estimate on day i based on last M days. 1 t ( M 1) 1 t Zvi Wiener i i (R j i M 1 i j j CF1 j R) (R j R ) 2 2 slide 29 Weighted Variance covariance Covariance on day i based on last M days. 1 12 t ( M 1) i (R j i M 1 1j R1 )( R2 j R2 ) It is important to check that the resulting matrix is positive definite! Zvi Wiener CF1 slide 30 Positive Quadratic Form For every vector x a we have x.A.x > 0 Only such a matrix can be used to define a norm. For example, this matrix can not have negative diagonal elements. Any variance- covariance matrix must be positive. Zvi Wiener CF1 slide 31 Positive Quadratic Form Needs["LinearAlgebra`MatrixManipulation`"]; ClearAll[ positiveForm ]; positiveForm[ a_?MatrixQ ] := Module[{aa, i}, aa = Table[ Det[ TakeMatrix[ a, {1, 1}, {i, i}] ], {i, Length[a]}]; { aa, If[ Count[ aa, t_ /; t < 0] > 0, False, True]} ]; Zvi Wiener CF1 slide 32 Stochastic (transition) Matrix Used to define a Markov chain (only the last state matters). A matrix P is stochastic if it is non-negative and sum of elements in each line is 1. One can easily see that 1 is an eigenvalue of any stochastic matrix. What is the eigenvector? Zvi Wiener CF1 slide 33 Markov chain • credit migration • prepayment and freezing of a program Zvi Wiener CF1 slide 34 Stochastic (transition) Matrix Theorem: P0 is stochastic iff (1,1,…1) is an eigenvector with an eigenvalue 1 and this is the maximal eigenvalue. If both P and PT are stochastic, then P is called double stochastic. Zvi Wiener CF1 slide 35 Cholesky decomposition The Cholesky decomposition writes a symmetric positive definite matrix as the product of an uppertriangular matrix and its transpose. In MMA Zvi Wiener CholeskyDecomposition[m] CF1 slide 36 Generating Random Samples We need to sample two normally distributed variables with correlation . If we can sample two independent Gaussian variables x1 and x2 then the required variables can be expressed as y1 x1 y2 x1 x2 1 Zvi Wiener 2 CF1 slide 37 Generating Random Samples We need to sample n normally distributed variables with correlation matrix ij, ( >0). Sample n independent Gaussian variables x1…xn. n yi aik xk y A.x k 1 n 1 a , and A A , A k 1 Zvi Wiener 2 ik T CF1 slide 38 Function of a matrix 1 A T JT 1 1 1 A T JTT JT T J T 2 1 2 A T J T n n 2 3 A A e E A 2! 3! A Zvi Wiener CF1 slide 39 ODE dx Ax dt x(t ) e Zvi Wiener x(t0 ) x0 A ( t t 0 ) CF1 x0 slide 40 ODE dx Ax f (t ) dt x(t ) e A ( t t 0 ) x(t0 ) x0 t x0 e A( t ) f ( )dt t0 Zvi Wiener CF1 slide 41 Bisection method f x If the function is monotonic, e.g. implied vol. Zvi Wiener CF1 slide 42 Newton’s method f x3 x2 Zvi Wiener CF1 x1 x slide 43 Solve and FindRoot Solve[ 0 = = x2- 0.8x3- 0.3, {x}] {{x -> -0.467297}, {x ->0.858648 -0.255363*I}, {x -> 0.858648 + 0.255363*I}} FindRoot[ x2 + Sin[x] - 0.8x3 - 0.3, {x, 0,1}] {x -> 0.251968} Zvi Wiener CF1 slide 44 Max, min of a multidimensional function • Gradient method • Solve a system of equations (both derivatives) 1 1 0.5 0.5 0 -1 0 -0.5 -0.5 0 0.5 1 Zvi Wiener CF1 -1 slide 45 Gradient method 1 0.5 0 -0.5 -1 -1 Zvi Wiener -0.5 0 CF1 0.5 1 slide 46 Level curve of a multivariate function ContourPlot[ x^2+y^3, {x,-2,2}, {y,- 2,2}] ContourPlot[ x^2+y^3, {x,-2,2}, {y,- 2,2}], Contours->{1 ,-0.5}, ContourShading->False]; Zvi Wiener CF1 slide 47 ContourPlot[ x^2+y^3, {x,-2,2}, {y,- 2,2}] 2 1 0 -1 -2 -2 Zvi Wiener -1 0 CF1 1 2 slide 48 ContourPlot[ x^2+y^3, {x,-2,2}, {y,- 2,2}], Contours->{1 ,-0.5}, ContourShading->False]; 2 1 0 -1 -2 -2 Zvi Wiener -1 0 CF1 1 2 slide 49 Example Consider a portfolio with two risk factors and benchmark duration of 6M. The VaR limit is 3 bp. and you have to make two decisions: a – % of assets kept in spread products q – duration mismatch we assume that all instruments (both treasuries and spread) have the same duration T+q months. Zvi Wiener CF1 slide 50 Contour Levels of VaR (static) 6 4 2 q - duration mismatch 0 -2 -4 -6 0 0.2 0.4 0.6 0.8 1 a (% of spread) Zvi Wiener CF1 slide 51 6 VaR=3 bp 4 2 q - duration mismatch 0 position -2 VaR=2 bp -4 -6 0 0.2 0.4 0.6 0.8 1 a (% of spread) Zvi Wiener CF1 slide 52 6 4 In order to reduce risk one can increase duration (in this case). 2 0 -2 -4 -6 0 q - duration mismatch Zvi Wiener 0.2 0.4 0.6 0.8 1 a (% of spread) CF1 slide 53 What we can do using limits 6 4 2 0 -2 -4 VaR = 6 bp -6 0 Zvi Wiener 0.2 0.4 CF1 0.6 0.8 1 slide 54 duration mismatch (yr) Position 2M, and 10% spread 5% weekly VaR=2.2 bp 0.2 0.1 0 -0.1 weekly VaR limit 3 bp -0.2 spread % 0 Zvi Wiener 0.2 0.4 CF1 0.6 0.8 1 slide 55 Splines x1 Zvi Wiener x2 x3 … CF1 xn slide 56 Splines <<Graphics`Spline` pts = {{0, 0}, {1, 2}, {2, 3}, {3, 1}, {4, 0}} Show[ Graphics[ Spline[pts, Cubic, SplineDots -> Automatic]]] Zvi Wiener CF1 slide 57 Splines pts = Table[{i, i + i^2 + (Random[] - 0.5)}, {i, 0, 1, .05}]; Show[Graphics[Spline[pts,Cubic,SplineDots ->Automatic]]] Zvi Wiener CF1 slide 58 Fitting data data = Table[7*x + 3 + 10*Random[], {x, 10}]; f[x_] := Evaluate[Fit[data, {1, x}, x]] Needs["Graphics`Graphics`"] DisplayTogether[ ListPlot[data, PlotStyle -> {AbsolutePointSize[3], RGBColor[1, 0, 0]}], Plot[f[x], {x, 0, 10}, PlotStyle -> RGBColor[0, 0, 1]] ]; Zvi Wiener CF1 slide 59 Fitting data 60 40 20 2 Zvi Wiener 4 6 CF1 8 10 slide 60 Fitting data data = {{1.0, 1.0, .126}, {2.0, 1.0, .219}, {1.0, 2.0, .076}, {2.0, 2.0, .126}, {.1, .0, .186}}; ff[x_, y_] = NonlinearFit[data, a*c*x/(1 + a*x + b*y), {x, y}, {a, b, c}]; ff[x, y] nonlinear, multidimensional Zvi Wiener CF1 slide 61 Orly - Nelson Siegel 0.145 MAKAM 0.14 0.135 0.13 0.125 50 Zvi Wiener 100 150 CF1 200 250 300 350 slide 62 Numerical Differentiation x f ( x x) f ( x) f ' ( x)x f " ( x) o x 2 2 x 2 2 f ( x x) f ( x) f ' ( x)x f " ( x) o x 2 f ( x x) f ( x x) 2 f ' ( x)x o x 2 2 f ( x x) f ( x x) f ' ( x) ox 2x Zvi Wiener CF1 slide 63 Numerical Differentiation x f ( x x) f ( x) f ' ( x)x f " ( x) o x 2 2 x 2 2 f ( x x) f ( x) f ' ( x)x f " ( x) o x 2 2 f ( x x) 2 f ( x) f ( x x) f " ( x)x o x 2 2 f ( x x) 2 f ( x) f ( x x) 2 f " ( x) O x 2 x Zvi Wiener CF1 slide 64 Finite Differences S Following P. Wilmott, “Derivatives” time Typically equal time and S (or logS) steps. Zvi Wiener CF1 slide 65 Finite Differences Time step t asset step S (i,k) node of the grid is t = T - kt, iS 0 i I, 0 k K assets value at each node is Vi V (iS , T kt ) k note the direction of time! Zvi Wiener CF1 slide 66 The Black-Scholes equation V 1 2 2 V V S (r rf ) S rV 0 2 t 2 S S 2 Linear parabolic PDE Final conditions V ( S , T ) Payoff ( S ) Boundary conditions ... Zvi Wiener CF1 slide 67 Transformation of BS V ( S , t ) ex U ( x, ) 1 2r 2 1, 2 U U 2 x 2 2 1 2r 2 1 , 4 S ex , 2r t T 2 . Zvi Wiener CF1 slide 68 Approximating V V ( S , t h) V ( S , t ) lim h 0 t h Vi Vi V (S , t ) t t k Zvi Wiener CF1 k 1 Ot slide 69 Approximating V V V (S , t ) S 2S k i 1 Zvi Wiener CF1 k i 1 O S 2 slide 70 Approximating V V (S , t ) 2 S 2 Zvi Wiener k i 1 2Vi V 2 S k CF1 k i 1 O S 2 slide 71 Bilinear Interpolation V1 V2 A3 A4 4 V AV j 1 4 A j 1 A2 A1 j j Area of the rectangle V3 V4 Zvi Wiener j CF1 slide 72 Final conditions and payoffs V ( S , T ) Payoff ( S ) Vi Payoff (iS ) 0 For example a European Call option Vi Max (iS E ,0) 0 Zvi Wiener CF1 slide 73 Boundary conditions Call For example a Call option k 0 V 0 For large S the Call value asymptotes to S-Ee-r(T-t) V IS Ee k I Zvi Wiener CF1 rkt slide 74 Boundary conditions Put For example a Put option k 0 V Ee rkt For large S VI 0 k Zvi Wiener CF1 slide 75 Boundary conditions S=0 V (0, t ) rV (0, t ) 0 t k 1 0 V V rV0k 1 t k 0 V (1 rt )V k 0 Zvi Wiener k 1 0 CF1 slide 76 Explicit scheme V V V a ( S , t ) 2 b( S , t ) c( S , t )V 0 t S S 2 k 1 Vi Vi 2Vi V k V ai 2 t S k k k Vi 1 Vi 1 k k 2 bi ci Vi O(t , S ) 2S k Zvi Wiener k i 1 CF1 k k i 1 slide 77 Explicit scheme k 1 Vi Vi 2Vi V k V ai 2 t S k k k Vi 1 Vi 1 bi cikVi k O(t , S 2 ) 2S k Vi k 1 k i 1 k k i 1 A V (1 B )Vi C V k k i i 1 k i k k k i i 1 2 2 O t , t S Local truncation error Zvi Wiener CF1 slide 78 Explicit scheme Vi k 1 A V (1 B )Vi C V k k i i 1 k i k k k i i 1 Value here is calculated S These values are already known time Zvi Wiener CF1 slide 79 Explicit scheme Vi k 1 A V (1 B )Vi C V k k i i 1 k i k k k i i 1 This equation is defined for 1i I-1, for i=1 and i=I we use boundary conditions. Zvi Wiener CF1 slide 80 Explicit scheme Vi k 1 A V (1 B )Vi C V k k i i 1 k i k k k i i 1 For the BS equation (with dividends) A ( i (r D)i )t / 2, k i 2 2 B ( i r )t , k i 2 2 C ( i (r D)i)t / 2, k i Zvi Wiener 2 2 CF1 slide 81 Explicit scheme 2a S 2 S , t b 2a Stability problems related to step sizes. These relationships should guarantee stability. Note that reducing asset step by half we must reduce the time step by a factor of four. Zvi Wiener CF1 slide 82 Explicit scheme Advantages easy to program, hard to make a mistake when unstable it is obvious coefficients can be S and t dependent Disadvantages there are restrictions on time step, which may cause slowness. Zvi Wiener CF1 slide 83 Implicit scheme V V V a ( S , t ) 2 b( S , t ) c( S , t )V 0 t S S 2 k 1 k 1 i 1 k 1 k 1 i 1 Vi Vi 2Vi V k 1 V ai 2 t S k 1 k 1 k 1 Vi 1 Vi 1 k 1 k 1 2 bi ci Vi O(t , S ) 2S k Zvi Wiener CF1 slide 84 Implicit scheme k 1 k 1 k 1 Vi k Vi k 1 V 2 V V k 1 i 1 i i 1 ai 2 t S k 1 k 1 k 1 Vi 1 Vi 1 k 1 k 1 2 bi ci Vi O(t , S ) 2S k 1 k 1 i i 1 Vi A V k Zvi Wiener k 1 i (1 B )Vi CF1 k 1 k 1 k 1 i i 1 C V slide 85 Implicit scheme k 1 k 1 i i 1 Vi A V k k 1 i (1 B )Vi k 1 k 1 k 1 i i 1 C V Values here are calculated S This value is used time Zvi Wiener CF1 slide 86 Crank-Nicolson scheme k 1 k 1 i k 1 i 1 k 1 k 1 i 1 Vi Vi a V 2Vi V 2 t 2 S aik Vi k1 2Vi k Vi k1 bik 1 Vi k11 Vi k11 2 2 S 2 2S k k k k 1 k bi Vi 1 Vi 1 ci ci k k 1 2 Vi Vi O(t , S ) 2 2S 2 2 k Zvi Wiener CF1 slide 87 Crank-Nicolson scheme k 1 k 1 i i 1 A V k 1 i (1 B )Vi k 1 k 1 k 1 i i 1 C V A V (1 B )Vi C V k k i i 1 Zvi Wiener k i CF1 k k k i i 1 slide 88 Crank-Nicolson scheme Values here are calculated S These values are used time Zvi Wiener CF1 slide 89 Crank-Nicolson scheme k 1 1 B1k 1 V1 A1k 1V0k 1 C1k 1 0 k 1 k 1 k 1 1 B2 A2 V2 0 0 0 0 0 1 BIk21 CIk21 VIk21 0 k 1 k 1 k 1 0 AI 1 1 BI 1 VI 1 0 A general form of the linear equation is: M k 1 k 1 L v M v r k k R k Note that M are tridiagonal! Zvi Wiener CF1 slide 90 Crank-Nicolson scheme Theoretically this equation can be solved as v k 1 M M k 1 1 L v r k k R k In practice this is inefficient! Zvi Wiener CF1 slide 91 LU decomposition M is tridiagonal, thus M=LU, where L is lower triangular, and U is upper triangular. In fact L has 1 on the diagonal and one subdiagonal only, U has a diagonal and one superdiagonal. Zvi Wiener CF1 slide 92 LU decomposition Then in order to solve Mv=q or LUv=q We will solve Lw=q first, and then Uv=w. Zvi Wiener CF1 slide 93 LU decomposition Very fast, especially when M is time independent. Disadvantages: Needs a big modification for American options Zvi Wiener CF1 slide 94 Other methods • SOR successive over relaxation • Douglas scheme • Three time-level scheme • Alternating direction method • Richardson Extrapolation • Hopscotch method • Multigrid methods Zvi Wiener CF1 slide 95 Multidimensional case Fixed t layer S These values are used to calculate space derivatives r Note additional boundary conditions. Zvi Wiener CF1 slide 96 Geometrical Brownian Motion dS rSdt SdB option e Zvi Wiener r (T t ) Eriskneutral payoff (S ) CF1 slide 97 Lognormal process dS rSdt SdB d (log S ) r 2 2 S (t ) S (0)e Zvi Wiener 2 r 2 dt dB t Z N ( 0 ,1) t CF1 slide 98 Euler Scheme dS rSdt SdB S rSt SZ N ( 0,1) t O(t ) Zvi Wiener CF1 slide 99 Milstein Scheme dS rSdt SdB S rSt SZ N ( 0,1) t 1 2 2 2 2 S ( Z N ( 0,1) 1)t O(t ) 2 Zvi Wiener CF1 slide 100 Stochastic Calculus Standard Normal 1 N ( x) 2 x e z2 2 dz Diffusion process dX ( X , t )dt ( X , t )dBt ABM dX dt dBt GBM dX Xdt XdBt Zvi Wiener CF1 slide 101 Ito’s Lemma dX ( X , t )dt ( X , t )dBt F F F 2 dX dF ( X , t ) dt dX 2 t X X 2 dt dt 0 dX 0 Zvi Wiener dX 0 dt CF1 slide 102 Y ln( X ) dX Xdt XdBt 1 1 2 dY 0dt dX 2 (dX ) X X 2 dY dt dBt 2 Y (t ) Y (0) 2 2 X (t ) X (0)e Zvi Wiener t t Z (0,1) 2 t t Z ( 0 ,1) 2 CF1 slide 103 Siegel’s paradox Consider two currencies X and Y. Define S an exchange rate (the number of units of currency Y for a unit of X). The risk-neutral process for S is dS (rY rX )Sdt S SdBt By Ito’s lemma the process for 1/S is 1 1 2 1 d rY rX S dt S dBt S S S Zvi Wiener CF1 slide 104 Siegel’s paradox The paradox is that the expected growth rate of 1/S is not ry - rX, but has a correction term. Zvi Wiener CF1 slide 105