Convex Function

lean4-proofoptimizationvisualization
f(λx+(1λ)y)λf(x)+(1λ)f(y)f(\lambda x + (1-\lambda)y) \le \lambda f(x) + (1-\lambda)f(y)

Statement

A function f:RRf : \mathbb{R} \to \mathbb{R} is convex if for all x,yx, y and λ[0,1]\lambda \in [0,1]:

f(λx+(1λ)y)λf(x)+(1λ)f(y).f(\lambda x + (1-\lambda)y) \le \lambda f(x) + (1-\lambda)f(y).

Geometrically: the chord joining (x,f(x))(x, f(x)) to (y,f(y))(y, f(y)) lies on or above the graph of ff.

Key example: f(x)=x2f(x) = x^2 is convex on all of R\mathbb{R} because 2=even2 = \text{even}. Mathlib records this as Even.convexOn_pow: for any even nn, xxnx \mapsto x^n is convex on R\mathbb{R} (univ).

Visualization

Chord above curve for f(x)=x2f(x) = x^2 between x=0x = 0 and x=2x = 2:

  f(x) = x²
  4 |        * (2, 4)
    |       /|
  3 |      / |  chord from (0,0) to (2,4)
    |     /  |  slope = 2, chord value at x=1: 2
  2 |    /   |
    |   *    |  graph value at x=1: 1
  1 |  /·    |  chord (2) ≥ graph (1) ✓
    | / ·    |
  0 *--------+---
    0    1   2
xxf(x)=x2f(x) = x^2chord at xx (from 00 to 22)chord \ge graph?
000yes
112yes
244yes (equal)

At λ=1/2\lambda = 1/2: f(1)=1120+124=2f(1) = 1 \le \frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 4 = 2. Convexity holds.

Second-derivative test: f(x)=2>0f''(x) = 2 > 0 everywhere, so ff is strictly convex.

Proof Sketch

  1. Expand the convexity inequality for f(x)=x2f(x) = x^2: need (λx+(1λ)y)2λx2+(1λ)y2(\lambda x + (1-\lambda)y)^2 \le \lambda x^2 + (1-\lambda)y^2.
  2. Rearrange: λ(1λ)(xy)20\lambda(1-\lambda)(x-y)^2 \ge 0, which holds since λ[0,1]\lambda \in [0,1].
  3. Mathlib path: Even.convexOn_pow (for nn even, applied with n=2n = 2, hn : Even 2) proves ConvexOn ℝ Set.univ (fun x ↦ x ^ 2). The lemma is in Mathlib.Analysis.Convex.Mul.

Connections

  • Jensen's Inequality (Convex)Jensen's Inequality (Convex)f ⁣(iwixi)iwif(xi),wi0,  wi=1f\!\left(\sum_i w_i x_i\right) \le \sum_i w_i f(x_i),\quad w_i \ge 0,\; \sum w_i = 1For a convex function, the image of a weighted sum is at most the weighted sum of imagesRead more → — Jensen's inequality is the direct extension of convexity to weighted sums and expectations
  • KKT ConditionsKKT Conditionsf(x)=iλigi(x),λigi(x)=0,λi0\nabla f(x^*) = \sum_i \lambda_i \nabla g_i(x^*),\quad \lambda_i g_i(x^*) = 0,\quad \lambda_i \ge 0Necessary optimality conditions for constrained optimization via Lagrangian gradient and complementary slacknessRead more → — KKT conditions are sufficient for constrained optimality when ff is convex
  • AM–GM InequalityAM–GM Inequalitya1++anna1ann\frac{a_1+\cdots+a_n}{n} \geq \sqrt[n]{a_1 \cdots a_n}The arithmetic mean is always at least as large as the geometric meanRead more → — AM–GM follows from the convexity of xlogxx \mapsto -\log x
  • Cauchy–Schwarz InequalityCauchy–Schwarz Inequalityu,vuv|\langle u, v \rangle| \leq \|u\| \, \|v\|The inner product of two vectors is bounded by the product of their normsRead more → — Cauchy–Schwarz can be proved using the convexity of xx2x \mapsto x^2

Lean4 Proof

import Mathlib.Analysis.Convex.Mul

/-- x² is convex on all of ℝ, via Even.convexOn_pow with n = 2. -/
theorem sq_convexOn : ConvexOn  Set.univ (fun x :  ↦ x ^ 2) :=
  even_two.convexOn_pow