Matrices as the machinery of portfolios
A portfolio problem is a matrix problem. The covariance matrix holds the variances and co-movements of the assets, and almost every task asks you to do something with it. scipy.linalg.solve(A, b) solves for the unknown . You should not compute the inverse inv(A) and multiply: the direct solve does less work and loses less accuracy. The Cholesky factorization, linalg.cholesky(S, lower=True), splits a covariance matrix as , where is lower triangular, which means zeros above the diagonal. If is a vector of independent standard normal numbers, then has covariance , and this is how you simulate correlated returns.
The eigenvalues of tell you how many independent sources of risk there really are. linalg.eigvalsh(S) returns them for a symmetric matrix, smallest first. For two assets with correlation the correlation matrix has eigenvalues and , so highly correlated assets have one big eigenvalue and one tiny one. The ratio of the largest to the smallest eigenvalue is the condition number. A large condition number means the matrix is nearly singular, that is, nearly impossible to invert, and a small change in the data can swing the answer of a solve a lot. Optimisers that take the inverse of are sensitive to exactly this.
- Solve, do not invert:
solve(A, b)is faster and more accurate thaninv(A) @ b. - Cholesky makes correlation: multiply independent normals by to get returns with covariance .
- Watch the condition number: highly correlated assets make nearly singular and solutions unstable.
Lab · what's graded
- The Cholesky factor20 pts
- Solving a system10 pts
- A condition number10 pts
- A factor's share of variance10 pts
- What each routine is for15 pts
- Simulating correlated returns10 pts
- Why not invert25 pts