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
| idea | rule | example |
|---|---|---|
| rate of change | Δy/Δx between two points: the secant's slope | x² from 1 to 2: 3/1 = 3 |
| derivative | the limit of Δy/Δx as the gap h → 0: the tangent's slope | ((1 + h)² − 1)/h = 2 + h → 2 |
| as a function | f′(x), or dy/dx, the slope at every x | x² → 2x: −3 at −1.5, 0 at 0, 2 at 1 |
| sign and size | positive: rising; negative: falling; zero: flat. The size is how fast | hero MSE at slope −3: −67.3333, so raise the slope |
| on a computer | forward difference (f(x + h) − f(x))/h; best near h ≈ √ε ≈ 1.49 × 10⁻⁸ | at h = 10⁻¹⁶: 0.0 |
The rules
| function | derivative | check |
|---|---|---|
xⁿ | n xⁿ⁻¹ | x³ at 2: 12 |
c f, f + g | c f′, f′ + g′ | 5x² at 1: 10; x² + x³ at 1: 5 |
eˣ, ln x | eˣ, 1/x | ln x at 2: 0.5 |
f g | f′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 iff′goes − to +, orf″ > 0; maximum if + to −, orf″ < 0. - Compare values to tell the global minimum from a local one:
x⁴ − 4x² + xhas−5.4442at−1.473and−2.6186at1.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
| idea | rule | example, x² + 3y² at (2, 1) |
|---|---|---|
| partial derivative | differentiate in one variable, others held fixed | ∂f/∂x = 2x = 4, ∂f/∂y = 6y = 6 |
| gradient | the vector of partials | ∇f = (4, 6), length 7.2111 |
| directional slope | ∇f · u, u a unit vector | largest 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 step | w ← w − η ∇L; diverges if η > 2/λ_max of the Hessian | hero cars: η = 0.01, limit 0.0291 |
Curvature, Taylor and Newton
- Second derivative
f″; HessianH, 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 variablesw ← w − H⁻¹∇L. One step on a quadratic loss; fails wheref″ ≤ 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 fromatob. - 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
- Forward: compute and keep every node's value.
- Backward: start from the output; multiply local derivatives back along each edge, reusing each one.
- Shape check: the gradient has the shape of the weights:
∇‖Xw − y‖² = 2Xᵀ(Xw − y),(d, n) @ (n,) → (d,). - Log loss through a sigmoid:
∂L/∂z = p − y, so∇ = Xᵀ(σ(Xθ) − y)/n.
Constraints
| idea | rule |
|---|---|
| tangency | at the best point on g = c, ∇f = λ∇g |
| Lagrangian | L = 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 ≤ c | multiplier μ ≥ 0; complementary slackness: μ = 0 unless active |
| ridge, lasso | least squares inside a circle (‖w‖₂ ≤ t) or a diamond (‖w‖₁ ≤ t, corners give zeros) |
| PCA | max uᵀCu with uᵀu = 1 gives Cu = λu |