SchoolWhool
Quizzes and saved progress, freeSign in freeSign in
Maths · Module 4Module material

Calculus for machine learning cheat sheet

Derivatives and their rules, minima, the gradient, curvature, integrals, convexity and the backward pass: the calculus model training uses, on one page. Free to read and share, no account needed.

The derivative

idearuleexample
rate of changeΔy/Δx between two points: the secant's slopex² from 1 to 2: 3/1 = 3
derivativethe limit of Δy/Δx as the gap h → 0: the tangent's slope((1 + h)² − 1)/h = 2 + h → 2
as a functionf′(x), or dy/dx, the slope at every xx² → 2x: −3 at −1.5, 0 at 0, 2 at 1
sign and sizepositive: rising; negative: falling; zero: flat. The size is how fasthero MSE at slope −3: −67.3333, so raise the slope
on a computerforward difference (f(x + h) − f(x))/h; best near h ≈ √ε ≈ 1.49 × 10⁻⁸at h = 10⁻¹⁶: 0.0

The rules

functionderivativecheck
xⁿn xⁿ⁻¹x³ at 2: 12
c f, f + gc f′, f′ + g′5x² at 1: 10; x² + x³ at 1: 5
eˣ, ln xeˣ, 1/xln x at 2: 0.5
f gf′g + f g′x eˣ at 0: 1
f(g(x))f′(g(x)) · g′(x) — outer derivative at the inside(3 − 2x)²: 2(3 − 2x)(−2) = −12 + 8x
σ(z) = 1/(1 + e⁻ᶻ)σ(1 − σ), at most 0.25 (at z = 0)0.105 at ±2, 0.0177 at 4

Check any rule with the central difference (f(x + h) − f(x − h))/(2h), h = 10⁻⁶.

Minima

  • Stationary point: f′(x) = 0. Minimum if f′ goes − to +, or f″ > 0; maximum if + to −, or f″ < 0.
  • Compare values to tell the global minimum from a local one: x⁴ − 4x² + x has −5.4442 at −1.473 and −2.6186 at 1.347.
  • Some functions have no minimum (log(1 + eˣ)); some equations have no formula (2x + eˣ = 0): iterate, e.g. bisection, to −0.351734.
  • One number for many: squared error → mean; absolute error → median (any point of a flat interval).

The gradient

idearuleexample, x² + 3y² at (2, 1)
partial derivativedifferentiate in one variable, others held fixed∂f/∂x = 2x = 4, ∂f/∂y = 6y = 6
gradientthe vector of partials∇f = (4, 6), length 7.2111
directional slope∇f · u, u a unit vectorlargest along ∇f, zero along the contour
identities∇(aᵀx) = a, ∇(xᵀx) = 2x, ∇(xᵀAx) = 2Ax (A symmetric)2 · diag(1, 3) · (2, 1) = (4, 6)
a stepw ← w − η ∇L; diverges if η > 2/λ_max of the Hessianhero cars: η = 0.01, limit 0.0291

Curvature, Taylor and Newton

  • Second derivative f″; Hessian H, the matrix of second partials. Its eigenvalues are the bending along its eigenvectors.
  • Taylor at a: f(a) + f′(a)(x − a) + ½ f″(a)(x − a)². At 0, x² + eˣ ≈ 1 + x + 1.5x².
  • Newton: x ← x − f′/f″; in many variables w ← w − H⁻¹∇L. One step on a quadratic loss; fails where f″ ≤ 0.

Integrals

  • Integral = the limit of strip sums: ∫₀¹ x² dx = 1/3.
  • Fundamental theorem: if F′ = f, then ∫ₐᵇ f dx = F(b) − F(a).
  • Density: total area 1; P(a ≤ X ≤ b) is the area from a to b.
  • AUC = area under the ROC curve = the fraction of (positive, negative) pairs ordered correctly (a tie counts half). It depends only on the scores' order.

Convexity

  • Convex: every chord on or above the curve. Then every local minimum is global. Strictly convex: at most one minimiser; there may be none.
  • Saddle point: zero gradient, Hessian eigenvalues of both signs (x² − y²).
  • Separable classes: log loss has no minimum; the weights grow forever.

The backward pass

  1. Forward: compute and keep every node's value.
  2. Backward: start from the output; multiply local derivatives back along each edge, reusing each one.
  3. Shape check: the gradient has the shape of the weights: ∇‖Xw − y‖² = 2Xᵀ(Xw − y), (d, n) @ (n,) → (d,).
  4. Log loss through a sigmoid: ∂L/∂z = p − y, so ∇ = Xᵀ(σ(Xθ) − y)/n.

Constraints

idearule
tangencyat the best point on g = c, ∇f = λ∇g
LagrangianL = f − λ(g − c); set all its partials, including ∂/∂λ, to zero
the multiplierλ = d f*/dc, the rate the best value moves with c, not the best value
inequality g ≤ cmultiplier μ ≥ 0; complementary slackness: μ = 0 unless active
ridge, lassoleast squares inside a circle (‖w‖₂ ≤ t) or a diamond (‖w‖₁ ≤ t, corners give zeros)
PCAmax uᵀCu with uᵀu = 1 gives Cu = λu