Affine Spaces and Affine Maps
A linear model with an intercept, , is not linear in the strict sense: it does not send to . 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 of and a support point . The set
is an affine subspace of dimension . In these are points, lines and planes anywhere, not only through the origin. With a basis of , every point of has a parametric form .
The solution set of (if non-empty) is exactly such a set: , as seen in Systems of Linear Equations.
Hyperplanes: the decision boundaries of linear classifiers
A hyperplane in is an affine subspace of dimension . It can be written with a normal vector and an offset :
splits space into two half-spaces, and . Logistic regression, the perceptron and the SVM all classify by which side of a hyperplane a point lies on.
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:
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: .
The homogeneous-coordinates trick turns an affine map into a linear one in one more dimension: append a 1 to , and
This is the bias trick of Data as Vectors and Matrices and the reason 3D graphics uses matrices.
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 before its activation; PyTorch's
nn.Linearstoresweightandbiasseparately. - Image augmentation libraries apply random affine transforms (rotate, scale, shear, shift) as matrices.
- A linear classifier's decision boundary is the hyperplane , and is the distance of a point to it.
Exercises
Exercise 1: Parametric to normal form
Write the line through with direction as .
Answer
(or any nonzero multiple).
Solution
A normal vector is orthogonal to the direction: works since . Then .
Exercise 2: Is it affine?
Is affine? If so, give and .
Answer
Yes: , .
Solution
Each output is a linear combination of the inputs plus a constant, which is exactly the form .
Exercise 3: Distance to a boundary (code)
Compute the distance from to the line .
Answer
Solution
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 with is…
- a subspace
- an affine subspace (hyperplane) not through the origin
- a single point
- empty
Which property do affine maps NOT always keep?
- Lines stay lines
- Parallel lines stay parallel
- The origin stays fixed
- Midpoints stay midpoints
In homogeneous coordinates, an affine map on becomes…
- a matrix
- a matrix acting on
- a scalar
- a nonlinear map