Maths for AI Stack
The mathematics that actually appears in AI work, with the AI use attached to every concept. Written for someone who already has the maths and needs the map — if you cannot name where a concept shows up, it does not belong in a compressed study plan.
Click any concept to expand it. The final group is the set of derivations worth being able to do on a whiteboard, which is a better test of readiness than any reading.
- Morning, ~4h — linear algebra, then calculus. Backpropagation needs both.
- Afternoon, ~3.5h — probability, then statistics, which is applied probability.
- Evening, ~1.5h — the eight derivations, written by hand. This is where you find out what you actually know.
- Only two hours? Chain rule, SVD, Bayes with base rates, MLE → cross-entropy, and derivations 2 and 5.
Linear algebra
Vectors & the Dot Product
The dot product multiplies two vectors elementwise and sums the result, giving a single number that measures alignment. Divide by the two magnitudes and you get cosine similarity, which measures direction alone and ignores length.
a · b = Σ aᵢbᵢ = ‖a‖‖b‖cos θ | cos(a,b) = (a·b)/(‖a‖‖b‖)
In AI: every embedding comparison, retrieval ranking, and attention score is a dot product. Normalise the vectors and cosine similarity is the dot product, which is why embeddings are stored normalised.
Matrix Multiplication as Composition
A matrix is a linear map. Multiplying matrices composes those maps, which is why the operation is associative but not commutative — applying a rotation then a scaling is not the same as the reverse.
y = Wx + b | (AB)x = A(Bx)
In AI: every dense layer is exactly this. It also explains why stacked linear layers without a non-linearity collapse into a single linear layer — composing linear maps gives a linear map, so depth buys nothing.
Shapes & Broadcasting
Batched computation means everything carries a leading batch dimension, and broadcasting silently expands mismatched dimensions of size 1 to fit. Convenient, and the source of bugs that run without error while computing the wrong thing.
[B, d] @ [d, k] → [B, k] | [B, 1] + [1, k] → [B, k]
In AI: most practical debugging is shape debugging. Writing the expected shape beside each line, and using einsum where indices get confusing, prevents more errors than any amount of theory.
Norms
A norm measures the size of a vector. L2 is Euclidean length and is smooth everywhere; L1 is the sum of absolute values and has a corner at zero, which is precisely why it produces sparsity.
‖x‖₁ = Σ|xᵢ| ‖x‖₂ = √(Σxᵢ²) ‖x‖∞ = max|xᵢ|
In AI: L2 penalty is weight decay, L1 is Lasso and drives coefficients to exactly zero, and gradient clipping rescales when the gradient norm exceeds a threshold. The sparsity difference between L1 and L2 is a standard question, and the geometric answer — the corner of the L1 ball meets the loss contour on an axis — is the one to give.
Rank & Linear Independence
Rank is the number of genuinely independent directions a matrix spans. A rank-deficient matrix has columns that are combinations of others, so it carries less information than its shape suggests.
rank(AB) ≤ min(rank A, rank B)
In AI: collinear features make a regression's coefficients unstable and uninterpretable. More importantly, rank is why LoRA works: a weight update turns out to be well-approximated by a low-rank product BA, so you train a fraction of the parameters and lose almost nothing.
Eigenvalues & Eigenvectors
An eigenvector is a direction the matrix only stretches, never rotates; the eigenvalue is the stretch factor. They expose the intrinsic axes of a transformation.
Av = λv
In AI: PCA is the eigendecomposition of the covariance matrix, with eigenvalues giving variance explained per component. Eigenvalue magnitude also governs stability — repeated multiplication by a matrix with eigenvalues above 1 explodes and below 1 vanishes, which is the exploding and vanishing gradient problem in recurrent networks.
Singular Value Decomposition
Every matrix, square or not, factors into a rotation, a scaling, and another rotation. The singular values in Σ are ordered by importance, so truncating after k of them gives the best possible rank-k approximation.
A = UΣVᵀ | Aₖ = UₖΣₖVₖᵀ is the optimal rank-k approximation
In AI: the most useful single result in applied linear algebra. It gives PCA without forming the covariance matrix, low-rank compression of weight matrices, the pseudo-inverse for least squares, latent semantic analysis, and the condition number σ_max/σ_min that tells you how numerically fragile a problem is.
Positive Semi-Definiteness
A symmetric matrix is positive semi-definite when xᵀAx ≥ 0 for every x — equivalently, when all its eigenvalues are non-negative. It is the matrix analogue of "non-negative".
xᵀAx ≥ 0 ∀x ⟺ all λᵢ ≥ 0
In AI: covariance matrices are PSD by construction, valid kernels must be PSD (Mercer's condition), and a function is convex exactly when its Hessian is PSD everywhere — which is the link between this concept and knowing whether an optimisation problem is easy.
Orthogonality
Orthogonal vectors have zero dot product; an orthogonal matrix has orthonormal columns, so it rotates or reflects without changing lengths. Its inverse is its transpose, which makes it cheap and numerically stable.
QᵀQ = I ⟹ Q⁻¹ = Qᵀ, ‖Qx‖ = ‖x‖
In AI: PCA components are orthogonal, which is what makes them independently interpretable. Orthogonal weight initialisation preserves gradient norm through depth, and the length-preserving property is why orthogonal transforms do not amplify numerical error.
Trace & Determinant
The trace is the sum of the diagonal, and equals the sum of eigenvalues. The determinant is the product of eigenvalues and measures how a transformation scales volume — zero determinant means the map collapses a dimension and cannot be inverted.
tr(A) = Σλᵢ det(A) = Πλᵢ tr(AB) = tr(BA)
In AI: the log-determinant of the Jacobian is the correction term in normalising flows, which is why those architectures are designed to make it cheap to compute. The cyclic property of trace is a workhorse in matrix-calculus derivations.
Calculus and optimisation
The Gradient
The vector of partial derivatives, pointing in the direction of steepest increase, with magnitude equal to the rate of change in that direction. Training moves against it.
∇f = [∂f/∂x₁, …, ∂f/∂xₙ]ᵀ
In AI: the entire optimisation story. A gradient of zero means a stationary point — which may be a minimum, a maximum, or a saddle, and in high dimensions is overwhelmingly likely to be a saddle.
The Chain Rule
The derivative of a composition is the product of the derivatives along the chain. For a deep network, that product runs backwards from the loss through every layer.
∂L/∂w = (∂L/∂y)(∂y/∂z)(∂z/∂w)
In AI: backpropagation is the chain rule and nothing more — applied efficiently by caching each layer's intermediate result instead of recomputing it. It also explains vanishing gradients directly: multiply many factors below 1 and the product goes to zero.
Jacobian & Hessian
The Jacobian is the matrix of first partials for a vector-valued function; the Hessian is the matrix of second partials for a scalar one, describing curvature.
J_ij = ∂fᵢ/∂xⱼ H_ij = ∂²f/∂xᵢ∂xⱼ
In AI: the Jacobian appears in normalising flows and in adversarial-robustness analysis. The Hessian is why second-order methods converge faster — and why they are rarely used, since it is n×n in the number of parameters. Adam is best understood as a cheap diagonal approximation to curvature.
Gradient Descent
Step against the gradient, repeatedly. The learning rate sets the step size: too large and it diverges, too small and it crawls or stalls in a flat region.
w ← w − α∇L(w)
In AI: stochastic gradient descent estimates the gradient from a mini-batch, which is noisier and vastly cheaper — and the noise itself helps escape saddle points. Momentum accumulates a moving average of past gradients to damp oscillation across steep, narrow valleys.
Convexity & Saddle Points
A convex function has one minimum and any local optimum is global. Neural network losses are not convex, so in principle optimisation could stall anywhere.
convex ⟺ Hessian PSD everywhere
In AI: the useful modern result is that in high dimensions saddle points, not local minima, are the obstacle — for a point to be a local minimum every one of thousands of eigenvalues must be positive, which is vanishingly unlikely. Saying this correctly is a good interview signal, because the folk explanation of "getting stuck in local minima" is wrong.
Taylor Expansion
Approximating a function near a point by its derivatives — first order gives the tangent line, second order adds curvature.
f(x+Δ) ≈ f(x) + ∇f·Δ + ½ΔᵀHΔ
In AI: gradient descent is optimisation of the first-order approximation; Newton's method uses the second-order one, which is why it takes better steps at much greater cost. It also underlies most convergence analysis and quadratic-penalty arguments.
Lagrange Multipliers & KKT
To optimise subject to constraints, add a multiplier per constraint and optimise the combined Lagrangian. The multiplier is the shadow price — how much the objective improves per unit of relaxed constraint. KKT conditions extend this to inequalities.
L(x, λ) = f(x) + λg(x)
In AI: the SVM dual comes from here, and so does the equivalence between a penalty and a constraint — ridge regression as an L2 penalty and as an explicit norm constraint are the same problem. It is also the bridge into linear and convex programming.
Automatic Differentiation
Not symbolic differentiation and not finite differences — autodiff applies the chain rule to the actual sequence of primitive operations, giving exact derivatives at machine precision.
reverse mode: one backward pass for all n parameter gradients
In AI: reverse mode is used because the loss is a single scalar and the parameters are many — cost is proportional to outputs, not inputs. Forward mode is the opposite and suits few inputs, many outputs. This asymmetry is why every deep learning framework is built around a backward pass.
Log, Exp & Numerical Stability
Logs turn products into sums and exponentials overflow quickly — exp(1000) is infinity in floating point. The log-sum-exp trick subtracts the maximum before exponentiating, which leaves the result unchanged and keeps every intermediate finite.
log Σ exp(xᵢ) = m + log Σ exp(xᵢ − m), m = max xᵢ
In AI: this is why softmax and cross-entropy are fused into one operation in every framework, and why you pass logits rather than probabilities to the loss function. Log-likelihoods are summed rather than multiplied for the same reason.
Probability
Random Variables & Distributions
A random variable maps outcomes to numbers. Discrete ones have a probability mass function, continuous ones a density — where the density at a point is not a probability, only its integral over an interval is.
Σ P(x) = 1 | ∫ p(x)dx = 1
In AI: a model output is a distribution, not a number — a classifier emits a categorical distribution, a regressor implicitly a Gaussian. Recognising that reframes training as fitting distributions.
Expectation, Variance, Covariance
Expectation is the probability-weighted average; variance is the expected squared deviation from it; covariance is the joint version for two variables. Expectation is linear whether or not the variables are independent, which is used constantly.
E[X] = Σ xP(x) Var(X) = E[X²] − E[X]² E[aX+bY] = aE[X]+bE[Y]
In AI: every loss function is an expectation estimated by a sample average over the batch. Covariance matrices are the object PCA decomposes, and the linearity of expectation is what makes mini-batch gradients unbiased estimates of the full gradient.
Bayes' Rule & Base Rates
Bayes inverts a conditional probability, combining a prior with the likelihood of the evidence. The denominator normalises over all hypotheses.
P(A|B) = P(B|A)P(A) / P(B)
In AI: the standard interview question is a 99% accurate test for a condition affecting 1 in 10,000 — the answer is under 1%, because the enormous healthy population produces far more false positives than the rare true ones. The general lesson carries directly into rare-event detection: a strong classifier can still generate mostly false alarms.
Independence vs Conditional Independence
Independence means knowing one tells you nothing about the other. Conditional independence means that holds once a third variable is fixed — a weaker and far more useful assumption.
P(A,B) = P(A)P(B) | P(A,B|C) = P(A|C)P(B|C)
In AI: naive Bayes assumes features are conditionally independent given the class, which is usually false and works anyway because the decision only needs the ranking to be right. Graphical models are entirely built out of conditional independence statements.
The Distributions Worth Knowing
Six cover most of what you meet. Bernoulli for one binary trial and Binomial for n of them; Categorical for one-of-k; Gaussian wherever many small effects add; Poisson for counts in a fixed interval; Exponential for waiting times; Beta and Dirichlet as priors over probabilities.
Beta is conjugate to Bernoulli · Dirichlet to Categorical
In AI: the output layer's distribution determines the loss — sigmoid with Bernoulli gives binary cross-entropy, softmax with Categorical gives cross-entropy, and a Gaussian assumption gives MSE. Conjugacy is what makes Bayesian updates closed-form, as in Thompson sampling for bandits.
Joint, Marginal, Conditional
The joint distribution covers all variables at once; marginalising sums or integrates a variable out; conditioning fixes one and renormalises.
P(x) = Σ_z P(x,z) P(x|z) = P(x,z)/P(z)
In AI: latent variable models are defined by a joint over observed and hidden variables, and the quantity you want — the marginal likelihood of the data — requires integrating the latent out. That integral is usually intractable, which is the entire reason variational inference and the ELBO exist.
Likelihood, MLE and MAP
Likelihood is the probability of the observed data as a function of the parameters — the same expression as a probability, read in the other direction. MLE picks the parameters maximising it; MAP adds a prior.
θ_MLE = argmax Σ log p(xᵢ|θ) θ_MAP = argmax [log p(x|θ) + log p(θ)]
In AI: the unifying idea behind most loss functions. Minimising cross-entropy is maximum likelihood for a categorical model; minimising MSE is maximum likelihood under Gaussian noise; and a Gaussian prior in MAP is L2 regularisation. Being able to state those three equivalences is worth more than memorising the losses.
Entropy & Cross-Entropy
Entropy measures the average surprise in a distribution — maximal when uniform, zero when certain. Cross-entropy measures the cost of encoding samples from p using a code built for q.
H(p) = −Σ p log p H(p,q) = −Σ p log q
In AI: the classification loss. Because the true distribution is one-hot, cross-entropy collapses to −log of the probability assigned to the correct class, so confident and wrong is punished sharply. Entropy also appears as an exploration bonus in reinforcement learning and as a measure of prediction uncertainty.
KL Divergence
The extra cost of using q when the truth is p — cross-entropy minus entropy. It is always non-negative, zero only when the distributions match, and not symmetric, so it is not a distance.
KL(p‖q) = Σ p log(p/q) = H(p,q) − H(p)
In AI: the regulariser in a VAE pulling the posterior toward the prior, the constraint keeping a fine-tuned policy near its reference model in RLHF and DPO, the objective in distillation, and a drift-detection statistic. The asymmetry matters in practice: forward KL spreads mass to cover everything, reverse KL concentrates on one mode.
Jensen's Inequality & the ELBO
For a concave function, the function of an average is at least the average of the function. Applying it to a log of an expectation produces a lower bound.
log E[X] ≥ E[log X] ⟹ log p(x) ≥ E_q[log p(x,z) − log q(z)]
In AI: this single step is where the evidence lower bound comes from. The intractable marginal likelihood is replaced by a bound you can optimise, and the gap between them is exactly the KL between the approximate and true posterior — which is why tightening the bound improves the approximation.
Monte Carlo & the Reparameterisation Trick
Estimate an expectation by averaging samples; error falls as 1/√n regardless of dimension, which is why sampling beats numerical integration in high dimensions. But sampling is not differentiable, so gradients cannot flow through it.
z = μ + σ⊙ε, ε ~ N(0, I)
In AI: the reparameterisation trick moves the randomness into a fixed noise source, leaving μ and σ as ordinary differentiable inputs. It is what makes VAEs trainable by backpropagation, and the same idea appears in dropout and in stochastic policies.
Bias-Variance Decomposition
Expected squared error splits into three parts: bias from the model being too simple, variance from sensitivity to the particular training sample, and irreducible noise you cannot remove.
E[(y − ŷ)²] = Bias² + Variance + σ²
In AI: the diagnostic that tells you which lever to pull. High training error means bias, so add capacity or features; low training and high validation error means variance, so add data or regularisation. It also explains ensembles — bagging reduces variance, boosting reduces bias.
Law of Large Numbers & CLT
The sample mean converges to the true mean as n grows, and its distribution approaches a Gaussian regardless of the underlying distribution's shape — provided variance is finite.
SE = σ/√n
In AI: the justification for estimating anything from a sample, including mini-batch gradients and evaluation metrics. The √n also sets expectations: quadrupling your evaluation set halves the uncertainty, which is why small test sets produce numbers that move for no reason.
Statistics
Estimators — Bias, Variance, Consistency
An estimator is a recipe for computing a parameter from data. It is unbiased if it is right on average, low-variance if it is stable across samples, and consistent if it converges to the truth as n grows.
Bias(θ̂) = E[θ̂] − θ
In AI: the frame for evaluating any measurement you report. Note that unbiased is not automatically better — a slightly biased estimator with much lower variance often gives smaller total error, which is exactly the argument for regularisation.
Standard Error vs Standard Deviation
Standard deviation describes the spread of the data and does not shrink with more data. Standard error describes the uncertainty in an estimate and shrinks as 1/√n. They are different quantities and are constantly confused.
SD: spread of x | SE = SD/√n: uncertainty in x̄
In AI: error bars on a metric should be standard errors. Quoting a standard deviation as if it were uncertainty in the mean overstates it dramatically, and this comes up whenever someone asks whether two model results actually differ.
Confidence Intervals
A 95% confidence interval is constructed so that, over repeated sampling, 95% of such intervals contain the true value. The randomness is in the interval, not in the parameter.
x̄ ± 1.96 · SE (large-sample, approximately)
In AI: report intervals rather than point estimates for any metric that will inform a decision. The width tells you what the experiment could have detected — an interval spanning everything the team cares about means the result is uninformative, not negative.
Hypothesis Testing & p-values
A p-value is the probability of seeing data at least this extreme if the null hypothesis were true. It is not the probability the null is true, and not the probability your result is a fluke.
p = P(data this extreme | H₀ true)
In AI: the standard for judging an A/B test. The mis-statement is so common that stating it correctly is itself a signal. Type I error is a false positive at rate α; Type II is a missed real effect.
Statistical Power
The probability of detecting an effect of a given size if it is genuinely there. It depends on the effect size, the variance, and the sample size — and required sample scales with the inverse square of the effect you want to detect.
Power = 1 − β | n ∝ σ²/δ²
In AI: compute this before running the experiment. Halving the detectable effect quadruples the sample needed, and if the traffic does not exist the honest conclusion is that the test cannot be run at that sensitivity — not that you run it and report a null.
Effect Size vs Significance
Significance says an effect is probably not zero. Effect size says whether it is large enough to matter. With enough data, trivially small differences become significant.
Cohen's d = (x̄₁ − x̄₂)/s_pooled
In AI: report the size of the improvement and its interval, not just that p < 0.05. A statistically significant 0.1% lift that costs a rewrite is a decision to decline, and conflating the two is how teams ship work that does not pay for itself.
Multiple Comparisons
Test twenty independent hypotheses at α = 0.05 and you expect one false positive by chance alone. Checking many metrics, segments, or model variants is exactly this situation.
P(at least one false positive) = 1 − (1−α)ᵐ
In AI: the reason a dashboard of thirty metrics always shows a "win" somewhere. Bonferroni is the blunt correction, false discovery rate the more practical one — and pre-registering the primary metric before looking is better than any correction afterwards.
A/B Test Design
Randomise at the right unit — usually the user, not the request, or a single user sees both variants and the arms contaminate. Fix the sample size and duration in advance, and cover full weekly cycles.
randomisation unit ≥ unit of the decision
In AI: the failure modes are peeking, which inflates false positives because you stop when it looks good; novelty effects that fade; and network interference where treated users affect control users. Guardrail metrics catch the harm the primary metric hides.
Bootstrap & Permutation Tests
Resample the data with replacement, recompute the statistic thousands of times, and read the uncertainty off the resulting spread. Permutation tests shuffle labels to build a null distribution directly.
CI = 2.5th to 97.5th percentile of the bootstrap statistic
In AI: the practical answer for any metric with no closed-form standard error — AUC, median latency, ranking metrics. It assumes almost nothing, costs only compute, and is badly underused relative to how often people give a metric with no uncertainty at all.
OLS & Coefficient Interpretation
Least squares fits by minimising squared residuals, with a closed-form solution. A coefficient means the expected change in y per unit of that x holding the other included variables constant — a phrase that carries the entire caveat.
β̂ = (XᵀX)⁻¹Xᵀy
In AI: the interpretable baseline, and the place where multicollinearity bites — correlated predictors make coefficients unstable and sign-flipping even when predictions are fine. Note that XᵀX is singular exactly when the features are linearly dependent, which links straight back to rank.
Calibration
A model is calibrated when its predicted probabilities match observed frequencies — of everything it scores 0.7, about 70% are positive. Discrimination and calibration are independent: a model can rank perfectly and still be badly calibrated.
P(y=1 | p̂=q) = q for all q
In AI: required whenever the number is consumed as a probability rather than a ranking — expected-value decisions, cost-based thresholds, or a human reading the score. Check with a reliability diagram; fix with Platt scaling or isotonic regression on held-out data.
Confounding & Simpson's Paradox
A confounder influences both treatment and outcome, creating an association with no causal path behind it. Simpson's paradox is the extreme case: a trend present in every subgroup reverses when the groups are pooled.
observed association = causal effect + confounding
In AI: the reason "users of this feature retain better" almost never means the feature caused it — engaged users both adopt features and retain. Randomisation removes confounding by construction, which is why an A/B test answers a question that no amount of observational analysis can.
The eight derivations
1 · Gradient of MSE
Start from the loss for a linear model, differentiate with respect to the weights, and keep the chain rule visible.
L = ½(ŷ−y)², ŷ = wᵀx ⟹ ∂L/∂w = (ŷ−y)·x
Why it matters: the simplest case where the gradient is "error times input", a pattern that recurs throughout backpropagation. The ½ is there purely so the 2 cancels.
2 · Softmax + cross-entropy gradient
Differentiate cross-entropy with respect to the logits, not the probabilities. The softmax Jacobian is messy on its own and almost everything cancels.
∂L/∂z = p − y
Why it matters: the single most elegant result in the list, and the most asked. The gradient is just predicted minus actual — which is why frameworks fuse softmax and cross-entropy into one operation, and why you pass logits to the loss.
3 · Backprop through a two-layer network
Forward: z₁ = W₁x, a₁ = σ(z₁), z₂ = W₂a₁. Then apply the chain rule backwards, writing each intermediate explicitly.
δ₂ = ∂L/∂z₂ ∂L/∂W₂ = δ₂a₁ᵀ δ₁ = (W₂ᵀδ₂)⊙σ′(z₁) ∂L/∂W₁ = δ₁xᵀ
Why it matters: doing this once by hand makes backpropagation permanently unmysterious, and it shows exactly where vanishing gradients enter — through the repeated σ′ factor.
4 · PCA via eigendecomposition and SVD
Centre the data, form the covariance matrix, and take its eigenvectors as the components. Then show the SVD route gives the same answer without forming the covariance at all.
C = XᵀX/(n−1) = VΛVᵀ | X = UΣVᵀ ⟹ λᵢ = σᵢ²/(n−1)
Why it matters: connects eigendecomposition, SVD, and covariance in one derivation, and explains why the SVD route is preferred numerically — squaring the matrix squares its condition number.
5 · Bayes with a low base rate
A 99% accurate test, a condition affecting 1 in 10,000. Work it in counts rather than symbols — a population of a million makes it immediate.
100 true positives ≈ 99 detected · 999,900 healthy → ~9,999 false positives · P ≈ 99/10,098 < 1%
Why it matters: the most frequently asked probability question in interviews, and the intuition behind why a strong rare-event classifier still produces mostly false alarms.
6 · The log-sum-exp trick
Show that subtracting the maximum before exponentiating leaves the result algebraically identical while keeping every exponential at most 1.
log Σexp(xᵢ) = m + log Σexp(xᵢ−m), m = max xᵢ
Why it matters: explains a real implementation detail you will meet in every framework, and demonstrates that you think about numerics rather than only about mathematics.
7 · The reparameterisation trick
Show why ∂/∂μ of a sample from N(μ, σ²) is undefined as written, then rewrite the sample so the randomness sits in a parameter-free term.
z ~ N(μ,σ²) → z = μ + σε, ε ~ N(0,1) ⟹ ∂z/∂μ = 1
Why it matters: a small algebraic move with an outsized consequence — it is what made variational inference trainable by backpropagation, and it is the standard VAE question.
8 · Bias-variance decomposition
Expand the expected squared error, add and subtract the expected prediction, and show the cross terms vanish.
E[(y−ŷ)²] = (E[ŷ]−f)² + E[(ŷ−E[ŷ])²] + σ²
Why it matters: turns a piece of vocabulary into something you can actually use for diagnosis, and the cancelling cross term is a good test of whether you are comfortable manipulating expectations.
Related: ML Stack for the models this maths underpins, Data Science Stack for the applied statistics, and Interview Rounds for how these come up in questions.
