Convex sets
Define convex sets, recognize them, and use the separating-hyperplane idea.
Convexity is the property that makes optimization tractable: on a convex problem, any local minimum is global and there are no saddles to escape. The geometry starts with convex sets, the shapes a feasible region can take before the objective is even introduced.
Definition
A set is convex if the line segment between any two of its points stays inside it: for all and ,
The point is a convex combination of and , tracing the segment from () to (). A disk, a solid ball, a half-space, and the whole space are convex; a ring, a crescent, and a pair of separated blobs are not[1].
The definition extends to any number of points: a convex set contains every convex combination with and .
Building blocks
Hyperplanes and half-spaces. A hyperplane and a half-space are convex. A half-space is the basic ingredient of linear constraints: each inequality carves out one.
Norm balls. is convex for any norm, by the triangle inequality. The , , and balls are all convex, which is why their unit balls were the feasible regions in the norms lesson.
Operations that preserve convexity
Convexity survives the operations used to assemble feasible regions:
- Intersection. If are convex, so is . A segment lying in both sets lies in their intersection. This extends to arbitrary intersections, so a polyhedron , an intersection of half-spaces, is convex.
- Affine images. If is convex, so is . Scaling, rotating, projecting, and translating a convex set keep it convex.
These rules let you certify convexity of a feasible region by construction, without checking the segment condition directly.
The separating hyperplane theorem
Theorem. If and are disjoint convex sets, there exists a hyperplane separating them: a nonzero and scalar with for all and for all [2].
The picture is that two non-overlapping convex shapes can always be split by a flat wall. This is the geometric root of duality (a separating hyperplane becomes a dual certificate) and of support vector machines (the separating hyperplane is the classifier). The disjointness and convexity are both essential; non-convex sets can interlock so that no flat wall separates them.
Check it on arrays
import numpy as np
rng = np.random.default_rng(0)
# Test convexity empirically: sample pairs, check midpoints stay inside.
def is_convex_region(inside, sampler, trials=100_000):
P = sampler(trials)
Q = sampler(trials)
P, Q = P[inside(P)], Q[inside(Q)]
m = min(len(P), len(Q))
theta = rng.random((m, 1))
mid = theta * P[:m] + (1 - theta) * Q[:m]
return np.all(inside(mid))
box = lambda n: rng.uniform(-2, 2, size=(n, 2))
# L2 ball: convex
in_ball = lambda X: np.linalg.norm(X, axis=1) <= 1.5
print("L2 ball convex:", is_convex_region(in_ball, box))
# Annulus (ring): NOT convex
in_ring = lambda X: (np.linalg.norm(X, axis=1) >= 0.8) & (np.linalg.norm(X, axis=1) <= 1.5)
print("annulus convex:", is_convex_region(in_ring, box))
# Polyhedron {x : A x <= b} is convex (intersection of half-spaces)
A = np.array([[1, 0], [0, 1], [-1, -1]]); b = np.array([1.0, 1.0, 1.0])
in_poly = lambda X: np.all(X @ A.T <= b, axis=1)
print("polyhedron convex:", is_convex_region(in_poly, box))The midpoint test confirms the ball and polyhedron are convex and flags the ring as non-convex.
Exercises
Exercise 1. Show directly from the definition that the half-space is convex, for any nonzero and scalar .
Solution
Step 1. Take any , so and . Pick and form the convex combination .
Step 2. Apply and use linearity:
Step 3. Both and , so each term is bounded by the same coefficient times :
So , and the segment stays inside. The half-space is convex.
Exercise 2. Prove the intersection rule: if and are convex, then is convex. Then explain in one sentence why this makes the polyhedron convex.
Solution
Step 1. Take , so and lie in both sets. Pick and let .
Step 2. Since and is convex, . The identical argument with gives .
Step 3. A point in both sets is in the intersection, so . The segment stays inside, so the intersection is convex.
The polyhedron is the intersection of the half-spaces , one per row of ; each is convex by Exercise 1, so their intersection is convex.
Exercise 3 (code). A convex set contains every convex combination of its points, not only midpoints. For the polyhedron with rows and , sample triples of interior points and random barycentric weights summing to one, and check the weighted combination stays inside.
Solution
import numpy as np
rng = np.random.default_rng(0)
A = np.array([[1, 0], [0, 1], [-1, -1]])
b = np.array([1.0, 1.0, 1.0])
inside = lambda X: np.all(X @ A.T <= b + 1e-12, axis=1)
# Draw points in the box, keep those inside the polyhedron.
box = rng.uniform(-2, 2, size=(300_000, 2))
pts = box[inside(box)]
P, Q, R = pts[0::3], pts[1::3], pts[2::3]
m = min(len(P), len(Q), len(R))
P, Q, R = P[:m], Q[:m], R[:m]
# Random convex weights on the simplex via the sort trick.
u = np.sort(rng.random((m, 2)), axis=1)
t1 = u[:, 0]
t2 = u[:, 1] - u[:, 0]
t3 = 1.0 - u[:, 1]
comb = t1[:, None] * P + t2[:, None] * Q + t3[:, None] * R
assert np.all(inside(comb))
print("all", m, "three-point convex combinations stayed inside")Every weighted combination of three interior points lands inside, confirming the definition extends from segments to arbitrary convex combinations.
With convex feasible regions defined, the next lesson puts a convex function on top of them.
References
- [1]Boyd, S., & Vandenberghe, L. (2004). Convex Optimization, §2.1-2.3. Cambridge University Press. Convex sets and the operations that preserve convexity.↩
- [2]Boyd, S., & Vandenberghe, L. (2004). Convex Optimization, §2.5. Cambridge University Press. The separating and supporting hyperplane theorems.↩