interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 7 · ML & applied AI · Lesson 1 of 13

Linear algebra, probability and statistics for ML

The linear algebra, calculus, probability and statistics that ML derivations and debugging rely on: shapes and products, gradients of common expressions, likelihood-based objectives and the uncertainty of measured results. Interviews probe these through short derivations and through questions such as whether a reported improvement is real.
About 5 hFocus mode

Step 1: Learn the ideas

  • Shapes, matrix-vector and matrix-matrix products, broadcasting
  • Gradients of linear, quadratic and least-squares expressions
  • Chain rule and Jacobians for vector-valued functions
  • Eigendecomposition, SVD, rank and conditioning
  • Random variables, expectation, variance and covariance
  • Bernoulli, categorical and Gaussian distributions
  • Maximum likelihood and MAP estimation
  • Standard errors, confidence intervals and the bootstrap
  • Hypothesis tests, p-values and paired comparisons

Why it matters

ML interview questions in this area are less about proofs than about whether you can work with the objects behind a model: the shapes in a batched layer, the gradient of a loss, why a loss has the form it has, and whether a reported gain is larger than noise. Gaps here show up as shape bugs, losses chosen by habit, and over-interpreted evaluation numbers. For research-leaning roles, expect short derivations on a whiteboard.

Core ideas

Shapes first. With a batch X of shape (n, d) and weights W of shape (d, k), X @ W is (n, k), and a bias of shape (k,) broadcasts across rows. Writing the shape next to every line is the fastest way to catch bugs, and the gradient of a scalar loss always has the same shape as the parameter it is taken with respect to.

Gradients of common expressions.

  • f(w) = a^T w has gradient a.
  • f(w) = w^T A w has gradient (A + A^T) w, which is 2 A w only when A is symmetric.
  • Least squares L(w) = ||X w - y||^2 has gradient 2 X^T (X w - y); setting it to zero gives the normal equations X^T X w = X^T y.
  • Softmax cross-entropy with logits z and one-hot target y has gradient softmax(z) - y with respect to z. Derive this once; it explains why frameworks fuse the two into a single operation on logits.

Eigenvalues and SVD. A symmetric matrix has orthogonal eigenvectors, and its eigenvalues say how much it stretches each of those directions. Every matrix has an SVD A = U S V^T: rotate, scale by the singular values, rotate. Consequences worth stating fluently: keeping the top k singular values gives the best rank-k approximation (the basis of PCA and low-rank compression, and a useful intuition for low-rank adapters such as LoRA); PCA directions are the right singular vectors of the centered data matrix; and the condition number, the ratio of largest to smallest singular value, measures how much small errors can be amplified. Nearly collinear features make X^T X ill-conditioned, which destabilizes least squares and slows gradient descent.

Probability and likelihood. Expectation is linear, Var(aX + b) = a^2 Var(X), and the variance of a sum includes covariance terms. Maximum likelihood chooses parameters maximizing sum_i log p(y_i | x_i; theta). Gaussian output noise with fixed variance turns this into minimizing squared error; Bernoulli or categorical outputs turn it into (binary) cross-entropy. MAP adds a log-prior: a zero-mean Gaussian prior on the weights gives an L2 penalty, a Laplace prior gives L1. This is how you justify a loss instead of reciting it.

Statistics of evaluation. A test accuracy is a mean of n Bernoulli outcomes, so its standard error is about sqrt(p (1 - p) / n). At p = 0.9 and n = 1000 that is roughly 0.0095, so a 95% interval spans about plus or minus 1.9 points: a half-point difference on such a set is not evidence. When two models are scored on the same examples, analyze per-example differences (a paired test or a bootstrap over examples), which removes the variance shared by both models. A p-value is the probability of data at least this extreme if the null hypothesis were true; it is not the probability that the null is true, and it says nothing about effect size.

Common misconceptions

  • "The gradient of x^T A x is 2Ax." Only for symmetric A; in general it is (A + A^T) x.
  • "Cross-entropy and MSE are arbitrary choices." Each is a negative log-likelihood under an assumed output distribution (Gaussian or categorical); choosing one is an assumption about the outputs.
  • "Eigenvalues and singular values are the same thing." They coincide for symmetric positive semidefinite matrices and match in absolute value for symmetric ones; in general they differ, and non-square matrices have singular values but no eigenvalues.
  • "p = 0.03 means a 97% chance the new model is better." The p-value is computed assuming no difference; it is not a posterior probability.
  • "More test data is the only fix for noisy comparisons." The standard error shrinks like 1/sqrt(n), so halving it needs four times the data; pairing and variance reduction are often cheaper.
  • "Overlapping confidence intervals mean no significant difference." Overlap of separate intervals is a conservative check; a paired analysis can detect a real difference even when the marginal intervals overlap.

How to approach it

Refresh with the CS229 linear algebra and probability reviews, then derive by hand: least squares, logistic regression, softmax cross-entropy. Verify each with central finite differences in float64. Revisit SVD with a small experiment, compressing a matrix at several ranks and comparing the error with the discarded singular values. For statistics, compute a bootstrap interval for a metric you care about. In interviews, state shapes and assumptions aloud; graduate-level interviews emphasize derivations, senior-level ones emphasize whether the evidence supports the decision.

Suggested order

  1. 1Read the CS229 "Linear Algebra Review and Reference" sections on matrix multiplication and Section 4 (Matrix Calculus), writing the shape of every expression.
  2. 2Read the CS229 "Review of Probability Theory" for random variables, expectation, variance and the common distributions; summarize each in one line.
  3. 3Work through Mathematics for Machine Learning Ch. 4 (Matrix Decompositions) for eigendecomposition and SVD, then Ch. 5 (Vector Calculus) for Jacobians and gradient identities.
  4. 4Read the CS229 main notes sections on the probabilistic interpretation of linear regression (Ch. 1) and on Bayesian statistics and regularization (Ch. 9), then redo the MLE-to-MSE and MAP-to-L2 derivations on paper.
  5. 5Derive the least-squares and softmax cross-entropy gradients by hand and check them in NumPy with central finite differences.
  6. 6Read D2L 22.10 (Statistics), then compute a bootstrap interval and a paired comparison for two classifiers scored on the same test set.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • Stanford CS229 Machine LearningStart here

    Stanford University

    Section: Linear Algebra Review and Reference (section notes), Section 4: Matrix Calculus

    Products, norms, rank, eigenvalues and gradients of linear and quadratic functions, including least squares, at exactly the depth ML derivations need.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • CS229 section notes: Review of Probability Theory

    Stanford University (CS229 section notes)

    Random variables, expectation, variance, covariance and common distributions as a one-sitting refresher.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Mathematics for Machine LearningStart here

    Cambridge University Press (free PDF on the companion site)

    Section: Ch. 4: Matrix Decompositions and Ch. 5: Vector Calculus

    Geometric intuition for eigendecomposition and SVD, matrix approximation, and the gradient identities used in backpropagation.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • The Matrix Calculus You Need For Deep Learning

    explained.ai

    Step-by-step Jacobians and the vector chain rule for differentiating batched expressions with correct shapes.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Dive into Deep Learning

    d2l.ai (open-source book)

    Section: 22.10. Statistics

    Estimators, bias and variance of estimators, hypothesis tests and confidence intervals with runnable code.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Practice by derivation plus small numerical checks. (1) Solve least squares three ways (normal equations, np.linalg.lstsq, gradient descent) on a nearly collinear design and compare stability and convergence. (2) Implement softmax cross-entropy and its gradient and check against central differences. (3) Compress a matrix with SVD at several ranks and plot the error against the discarded singular values. (4) Simulate two classifiers on 1,000 shared test examples and compare a paired bootstrap interval for the accuracy difference with an unpaired one.

No external problems are linked; follow the guidance above.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1What is the shape of the gradient of a scalar loss with respect to a (d, k) weight matrix, and why?
  2. Question 2Why does X^T X become ill-conditioned when features are nearly collinear, and what does that do to least squares?
  3. Question 3What does the top right singular vector of a centered data matrix represent?
  4. Question 4Which output distribution turns maximum likelihood into binary cross-entropy?
  5. Question 5Two models differ by 0.4 accuracy points on 2,000 shared test examples. What would you compute before calling it an improvement?
  6. Question 6What does a p-value of 0.01 mean, precisely, and what does it not mean?

You’re done when you can

  • Derive the gradient of softmax cross-entropy with respect to the logits on paper in under 10 minutes, with shapes.
  • Explain without notes why minimizing MSE is maximum likelihood under Gaussian noise and why L2 corresponds to a Gaussian prior.
  • Explain SVD, rank-k approximation and the condition number in two minutes, with one ML example of each.
  • Compute a 95% confidence interval for a test accuracy and decide whether a stated improvement is distinguishable from noise.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Unlocks next: Data splits, leakage and data quality, Baselines, bias-variance, regularization and optimization

Your notes and study time

Log study time