A Practical Guide to Quantitative Finance Interviews
Contents

3.6 Linear Algebra

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×1n\times1 (column) vector is a one-dimensional array. It can represent the coordinates of a point in the RnR^n (nn-dimensional) Euclidean space.

51

Inner product/dot product: the inner product (or dot product) of two RnR^n vectors xx and yy is defined as ∑i=1nxiyi=xTy\displaystyle\sum_{i=1}^nx_iy_i=x^Ty

Euclidean norm: ∥x∥=∑i=1nxi2=xTx; ∥x−y∥=(x−y)T(x−y)\displaystyle\|x\|=\sqrt{\sum_{i=1}^nx_i^2}=\sqrt{x^Tx};\ \|x-y\|=\sqrt{(x-y)^T(x-y)}

Then angle θ\theta between RnR^n vectors xx and yy has the property that cos⁡θ=xTy∥x∥∥y∥\displaystyle\cos\theta=\frac{x^Ty}{\|x\|\|y\|}. xx and yy are orthogonal if xTy=0x^Ty=0. The correlation coefficient of two random variables can be viewed as the cosine of the angle between them in Euclidean space (ρ=cos⁡θ\rho=\cos\theta).

There are 3 random variables xx, yy and zz. The correlation between xx and yy is 0.8 and the correlation between xx and zz is 0.8. What is the maximum and minimum correlation between yy and zz?

Solution: We can consider random variables xx, yy and zz as vectors. Let θ\theta be the angle between xx and yy, then we have cos⁡θ=ρx,y=0.8\cos\theta=\rho_{x,y}=0.8. Similarly the angle between xx and zz is θ\theta as well. For yy and zz to have the maximum correlation, the angle between them needs to be the smallest. In this case, the minimum angle is 0 (when vector yy and zz are in the same direction) and the correlation is 1. For the minimum correlation, we want the maximum angle between yy and zz, which is the case shown in Figure 3.2.

Triangle showing unit vectors y and z, each making angle theta with vector x of length0.8. Half the base is0.6; a dashed perpendicular of length h links z to the y side, whose upper segment is0.28.
Figure 3.2 Minimum correlation and maximum angle between vectors yy and zz

If you still remember some trigonometry, all you need is that

cos⁡(2θ)=(cos⁡θ)2−(sin⁡θ)2=0.82−0.62=0.28\cos(2\theta)=(\cos\theta)^2-(\sin\theta)^2=0.8^2-0.6^2=0.28

Otherwise, you can solve the problem using Pythagoras’s Theorem:

0.8×1.2=1×h⇒h=0.960.8\times1.2=1\times h\Rightarrow h=0.96 cos⁡2θ=12−0.962=0.28\cos2\theta=\sqrt{1^2-0.96^2}=0.28 52

QR decomposition

QR decomposition: For each non-singular n×nn\times n matrix AA, there is a unique pair of orthogonal matrix QQ and upper-triangular matrix RR with positive diagonal elements such that A=QRA=QR.10

QR decomposition is often used to solve linear systems Ax=bAx=b when AA is a non-singular matrix. Since QQ is an orthogonal matrix, Q−1=QTQ^{-1}=Q^T and QRx=b⇒Rx=QTbQRx=b\Rightarrow Rx=Q^Tb. Because RR is an upper-triangular matrix, we can begin with xnx_n (the equation is simply Rn,nxn=(QTb)nR_{n,n}x_n=(Q^Tb)_n), and recursively calculate all xix_i, ∀i=n,n−1,⋯ ,1\forall i=n,n-1,\cdots,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 nn observations can be expressed as

yi=β0xi,0+β1xi,1+⋯+βp−1xi,p−1+εi, ∀i=1,⋯ ,ny_i=\beta_0x_{i,0}+\beta_1x_{i,1}+\cdots+\beta_{p-1}x_{i,p-1}+\varepsilon_i,\ \forall i=1,\cdots,n, where xi0≡1, ∀ix_{i0}\equiv1,\ \forall i, is the intercept term and xi,1,⋯ ,xi,p−1x_{i,1},\cdots,x_{i,p-1} are p−1p-1 exogenous regressors.

The goal of the linear least squares regression is to find a set of β=[β0,β1,⋯ ,βp−1]T\beta=[\beta_0,\beta_1,\cdots,\beta_{p-1}]^T that makes ∑i=1nεi2\sum_{i=1}^n\varepsilon_i^2 the smallest. Let’s express the linear regression in matrix format:

Y=Xβ+εY=X\beta+\varepsilon, where Y=[Y1,Y2,⋯ ,Yn]TY=[Y_1,Y_2,\cdots,Y_n]^T and ε=[ε1,ε2,⋯ ,εn]T\varepsilon=[\varepsilon_1,\varepsilon_2,\cdots,\varepsilon_n]^T are both n×1n\times1 column vectors; XX is a n×pn\times p matrix with each column representing a regressor (including the intercept) and each row representing an observation. Then the problem becomes

min⁡βf(β)=min⁡β∑i=1nεi2=min⁡β(Y−Xβ)T(Y−Xβ)\min_\beta f(\beta)=\min_\beta\sum_{i=1}^n\varepsilon_i^2=\min_\beta(Y-X\beta)^T(Y-X\beta) 53

To minimize the function f(β)f(\beta), taking the first derivative11 of f(β)f(\beta) with respect to β\beta, we have f′(β)=2XT(Y−Xβ)=0⇒(XTX)β^=XTYf'(\beta)=2X^T(Y-X\beta)=0\Rightarrow(X^TX)\hat\beta=X^TY, where (XTX)(X^TX) is a p×pp\times p symmetric matrix and XTYX^TY is a p×1p\times1 column vector.

Let A=(XTX)A=(X^TX) and b=XTYb=X^TY, then the problem becomes Aβ^=bA\hat\beta=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 β^\hat\beta as β^=(XTX)−1XTY\hat\beta=(X^TX)^{-1}X^TY.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):

  1. The relationship between YY and XX is linear: Y=Xβ+εY=X\beta+\varepsilon.
  2. E[εi]=0, ∀i=1,⋯ ,nE[\varepsilon_i]=0,\ \forall i=1,\cdots,n.
  3. var⁡(εi)=σ2, i=1,⋯ ,n\operatorname{var}(\varepsilon_i)=\sigma^2,\ i=1,\cdots,n (constant variance), and E[εiεj]=0, i≠jE[\varepsilon_i\varepsilon_j]=0,\ i\ne j (uncorrelated errors).
  4. No perfect multicollinearity: ρ(xi,xj)≠±1, i≠j\rho(x_i,x_j)\ne\pm1,\ i\ne j where ρ(xi,xj)\rho(x_i,x_j) is the correlation of regressors xix_i and xjx_j.
  5. ε\varepsilon and xix_i 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 AA be an n×nn\times n matrix with elements {Ai,j}\{A_{i,j}\}, where i,j=1,⋯ ,ni,j=1,\cdots,n. The determinant of AA is defined as a scalar: det⁡(A)=∑pψ(p)a1,p1a2,p2⋯an,pn\det(A)=\sum_p\psi(p)a_{1,p_1}a_{2,p_2}\cdots a_{n,p_n}, where p=(p1,p2,⋯ ,pn)p=(p_1,p_2,\cdots,p_n) is any permutation of (1,2,⋯ ,n)(1,2,\cdots,n); the sum is taken over all n!n! possible permutations; and

54 ψ(p)={1,if p can be coverted to natural order by even number of exchanges−1,if p can be coverted to natural order by odd number of exchanges.\psi(p)=\begin{cases}1,&\text{if }p\text{ can be coverted to natural order by even number of exchanges}\\-1,&\text{if }p\text{ can be coverted to natural order by odd number of exchanges}\end{cases}.

For example, determinants of 2×22\times2 and 3×33\times3 matrices can be calculated as

det⁡([abcd])=ad−bc,det⁡([abcdefghi])=aei+bfg+cdh−ceg−afh−bdi.\det\left(\begin{bmatrix}a&b\\c&d\end{bmatrix}\right)=ad-bc,\quad\det\left(\begin{bmatrix}a&b&c\\d&e&f\\g&h&i\end{bmatrix}\right)=aei+bfg+cdh-ceg-afh-bdi.

13

Determinant properties: det⁡(AT)=det⁡(A), det⁡(AB)=det⁡(A)det⁡(B), det⁡(A−1)=1det⁡(A)\det(A^T)=\det(A),\ \det(AB)=\det(A)\det(B),\ \displaystyle\det(A^{-1})=\frac{1}{\det(A)}

Eigenvalue: Let AA be an n×nn\times n matrix. A real number λ\lambda is called an eigenvalue of AA if there exists a nonzero vector xx in RnR^n such that Ax=λxAx=\lambda x. Every nonzero vector xx satisfying this equation is called an eigenvector of AA associated with the eigenvalue λ\lambda.

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−λIA-\lambda I, where II is an n×nn\times n identity matrix with ones on the main diagonal and zeros elsewhere, is called the characteristic polynomial of AA. The equation det⁡(A−λI)=0\det(A-\lambda I)=0 is called the characteristic equation of AA. The eigenvalues of AA are the real roots of the characteristic equation of AA. Using the characteristic equation, we can also show that λ1λ2⋯λn=det⁡(A)\lambda_1\lambda_2\cdots\lambda_n=\det(A) and ∑i=1nλi=trace⁡(A)=∑i=1nAi,i\sum_{i=1}^n\lambda_i=\operatorname{trace}(A)=\sum_{i=1}^nA_{i,i}.

AA is diagonalizable if and only if it has linearly independent eigenvectors.15 Let λ1, λ2, ⋯ , λn\lambda_1,\ \lambda_2,\ \cdots,\ \lambda_n be the eigenvalues of AA, x1, x2, ⋯ , xnx_1,\ x_2,\ \cdots,\ x_n be the corresponding eigenvectors. and X=[x1∣x2∣⋯∣xn]X=[x_1\mid x_2\mid\cdots\mid x_n], then

X−1AX=[λ1λ2⋱λn]=D⇒A=XDX−1⇒Ak=XDkX−1.X^{-1}AX=\begin{bmatrix}\lambda_1&&&\\&\lambda_2&&\\&&\ddots&\\&&&\lambda_n\end{bmatrix}=D\Rightarrow A=XDX^{-1}\Rightarrow A^k=XD^kX^{-1}. 55

If matrix A=[2112]\displaystyle A=\begin{bmatrix}2&1\\1&2\end{bmatrix}, what are the eigenvalues and eigenvectors of AA?

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 λ\lambda be an eigenvalue and x=[x1x2]\displaystyle x=\begin{bmatrix}x_1\\x_2\end{bmatrix} be its corresponding eigenvector. By definition, we have

Ax=[2112][x1x2]=[2x1+x2x1+2x2]=λx=[λx1λx2]⇒{2x1+x2=λx1x1+2x2=λx2⇒3(x1+x2)=λ(x1+x2)\begin{aligned} Ax=\begin{bmatrix}2&1\\1&2\end{bmatrix}\begin{bmatrix}x_1\\x_2\end{bmatrix}&=\begin{bmatrix}2x_1+x_2\\x_1+2x_2\end{bmatrix}=\lambda x=\begin{bmatrix}\lambda x_1\\\lambda x_2\end{bmatrix}\\ &\Rightarrow\begin{cases}2x_1+x_2=\lambda x_1\\x_1+2x_2=\lambda x_2\end{cases}\Rightarrow3(x_1+x_2)=\lambda(x_1+x_2) \end{aligned}

So either λ=3\lambda=3, in which case x1=x2x_1=x_2 (plug λ=3\lambda=3 into equation 2x1+x2=λx12x_1+x_2=\lambda x_1) and the corresponding normalized eigenvector is [1/21/2]\displaystyle\begin{bmatrix}1/\sqrt{2}\\1/\sqrt{2}\end{bmatrix}, or x1+x2=0x_1+x_2=0, in which case the normalized eigenvector is [1/2−1/2]\displaystyle\begin{bmatrix}1/\sqrt{2}\\-1/\sqrt{2}\end{bmatrix} and λ=1\lambda=1 (plug x2=−x1x_2=-x_1 into equation 2x1+x2=λx12x_1+x_2=\lambda x_1).

Approach B: Use equation det⁡(A−λI)=0\det(A-\lambda I)=0.

det⁡(A−λI)=0⇒(2−λ)(2−λ)−1=0\det(A-\lambda I)=0\Rightarrow(2-\lambda)(2-\lambda)-1=0. Solving the equation, we have λ1=1\lambda_1=1 and λ2=3\lambda_2=3. Applying the eigenvalues to Ax=λxAx=\lambda x, we can get the corresponding eigenvectors.

Approach C: Use equations λ1⋅λ2⋯λn=det⁡(A)\lambda_1\cdot\lambda_2\cdots\lambda_n=\det(A) and ∑i=1nλi=trace⁡(A)=∑i=1nAi,i\sum_{i=1}^n\lambda_i=\operatorname{trace}(A)=\sum_{i=1}^nA_{i,i}.

det⁡(A)=2×2−1×1=3\det(A)=2\times2-1\times1=3 and trace⁡(A)=2×2=4\operatorname{trace}(A)=2\times2=4.

So we have λ1×λ2=3λ1+λ2=4}⇒{λ1=1λ2=3\displaystyle\left.\begin{aligned}\lambda_1\times\lambda_2&=3\\\lambda_1+\lambda_2&=4\end{aligned}\right\}\Rightarrow\begin{cases}\lambda_1=1\\\lambda_2=3\end{cases}. Again apply the eigenvalues to Ax=λxAx=\lambda x, and we can get the corresponding eigenvectors.

56

Positive semidefinite/definite matrix

When AA is a symmetric n×nn\times n matrix, as in the cases of covariance and correlation matrices, all the eigenvalues of AA are real numbers. Furthermore, all eigenvectors that belong to distinct eigenvalues of AA are orthogonal.

Each of the following conditions is a necessary and sufficient condition to make a symmetric matrix AA positive semidefinite:

  1. xTAx≥0x^TAx\ge0 for any n×1n\times1 vector xx.
  2. All eigenvalues of AA are nonnegative.
  3. All the upper left (or lower right) submatrices AK, K=1,⋯ ,nA_K,\ K=1,\cdots,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 AA positive definite:

  1. xTAx>0x^TAx>0 for any nonzero n×1n\times1 vector xx.
  2. All eigenvalues of AA are positive.
  3. All the upper left (or lower right) submatrices AK, K=1,⋯ ,nA_K,\ K=1,\cdots,n have positive determinants.

There are 3 random variables xx, yy and zz. The correlation between xx and yy is 0.8 and the correlation between xx and zz is 0.8. What is the maximum and minimum correlation between yy and zz?

Solution: The problem can be solved using the positive semidefiniteness property of the correlation matrix.

Let the correlation between yy and zz be ρ\rho, then the correlation matrix for xx, yy and zz is

P=[10.80.80.81ρ0.8ρ1].\mathrm{P}=\begin{bmatrix}1&0.8&0.8\\0.8&1&\rho\\0.8&\rho&1\end{bmatrix}. 57 det⁡(P)=1×det⁡([1ρρ1])−0.8×det⁡([0.80.8ρ1])+0.8×det⁡([0.80.81ρ])=(1−ρ2)−0.8×(0.8−0.8ρ)+0.8×(0.8ρ−0.8)=−0.28+1.28ρ−ρ2≥0⇒(ρ−1)(ρ−0.28)≤0⇒0.28≤ρ≤1\begin{aligned} \det(\mathrm{P})&=1\times\det\left(\begin{bmatrix}1&\rho\\\rho&1\end{bmatrix}\right)-0.8\times\det\left(\begin{bmatrix}0.8&0.8\\\rho&1\end{bmatrix}\right)+0.8\times\det\left(\begin{bmatrix}0.8&0.8\\1&\rho\end{bmatrix}\right)\\ &=(1-\rho^2)-0.8\times(0.8-0.8\rho)+0.8\times(0.8\rho-0.8)=-0.28+1.28\rho-\rho^2\ge0\\ &\Rightarrow(\rho-1)(\rho-0.28)\le0\Rightarrow0.28\le\rho\le1 \end{aligned}

So the maximum correlation between yy and zz is 1, the minimum is 0.28.

LU decomposition and Cholesky decomposition

Let AA be a nonsingular n×nn\times n matrix. LU decomposition expresses AA as the product of a lower and upper triangular matrix: A=LUA=LU.17

LU decomposition can be use to solve Ax=bAx=b and calculate the determinant of AA:

LUx=b⇒Ux=y, Ly=b;det⁡(A)=det⁡(L)det⁡(U)=∏i=1nLi,i∏j=1nUj,j.LUx=b\Rightarrow Ux=y,\ Ly=b;\quad\det(A)=\det(L)\det(U)=\prod_{i=1}^nL_{i,i}\prod_{j=1}^nU_{j,j}.

When AA is a symmetric positive definite matrix, Cholesky decomposition expresses AA as A=RTRA=R^TR, where RR is a unique upper-triangular matrix with positive diagonal entries. Essentially, it is a LU decomposition with the property L=UTL=U^T.

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)N(0,1) (standard normal distribution) random variables with correlation ρ\rho if you have a random number generator for standard normal distribution?

Solution: Two N(0,1)N(0,1) random variables x1, x2x_1,\ x_2 with a correlation ρ\rho can be generated from independent N(0,1)N(0,1) random variables z1, z2z_1,\ z_2 using the following equations:

x1=z1x_1=z_1 x2=ρz1+1−ρ2z2x_2=\rho z_1+\sqrt{1-\rho^2}z_2

It is easy to confirm that var⁡(x1)=var⁡(z1)=1, var⁡(x2)=ρ2var⁡(z1)+(1−ρ2)var⁡(z2)=1\operatorname{var}(x_1)=\operatorname{var}(z_1)=1,\ \operatorname{var}(x_2)=\rho^2\operatorname{var}(z_1)+(1-\rho^2)\operatorname{var}(z_2)=1, and cov⁡(x1,x2)=cov⁡(z1,ρz1+1−ρ2z2)=cov⁡(z1,ρz1)=ρ\operatorname{cov}(x_1,x_2)=\operatorname{cov}(z_1,\rho z_1+\sqrt{1-\rho^2}z_2)=\operatorname{cov}(z_1,\rho z_1)=\rho.

This approach is a basic example using Cholesky decomposition to generate correlated random numbers. To generate correlated random variables that follow a nn-dimensional 58 multivariate normal distribution X=[X1,X2,⋯ ,Xn]T∼N(μ,Σ)X=[X_1,X_2,\cdots,X_n]^T\sim N(\mu,\Sigma) with mean μ=[μ1,μ2,⋯ ,μn]T\mu=[\mu_1,\mu_2,\cdots,\mu_n]^T and covariance matrix Σ\Sigma (a n×nn\times n positive definite matrix)18, we can decompose the covariance matrix Σ\Sigma into RTRR^TR and generate nn independent N(0,1)N(0,1) random variables z1, z2,⋯ ,znz_1,\ z_2,\cdots,z_n. Let vector Z=[z1,z2,⋯ ,zn]TZ=[z_1,z_2,\cdots,z_n]^T, then XX can be generated as X=μ+RTZX=\mu+R^TZ.19

Alternatively, XX can also be generated using another important matrix decomposition called singular value decomposition (SVD): For any n×pn\times p matrix XX, there exists a factorization of the form X=UDVTX=UDV^T, where UU and VV are n×pn\times p and p×pp\times p orthogonal matrices, with columns of UU spanning the column space of XX, and the columns of VV spanning the row space; DD is a p×pp\times p diagonal matrix called the singular values of XX. For a positive definite covariance matrix, we have V=UV=U and Σ=UDUT\Sigma=UDU^T. Furthermore, DD is the diagonal matrix of eigenvalues λ1, λ2,⋯ ,λn\lambda_1,\ \lambda_2,\cdots,\lambda_n and UU is the matrix of nn corresponding eigenvectors. Let D1/2D^{1/2} be a diagonal matrix with diagonal elements λ1, λ2, ⋯ , λn\sqrt{\lambda_1},\ \sqrt{\lambda_2},\ \cdots,\ \sqrt{\lambda_n}, then it is clear that D=(D1/2)2=(D1/2)(D1/2)TD=(D^{1/2})^2=(D^{1/2})(D^{1/2})^T and Σ=UD1/2(UD1/2)T\Sigma=UD^{1/2}(UD^{1/2})^T. Again, if we generate a vector of nn independent N(0,1)N(0,1) random variables Z=[z1,z2,⋯ ,zn]TZ=[z_1,z_2,\cdots,z_n]^T, XX can be generated as X=μ+(UD1/2)ZX=\mu+(UD^{1/2})Z.