TExcelTAD
Home › Optimization › Operations That Preserve Convexity

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 ∑iwifi\sum_i w_if_i wi≥0w_i \ge 0, fif_i convex convex
composition with affine map f(Ax+b)f(A\mathbf{x} + \mathbf{b}) ff convex convex
pointwise maximum max⁡ifi(x)\max_i f_i(\mathbf{x}) every fif_i convex convex
supremum sup⁡yg(x,y)\sup_{\mathbf{y}} g(\mathbf{x}, \mathbf{y}) gg convex in x\mathbf{x} for each y\mathbf{y} convex
composition h(g(x))h(g(\mathbf{x})) hh convex and non-decreasing, gg convex convex
composition h(g(x))h(g(\mathbf{x})) hh convex and non-increasing, gg concave convex
partial minimization inf⁡y∈Cg(x,y)\inf_{\mathbf{y} \in C} g(\mathbf{x}, \mathbf{y}) gg jointly convex, CC convex convex
perspective t f(x/t)t\,f(\mathbf{x}/t) ff convex, t>0t > 0 convex

Products and differences of convex functions are not convex in general: x⋅x=x2x \cdot x = x^2 is, but x⋅yx \cdot y is not, and x2−2x2=−x2x^2 - 2x^2 = -x^2 is concave.

Recognising common objectives

  • Least squares ∥Ax−b∥22\lVert A\mathbf{x} - \mathbf{b} \rVert_2^2: the convex ∥⋅∥22\lVert \cdot \rVert_2^2 composed with an affine map.
  • Lasso ∥Ax−b∥22+λ∥x∥1\lVert A\mathbf{x} - \mathbf{b} \rVert_2^2 + \lambda\lVert \mathbf{x} \rVert_1: a sum of two convex terms with λ≥0\lambda \ge 0.
  • Logistic loss ∑ilog⁡(1+e−yiaiTx)\sum_i \log(1 + e^{-y_i\mathbf{a}_i^T\mathbf{x}}): the convex, increasing log⁡(1+eu)\log(1 + e^u) of an affine function.
  • Worst-case loss max⁡k∥Akx−bk∥\max_k \lVert A_k\mathbf{x} - \mathbf{b}_k \rVert: a maximum of convex functions.
  • Largest eigenvalue λmax⁡(X)=sup⁡∥v∥=1vTXv\lambda_{\max}(X) = \sup_{\lVert \mathbf{v} \rVert = 1}\mathbf{v}^TX\mathbf{v}: a supremum of linear functions of XX.
python · try it yourself
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 f=h∘gf = h \circ g with scalar functions, the chain rule gives

f′′(x)=h′′(g(x)) g′(x)2+h′(g(x)) g′′(x)f''(x) = h''(g(x))\,g'(x)^2 + h'(g(x))\,g''(x)

If hh is convex (h′′≥0h'' \ge 0) and non-decreasing (h′≥0h' \ge 0) and gg is convex (g′′≥0g'' \ge 0), both terms are non-negative. Drop "non-decreasing" and the rule fails: h(u)=u2h(u) = u^2 is convex but decreasing for u<0u < 0, and g(x)=x2−1g(x) = x^2 - 1 is convex, yet h(g(x))=(x2−1)2h(g(x)) = (x^2 - 1)^2 has two separate minima at x=±1x = \pm 1, so it cannot be convex.

python · try it yourself
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 g(x,y)g(\mathbf{x}, \mathbf{y}) is jointly convex, minimizing out y\mathbf{y} leaves a convex function of x\mathbf{x}. Example: the distance to a convex set, dist⁡(x,C)=inf⁡y∈C∥x−y∥\operatorname{dist}(\mathbf{x}, C) = \inf_{\mathbf{y} \in C}\lVert \mathbf{x} - \mathbf{y} \rVert, is convex. A machine-learning example: the Huber loss is the partial minimum of 12(r−s)2+∣s∣\frac{1}{2}(r - s)^2 + \lvert s \rvert over ss.

python · try it yourself
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 f(x)=e∥x∥2f(\mathbf{x}) = e^{\lVert \mathbf{x} \rVert_2} convex?

Answer

Yes.

Solution

h(u)=euh(u) = e^u is convex and non-decreasing, and g(x)=∥x∥2g(\mathbf{x}) = \lVert \mathbf{x} \rVert_2 is convex, so the composition rule applies.

Exercise 2: Which rule fails?

Is f(x)=log⁡(x)f(x) = \log(x) for x>0x > 0 convex? What about −log⁡(x2+1)-\log(x^2 + 1)?

Answer

log⁡x\log x is concave, not convex. −log⁡(x2+1)-\log(x^2 + 1) is not convex either.

Solution

(log⁡x)′′=−1/x2<0(\log x)'' = -1/x^2 < 0. For g(x)=−log⁡(x2+1)g(x) = -\log(x^2 + 1), g′′(x)=2(x2−1)(x2+1)2g''(x) = \frac{2(x^2 - 1)}{(x^2 + 1)^2}, which is negative for ∣x∣<1\lvert x \rvert < 1. The rules do not apply because −log⁡-\log is decreasing but x2+1x^2 + 1 is convex, not concave.

Exercise 3: Pointwise maximum (code)

Check numerically that f(x)=max⁡(2x−1,−x,0.5x)f(x) = \max(2x - 1, -x, 0.5x) satisfies the chord inequality on 10,000 random pairs.

Answer

No violations: a maximum of linear functions is convex.

Solution
python · try it yourself
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 f1f_1 and f2f_2 are convex, which is always convex?

  1. f1−f2f_1 - f_2
  2. f1⋅f2f_1 \cdot f_2
  3. max⁡(f1,f2)\max(f_1, f_2)
  4. min⁡(f1,f2)\min(f_1, f_2)

h(g(x))h(g(x)) is convex when gg is convex and hh is…

  1. convex and non-decreasing
  2. concave
  3. convex and non-increasing
  4. any increasing function

The lasso objective ∥Ax−b∥22+λ∥x∥1\lVert A\mathbf{x} - \mathbf{b} \rVert_2^2 + \lambda\lVert \mathbf{x} \rVert_1 with λ≥0\lambda \ge 0 is…

  1. convex
  2. concave
  3. neither
  4. convex only when AA is square