ML Foundations Lecture 7 of 14
Part II — Calculus of One Variable · Lecture 7

Optimisation in One Dimension

Training a model is minimising a function. This lecture assembles the complete one-dimensional theory — critical points, the second-derivative test, and convexity — so that when we go to a million dimensions in Lecture 12, only the bookkeeping changes.

By the end you can

  • Find and classify critical points using the first and second derivative tests
  • State and use the definition of a convex function, and prove \(f''\ge0 \Rightarrow\) convex
  • Explain precisely what convexity buys you: every local minimum is global
  • Derive the least-squares estimator for a one-parameter model by hand
  • Implement backtracking line search and explain what problem it solves

Assumed

  • Lectures 4–6: derivatives, the chain rule, Taylor expansion
  • Solving quadratics and cubics by factoring

1What we are looking for

Definition — minima

Let \(f:S\to\RR\) with \(S\subseteq\RR\), and \(w^\star\in S\).

Maximisers are defined by reversing the inequality. A point that is one or the other is an extremum.

The \(\delta\) in the local definition is the same \(\delta\) from Lecture 3: "in some neighbourhood". Global means everywhere; local means only that you cannot improve by looking nearby. Gradient descent, which only ever looks nearby, can only ever guarantee the local version — which is why the whole field cares so much about when the two coincide.

2Critical points

Theorem — Fermat's rule (interior extrema are critical)

If \(f\) has a local extremum at an interior point \(w^\star\) of its domain, and \(f\) is differentiable at \(w^\star\), then \(f'(w^\star)=0\).

Proof (for a local minimum). By definition there is \(\delta>0\) with \(f(w^\star+h)\ge f(w^\star)\) whenever \(|h|<\delta\). So the numerator \(f(w^\star+h)-f(w^\star)\) is \(\ge0\) for all such \(h\). Now look at the difference quotient from each side:

\[ h>0:\quad \frac{f(w^\star+h)-f(w^\star)}{h} \ge 0 \quad\Longrightarrow\quad f'(w^\star)=\lim_{h\to0^+}(\cdots)\ \ge 0,\] \[ h<0:\quad \frac{f(w^\star+h)-f(w^\star)}{h} \le 0 \quad\Longrightarrow\quad f'(w^\star)=\lim_{h\to0^-}(\cdots)\ \le 0.\]

The sign flips because we divide a non-negative numerator by a negative \(h\). Since \(f\) is differentiable, both one-sided limits equal the same number \(f'(w^\star)\). A number that is both \(\ge0\) and \(\le0\) is \(0\). ∎

Common trap

Read the direction of that implication carefully. It says extremum ⟹ critical. It does not say the reverse. Three ways \(f'(w)=0\) fails to be a minimum:

3Classifying critical points

Fermat's rule narrows the search to critical points. To sort them we use curvature, and Taylor tells us why it works. Expand about a critical point \(w^\star\), where \(f'(w^\star)=0\):

\[f(w^\star+h) = f(w^\star) + \underbrace{f'(w^\star)}_{=\,0}h + \tfrac12 f''(w^\star)h^2 + O(h^3) = f(w^\star) + \tfrac12 f''(w^\star)h^2 + O(h^3).\]

For small \(h\) the quadratic term dominates the cubic tail, and \(h^2>0\) regardless of direction. So the sign of \(f(w^\star+h)-f(w^\star)\) is the sign of \(f''(w^\star)\).

Theorem — second derivative test

Let \(f'(w^\star)=0\) and let \(f''\) be continuous near \(w^\star\).

The third case is genuinely undecided, not a technicality: \(w^4\) (min), \(-w^4\) (max) and \(w^3\) (neither) all have \(f'=f''=0\) at the origin.

Critical points of f(w) = w⁴ + a·w³ + b·w² — reshape and re-classifyInteractive
ff′local minimumlocal maximuminflection / degenerate
0.00
-3.00
Critical points are where the purple f′ curve crosses zero. Each is classified by the SIGN OF THE SLOPE OF f′ there — which is f″. Watch the two minima merge into one as b rises through zero: at the merge point f″ = 0 and the test goes silent, exactly the inconclusive case. Set a = 0, b = −3 for the symmetric double well; add a ≠ 0 to tilt it and make one well deeper than the other.
Check yourself
\(f(w)=w^3-3w^2+2\). Find and classify all critical points.
Correct. \(f'(w)=3w^2-6w=3w(w-2)\), zero at \(w=0\) and \(w=2\). Then \(f''(w)=6w-6\).
At \(w=0\): \(f''=-6<0\) → local maximum, value \(f(0)=2\).
At \(w=2\): \(f''=6>0\) → local minimum, value \(f(2)=8-12+2=-2\).

Note neither is global: a cubic runs to \(\pm\infty\), so \(f\) has no global extrema on \(\RR\). "Local minimum" and "the answer to your optimisation problem" are different things.
Not quite. Two adjacent local minima would require a local maximum between them (the curve must come back up and go back down), so this is impossible for a function with only two critical points. Concretely, \(f''(0)=-6\), which is negative: the curve opens downwards at \(w=0\).
Not quite. \(w=1\) is where \(f''=0\) — the inflection point, where curvature changes sign. That is a notable feature of the graph but it is not a critical point: \(f'(1)=3-6=-3\neq0\). Critical points come from \(f'=0\); inflections come from \(f''=0\). Different equations, different meanings.

4Convexity: the property that makes optimisation tractable

Everything so far is local. Convexity is the assumption that upgrades local statements to global ones, and it is the dividing line between optimisation problems we can solve and problems we can only attack heuristically.

Definition — convex function

\(f\) is convex on an interval \(I\) if for all \(u,v\in I\) and all \(\lambda\in[0,1]\),

\[ f\big(\lambda u + (1-\lambda)v\big) \;\le\; \lambda f(u) + (1-\lambda)f(v). \]

It is strictly convex if the inequality is strict whenever \(u\neq v\) and \(\lambda\in(0,1)\).

Intuition

The left side is the function evaluated somewhere on the segment between \(u\) and \(v\); the right side is the corresponding height on the straight chord joining \((u,f(u))\) to \((v,f(v))\). So convexity says: the chord never dips below the curve. Equivalently, the region above the graph is a convex set — you can stretch a rubber band between any two points on the graph and it will never cut through.

The chord testInteractive
fchord from u to vthe mixed point λu+(1−λ)v
0
-1.60
1.80
0.50
Convex means the orange chord sits on or above the blue curve for EVERY choice of u and v. For w² and eʷ you will not be able to break it. For w⁴ − 3w² you can: choose u and v in the two different wells and the chord dives under the central bump. Note |w| is convex despite the corner — convexity does not require differentiability, which is exactly why L1 regularisation is still a tractable problem.
Theorem — the second derivative test for convexity

If \(f\) is twice differentiable on an interval \(I\), then

\[ f \text{ is convex on } I \quad\Longleftrightarrow\quad f''(w)\ge 0 \text{ for all } w\in I. \]

And \(f''(w)>0\) everywhere implies strictly convex. (The converse of that last one fails: \(w^4\) is strictly convex but \(f''(0)=0\).)

Proving f″ ≥ 0 ⟹ f lies above every tangent ⟹ f is convex0 / 6
\[f(v) = f(u) + f'(u)(v-u) + \tfrac12 f''(\xi)(v-u)^2 \quad\text{for some }\xi\text{ between }u,v\]
Taylor with Lagrange remainder, n = 1. Straight from Lecture 6. This is exact, not approximate — the mystery point \(\xi\) absorbs all the error.
\[f''(\xi)\ge 0 \text{ and } (v-u)^2\ge 0 \quad\Longrightarrow\quad f(v) \;\ge\; f(u)+f'(u)(v-u)\]
Drop a non-negative term. The remainder is a product of two non-negative quantities, so removing it can only decrease the right-hand side. This inequality is the tangent-line characterisation: a convex function lies on or above each of its tangent lines, everywhere. It is worth remembering in its own right.
\[\text{Let } m=\lambda u+(1-\lambda)v. \quad \begin{cases} f(u)\ge f(m)+f'(m)(u-m)\\ f(v)\ge f(m)+f'(m)(v-m)\end{cases}\]
Apply the tangent inequality twice, both times with the tangent taken at the mixed point \(m\). This is the standard trick: compare both endpoints to the point in the middle.
\[\lambda f(u) + (1-\lambda)f(v) \;\ge\; f(m) + f'(m)\Big[\lambda(u-m)+(1-\lambda)(v-m)\Big]\]
Take a weighted average. Multiply the first inequality by \(\lambda\ge0\) and the second by \((1-\lambda)\ge0\) — multiplying by non-negative numbers preserves \(\ge\) — then add. The \(f(m)\) terms combine because \(\lambda+(1-\lambda)=1\).
\[\lambda(u-m)+(1-\lambda)(v-m) = \big[\lambda u + (1-\lambda)v\big] - m = m - m = 0\]
The bracket vanishes. This is the whole point of choosing the tangent at \(m\): the first-order terms cancel exactly, by the definition of \(m\).
\[\lambda f(u)+(1-\lambda)f(v) \;\ge\; f(m) = f\big(\lambda u+(1-\lambda)v\big) \qquad\blacksquare\]
That is the definition of convexity. Chord above curve. Note what the proof reveals: convexity is really the statement that the function never curves away downwards, and \(f''\ge0\) is the local certificate for that.
Theorem — why we care

Let \(f\) be convex on an interval \(I\). Then:

  1. Every local minimum of \(f\) is a global minimum.
  2. The set of minimisers is an interval (possibly a single point, possibly empty).
  3. If \(f\) is differentiable, then \(f'(w^\star)=0\) is sufficient, not merely necessary, for \(w^\star\) to be a global minimum.
  4. If \(f\) is strictly convex, the minimiser is unique when it exists.

Proof of (1). Let \(w^\star\) be a local minimum, so \(f(w^\star)\le f(w)\) for all \(w\) within \(\delta\). Suppose for contradiction some \(v\in I\) has \(f(v)<f(w^\star)\). Consider points on the segment towards \(v\): for \(\lambda\in(0,1)\) close to 1, the point \(m_\lambda=\lambda w^\star+(1-\lambda)v\) lies within \(\delta\) of \(w^\star\). Convexity gives

\[ f(m_\lambda) \le \lambda f(w^\star) + (1-\lambda)f(v) < \lambda f(w^\star)+(1-\lambda)f(w^\star) = f(w^\star), \]

where the strict inequality used \(f(v)<f(w^\star)\) and \(1-\lambda>0\). So \(m_\lambda\) is a nearby point with a strictly smaller value — contradicting local minimality. Hence no such \(v\) exists. ∎

Where this shows up in ML

This theorem is the reason the field is split in two.

One warning about vocabulary: people say "the loss surface is non-convex" to mean the loss as a function of the parameters. Cross-entropy is convex in the network's outputs; it is the composition with the network that destroys convexity. Composition does not preserve convexity, and that is the whole difficulty of deep learning in one sentence.

5A real example: least squares by hand

Let us do a complete optimisation problem from start to finish. Given data \((x_1,y_1),\dots,(x_n,y_n)\), fit the one-parameter model \(\hat y = w x\) by minimising squared error.

Deriving the least-squares estimator0 / 5
\[J(w) = \frac{1}{2}\sum_{i=1}^{n}\big(w x_i - y_i\big)^2\]
The objective. Sum of squared residuals. The \(\tfrac12\) is a convention: it cancels the 2 from differentiating a square, and since scaling by a positive constant cannot move a minimiser (Lecture 2's argmax theorem), it changes nothing.
\[J'(w) = \sum_{i=1}^{n}\big(wx_i-y_i\big)\,x_i\]
Differentiate. Linearity moves \(\frac{d}{dw}\) inside the sum. Each term is a chain rule: outer \(\tfrac12 u^2\) with derivative \(u\), inner \(u=wx_i-y_i\) with derivative \(x_i\).
\[J'(w) = w\sum_{i} x_i^2 - \sum_i x_iy_i \;\stackrel{!}{=}\; 0\]
Expand and set to zero. Distribute \(x_i\) and split the sum. Note \(w\) does not depend on \(i\), so it factors out of the first sum — this is what makes the problem linear in \(w\).
\[\boxed{\;\hat w = \frac{\sum_i x_iy_i}{\sum_i x_i^2}\;}\]
Solve. A closed form — no iteration needed. Valid provided \(\sum x_i^2\neq0\), i.e. not every \(x_i\) is zero. Notice the structure: it is a ratio of a cross term to a self term, which in Lecture 10 you will recognise as \(\frac{\langle \vx,\vy\rangle}{\langle\vx,\vx\rangle}\) — the formula for projecting \(\vy\) onto \(\vx\).
\[J''(w) = \sum_i x_i^2 \;>\; 0\]
Confirm it is a minimum, and a global one. \(J''\) is a sum of squares, hence non-negative, and strictly positive unless all \(x_i=0\). So \(J\) is strictly convex, the critical point is a strict local minimum by the second-derivative test, and by the convexity theorem it is the unique global minimiser. This is why linear regression has no local-minimum problem, ever.
Least squares: the loss J(w) and the fit it producesInteractive
data + fitted lineJ(w)current woptimal ŵ
0.40
0.70
Left: the data and your candidate line, with residuals drawn in. Right: the loss as a function of w — a perfect parabola, because J is quadratic in w. Slide w and watch the point roll along the parabola. The green marker is the closed-form optimum ŵ = Σxy / Σx²; no search was needed to find it.

6When you cannot solve for the optimum: line search

Least squares had a closed form. Most problems do not, and then we iterate. Lecture 6 showed the step size must satisfy \(\eta<2/L\), but \(L\) is usually unknown. The practical answer is to test each step rather than predict it.

Backtracking line search — choosing η adaptivelyPython · NumPy

7Exercises

★

Find and classify all critical points:

(a) \(f(w)=w^3-6w^2+9w+1\)    (b) \(f(w)=we^{-w}\)    (c) \(f(w)=w^4-8w^2\)

(a) \(f'=3w^2-12w+9=3(w-1)(w-3)\), so \(w=1,3\). \(f''=6w-12\).
\(f''(1)=-6<0\) → local max, \(f(1)=5\). \(f''(3)=6>0\) → local min, \(f(3)=27-54+27+1=1\).

(b) Product rule: \(f'=e^{-w}-we^{-w}=e^{-w}(1-w)\). Since \(e^{-w}>0\) always, the only critical point is \(w=1\). Then \(f''=e^{-w}(w-2)\), so \(f''(1)=-e^{-1}<0\) → local maximum, \(f(1)=1/e\).
It is in fact the global maximum: \(f\to0\) as \(w\to+\infty\) and \(f\to-\infty\) as \(w\to-\infty\).

(c) \(f'=4w^3-16w=4w(w^2-4)=4w(w-2)(w+2)\), so \(w=0,\pm2\). \(f''=12w^2-16\).
\(f''(0)=-16<0\) → local max, \(f(0)=0\). \(f''(\pm2)=48-16=32>0\) → local minima, \(f(\pm2)=16-32=-16\).
Both minima are global (equal value, and \(f\to+\infty\) at both ends). A symmetric double well — and a concrete example of a function with two distinct global minimisers, so the minimiser is not unique. Consistent with the theorem: \(f\) is not convex.

★★

Determine whether each is convex on the stated interval, using \(f''\):

(a) \(f(w)=e^{w}\) on \(\RR\)    (b) \(f(w)=-\ln w\) on \((0,\infty)\)    (c) \(f(w)=w\ln w\) on \((0,\infty)\)    (d) \(f(w)=\log(1+e^{w})\) on \(\RR\)

Then say which machine-learning object each of (b), (c), (d) is a piece of.

(a) \(f''=e^w>0\) everywhere → strictly convex. ✓

(b) \(f'=-1/w\), \(f''=1/w^2>0\) on \((0,\infty)\) → strictly convex. ✓
ML: this is the negative log-likelihood of a single outcome, and the reason cross-entropy is convex in the predicted probability.

(c) \(f'=\ln w+1\), \(f''=1/w>0\) on \((0,\infty)\) → strictly convex. ✓
ML: \(\sum p\ln p\) is negative entropy, so entropy \(H=-\sum p\ln p\) is concave — which is why the uniform distribution maximises it (Lecture 2's exercise), and why maximum-entropy arguments are well-posed.

(d) This is softplus. From Lecture 5, \(f'=\sigma(w)\), and from the same lecture \(f''=\sigma'(w)=\sigma(1-\sigma)\). Since \(0<\sigma<1\), the product is strictly positive → strictly convex. ✓
ML: \(\log(1+e^{-yz})\) is the logistic loss. Its convexity in \(z\) is exactly why logistic regression is a convex problem — and why composing it with a neural network's \(z(\vt)\) is not.

★★

Extend the least-squares derivation to the two-parameter model \(\hat y = w x + b\).

Set both partial derivatives to zero and solve the resulting \(2\times2\) system. Show that the fitted line always passes through \((\bar x,\bar y)\), the centroid of the data.

\(J(w,b)=\frac12\sum_i (wx_i+b-y_i)^2\). Treat one variable at a time (formally, these are partial derivatives — Lecture 12 makes that rigorous, but the mechanics are identical).

\[\frac{\partial J}{\partial b} = \sum_i (wx_i+b-y_i) = 0 \quad\Longrightarrow\quad w\sum x_i + nb = \sum y_i.\]

Divide by \(n\): \(w\bar x + b = \bar y\). This already proves the centroid claim: setting \(x=\bar x\) in the fitted line gives \(w\bar x + b = \bar y\) ✓. It holds for any data, and it is a direct consequence of the residuals summing to zero.

\[\frac{\partial J}{\partial w} = \sum_i (wx_i+b-y_i)x_i = 0 \quad\Longrightarrow\quad w\sum x_i^2 + b\sum x_i = \sum x_iy_i.\]

Substitute \(b=\bar y - w\bar x\) into the second equation:

\[w\sum x_i^2 + (\bar y-w\bar x)\,n\bar x = \sum x_iy_i \;\Longrightarrow\; w\left(\sum x_i^2 - n\bar x^2\right) = \sum x_iy_i - n\bar x\bar y.\] \[\boxed{\;\hat w = \frac{\sum_i (x_i-\bar x)(y_i-\bar y)}{\sum_i (x_i-\bar x)^2} = \frac{\Cov(x,y)}{\Var(x)}, \qquad \hat b = \bar y - \hat w\bar x.\;}\]

The last rewriting uses the identities \(\sum x_iy_i - n\bar x\bar y = \sum(x_i-\bar x)(y_i-\bar y)\) and \(\sum x_i^2 - n\bar x^2 = \sum(x_i-\bar x)^2\), both worth verifying. The result is one of the most quoted formulas in statistics — slope equals covariance over variance — and you have now derived it from nothing but Fermat's rule.

★★★

Coding + proof. Consider minimising the logistic loss for a single data point with label \(y=1\):

\[ \ell(w) = \log\!\left(1+e^{-wx}\right), \qquad x>0 \text{ fixed}. \]
  1. Compute \(\ell'\) and \(\ell''\), and prove \(\ell\) is strictly convex.
  2. Show that \(\ell\) has no minimiser: \(\inf \ell = 0\) but the infimum is not attained. What does this mean for training?
  3. Add L2 regularisation, \(\ell_\lambda(w)=\ell(w)+\frac{\lambda}{2}w^2\), and show a unique finite minimiser now exists for every \(\lambda>0\).
  4. Verify numerically: run gradient descent on both, for 10 000 steps, and report \(w\).

1. Chain rule (and Lecture 5's softplus result):

\[\ell'(w) = \frac{-x e^{-wx}}{1+e^{-wx}} = -x\,\big(1-\sigma(wx)\big) = -x\,\sigma(-wx).\] \[\ell''(w) = x^2\,\sigma(wx)\big(1-\sigma(wx)\big).\]

Since \(x>0\) and \(0<\sigma<1\) strictly, \(\ell''>0\) everywhere → strictly convex. ✓

2. \(\ell'(w)=-x\sigma(-wx)\) is strictly negative for every \(w\), because \(\sigma\) is never zero. So \(\ell\) is strictly decreasing on all of \(\RR\) and has no critical point. As \(w\to\infty\), \(e^{-wx}\to0\) so \(\ell(w)\to\log 1=0\); and \(\ell(w)>0\) always. Hence \(\inf\ell=0\), approached but never attained. ∎

What this means: on linearly separable data, logistic regression has no finite optimum — the weights diverge to infinity as the model becomes ever more confident. In practice you see the training loss creep towards zero while \(\|\vw\|\) grows without bound, the model becomes wildly overconfident, and its probability estimates stop being calibrated. This is a real, commonly-encountered failure, not a pathological example. It is also why sklearn.LogisticRegression applies L2 regularisation by default — a decision that surprises people until they know this.

3. \(\ell_\lambda'' = \ell'' + \lambda > 0\), so \(\ell_\lambda\) is still strictly convex. For existence: \(\ell_\lambda(w) \ge \frac{\lambda}{2}w^2 \to \infty\) as \(|w|\to\infty\) (the function is coercive), so a minimum must be attained at a finite point. Strict convexity makes it unique. ∎

The minimiser solves \(x\,\sigma(-\hat wx) = \lambda \hat w\) — no closed form, but a well-posed problem with a guaranteed answer, which is all an iterative method needs.

import numpy as np
x, lam, eta = 1.0, 0.1, 0.5
sig = lambda z: 1/(1+np.exp(-z))
for name, grad in [("unregularised", lambda w: -x*sig(-w*x)),
                   ("L2, lam=0.1",   lambda w: -x*sig(-w*x) + lam*w)]:
    w = 0.0
    for _ in range(10000): w -= eta*grad(w)
    print(f"{name:16s} w = {w:.6f}")

The unregularised run drifts steadily upward and keeps going — after 10 000 steps it is at \(w\approx 8.52\) and still climbing logarithmically, never settling. Run it for 100 000 steps and you will get roughly \(w\approx 10.8\); there is no limit. The regularised run converges to \(w\approx 1.6335\) and stays there, with the gradient at machine zero.

Check the regularised answer against the stationarity condition: \(\sigma(-1.6335)=0.16335\) and \(\lambda\hat w = 0.1\times1.6335=0.16335\) ✓. Note also how much the regulariser pulls the weight in — from unbounded growth down to 1.63 — which is the bias-variance tradeoff appearing in its simplest possible form.


Next: Part III. Real data has more than one feature, so a parameter is a vector and a model is a matrix. Lecture 8 builds vectors, spans and inner products from the axioms, and the geometry you develop there is what makes Lecture 12's gradients intelligible rather than just notation.