Operations That Preserve Convexity
Real objectives are built from pieces: a loss plus a penalty, a maximum over scenarios, a norm of a residual. Instead of checking Hessians, you recognise convexity by how the pieces are combined. These rules are exactly what convex modelling tools check automatically.
The rules
| Operation | Condition | Result |
|---|---|---|
| non-negative weighted sum | , convex | convex |
| composition with affine map | convex | convex |
| pointwise maximum | every convex | convex |
| supremum | convex in for each | convex |
| composition | convex and non-decreasing, convex | convex |
| composition | convex and non-increasing, concave | convex |
| partial minimization | jointly convex, convex | convex |
| perspective | convex, | convex |
Products and differences of convex functions are not convex in general: is, but is not, and is concave.
Recognising common objectives
- Least squares : the convex composed with an affine map.
- Lasso : a sum of two convex terms with .
- Logistic loss : the convex, increasing of an affine function.
- Worst-case loss : a maximum of convex functions.
- Largest eigenvalue : a supremum of linear functions of .
import numpy as np
rng = np.random.default_rng(4)
A = rng.normal(size=(20, 3)); b = rng.normal(size=20)
# a composite objective: worst residual of three subsets + L1 penalty
def f(x):
parts = [np.linalg.norm(A[i::3] @ x - b[i::3]) for i in range(3)]
return max(parts) + 0.5 * np.abs(x).sum()
violations = 0
for _ in range(5000):
x, y = rng.normal(size=(2, 3)) * 2
t = rng.uniform()
violations += f(t * x + (1 - t) * y) > t * f(x) + (1 - t) * f(y) + 1e-12
print("chord violations in 5000 trials:", violations)chord violations in 5000 trials: 0
No violations, as the rules predict: a maximum of norms of affine maps plus a weighted norm.
The composition rule in one dimension
For with scalar functions, the chain rule gives
If is convex () and non-decreasing () and is convex (), both terms are non-negative. Drop "non-decreasing" and the rule fails: is convex but decreasing for , and is convex, yet has two separate minima at , so it cannot be convex.
import numpy as np
x = np.linspace(-1.5, 1.5, 7)
f = (x**2 - 1)**2
f2 = 12 * x**2 - 4 # second derivative
print("x: ", x)
print("f'':", f2)x: [-1.5 -1. -0.5 0. 0.5 1. 1.5]
f'': [23. 8. -1. -4. -1. 8. 23.]
The second derivative is negative near 0, so this composition is not convex.
Partial minimization in action
If is jointly convex, minimizing out leaves a convex function of . Example: the distance to a convex set, , is convex. A machine-learning example: the Huber loss is the partial minimum of over .
import numpy as np
s = np.linspace(-10, 10, 200001)
def partial_min(r):
return float(np.min(0.5 * (r - s)**2 + np.abs(s)))
huber = lambda r: 0.5 * r**2 if abs(r) <= 1 else abs(r) - 0.5
for r in (-3.0, -0.5, 0.0, 0.8, 2.0):
print(f"r={r:5.1f} min over s: {partial_min(r):.4f} Huber: {huber(r):.4f}")r= -3.0 min over s: 2.5000 Huber: 2.5000
r= -0.5 min over s: 0.1250 Huber: 0.1250
r= 0.0 min over s: 0.0000 Huber: 0.0000
r= 0.8 min over s: 0.3200 Huber: 0.3200
r= 2.0 min over s: 1.5000 Huber: 1.5000
In software
- CVXPY applies these rules ("DCP rules") to every expression and refuses non-convex ones with a
DCPError, so a model that compiles is guaranteed convex. - Robust ML losses (Huber, quantile, hinge) are designed as convex compositions so training keeps its guarantees.
Exercises
Exercise 1: Apply the rules
Is convex?
Answer
Yes.
Solution
is convex and non-decreasing, and is convex, so the composition rule applies.
Exercise 2: Which rule fails?
Is for convex? What about ?
Answer
is concave, not convex. is not convex either.
Solution
. For , , which is negative for . The rules do not apply because is decreasing but is convex, not concave.
Exercise 3: Pointwise maximum (code)
Check numerically that satisfies the chord inequality on 10,000 random pairs.
Answer
No violations: a maximum of linear functions is convex.
Solution
import numpy as np
rng = np.random.default_rng(9)
f = lambda x: np.maximum.reduce([2 * x - 1, -x, 0.5 * x])
x, y, t = rng.normal(size=(3, 10000)) * 3
t = np.abs(t) % 1
print(int(np.sum(f(t * x + (1 - t) * y) > t * f(x) + (1 - t) * f(y) + 1e-12)))0
Test yourself
If and are convex, which is always convex?
is convex when is convex and is…
- convex and non-decreasing
- concave
- convex and non-increasing
- any increasing function
The lasso objective with is…
- convex
- concave
- neither
- convex only when is square