Linear algebra is extensively used in applied quantitative finance because of its role in statistics, optimization, Monte Carlo simulation, signal processing, etc. Not surprisingly, it is also a comprehensive mathematical field that covers many topics. In this section, we discuss several topics that have significant applications in statistics and numerical methods.
Vectors
An n×1 (column) vector is a one-dimensional array. It can represent the coordinates of a point in the Rn (n-dimensional) Euclidean space.
51
Inner product/dot product: the inner product (or dot product) of two Rn vectors x and y is defined as i=1∑nxiyi=xTy.
Then angle θ between Rn vectors x and y has the property that cosθ=∥x∥∥y∥xTy. x and y are orthogonal if xTy=0. The correlation coefficient of two random variables can be viewed as the cosine of the angle between them in Euclidean space (ρ=cosθ).
There are 3 random variables x, y and z. The correlation between x and y is 0.8 and the correlation between x and z is 0.8. What is the maximum and minimum correlation between y and z?
Solution: We can consider random variables x, y and z as vectors. Let θ be the angle between x and y, then we have cosθ=ρx,y=0.8. Similarly the angle between x and z is θ as well. For y and z to have the maximum correlation, the angle between them needs to be the smallest. In this case, the minimum angle is 0 (when vector y and z are in the same direction) and the correlation is 1. For the minimum correlation, we want the maximum angle between y and z, which is the case shown in Figure 3.2.
Figure 3.2 Minimum correlation and maximum angle between vectors y and z
If you still remember some trigonometry, all you need is that
cos(2θ)=(cosθ)2−(sinθ)2=0.82−0.62=0.28
Otherwise, you can solve the problem using Pythagoras's Theorem:
0.8×1.2=1×h⇒h=0.96cos2θ=12−0.962=0.2852
QR decomposition
QR decomposition: For each non-singular n×n matrix A, there is a unique pair of orthogonal matrix Q and upper-triangular matrix R with positive diagonal elements such that A=QR.10
QR decomposition is often used to solve linear systems Ax=b when A is a non-singular matrix. Since Q is an orthogonal matrix, Q−1=QT and QRx=b⇒Rx=QTb. Because R is an upper-triangular matrix, we can begin with xn (the equation is simply Rn,nxn=(QTb)n), and recursively calculate all xi,∀i=n,n−1,⋯,1.
If the programming language you are using does not have a function for the linear least squares regression, how would you design an algorithm to do so?
Solution: The linear least squares regression is probably the most widely used statistical analysis method. Let's go over a standard approach to solving linear least squares regressions using matrices. A simple linear regression with n observations can be expressed as
where xi0≡1,∀i, is the intercept term and xi,1,⋯,xi,p−1 are p−1 exogenous regressors.
The goal of the linear least squares regression is to find a set of β=[β0,β1,⋯,βp−1]T that makes i=1∑nεi2 the smallest. Let's express the linear regression in matrix format: Y=Xβ+ε, where Y=[Y1,Y2,⋯,Yn]T and ε=[ε1,ε2,⋯,εn]T are both n×1 column vectors; X is a n×p matrix with each column representing a regressor (including the intercept) and each row representing an observation. Then the problem becomes
βminf(β)=βmini=1∑nεi2=βmin(Y−Xβ)T(Y−Xβ)53
To minimize the function f(β), taking the first derivative11 of f(β) with respect to β, we have f′(β)=2XT(Y−Xβ^)=0⇒(XTX)β^=XTY, where (XTX) is a p×p symmetric matrix and XTY is a p×1 column vector.
Let A=(XTX) and b=XTY, then the problem becomes Aβ^=b, which can be solved using QR decomposition as we described.
Alternatively, if the programming language has a function for matrix inverse, we can directly calculate β^ as β^=(XTX)−1XTY.12
Since we are discussing linear regressions, it's worthwhile to point out the assumptions behind the linear least squares regression (a common statistics question at interviews):
The relationship between Y and X is linear: Y=Xβ+ε.
E[εi]=0,∀i=1,⋯,n.
var(εi)=σ2,i=1,⋯,n (constant variance), and E[εiεj]=0,i=j (uncorrelated errors).
No perfect multicollinearity: ρ(xi,xj)=±1,i=j where ρ(xi,xj) is the correlation of regressors xi and xj.
εi and xi are independent.
Surely in practice, some of these assumptions are violated and the simple linear least squares regression is no longer the best linear unbiased estimator (BLUE). Many econometrics books dedicate significant chapters to addressing the effects of assumption violations and corresponding remedies.
Determinant, eigenvalue and eigenvector
Determinant: Let A be an n×n matrix with elements {Ai,j}, where i,j=1,⋯,n. The determinant of A is defined as a scalar: det(A)=p∑ψ(p)a1,p1a2,p2⋯an,pn, where p=(p1,p2,⋯,pn) is any permutation of (1,2,⋯,n); the sum is taken over all n! possible permutations; and
54ψ(p)={1,−1,if p can be coverted to natural order by even number of exchangesif p can be coverted to natural order by odd number of exchanges.
For example, determinants of 2×2 and 3×3 matrices can be calculated as
Eigenvalue: Let A be an n×n matrix. A real number λ is called an eigenvalue of A if there exists a nonzero vector x in Rn such that Ax=λx. Every nonzero vector x satisfying this equation is called an eigenvector of A associated with the eigenvalue λ.
Eigenvalues and eigenvectors are crucial concepts in a variety of subjects such as ordinary differential equations, Markov chains, principal component analysis (PCA), etc. The importance of determinant lies in its relationship to eigenvalues/eigenvectors.14
The determinant of matrix A−λI, where I is an n×n identity matrix with ones on the main diagonal and zeros elsewhere, is called the characteristic polynomial of A. The equation det(A−λI)=0 is called the characteristic equation of A. The eigenvalues of A are the real roots of the characteristic equation of A. Using the characteristic equation, we can also show that λ1λ2⋯λn=det(A) and i=1∑nλi=trace(A)=i=1∑nAi,i.
A is diagonalizable if and only if it has linearly independent eigenvectors.15 Let λ1,λ2,⋯,λn be the eigenvalues of A, x1,x2,⋯,xn be the corresponding eigenvectors. and X=[x1∣x2∣⋯∣xn], then
X−1AX=λ1λ2⋱λn=D⇒A=XDX−1⇒Ak=XDkX−1.55
If matrix A=[2112], what are the eigenvalues and eigenvectors of A?
Solution: This is a simple example of eigenvalues and eigenvectors. It can be solved using three related approaches:
Approach A: Apply the definition of eigenvalues and eigenvectors directly.
Let λ be an eigenvalue and x=[x1x2] be its corresponding eigenvector. By definition, we have
So either λ=3, in which case x1=x2 (plug λ=3 into equation 2x1+x2=λx1) and the corresponding normalized eigenvector is [1/21/2], or x1+x2=0, in which case the normalized eigenvector is [1/2−1/2] and λ=1 (plug x2=−x1 into equation 2x1+x2=λx1).
Approach B: Use equation det(A−λI)=0.
det(A−λI)=0⇒(2−λ)(2−λ)−1=0. Solving the equation, we have λ1=1 and λ2=3. Applying the eigenvalues to Ax=λx, we can get the corresponding eigenvectors.
Approach C: Use equations λ1⋅λ2⋯λn=det(A) and i=1∑nλi=trace(A)=i=1∑nAi,i.
det(A)=2×2−1×1=3 and trace(A)=2×2=4.
So we have λ1×λ2λ1+λ2=3=4}⇒{λ1=1λ2=3. Again apply the eigenvalues to Ax=λx, and we can get the corresponding eigenvectors.
56
Positive semidefinite/definite matrix
When A is a symmetric n×n matrix, as in the cases of covariance and correlation matrices, all the eigenvalues of A are real numbers. Furthermore, all eigenvectors that belong to distinct eigenvalues of A are orthogonal.
Each of the following conditions is a necessary and sufficient condition to make a symmetric matrix Apositive semidefinite:
xTAx≥0 for any n×1 vector x.
All eigenvalues of A are nonnegative.
All the upper left (or lower right) submatrices AK,K=1,⋯,n have nonnegative determinants.16
Covariance/correlation matrices must also be positive semidefinite. If there is no perfect linear dependence among random variables, the covariance/correlation matrix must also be positive definite. Each of the following conditions is a necessary and sufficient condition to make a symmetric matrix Apositive definite:
xTAx>0 for any nonzero n×1 vector x.
All eigenvalues of A are positive.
All the upper left (or lower right) submatrices AK,K=1,⋯,n have positive determinants.
There are 3 random variables x, y and z. The correlation between x and y is 0.8 and the correlation between x and z is 0.8. What is the maximum and minimum correlation between y and z?
Solution: The problem can be solved using the positive semidefiniteness property of the correlation matrix.
Let the correlation between y and z be ρ, then the correlation matrix for x, y and z is
When A is a symmetric positive definite matrix, Cholesky decomposition expresses A as A=RTR, where R is a unique upper-triangular matrix with positive diagonal entries. Essentially, it is a LU decomposition with the property L=UT.
Cholesky decomposition is useful in Monte Carlo simulation to generate correlated random variables as shown in the following problem:
How do you generate two N(0,1) (standard normal distribution) random variables with correlation ρ if you have a random number generator for standard normal distribution?
Solution: Two N(0,1) random variables x1,x2 with a correlation ρ can be generated from independent N(0,1) random variables z1,z2 using the following equations:
x1=z1x2=ρz1+1−ρ2z2
It is easy to confirm that var(x1)=var(z1)=1,var(x2)=ρ2var(z1)+(1−ρ2)var(z2)=1,
and cov(x1,x2)=cov(z1,ρz1+1−ρ2z2)=cov(z1,ρz1)=ρ.
This approach is a basic example using Cholesky decomposition to generate correlated random numbers. To generate correlated random variables that follow a n-dimensional
58
multivariate normal distribution X=[X1,X2,⋯,Xn]T∼N(μ,Σ) with mean μ=[μ1,μ2,⋯,μn]T and covariance matrix Σ (a n×n positive definite matrix)18, we can decompose the covariance matrix Σ into RTR and generate n independent N(0,1) random variables z1,z2,⋯,zn. Let vector Z=[z1,z2,⋯,zn]T, then X can be generated as X=μ+RTZ.19
Alternatively, X can also be generated using another important matrix decomposition called singular value decomposition (SVD): For any n×p matrix X, there exists a factorization of the form X=UDVT, where U and V are n×p and p×p orthogonal matrices, with columns of U spanning the column space of X, and the columns of V spanning the row space; D is a p×p diagonal matrix called the singular values of X.
For a positive definite covariance matrix, we have V=U and Σ=UDUT. Furthermore, D is the diagonal matrix of eigenvalues λ1,λ2,⋯,λn and U is the matrix of n corresponding eigenvectors. Let D1/2 be a diagonal matrix with diagonal elements λ1,λ2,⋯,λn, then it is clear that D=(D1/2)2=(D1/2)(D1/2)T and Σ=UD1/2(UD1/2)T. Again, if we generate a vector of n independent N(0,1) random variables Z=[z1,z2,⋯,zn]T, X can be generated as X=μ+(UD1/2)Z.