Before You Start
- Required knowledge
- Vector operations, matrix multiplication, probability, and least squares.
- Libraries used
- NumPy
- Dataset and source
- Generated matrices and probability simulations inside the notebook.
- Expected output
- Projection and PSD checks, least-squares comparisons, and covariance-eigenvector versus SVD results.
Lesson Videos
Math Lab — Theory & Concepts
Read this before attempting the exercises. This lab combines linear algebra and probability concepts in hands-on coding exercises.
1. PSD Matrices in Practice
A matrix A is positive semi-definite (PSD) if vᵀAv ≥ 0 for all v.
Quick check: A = XᵀX is always PSD because vᵀXᵀXv = ‖Xv‖² ≥ 0.
# Verify PSD: all eigenvalues ≥ 0
eigvals = np.linalg.eigvalsh(A)
assert np.all(eigvals >= -1e-10), "Not PSD!"
Where PSD appears in ML:
- ●Covariance matrices (always PSD)
- ●Kernel matrices in SVMs (must be PSD)
- ●Hessian of convex loss functions (PSD at minimum)
2. Covariance & Correlation Matrices
X = np.random.randn(100, 3) # 100 samples, 3 features
# Covariance matrix
cov = np.cov(X.T) # shape (3, 3)
# or manually:
X_centered = X - X.mean(axis=0)
cov_manual = (X_centered.T @ X_centered) / (len(X) - 1)
# Correlation matrix (normalized covariance)
corr = np.corrcoef(X.T) # values in [-1, 1]
3. Sampling from Distributions
rng = np.random.default_rng(42)
# Uniform
rng.uniform(0, 1, size=1000)
# Normal
rng.normal(loc=0, scale=1, size=1000)
# Multivariate normal
mean = [0, 0]
cov = [[1, 0.5], [0.5, 1]]
samples = rng.multivariate_normal(mean, cov, size=1000)
4. Law of Large Numbers & CLT
LLN: As n → ∞, the sample mean converges to the true mean.
CLT: The distribution of sample means is approximately normal, regardless of the underlying distribution (for large n).
# Demonstrate CLT: means of uniform samples → normal
sample_means = [np.random.uniform(0, 1, size=30).mean() for _ in range(10000)]
# sample_means is approximately Normal(0.5, 1/(12*30))
ML Connection: CLT justifies why many ML methods assume normality — we're often working with averages (batch means, gradient estimates).
5. Matrix Calculus Quick Reference
| Expression | Derivative |
|---|---|
| aᵀx | a |
| xᵀAx | (A + Aᵀ)x = 2Ax (if A symmetric) |
| ‖Ax - b‖² | 2Aᵀ(Ax - b) |
| log det(A) | A⁻ᵀ |
ML Use: The normal equation for linear regression comes from setting ∂/∂w ‖Xw - y‖² = 0, giving w = (XᵀX)⁻¹Xᵀy.
Now you're ready to attempt the exercises. Switch to the Exercise tab and start coding!
LU Decomposition (Visual)

Use LU to solve Ax=b via forward/back substitution instead of explicit matrix inverse.
Ready to practice?
Math Lab — FAANG-Level Mixed Problems
This lab is a problem set + mini-verification notebook covering projection matrices, PSD checks, least squares, Bayes intuition, and PCA links.
Projection Matrix Properties
Problem 1 — Projection Matrix Properties
Let P be a projection matrix onto a subspace.
- ●Show that P^2 = P (idempotent).
- ●For orthogonal projection, show P = P^T.
Task 1.1
Construct projection onto span(u) and verify both properties.
PSD Matrix Check
Problem 2 — PSD Matrix Check
Show that for any matrix X, A = X^T X is positive semidefinite (PSD).
Task 2.1
Sample random X, build A, and verify v^T A v >= 0 for many random vectors v.
Least Squares Derivation
Problem 3 — Least Squares Derivation
Derive the normal equations for minimizing ||Xw - y||^2.
Task 3.1
Compare w_hat from solve vs np.linalg.lstsq.
Bayes + Base Rate
Problem 4 — Bayes + Base Rate (Derivation)
Re-derive P(D|+) for the disease test scenario and explain the base-rate fallacy.
Task 4.1
Compute analytic posterior and verify with simulation.
PCA Link
Problem 5 — PCA Link
Explain why PCA components are eigenvectors of the covariance matrix.
Task 5.1
Compute covariance eigenvectors and compare with SVD directions.