1.2.8Calculus & Optimization Basics

Convex vs non-convex functions

1,974 words9 min readdifficulty · medium4 backlinks

WHY do we care? (the 80/20 core)

Machine learning is mostly minimizing a loss function L(θ)L(\theta). The whole game is: find parameters θ\theta that make loss small.

  • If LL is convex: there's essentially one basin. Follow the slope downhill → guaranteed to reach the best answer. (Linear/logistic regression, SVM.)
  • If LL is non-convex: multiple valleys, ridges, saddle points. Downhill can strand you in a mediocre valley (a bad local minimum). (Neural networks.)

So "convex vs non-convex" tells you how hard your optimization is and whether you can trust the answer you converged to.


WHAT exactly is convexity?


HOW do we check convexity? (three lenses)

Lens 1 — The chord test (definition itself)

Pick the midpoint λ=12\lambda = \tfrac12: convex means f ⁣(x+y2)f(x)+f(y)2.f\!\left(\frac{x+y}{2}\right) \le \frac{f(x)+f(y)}{2}. The average of the outputs is at least the output of the average.

Lens 2 — First-order (tangent line) condition

For a differentiable ff, convex     \iff the graph lies above every tangent line: f(y)f(x)+f(x)(yx)x,y.f(y) \ge f(x) + \nabla f(x)^\top (y - x) \quad \forall x,y.

Lens 3 — Second-order (curvature) condition — the practical one

For twice-differentiable ff:

  • 1D: convex     f(x)0\iff f''(x) \ge 0 everywhere (curves upward / never bends down).
  • Multivariate: convex     \iff the Hessian 2f(x)\nabla^2 f(x) is positive semidefinite (PSD) everywhere, i.e. v2f(x)v0v^\top \nabla^2 f(x)\, v \ge 0 for all vv.

Deriving the 1D second-order test from scratch

We show: if f(x)0f''(x)\ge 0 everywhere, then the tangent-line condition (Lens 2) holds.

Step 1 — Taylor with exact remainder. For some cc between xx and yy: f(y)=f(x)+f(x)(yx)+12f(c)(yx)2.f(y) = f(x) + f'(x)(y-x) + \tfrac12 f''(c)\,(y-x)^2. Why this step? Taylor's theorem gives an exact expression, isolating the curvature term.

Step 2 — Bound the remainder. Since f(c)0f''(c)\ge 0 and (yx)20(y-x)^2 \ge 0: 12f(c)(yx)20.\tfrac12 f''(c)(y-x)^2 \ge 0. Why this step? This is the only unknown term; nonnegative curvature makes it nonnegative.

Step 3 — Conclude. f(y)f(x)+f(x)(yx).f(y) \ge f(x) + f'(x)(y-x). Why this step? That's exactly Lens 2 — graph above tangent — hence convex. \blacksquare


Figure — Convex vs non-convex functions

Worked examples


Common mistakes (Steel-manned)


Flashcards

Convex function definition (inequality form)
f(λx+(1λ)y)λf(x)+(1λ)f(y)f(\lambda x+(1-\lambda)y) \le \lambda f(x)+(1-\lambda)f(y) for all λ[0,1]\lambda\in[0,1] — curve below the chord.
Geometric meaning of convexity
The line segment joining any two points on the graph lies on or above the graph (bowl shape).
Second-order convexity test in 1D
f(x)0f''(x)\ge 0 for ALL xx in the domain.
Multivariate convexity test
Hessian 2f(x)\nabla^2 f(x) is positive semidefinite everywhere (vHv0v^\top H v \ge 0).
Key optimization payoff of convexity
Every local minimum is a global minimum (stationary point ⇒ global min).
Is x3x^3 convex?
No — f=6xf''=6x changes sign; x=0x=0 is an inflection point.
Is x2+y2x^2+y^2 convex?
Yes — Hessian =diag(2,2)=\mathrm{diag}(2,2), positive definite everywhere.
Why is x2y2x^2-y^2 non-convex?
Hessian eigenvalues 2,22,-2 (indefinite); origin is a saddle point.
What functions are both convex AND concave?
Only affine (linear + constant) functions.
Why do neural nets have non-convex loss?
Composition of nonlinear activations creates multiple minima/saddles ⇒ Hessian not PSD everywhere.
First-order convexity condition
f(y)f(x)+f(x)(yx)f(y)\ge f(x)+\nabla f(x)^\top(y-x) — graph lies above every tangent line.

Recall Feynman: explain to a 12-year-old

Imagine a skateboard ramp. A convex ramp is a single smooth U-shaped bowl: no matter where you drop a marble, it always rolls down to the same lowest point. Easy — you can't get stuck. A non-convex ramp is a bumpy skate park with many dips. Your marble might stop in a small dip that isn't the deepest one, thinking "I'm done!" while a deeper valley sits right over the hill. Convex = one honest bowl; non-convex = tricky landscape with fake bottoms.


Connections

Concept Map

needs

defined by

requires

type

type

guarantees

enables

risks

examples

examples

checked by

checked by

checked by

implies

ML minimizes loss L theta

Convexity property

Chord definition f of avg <= avg of f

Convex domain set

Convex single valley bowl

Non-convex many valleys

Local min is global min

Gradient descent finds best

Trapped in bad local min

Linear/logistic regression SVM

Neural networks

Chord test

Tangent line f above tangent

Hessian PSD f'' >= 0

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, machine learning me hum hamesha ek loss function ko minimize karte hain — matlab aise parameters dhoondhna jaha error sabse kam ho. Ab yaha sawal aata hai: function ka shape kaisa hai? Agar function convex hai, toh woh ek single katora (bowl) jaisa hota hai. Kahin se bhi marble chhodo, woh ghum-ghum ke same sabse neeche wale point pe pahunch jaata hai. Iska matlab: gradient descent chalao, aur guaranteed global minimum mil jaayega. No tension of getting stuck.

Convex check karne ka sabse practical tareeka: second derivative. 1D me agar f(x)0f''(x) \ge 0 har jagah (poore domain me, sirf ek point pe nahi!), toh function convex hai. Multi-dimension me yahi cheez Hessian matrix ke through hoti hai — Hessian positive semidefinite honi chahiye (saare eigenvalues 0\ge 0). Jaise x2x^2 ka f=2f''=2 hamesha positive — convex. Lekin x3x^3 ka f=6xf''=6x kabhi negative kabhi positive — isliye NON-convex.

Non-convex functions me kai saare valleys hote hain — bilkul bumpy skate park jaisa. Gradient descent kisi chhoti si dip me phas sakta hai (local minimum) aur soch le "bas ho gaya," jabki asli deepest valley pahaad ke us paar hai. Neural networks ka loss aisa hi non-convex hota hai, kyunki nonlinear activations layer-by-layer milke bahut tedhi-medhi landscape bana dete hain.

Yaad rakhne ka simple funda: convex ka matlab local minimum hi global minimum hota hai — yahi convexity ka asli power hai. Non-convex me yeh guarantee tut jaati hai. Isliye jab bhi koi optimization problem dekho, pehle poochho: "Yeh convex hai kya?" — agar haan, toh life easy; agar nahi, toh tricks (random restarts, momentum, etc.) chahiye.

Go deeper — visual, from zero

Test yourself — Calculus & Optimization Basics

Connections