Vectors and Matrices
A vector is a list of numbers; a matrix is a table of numbers. Linear algebra is the language of multi-factor models, portfolio optimization, and machine learning.
Vectors: Ordered Lists of Numbers
A vector is the simplest data structure in quantitative finance. It is an ordered collection of numbers, nothing more, nothing less.
Picture a portfolio with five assets. The vector of expected returns, in percent per month, might look like this:
Each entry corresponds to one asset. The order matters, asset 3’s expected return is 3.2, not 0.8.
The Geometric View
Vectors have a geometric life too. A 2-dimensional vector like can be drawn as an arrow starting at the origin and ending at the point (3, 4) in a coordinate plane. The norm (or length) of a vector is the distance from the origin to its tip:
For , the norm is . This is the Pythagorean theorem in action.
In a 500-asset portfolio, each asset is its own dimension. You are working in a 500-dimensional space, which is impossible to visualize, but the algebra stays the same.
Vector Operations
Vectors support three basic operations.
Addition adds corresponding entries: .
Scalar multiplication multiplies every entry by a constant: .
The dot product (inner product) multiplies vectors entry-by-entry and sums the results:
The dot product of and is .
The dot product leads to one of the most useful concepts: orthogonality. Two vectors are orthogonal when their dot product equals zero. Geometrically, they are perpendicular, they point in completely unrelated directions. This matters in statistics when you want to confirm that one variable adds independent information that another cannot provide.
Matrices: Tables That Transform Data
A matrix is a rectangular table of numbers with rows and columns. If a vector is a single column of data, a matrix is several columns side by side.
A matrix with rows and columns is called an matrix. The entry in row , column is written .
Two matrices appear constantly in quant finance:
The returns matrix of size contains time periods of returns for assets. Row holds all assets’ returns on day ; column holds asset ‘s returns across all days.
The covariance matrix of size contains the variances of each asset on the diagonal and the covariances between each pair off the diagonal. It is always symmetric, because covariance is symmetric.
The Transpose
The transpose operation swaps rows and columns. If is , then is . The first row becomes the first column.
In Python with NumPy:
import numpy as np
A = np.array([[1, 4],
[2, 5],
[3, 6]])
print(A.shape) # (3, 2)
print(A.T.shape) # (2, 3)
PROMPT
Use numpy to create a 3x2 matrix and print its shape along with the shape of its transpose.
Matrix Operations
Matrices add and multiply by scalars just like vectors, entry by entry. The interesting operation is matrix multiplication, which combines two matrices.
To multiply (size ) by (size ), you compute each entry as the dot product of row of with column of :
The resulting matrix has size . Notice that the inner dimensions must match, must have as many columns as has rows.
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# Matrix multiplication
C = A @ B # or np.matmul(A, B)
print(C)
# [[19 22]
# [43 50]]
PROMPT
Use numpy to multiply two 2x2 matrices with the @ operator and print the result.
The Dot Product as Matrix Multiplication
A key insight: taking the dot product of two vectors is the same as multiplying a row vector by a column vector. This is the foundation of portfolio return calculations.
Given a weight vector (the proportion of capital in each asset) and a return vector :
The portfolio return is the weighted sum of individual asset returns, a single dot product.
import numpy as np
# Five assets: expected monthly returns
expected_returns = np.array([2.1, 1.4, 3.2, 0.8, 1.9]) # percent
# Portfolio weights (must sum to 1)
weights = np.array([0.30, 0.20, 0.25, 0.10, 0.15])
# Portfolio return = dot product of weights and returns
portfolio_return = weights @ expected_returns
print(f"Expected portfolio return: {portfolio_return:.2f}%")
PROMPT
Use numpy to compute a portfolio’s expected return as the dot product of an asset-weight vector and a returns vector.
This simple operation, one dot product, is the backbone of portfolio return calculations. Every time you see a portfolio’s expected return, somewhere a weight vector is being dotted with a return vector.
References
Strang, Introduction to Linear Algebra, 5th edition, Wellesley-Cambridge Press, Chapters 1–2. ISBN: 978-0980232776.
Lax, Linear Algebra and Its Applications, 2nd edition, Wiley, Chapter 1. ISBN: 978-0471751564.