TExcelTAD
Home › Math for Machine Learning › Affine Spaces and Affine Maps

Affine Spaces and Affine Maps

A linear model with an intercept, f(x)=wTx+bf(\mathbf{x}) = \mathbf{w}^T\mathbf{x} + b, is not linear in the strict sense: it does not send 0\mathbf{0} to 00. It is affine. Affine spaces (shifted subspaces) and affine maps (linear map plus shift) are the honest description of decision boundaries, solution sets and neural layers.

Affine subspaces

Take a subspace UU of VV and a support point x0\mathbf{x}_0. The set

L=x0+U={x0+u:u∈U}L = \mathbf{x}_0 + U = \{\mathbf{x}_0 + \mathbf{u} : \mathbf{u} \in U\}

is an affine subspace of dimension dim⁡U\dim U. In R3\mathbb{R}^3 these are points, lines and planes anywhere, not only through the origin. With a basis b1,…,bk\mathbf{b}_1, \ldots, \mathbf{b}_k of UU, every point of LL has a parametric form x0+λ1b1+⋯+λkbk\mathbf{x}_0 + \lambda_1\mathbf{b}_1 + \cdots + \lambda_k\mathbf{b}_k.

The solution set of Ax=bA\mathbf{x} = \mathbf{b} (if non-empty) is exactly such a set: xp+ker⁡(A)\mathbf{x}_p + \ker(A), as seen in Systems of Linear Equations.

Hyperplanes: the decision boundaries of linear classifiers

A hyperplane in RD\mathbb{R}^D is an affine subspace of dimension D−1D - 1. It can be written with a normal vector w\mathbf{w} and an offset bb:

H={x∈RD:wTx+b=0}H = \{\mathbf{x} \in \mathbb{R}^D : \mathbf{w}^T\mathbf{x} + b = 0\}

HH splits space into two half-spaces, wTx+b>0\mathbf{w}^T\mathbf{x} + b > 0 and <0< 0. Logistic regression, the perceptron and the SVM all classify by which side of a hyperplane a point lies on.

python · try it yourself
import numpy as np

w, b = np.array([2.0, -1.0]), -1.0            # boundary: 2x - y - 1 = 0
points = np.array([[2.0, 1.0], [0.0, 0.0], [1.0, 1.0], [3.0, 2.0]])
scores = points @ w + b
for p, s in zip(points, scores):
    side = "positive" if s > 0 else ("on H" if s == 0 else "negative")
    print(p, "score", s, "->", side)
# a parametric form of the same line: support point + direction orthogonal to w
x0 = np.array([0.5, 0.0]); d = np.array([1.0, 2.0])
print("points on H:", [float(w @ (x0 + t * d) + b) for t in (-1, 0, 2)])
[2. 1.] score 2.0 -> positive
[0. 0.] score -1.0 -> negative
[1. 1.] score 0.0 -> on H
[3. 2.] score 3.0 -> positive
points on H: [0.0, 0.0, 0.0]

Affine maps

An affine map is a linear map followed by a translation:

ϕ(x)=Ax+b\phi(\mathbf{x}) = A\mathbf{x} + \mathbf{b}

Affine maps keep lines as lines, keep parallel lines parallel and keep ratios along a line (midpoints stay midpoints), but they move the origin. The composition of two affine maps is affine: A2(A1x+b1)+b2=(A2A1)x+(A2b1+b2)A_2(A_1\mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2 = (A_2A_1)\mathbf{x} + (A_2\mathbf{b}_1 + \mathbf{b}_2).

The homogeneous-coordinates trick turns an affine map into a linear one in one more dimension: append a 1 to x\mathbf{x}, and

[Ax+b1]=[Ab0T1][x1]\begin{bmatrix} A\mathbf{x} + \mathbf{b} \\ 1 \end{bmatrix} = \begin{bmatrix} A & \mathbf{b} \\ \mathbf{0}^T & 1 \end{bmatrix} \begin{bmatrix} \mathbf{x} \\ 1 \end{bmatrix}

This is the bias trick of Data as Vectors and Matrices and the reason 3D graphics uses 4×44 \times 4 matrices.

python · try it yourself
import numpy as np

A1, b1 = np.array([[1.0, 2.0], [0.0, 1.0]]), np.array([1.0, -1.0])
A2, b2 = np.array([[0.0, -1.0], [1.0, 0.0]]), np.array([3.0, 0.0])
def hom(A, b):
    M = np.eye(3); M[:2, :2] = A; M[:2, 2] = b; return M
x = np.array([2.0, 1.0])
direct = A2 @ (A1 @ x + b1) + b2
via_matrix = (hom(A2, b2) @ hom(A1, b1) @ np.append(x, 1.0))[:2]
print("direct:", direct, " homogeneous:", via_matrix)
mid = A1 @ ((x + np.array([0.0, 3.0])) / 2) + b1
print("midpoint preserved:", np.allclose(mid, ((A1 @ x + b1) + (A1 @ [0.0, 3.0] + b1)) / 2))
direct: [3. 5.]  homogeneous: [3. 5.]
midpoint preserved: True

In software

  • Every dense layer computes the affine map Wx+bW\mathbf{x} + \mathbf{b} before its activation; PyTorch's nn.Linear stores weight and bias separately.
  • Image augmentation libraries apply random affine transforms (rotate, scale, shear, shift) as 2×32 \times 3 matrices.
  • A linear classifier's decision boundary is the hyperplane wTx+b=0\mathbf{w}^T\mathbf{x} + b = 0, and ∣wTx+b∣/∥w∥\lvert \mathbf{w}^T\mathbf{x} + b \rvert / \lVert \mathbf{w} \rVert is the distance of a point to it.

Exercises

Exercise 1: Parametric to normal form

Write the line through (1,2)(1, 2) with direction (3,1)(3, 1) as wTx+b=0\mathbf{w}^T\mathbf{x} + b = 0.

Answer

x−3y+5=0x - 3y + 5 = 0 (or any nonzero multiple).

Solution

A normal vector is orthogonal to the direction: w=(1,−3)\mathbf{w} = (1, -3) works since 3−3=03 - 3 = 0. Then b=−wT(1,2)=−(1−6)=5b = -\mathbf{w}^T(1, 2) = -(1 - 6) = 5.

Exercise 2: Is it affine?

Is ϕ(x,y)=(x+y+1,2y−3)\phi(x, y) = (x + y + 1, 2y - 3) affine? If so, give AA and b\mathbf{b}.

Answer

Yes: A=[1102]A = \begin{bmatrix} 1 & 1 \\ 0 & 2 \end{bmatrix}, b=(1,−3)\mathbf{b} = (1, -3).

Solution

Each output is a linear combination of the inputs plus a constant, which is exactly the form Ax+bA\mathbf{x} + \mathbf{b}.

Exercise 3: Distance to a boundary (code)

Compute the distance from (3,4)(3, 4) to the line 3x+4y−5=03x + 4y - 5 = 0.

Answer
Solution
∣3⋅3+4⋅4−5∣32+42=205=4\frac{\lvert 3 \cdot 3 + 4 \cdot 4 - 5 \rvert}{\sqrt{3^2 + 4^2}} = \frac{20}{5} = 4
python · try it yourself
import numpy as np
w, b, p = np.array([3.0, 4.0]), -5.0, np.array([3.0, 4.0])
print(abs(w @ p + b) / np.linalg.norm(w))
4.0

Test yourself

The set {x:wTx+b=0}\{\mathbf{x} : \mathbf{w}^T\mathbf{x} + b = 0\} with b≠0b \ne 0 is…

  1. a subspace
  2. an affine subspace (hyperplane) not through the origin
  3. a single point
  4. empty

Which property do affine maps NOT always keep?

  1. Lines stay lines
  2. Parallel lines stay parallel
  3. The origin stays fixed
  4. Midpoints stay midpoints

In homogeneous coordinates, an affine map on R2\mathbb{R}^2 becomes…

  1. a 2×22 \times 2 matrix
  2. a 3×33 \times 3 matrix acting on (x,y,1)(x, y, 1)
  3. a scalar
  4. a nonlinear map