Single Variable Calculus

Dive into Deep Learning · §34.1

Single-Variable Calculus
linear approximation · differentiation rules · Mean Value Theorem · Taylor remainder · descent lemma · Newton’s method · subgradients

Local Slope Prediction and Curvature Drift

Motivation

  • 1D Logistic Loss Benchmark: \ell(w) = \log(1 + e^{-w}) = s(-w) at w = 0 has \ell(0) = \log 2 \approx 0.693147 and slope \ell'(0) = -\tfrac{1}{2}.
  • First-Order Prediction: Stepping to w = 0.1 along the tangent line predicts 0.693147 - 0.05 = 0.643147.
  • Second-Order Curvature Gap: True loss \ell(0.1) \approx 0.644397 differs by 0.001250 = \tfrac{1}{2}\ell''(0)(0.1)^2 (\ell''(0) = 1/4).

Zooming In: Smooth Curves Flatten onto Tangents

Geometric Intuition

  • Local linearity: When we magnify the graph of a smooth function f(x) around a base point x_0, the curve flattens until it is visually indistinguishable from a straight line.
  • Shrink the viewing window: For f(x) = \sin(x^x) near x_0 = 2, shrinking the half-width tenfold from 0.6 to 0.06 and then 0.006 causes the curved graph (solid) to collapse onto its tangent line (dashed).
  • The core optimization question: The derivative is the slope of this limiting line, while the second derivative measures how rapidly the curve bends away from it as the window widens.

From Secant Difference Quotients to Tangents

The Derivative

  • Secant slope (difference quotient): The straight line joining (x, f(x)) and (x+\epsilon, f(x+\epsilon)) has slope: \frac{f(x+\epsilon) - f(x)}{\epsilon}
  • Passing to the limit: As \epsilon \to 0, the secant line rotates into the tangent line, and its limiting slope defines the derivative f'(x) = \frac{df}{dx}(x).
  • When the limit exists: We say f is differentiable at x; if left and right secant limits disagree (as for |x| at x = 0), no tangent line exists.

Why Finite-Difference Step Sizes Are Fragile

Numerical Pitfalls

How small must \epsilon be before a finite difference (f(x+\epsilon)-f(x))/\epsilon accurately estimates f'(x)? There is no universal threshold because higher-order terms can dominate at moderate step sizes:

  • A high-curvature polynomial: Take f(x) = x^2 + 1701(x-4)^3 at x = 4, where the true derivative is f'(4) = 2(4) = 8.
  • Exact difference quotient: Expanding f(4+\epsilon) - f(4) = 8\epsilon + \epsilon^2 + 1701\epsilon^3 yields the secant slope 8 + \epsilon + 1701\epsilon^2.
  • Delayed convergence: At \epsilon = 0.1, the cubic term adds 17.01, yielding 25.11 (over 3\times the true slope). Automatic differentiation avoids choosing \epsilon entirely by propagating exact derivative rules.
epsilon = 0.10000 -> 25.11000
epsilon = 0.00100 -> 8.00270
epsilon = 0.00010 -> 8.00012
epsilon = 0.00001 -> 8.00001

Linear Approximation and Little-o Remainder

First-Order Model

  • Linear Approximation Equivalence: f is differentiable at x with derivative f'(x) iff f(x + \epsilon) = f(x) + \epsilon f'(x) + o(\epsilon), where \lim_{\epsilon \to 0} r(\epsilon)/\epsilon = 0.
  • Proof: Dividing r(\epsilon) = f(x+\epsilon) - f(x) - \epsilon a by \epsilon shows r(\epsilon)/\epsilon \to 0 \iff (f(x+\epsilon)-f(x))/\epsilon \to a = f'(x).
  • Continuity: f(x+\epsilon) - f(x) = \epsilon f'(x) + o(\epsilon) \to 0.

Elementary Derivatives and Why Base e Is Special

Differentiation Rules

  • Closed-Form Primitives: Autodiff decomposes every loss into constants, powers (nx^{n-1}), exponentials (e^x), logarithms (1/x), and trig functions.
  • Why Base e Is Unique: Factoring (a^{x+\epsilon}-a^x)/\epsilon = a^x(a^\epsilon-1)/\epsilon \to (\log a)a^x shows e is the unique base with unit slope at x = 0.

Proving Sum, Product, and Chain Rules Locally

Algebra of Derivatives

  • Sum Rule (g+h)' = g' + h': Adding linear models combines two o(\epsilon) remainders into o(\epsilon).
  • Product Rule (gh)' = gh' + g'h: Multiplying linear models makes the corner term \epsilon^2 g'h' = o(\epsilon) vanish.
  • Chain Rule (g \circ h)'(x) = g'(h(x))h'(x): Composition multiplies local slopes: \Delta g \approx g'(h(x))\Delta h \approx \epsilon g'(h(x))h'(x).

Inverse Functions, Softplus, Sigmoid, and Logit

Running Example

  • Softplus \to Sigmoid: Differentiating s(x) = \log(1 + e^x) gives s'(x) = \frac{e^x}{1+e^x} = \frac{1}{1+e^{-x}} = \sigma(x).
  • Sigmoid Curvature: \sigma'(x) = \sigma(x)(1 - \sigma(x)) \le \tfrac{1}{4} (peaking at x = 0).
  • Logit Inverse (\sigma^{-1}(p) = \log\frac{p}{1-p}): By g'(y) = 1/f'(g(y)), \mathrm{logit}'(p) = \frac{1}{p(1-p)} (= 4 at p = 1/2).

Numerical Verification of Quadratic Error Decay

Empirical Check

For the logistic loss \ell(w) = s(-w) = \log(1+e^{-w}), the chain rule gives \ell'(0) = -\sigma(0) = -\tfrac{1}{2} and curvature \ell''(0) = \sigma(0)(1-\sigma(0)) = \tfrac{1}{4}. Shrinking \epsilon tenfold shrinks the linear-model gap a hundredfold:

eps = 0.1  : l = 0.644397, line = 0.643147, gap = 1.25e-03, gap / eps^2 = 0.1249
eps = 0.01 : l = 0.688160, line = 0.688147, gap = 1.25e-05, gap / eps^2 = 0.1250
eps = 0.001: l = 0.692647, line = 0.692647, gap = 1.25e-07, gap / eps^2 = 0.1250

The ratio \text{gap}/\epsilon^2 converges to \tfrac{1}{2}\ell''(0) = \tfrac{1}{8} = 0.1250. The slight discrepancy 0.1249 at \epsilon = 0.1 comes from the fourth-order Taylor term -\epsilon^4/192.

Fermat’s Lemma and the Mean Value Theorem

From Points to Intervals

To infer function behavior across an entire interval [a, b] from pointwise derivatives, calculus relies on Fermat’s lemma and the Mean Value Theorem (MVT):

  • Fermat’s lemma: If f attains an interior local extremum at \xi and is differentiable there, left and right difference quotients have opposite signs, forcing f'(\xi) = 0.
  • Mean Value Theorem: If f is continuous on [a,b] and differentiable on (a,b), subtracting the secant chord and applying Rolle’s theorem yields an interior \xi \in (a,b) where the tangent is parallel to the chord: f'(\xi) = \frac{f(b) - f(a)}{b - a}
  • Differentiability everywhere inside is essential: On [-1,1], |x| has a horizontal chord (|-1|=|1|), yet no point has slope 0.

Consequences of MVT and 1D Gradient Descent

First-Order Optimization

  • Interval guarantees from MVT: Since f(x_2) - f(x_1) = f'(\xi)(x_2 - x_1), f' > 0 on an interval implies f is strictly increasing, f' < 0 implies strictly decreasing, and a sign change of f' from - to + across x_0 certifies a strict local minimum.
  • 1D gradient descent update: Stepping opposite the slope by \epsilon = -\eta f'(x_t) with learning rate \eta > 0 gives: x_{t+1} = x_t - \eta\, f'(x_t)
  • First-order drop vs. overshoot: The linear model predicts f(x - \eta f'(x)) - f(x) \approx -\eta f'(x)^2 < 0, but because a line ignores curvature, too large an \eta overshoots the valley.

Five Step-Size Regimes on a Quadratic Bowl

Stability Threshold

On f(x) = x^2, where f'(x) = 2x and curvature is f''(x) \equiv 2, gradient descent obeys the exact recurrence x_{t+1} = (1 - 2\eta)x_t = (1 - 2\eta)^{t+1}x_0, converging if and only if |1 - 2\eta| < 1 (0 < \eta < 1 = 2/f''):

  • 0 < \eta < \tfrac{1}{2} (Cautious monotone): At \eta = 0.05, iterates creep down one side of the bowl (x_{10} = 0.349).
  • \eta = \tfrac{1}{2} = 1/f'' (Exact one-step): Lands at x_1 = 0 immediately.
  • \tfrac{1}{2} < \eta < 1 (Damped oscillation): At \eta = 0.9, iterates jump across the valley and decay (x_{10} = 0.107).
  • \eta = 1 = 2/f'' vs. \eta > 1: At \eta = 1, iterates bounce forever between \pm x_0; at \eta = 1.1, they explode (x_{10} = +6.19).

eta = 0.05 -> x_10 = +0.34868
eta = 0.50 -> x_10 = +0.00000
eta = 0.90 -> x_10 = +0.10737
eta = 1.00 -> x_10 = +1.00000
eta = 1.10 -> x_10 = +6.19174

Curvature and the Second-Derivative Test

Second-Order Geometry

  • Second derivative f'' = \frac{d^2 f}{dx^2}: Measures the rate of change of the slope f'.
  • Strict local minimum (f'(x_0)=0, f''(x_0)>0): Since \lim_{\epsilon \to 0} f'(x_0+\epsilon)/\epsilon > 0, the slope f' is negative just left of x_0 and positive just right; by MVT, f falls into x_0 and rises out of it (top row: bowl).
  • Strict local maximum (f'(x_0)=0, f''(x_0)<0): Slope f' goes from + to - (middle row: dome).
  • Inconclusive flat case (f''(x_0)=0): At x_0 = 0, x^3 (inflection), x^4 (minimum), and -x^4 (maximum) all have f'(0) = f''(0) = 0 (bottom row).

Higher-Order Local Models: Taylor Polynomials

Taylor Expansion

  • Matching n derivatives at x_0: The unique degree-n polynomial whose value and first n derivatives match f at x_0 is the Taylor polynomial: P_n(x) = \sum_{i=0}^{n} \frac{f^{(i)}(x_0)}{i!}(x - x_0)^i
  • Tangent line (P_1) vs. best parabola (P_2): For \sin x, P_2(x) = \sin x_0 + \cos x_0(x-x_0) - \tfrac{1}{2}\sin x_0(x-x_0)^2 hugs the bend over a much wider window than P_1.
  • Softplus at x_0 = 0: Since s'''(0) = 0 and s^{(4)}(0) = -\tfrac{1}{8}, P_4(x) = \log 2 + \tfrac{x}{2} + \tfrac{x^2}{8} - \tfrac{x^4}{192}, matching s(0.5) to 5 \times 10^{-6}.

Lagrange Remainder: Exact Error via Rolle’s Theorem

Error Bounds

  • Exact Lagrange Remainder: For (n+1)-times differentiable f, f(x) = P_n(x) + \frac{f^{(n+1)}(\xi)}{(n+1)!}(x - x_0)^{n+1} for some \xi strictly between x_0 and x.
  • Telescoping Proof: Varying the base point t in G(t) and applying Rolle’s theorem (G'(\xi) = 0) cancels adjacent terms by the product rule.
  • Core Special Cases: n = 0 is MVT; n = 1 gives f(x_0) + f'(x_0)u + \tfrac{1}{2}f''(\xi)u^2.

Verifying Taylor Remainder Scaling on Log-Log Axes

Order of Accuracy

When |f^{(n+1)}| is bounded near x_0, the worst-case error of P_n on |x - x_0| \le h scales as O(h^{n+1}), so halving the window h divides the error by 2^{n+1} (4 for P_1, 8 for P_2, and 16 for P_3):

  • Testing e^x around x_0 = 0: Here P_n(x) = \sum_{k=0}^n x^k/k! and |e^x - P_n(x)| = \frac{e^\xi |x|^{n+1}}{(n+1)!}.
  • Successive halving ratios: Across windows h \in \{0.4, 0.2, 0.1, 0.05\}, consecutive error ratios approach 2^{n+1} = 4, 8, 16.
  • Log-log slopes: On log-log axes, \log(\text{error}) \approx (n+1)\log h + c plots as straight lines of slopes 2, 3, and 4.
n = 1: ratios between successive windows 4.3 4.1 4.1, predicted 4
n = 2: ratios between successive windows 8.4 8.2 8.1, predicted 8
n = 3: ratios between successive windows 16.7 16.3 16.2, predicted 16

Analytic vs. Smooth Functions: When Series Fail

Analyticity Pitfall

  • When the infinite series converges to f: For e^x, |R_n(x)| \le \frac{e^{|x|}|x|^{n+1}}{(n+1)!} \to 0 as n \to \infty for every x \in \mathbb{R} because factorials outgrow powers, proving e^x = \sum_{n=0}^\infty x^n/n!.
  • Smooth (C^\infty) does not imply analytic: The flat-valley function f(x) = e^{-1/x^2} (with f(0) = 0) is infinitely differentiable everywhere, yet f^{(n)}(0) = 0 for every n \ge 0.
  • Why optimization uses finite P_n: Its Taylor series at 0 is identically 0, matching f only at x = 0. All optimization proofs therefore use finite P_1 or P_2 with the exact Lagrange remainder R_n.

The Descent Lemma and Safe Step Sizes

Optimization Guarantee

Applying the n = 1 Lagrange remainder converts a bound L on curvature (|f''| \le L, or L-Lipschitz gradient |f'(u)-f'(v)| \le L|u-v|) into a guaranteed safe step size for gradient descent:

  • Quadratic upper ceiling: Since f''(\xi) \le L, any step u obeys f(x+u) \le f(x) + f'(x)u + \tfrac{L}{2}u^2.
  • Substituting u = -\eta f'(x): f\bigl(x - \eta f'(x)\bigr) \le f(x) - \eta\Bigl(1 - \frac{L\eta}{2}\Bigr)f'(x)^2
  • Safe range & optimal step: The loss strictly decreases whenever 0 < \eta < 2/L and f'(x) \ne 0. The guaranteed decrease is maximized at \eta = 1/L (the vertex of the ceiling parabola), yielding a drop of at least f'(x)^2/(2L).
  • Logistic loss check: Since \ell''(w) \le \tfrac{1}{4} = L, every \eta < 8 is globally safe and \eta = 1/L = 4 is optimal.

Newton’s Method: Jumping to the Parabola Minimum

Second-Order Optimization

  • Using local curvature f''(x_t) instead of global L: Fit the local Taylor parabola q(x) = f(x_t) + f'(x_t)(x-x_t) + \tfrac{1}{2}f''(x_t)(x-x_t)^2.
  • Minimizing q(x) when f''(x_t) > 0: Setting q'(x) = f'(x_t) + f''(x_t)(x-x_t) = 0 yields Newton’s update: x_{t+1} = x_t - \frac{f'(x_t)}{f''(x_t)}
  • Quadratic convergence near x^\star: Expanding 0 = f'(x^\star) around x_t to first order with Lagrange remainder gives the error recurrence e_{t+1} = \frac{f'''(\xi_t)}{2f''(x_t)}e_t^2, squaring the error each step.

Newton’s Quadratic Convergence vs. Far-Out Divergence

Basin of Attraction

Near a minimizer with f''(x^\star) > 0, Newton’s method doubles the number of correct digits per step—but far from x^\star, small local curvature makes the parabola nearly flat and flings iterates past the basin of attraction:

t = 0: x = 2.000000000000, error = 1.0e+00
t = 1: x = 1.416666666667, error = 4.2e-01
t = 2: x = 1.110534409842, error = 1.1e-01
t = 3: x = 1.010636768405, error = 1.1e-02
t = 4: x = 1.000111557304, error = 1.1e-04
t = 5: x = 1.000000012443, error = 1.2e-08
from x0 = 1.4: -1.41, 1.45, -1.55, 1.85, -2.89, 8.71
from x0 = 1.3: -1.16, 0.86, -0.37, 0.03, -0.00, 0.00

On the strictly convex function f(x) = x\arctan x - \tfrac{1}{2}\log(1+x^2), where f'(x)=\arctan x and f''(x)=1/(1+x^2), Newton converges from x_0 = 1.3 but diverges wildly from x_0 = 1.4 (|x_0| \gtrsim 1.39) unless the step is damped.

Nondifferentiable Corners and Subdifferentials

Nonsmooth Calculus

  • Disagreeing one-sided slopes: At x = 0, |x| has f'_-(0) = -1 and f'_+(0) = +1, while \mathrm{ReLU}(x) = \max(0, x) has f'_-(0) = 0 and f'_+(0) = +1.
  • Convex subgradients: A slope g is a subgradient of a convex function f at x if its line supports the graph from below for all y: f(y) \ge f(x) + g\,(y - x)
  • The subdifferential \partial f(x): At a corner, supporting slopes form the interval [f'_-(x), f'_+(x)], so \partial |x|(0) = [-1, 1] and \partial\,\mathrm{ReLU}(0) = [0, 1].
  • Convex optimality: A point x^\star is a global minimum of convex f if and only if 0 \in \partial f(x^\star).

Smoothing ReLU Explains the Subdifferential Interval

Limiting Slopes

  • Sharpened softplus approximation: The smooth function \mathrm{softplus}_\beta(x) = \frac{1}{\beta}\log(1 + e^{\beta x}) converges uniformly to \mathrm{ReLU}(x) as \beta \to \infty.
  • Slopes fill [0, 1] near the kink: Its derivative is \sigma(\beta x), which transitions smoothly from 0 to 1 across a window of width O(1/\beta) around the origin.
  • Framework convention at x = 0: At the exact corner x = 0, frameworks return one fixed value from \partial f(0) by convention:
d|x|/dx at 0: autograd returns 0.0
ReLU' at 0: autograd returns 0.0

Chained Subgradient Quirks in Floating Point

Autodiff at Kinks

Chaining fixed subgradient conventions across a composite graph can produce a number that is not a valid subgradient of the composite function:

  • The identity function written with two kinks: For \varphi(x) = \mathrm{ReLU}(x) - \mathrm{ReLU}(-x) = x, the true derivative is 1 everywhere, yet at x = 0 mechanical chaining with \mathrm{ReLU}'(0) = 0 returns 0 + 0 = 0:
autograd: phi'(0) = 0.0  (true slope: 1.0)

Why SGD training is unaffected (Bertoin et al., 2021, as corrected in their 2023 erratum): Triggering this mismatch requires a pre-activation to land on 0.0 exactly. In 32-bit float32 arithmetic, changing \mathrm{ReLU}'(0) within [0, 1] alters gradients on only a few minibatches per million by \sim 10^{-3}, producing identical training and test accuracy.

Concept Check: Single-Variable Calculus

Review & Practice

Work out these three questions using the Taylor remainder, descent lemma, and subdifferential definition:

  1. Taylor error scaling: For a smooth function with bounded fourth derivative, by what factor does the worst-case error of the cubic Taylor polynomial P_3(x) on |x - x_0| \le h shrink when h is cut by a factor of 3?
  2. Safe learning rate: Suppose a loss \ell(w) satisfies |\ell''(w)| \le L = 10 everywhere. Which step sizes \eta are guaranteed by the descent lemma to decrease \ell, and which \eta maximizes the guaranteed one-step drop?
  3. Subgradient optimality: For F(x) = |x| + \frac{1}{2}(x - 1)^2, use \partial F(0) = \partial|x|(0) + (0 - 1) to check whether x = 0 is the global minimizer.

Answers:

  1. By the Lagrange remainder, |R_3(x)| \le \frac{\max|f^{(4)}|}{4!}h^4, so dividing h by 3 shrinks the error bound by 3^4 = 81.
  2. Every step size 0 < \eta < 2/L = 0.2 is guaranteed to decrease \ell, and \eta^\star = 1/L = 0.1 maximizes the guaranteed decrease \ell'(w)^2 / (2L) = \ell'(w)^2 / 20.
  3. At x = 0, \partial F(0) = [-1, 1] - 1 = [-2, 0]. Since 0 \in [-2, 0], the optimality condition 0 \in \partial F(0) holds and x = 0 is the unique global minimizer.

Summary: Single-Variable Calculus Toolkit

Key Takeaways

Concept / Theorem Mathematical Formulation Deep Learning & Optimization Role
Linear Approximation f(x+\epsilon) = f(x) + \epsilon f'(x) + o(\epsilon) Local slope model; proves sum, product, and chain rules
Softplus & Sigmoid s(x)=\log(1+e^x), s'=\sigma, s''=\sigma(1-\sigma) \le \tfrac{1}{4} Smooth counterpart of \mathrm{ReLU}; binary logistic loss curvature
Mean Value Theorem f'(\xi) = \frac{f(b)-f(a)}{b-a} for some \xi \in (a,b) Connects pointwise derivatives to interval monotonicity
Taylor & Lagrange R_n f(x) = \sum_{i=0}^n \frac{f^{(i)}(x_0)}{i!}(x-x_0)^i + \frac{f^{(n+1)}(\xi)}{(n+1)!}(x-x_0)^{n+1} Quantifies O(h^{n+1}) truncation error of degree-n local models
Descent Lemma f(x-\eta f'(x)) \le f(x) - \eta(1 - \frac{L\eta}{2})f'(x)^2 Certifies safe learning rates 0 < \eta < 2/L and optimal \eta = 1/L
Newton’s Method x_{t+1} = x_t - f'(x_t)/f''(x_t), e_{t+1} \approx \frac{f'''(x^\star)}{2f''(x^\star)}e_t^2 Minimizes local parabola P_2; quadratic local convergence
Subdifferential \partial f(x) = \{g : f(y) \ge f(x) + g(y-x)\ \forall y\} Handles \mathrm{ReLU} & \|x\| kinks; global minimum iff 0 \in \partial f(x)