Skip to content

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.

6 of 6 ~4 min

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:

r=[2.11.43.20.81.9]\mathbf{r} = \begin{bmatrix} 2.1 \\ 1.4 \\ 3.2 \\ 0.8 \\ 1.9 \end{bmatrix}

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 v=[3,4]\mathbf{v} = [3, 4] 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:

∥v∥=v12+v22+⋯+vn2\|\mathbf{v}\| = \sqrt{v_1^2 + v_2^2 + \cdots + v_n^2}

For v=[3,4]\mathbf{v} = [3, 4], the norm is 9+16=5\sqrt{9 + 16} = 5. 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: [1,2]+[3,4]=[4,6][1, 2] + [3, 4] = [4, 6].

Scalar multiplication multiplies every entry by a constant: 2×[1,2]=[2,4]2 \times [1, 2] = [2, 4].

The dot product (inner product) multiplies vectors entry-by-entry and sums the results:

v⋅w=∑i=1nviwi\mathbf{v} \cdot \mathbf{w} = \sum_{i=1}^{n} v_i w_i

The dot product of v=[1,2,3]\mathbf{v} = [1, 2, 3] and w=[4,5,6]\mathbf{w} = [4, 5, 6] is 1×4+2×5+3×6=321 \times 4 + 2 \times 5 + 3 \times 6 = 32.

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 mm rows and nn columns is called an m×nm \times n matrix. The entry in row ii, column jj is written aija_{ij}.

Two matrices appear constantly in quant finance:

The returns matrix R\mathbf{R} of size T×nT \times n contains TT time periods of returns for nn assets. Row tt holds all assets’ returns on day tt; column jj holds asset jj‘s returns across all days.

The covariance matrix Σ\mathbf{\Sigma} of size n×nn \times n contains the variances of each asset on the diagonal and the covariances between each pair off the diagonal. It is always symmetric, Σij=Σji\Sigma_{ij} = \Sigma_{ji} because covariance is symmetric.

The Transpose

The transpose operation swaps rows and columns. If A\mathbf{A} is m×nm \times n, then A⊤\mathbf{A}^\top is n×mn \times m. The first row becomes the first column.

[A⊤]ij=aji[\mathbf{A}^\top]_{ij} = a_{ji}

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 A\mathbf{A} (size m×nm \times n) by B\mathbf{B} (size n×pn \times p), you compute each entry (i,j)(i,j) as the dot product of row ii of A\mathbf{A} with column jj of B\mathbf{B}:

(AB)ij=∑k=1naikbkj(\mathbf{A}\mathbf{B})_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj}

The resulting matrix has size m×pm \times p. Notice that the inner dimensions must match, A\mathbf{A} must have as many columns as B\mathbf{B} 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 w\mathbf{w} (the proportion of capital in each asset) and a return vector r\mathbf{r}:

rp=w⊤r=∑i=1nwirir_p = \mathbf{w}^\top \mathbf{r} = \sum_{i=1}^{n} w_i r_i

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.