S30 Logo
S30 AI Labwww.thes30.com
Back
#8

Math Lab (Linear Algebra + Probability)

Easy📐 Math for MLW2 D5

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.

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

ExpressionDerivative
aᵀxa
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)

LU Decomposition Overview

Use LU to solve Ax=b via forward/back substitution instead of explicit matrix inverse.

Ready to practice?

Python 3 — Notebook
0/6 solvedSubstack Notes
1
Dataset & Setup

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.

Loading editor...
1

Projection Matrix Properties

2
Construct projection and verify properties
2

Problem 1 — Projection Matrix Properties

Let P be a projection matrix onto a subspace.

  1. ●Show that P^2 = P (idempotent).
  2. ●For orthogonal projection, show P = P^T.

Task 1.1

Construct projection onto span(u) and verify both properties.

Explain: What does idempotent mean geometrically for projections?
Loading editor...
2

PSD Matrix Check

3
Verify X^T X is PSD
1

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.

Explain: Why does v^T X^T X v equal ||Xv||^2?
Loading editor...
3

Least Squares Derivation

4
Normal equation vs lstsq
1

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.

Explain: Why is solve/lstsq preferred over explicit matrix inverse?
Loading editor...
4

Bayes + Base Rate

5
Analytic vs simulated P(D|+)
1

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.

Explain: Why can P(D|+) still be low when test sensitivity is high?
Loading editor...
5

PCA Link

6
Covariance eigenvectors vs SVD directions
1

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.

Explain: Why do right singular vectors align with covariance eigenvectors?
Loading editor...

Need help? Share feedback