Matrix Operations
Matrix multiplication is not element-wise; it combines transformations. Understanding how to multiply matrices, invert them, and interpret the results is core to regression and risk models.
Matrix Multiplication: The Rules
Matrix multiplication is not something you do entry-by-entry. It is a deliberate combination of rows and columns, and it has specific rules that differ from everyday arithmetic.
To multiply (size ) by (size ), you produce an matrix. The entry at row , column is the dot product of row from with column from :
This has two immediate consequences. First, the inner dimensions must match, the number of columns in must equal the number of rows in . Second, matrix multiplication is not commutative: in general. The order matters.
Matrix multiplication is associative: , and distributive: .
The Identity Matrix and Inverses
The identity matrix plays the same role in matrix multiplication that 1 plays in scalar multiplication. It is a square matrix with 1s on the diagonal and 0s everywhere else:
For any compatible matrix : and .
The inverse of a square matrix , written , is the matrix that satisfies:
Not all matrices have inverses. A matrix that cannot be inverted is called singular or non-invertible. Singularity is not a rare edge case, it is a warning sign that your matrix contains redundant information, like two identical columns.
The determinant is a scalar derived from the matrix that tells you whether inversion is possible. If , the matrix is singular. If , an inverse exists.
Properties of Inverses and Transposes
A few algebraic rules are used constantly in derivations:
Notice the order reverses. To undo the operation then , you first undo then .
Again, order reverses when you transpose a product.
The trace of a matrix is the sum of its diagonal elements. It has the useful property of being invariant under transposition: .
Solving Linear Systems
One of the most common problems in quantitative finance is solving for unknowns in a system of linear equations. This problem can be written compactly as:
where is a known matrix of coefficients, is a known right-hand side vector, and is the unknown vector we want to find.
If is square and invertible, the solution is simply:
However, there is one important practical point: never solve a linear system by explicitly computing the inverse. Computing and then multiplying is slower and numerically less stable than directly solving the system. Use a solver instead.
import numpy as np
# 2x + y = 8
# x + 3y = 9
A = np.array([[2, 1],
[1, 3]])
b = np.array([8, 9])
# Preferred: solve directly
x = np.linalg.solve(A, b)
print(f"Solution: x = {x[0]}, y = {x[1]}")
# Check by multiplying
print(np.allclose(A @ x, b)) # True
# Compare with inverse (do not use in practice)
A_inv = np.linalg.inv(A)
x_inv = A_inv @ b
print(f"Same result via inverse: {x_inv}")
PROMPT
Use numpy to solve a 2x2 linear system with np.linalg.solve and verify the solution with matrix-vector multiplication.
A Financial Example: Portfolio Weights from Target Returns
Suppose you have a covariance matrix and you want to find the portfolio weights that produce a target expected return given that the expected return of each asset is . This leads to a system of linear equations:
With the constraint that weights sum to 1 (), you have two equations and unknowns. For a specific case with two assets, you can solve this directly:
import numpy as np
# Two assets
mu = np.array([0.08, 0.12]) # expected returns
ones = np.array([1.0, 1.0]) # sum-to-1 constraint
# Target portfolio return of 10%
target_return = 0.10
# Build the system A @ w = b
# Equation 1: w1 + w2 = 1
# Equation 2: 0.08*w1 + 0.12*w2 = 0.10
A = np.array([[1, 1],
[mu[0], mu[1]]])
b = np.array([1, target_return])
w = np.linalg.solve(A, b)
print(f"Weights: {w}")
print(f"Sum: {w.sum():.4f}, Return: {w @ mu:.4f}")
PROMPT
Use numpy to solve a small linear system that finds portfolio weights summing to 1 and hitting a target return.
For larger systems with many assets and constraints, the same principles apply, you are solving , and numpy.linalg.solve (or the QR decomposition behind it) is the right tool.
References
Strang, Introduction to Linear Algebra, 5th edition, Wellesley-Cambridge Press, Chapters 2–3. ISBN: 978-0980232776.
Lax, Linear Algebra and Its Applications, 2nd edition, Wiley, Chapters 1–2. ISBN: 978-0471751564.