Quantitative Finance · Book 18 · Careers

The Interview Book

The Interview Book · Careers

15Linear Algebra and Calculus

A risk system rejects the morning’s correlation matrix: one eigenvalue is −0.8-0.8. The quant on call is asked, before touching any code, why three pairwise correlations that each look reasonable can be impossible together, and what the nearest valid matrix is. Linear algebra and calculus questions in interviews are rarely about technique for its own sake; they are about the few facts that recur in finance (a covariance matrix must be positive semidefinite, a regression is a projection, a Gaussian integral has a closed form, a constrained optimum has a multiplier) and about using them quickly.

15.1 Eigenvalues, definiteness and correlation matrices

A covariance or correlation matrix is symmetric positive semidefinite: w⊤Σww^\top \Sigma w is the variance of the portfolio ww and cannot be negative (One Quant Book 4, chapter 22). Equivalently all eigenvalues are non-negative, or all principal minors are. Pairwise estimation, missing data and hand-edited stress scenarios break this property, and interviews ask how to see it and repair it.

Proposition 15.1 (The third correlation)

Given ρ12\rho_{12} and ρ13\rho_{13}, the 3×33 \times 3 correlation matrix is positive semidefinite exactly when

ρ12ρ13−(1−ρ122)(1−ρ132)  ≤  ρ23  ≤  ρ12ρ13+(1−ρ122)(1−ρ132).\rho_{12}\rho_{13} - \sqrt{(1 - \rho_{12}^2)(1 - \rho_{13}^2)} \;\le\; \rho_{23} \;\le\; \rho_{12}\rho_{13} + \sqrt{(1 - \rho_{12}^2)(1 - \rho_{13}^2)}.

Proof. The determinant 1−ρ122−ρ132−ρ232+2ρ12ρ13ρ231 - \rho_{12}^2 - \rho_{13}^2 - \rho_{23}^2 + 2\rho_{12}\rho_{13}\rho_{23} is a concave quadratic in ρ23\rho_{23} whose roots are the two bounds; between them it is non-negative, and the 2×22 \times 2 minors are non-negative for correlations in [−1,1][-1, 1]. ∎

The feasible values of ( _12, _23) fill the inside of each ellipse (). When _13 = 0.9 the ellipse is thin: for a given _12 the range of _23 is narrow, and two assets each highly correlated with a third must be correlated with each other. Data: fig_iv_corr.py.
Figure 15.1. The feasible values of (ρ12,ρ23)(\rho_{12}, \rho_{23}) fill the inside of each ellipse (Proposition 15.1). When ρ13=0.9\rho_{13} = 0.9 the ellipse is thin: for a given ρ12\rho_{12} the range of ρ23\rho_{23} is narrow, and two assets each highly correlated with a third must be correlated with each other. Data: fig_iv_corr.py.

Example 15.2 (Equicorrelation)

An n×nn \times n matrix with ones on the diagonal and ρ\rho elsewhere is (1−ρ)I+ρ11⊤(1 - \rho)I + \rho\mathbf 1\mathbf 1^\top. Its eigenvalues are 1+(n−1)ρ1 + (n - 1)\rho, with eigenvector 1\mathbf 1, and 1−ρ1 - \rho with multiplicity n−1n - 1, so it is a correlation matrix exactly when −1/(n−1)≤ρ≤1-1/(n-1) \le \rho \le 1. Ten assets cannot all be pairwise correlated at −0.2-0.2.

Method 15.3 (Repairing a correlation matrix)

  1. Check: compute the smallest eigenvalue; a Cholesky factorisation that fails is the fast test.
  2. Repair by clipping: set negative eigenvalues to zero (or a small floor), rebuild, and rescale to a unit diagonal (eigenvalue clipping, One Quant Book 4, chapter 22).
  3. Repair by the nearest correlation matrix in the Frobenius norm (Higham, 2002): alternate projections onto the positive semidefinite cone and onto unit-diagonal matrices.
  4. Say which entries moved most, since they are the ones the data did not support.

15.2 Projections and least squares

Ordinary least squares projects the vector of observations yy onto the column space of the regressors XX. The hat matrix H=X(X⊤X)−1X⊤H = X(X^\top X)^{-1}X^\top is symmetric and idempotent, its eigenvalues are 0 and 1, and its trace is the number of regressors pp; its diagonal entries are the leverages, which average p/np/n. The residual y−Hyy - Hy is orthogonal to every column of XX, and R2R^2 is the squared cosine of the angle between the centred yy and its projection.

Example 15.4 (A rank-one update)

I+vv⊤I + vv^\top has eigenvalue 1+∥v∥21 + \|v\|^2 in the direction of vv and 1 on the orthogonal complement, and its inverse is I−vv⊤/(1+∥v∥2)I - vv^\top/(1 + \|v\|^2) (Sherman–Morrison). A one-factor covariance matrix σ2I+ββ⊤\sigma^2 I + \beta\beta^\top is this form, and its inverse is what minimum-variance portfolios need.

15.3 Integrals, series and expansions

A handful of results covers most calculus questions: ∫e−x2/2 dx=2π\int e^{-x^2/2}\,dx = \sqrt{2\pi}; for a standard normal ZZ, E[eaZ]=ea2/2\E[e^{aZ}] = e^{a^2/2}, E[Z4]=3\E[Z^4] = 3 and E[Z+]=1/2π\E[Z^+] = 1/\sqrt{2\pi}; ∑k≥1kxk=x/(1−x)2\sum_{k \ge 1} k x^k = x/(1-x)^2 for ∣x∣<1|x| < 1; and Taylor’s theorem with a remainder, which turns “approximately” into a bound.

Method 15.5 (Integrals in an interview)

  1. Look for a density: an integrand that is a density times something is an expectation.
  2. Complete the square in an exponent; it turns a Gaussian integral with a linear term into a shifted one.
  3. Use symmetry to kill odd moments.
  4. Differentiate under the integral sign, or with respect to a parameter of a known series.

15.4 Optimisation with a constraint

A constrained optimum sets the gradient of the objective parallel to the gradient of the constraint. The minimum-variance portfolio fully invested (1⊤w=1\mathbf 1^\top w = 1) is w=Σ−11/(1⊤Σ−11)w = \Sigma^{-1}\mathbf 1/(\mathbf 1^\top\Sigma^{-1}\mathbf 1); the portfolio of highest expected return at a given volatility is proportional to Σ−1μ\Sigma^{-1}\mu, scaled to the volatility (One Quant Book 7, chapter 25, on mean–variance construction).

Example 15.6 (Two assets)

With volatilities σ1,σ2\sigma_1, \sigma_2 and correlation ρ\rho, the minimum-variance weight on the first asset is (σ22−ρσ1σ2)/(σ12+σ22−2ρσ1σ2)(\sigma_2^2 - \rho\sigma_1\sigma_2)/(\sigma_1^2 + \sigma_2^2 - 2\rho\sigma_1\sigma_2). With 20%, 10% and 0.3 it is about 0.105, for a portfolio volatility of about 9.8%, below that of either asset.

15.5 Worked answers

Example 15.7 (The volatility of a pair trade)

“Long one unit of a stock with 20% volatility, short one unit of a peer with 25%, correlation 0.9. What is the volatility of the pair?” Write the quadratic form w⊤Σww^\top\Sigma w with w=(1,−1)w = (1, -1): 0.22+0.252−2×0.9×0.2×0.25=0.04+0.0625−0.09=0.01250.2^2 + 0.25^2 - 2 \times 0.9 \times 0.2 \times 0.25 = 0.04 + 0.0625 - 0.09 = 0.0125, a volatility of 0.0125≈11.2%\sqrt{0.0125} \approx 11.2\%. Checks: at correlation 1 the pair’s volatility would be the difference, 5%; at 0 it would be 0.1025≈32%\sqrt{0.1025} \approx 32\%; 11% lies between and near the high-correlation end. The answer also shows why pairs are sized by volatility: with the short scaled to 0.2/0.25=0.80.2/0.25 = 0.8 units the variance falls to 0.04+0.04−2×0.9×0.04=0.0080.04 + 0.04 - 2 \times 0.9 \times 0.04 = 0.008, about 8.9%.

Example 15.8 (Integration by parts against a normal)

“For a standard normal ZZ, compute E[Zsin⁡Z]\E[Z\sin Z].” Since φ′(z)=−zφ(z)\varphi'(z) = -z\varphi(z), integrating by parts gives E[Zf(Z)]=E[f′(Z)]\E[Zf(Z)] = \E[f'(Z)] for smooth ff of moderate growth. So E[Zsin⁡Z]=E[cos⁡Z]\E[Z\sin Z] = \E[\cos Z], the real part of the characteristic function at 1: e−1/2≈0.607e^{-1/2} \approx 0.607. The identity is worth knowing by name (Stein’s lemma), because it also gives E[Z4]=3E[Z2]=3\E[Z^4] = 3\E[Z^2] = 3 in one line and appears in the Greeks of Chapter 17.

Example 15.9 (Newton’s method on a transcendental equation)

“Solve xex=1xe^x = 1 to four decimals, by hand.” With g(x)=xex−1g(x) = xe^x - 1 and g′(x)=(1+x)exg'(x) = (1 + x)e^x, start at x0=0.5x_0 = 0.5, where g=0.5×1.6487−1=−0.1756g = 0.5 \times 1.6487 - 1 = -0.1756: the step is 0.1756/(1.5×1.6487)≈0.0710.1756/(1.5 \times 1.6487) \approx 0.071, so x1≈0.5710x_1 \approx 0.5710. One more step gives x2≈0.56716x_2 \approx 0.56716, and the next changes only the sixth decimal: 0.56710.5671. Newton roughly doubles the number of correct digits at each step, which is why the same method solves for an implied volatility or a yield in two or three iterations from a sensible start.

15.6 Question bank

Interview question 15.1 ★ researcher, bank • any

What are the eigenvalues and eigenvectors of (2112)\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}?

Solution

Solution of Interview question 15.1.

The matrix is I+11⊤I + \mathbf 1\mathbf 1^\top in two dimensions: eigenvalue 3 with eigenvector (1,1)(1, 1) and 1 with (1,−1)(1, -1). Trace 4 and determinant 3 confirm.

What the interviewer is looking for: recognising the structure and checking with trace and determinant.

Interview question 15.2 ★ researcher, risk • bank

Is the matrix with unit diagonal, ρ12=ρ13=0.9\rho_{12} = \rho_{13} = 0.9 and ρ23=−0.9\rho_{23} = -0.9 a valid correlation matrix? How do you see it quickly?

Solution

Solution of Interview question 15.2.

No. By Proposition 15.1, with ρ12=ρ13=0.9\rho_{12} = \rho_{13} = 0.9 the third correlation must lie in [0.81−0.19,0.81+0.19]=[0.62,1][0.81 - 0.19, 0.81 + 0.19] = [0.62, 1]. The determinant is 1−3×0.81+2×0.9×0.9×(−0.9)=−2.888<01 - 3 \times 0.81 + 2 \times 0.9 \times 0.9 \times (-0.9) = -2.888 < 0, and the eigenvalues are −0.8-0.8, 1.9 and 1.9. Intuitively, assets 2 and 3 cannot both move with asset 1 and against each other.

What the interviewer is looking for: a feasibility check by the bound or the determinant, with the intuition.

Interview question 15.3 ★ researcher, bank • bank

For a standard normal ZZ, compute E[eZ]\E[e^Z] and E[Z4]\E[Z^4].

Solution

Solution of Interview question 15.3.

Completing the square, E[eZ]=e1/2≈1.649\E[e^Z] = e^{1/2} \approx 1.649 (the lognormal mean). E[Z4]=3\E[Z^4] = 3 (the fourth moment of a normal, used as the benchmark kurtosis).

What the interviewer is looking for: the moment generating function and the normal kurtosis.

Interview question 15.4 ★ researcher, trader • any

Compute ∑k≥1k/2k\sum_{k \ge 1} k/2^k.

Solution

Solution of Interview question 15.4.

Differentiate ∑xk=1/(1−x)\sum x^k = 1/(1-x): ∑kxk=x/(1−x)2\sum kx^k = x/(1-x)^2, which at x=12x = \tfrac12 is 2. (It is also the mean of a geometric waiting time.)

What the interviewer is looking for: differentiating a known series.

Interview question 15.5 ★★ risk, researcher • bank

Two assets have correlations 0.8 and 0.6 with a third. What values can their correlation with each other take?

Solution

Solution of Interview question 15.5.

0.48±(1−0.64)(1−0.36)=0.48±0.480.48 \pm \sqrt{(1 - 0.64)(1 - 0.36)} = 0.48 \pm 0.48: any value from 0 to 0.96.

What the interviewer is looking for: the determinant condition applied with numbers.

Interview question 15.6 ★★ researcher • systematic fund

Ten assets are all pairwise correlated at ρ\rho. What are the eigenvalues of the correlation matrix, and what is the smallest possible ρ\rho?

Solution

Solution of Interview question 15.6.

Eigenvalue 1+9ρ1 + 9\rho (eigenvector 1\mathbf 1) and 1−ρ1 - \rho with multiplicity 9. Non-negativity needs ρ≥−1/9\rho \ge -1/9: ten assets can be at most about −0.11-0.11 correlated with each other on average, because their equally weighted sum cannot have negative variance.

What the interviewer is looking for: the equicorrelation spectrum and the portfolio interpretation of the bound.

Interview question 15.7 ★★ researcher, mle • systematic fund

In a regression of 250 observations on an intercept and four regressors, what are the trace and the eigenvalues of the hat matrix? What is the average leverage, and why does it matter?

Solution

Solution of Interview question 15.7.

HH is a projection onto a five-dimensional space: eigenvalues 1 (five times) and 0 (245 times), trace 5. The average leverage is 5/250=0.025/250 = 0.02; observations with leverage several times that (extreme regressor values) pull the fit towards themselves and deserve a look, as does any regression whose p/np/n is not small.

What the interviewer is looking for: the projection’s spectrum and the meaning of leverage.

Interview question 15.8 ★★ risk, trader • asset manager

Two assets have volatilities 20% and 10% and correlation 0.3. What fully invested portfolio has the lowest variance, and what is its volatility?

Solution

Solution of Interview question 15.8.

w1=(0.01−0.3×0.02)/(0.04+0.01−2×0.3×0.02)=0.004/0.038≈0.105w_1 = (0.01 - 0.3 \times 0.02)/(0.04 + 0.01 - 2 \times 0.3 \times 0.02) = 0.004/0.038 \approx 0.105 in the volatile asset, 0.895 in the other, for a volatility of about 9.8%, lower than the less volatile asset’s 10% because the correlation is below σ2/σ1=0.5\sigma_2/\sigma_1 = 0.5.

What the interviewer is looking for: the two-asset formula and the diversification condition.

Interview question 15.9 ★★ bank, researcher • bank

For a standard normal ZZ, compute E[max⁡(Z,0)]\E[\max(Z, 0)]. Where does this number appear in option pricing?

Solution

Solution of Interview question 15.9.

E[Z+]=∫0∞zφ(z) dz=φ(0)=1/2π≈0.399\E[Z^+] = \int_0^\infty z\varphi(z)\,dz = \varphi(0) = 1/\sqrt{2\pi} \approx 0.399. It gives the at-the-money approximation of an option: a forward at-the-money call is worth about 0.4 SσT0.4\,S\sigma\sqrt T (Chapter 17).

What the interviewer is looking for: the integral by the density’s derivative, and the link to option prices.

Interview question 15.10 ★★★ risk, developer • bank

Find the valid correlation matrix nearest, in the Frobenius norm, to the invalid matrix of Interview question 15.2. How far does it move, and which algorithm scales to a thousand assets?

Solution

Solution of Interview question 15.10.

By symmetry the answer keeps the pattern of signs with equal magnitudes; alternating projections (or eigenvalue clipping, which coincides here) give ρ12=ρ13=0.5\rho_{12} = \rho_{13} = 0.5 and ρ23=−0.5\rho_{23} = -0.5, with eigenvalues 0, 1.5, 1.5: every entry moves by 0.4, a Frobenius distance of 6×0.16≈0.98\sqrt{6 \times 0.16} \approx 0.98. For a thousand assets use the alternating-projections algorithm with Dykstra’s correction (each step one eigendecomposition, O(n3)O(n^3)) or a Newton method on the dual (Higham’s paper and its successors), and report which entries moved.

What the interviewer is looking for: the structure of the answer, its distance, and the algorithm at scale.

Interview question 15.11 ★★★ researcher, bank • systematic fund

With v=(1,2,2)⊤v = (1, 2, 2)^\top, what are the eigenvalues of I+vv⊤I + vv^\top, and what is its inverse? Why does this matter for a one-factor covariance model?

Solution

Solution of Interview question 15.11.

∥v∥2=9\|v\|^2 = 9: eigenvalues 10 (along vv) and 1, 1. The inverse is I−vv⊤/10I - vv^\top/10 by Sherman–Morrison. A one-factor covariance σ2I+ββ⊤\sigma^2 I + \beta\beta^\top is a scaled version, so its inverse (needed for minimum-variance weights or a Mahalanobis distance) costs O(n)O(n), not O(n3)O(n^3).

What the interviewer is looking for: the spectrum of a rank-one update and the computational payoff.

Interview question 15.12 ★★★ researcher, trader • multi-manager fund

Two uncorrelated assets have expected returns 5% and 3% and volatilities 20% and 10%. Which portfolio has the highest expected return at a volatility of 10%, with no budget constraint? What are its weights, its expected return and its Sharpe ratio?

Solution

Solution of Interview question 15.12.

Maximise μ⊤w\mu^\top w subject to w⊤Σw=0.01w^\top\Sigma w = 0.01: w∝Σ−1μ=(0.05/0.04,0.03/0.01)=(1.25,3)w \propto \Sigma^{-1}\mu = (0.05/0.04, 0.03/0.01) = (1.25, 3), whose volatility is 1.252×0.04+32×0.01≈0.39\sqrt{1.25^2 \times 0.04 + 3^2 \times 0.01} \approx 0.39; scale by 0.1/0.390.1/0.39 to w≈(0.320,0.768)w \approx (0.320, 0.768). Expected return about 3.91%, Sharpe ratio about 0.39, higher than either asset’s 0.25 and 0.30.

What the interviewer is looking for: the Lagrangian solution Σ−1μ\Sigma^{-1}\mu and the scaling.

Interview question 15.13 ★★★ bank, researcher • any

Bound the error of 1+x+x2/21 + x + x^2/2 as an approximation of exe^x at x=0.1x = 0.1, and compare with the actual error.

Solution

Solution of Interview question 15.13.

The Lagrange remainder is eξx3/6e^\xi x^3/6 for some ξ∈(0,x)\xi \in (0, x), at most e0.1×0.001/6≈0.000184e^{0.1} \times 0.001/6 \approx 0.000184. The actual error is e0.1−1.105≈0.000171e^{0.1} - 1.105 \approx 0.000171, inside the bound.

What the interviewer is looking for: the remainder term, bounded honestly.

Sources and further reading

  • N. J. Higham, “Computing the nearest correlation matrix—a problem from finance”, IMA Journal of Numerical Analysis 22(3), 2002, 329–343.
  • One Quant Book 4, chapters 16, 22 and 25 (least squares, covariance estimation, numerical linear algebra); One Quant Book 7, chapter 25 (portfolio construction).