Probability Distributions, Maximum Likelihood & MAP
Random variables, Bayes' Theorem, and deriving loss functions from probabilistic priors.
Reference notes. Condensed summaries of each topic, not step-by-step lessons. The Core Course is the one to work through.
Inspect Architecture: Linear regression1. The idea
Machine learning loss functions are not arbitrary heuristics — they are negative log-likelihoods derived from fundamental probability distributions. Maximum Likelihood Estimation (MLE) finds parameters maximizing data likelihood, while Maximum A Posteriori (MAP) incorporates Bayesian parameter priors that directly yield L1 (Lasso) and L2 (Ridge) regularization.
2. The math
• Assume observational data y_i = f_theta(x_i) + eps_i, where noise eps_i ~ N(0, sigma^2) is i.i.d.
• The conditional likelihood for sample i is p(y_i | x_i, theta) = (1 / sqrt(2 pi sigma^2)) * exp(-(y_i - f_theta(x_i))^2 / (2 sigma^2)).
• The joint log-likelihood over the dataset D is log L(theta) = sum_{i=1}^N [-0.5 * log(2 pi sigma^2) - (y_i - f_theta(x_i))^2 / (2 sigma^2)].
• Discard constants independent of theta and negate to minimize: argmin_theta (1 / (2 sigma^2)) sum_{i=1}^N (y_i - f_theta(x_i))^2 = argmin_theta MSE(theta). Q.E.D.
3. How it works
4. The code (python)
import numpy as np
# Comparing MLE (Linear Regression), MAP with Gaussian Prior (Ridge), and MAP with Laplace Prior (Lasso)
def demonstrate_mle_vs_map():
np.random.seed(42)
N, D = 20, 8 # Small sample size where regularization matters
X = np.random.randn(N, D)
true_w = np.array([3.0, -2.0, 0.0, 0.0, 1.5, 0.0, 0.0, -1.0])
y = np.dot(X, true_w) + np.random.randn(N) * 0.5
# 1. MLE: (X^T X)^(-1) X^T y
w_mle = np.linalg.solve(np.dot(X.T, X) + 1e-6 * np.eye(D), np.dot(X.T, y))
# 2. MAP with Gaussian Prior (Ridge / L2): (X^T X + lambda * I)^(-1) X^T y
lambda_ridge = 2.0
w_ridge = np.linalg.solve(np.dot(X.T, X) + lambda_ridge * np.eye(D), np.dot(X.T, y))
print("True Weights: ", np.round(true_w, 2))
print("MLE (Overfit): ", np.round(w_mle, 2))
print("MAP Ridge (L2): ", np.round(w_ridge, 2))
if __name__ == "__main__":
demonstrate_mle_vs_map()5. Check yourself
In the catalog
Finished this chapter?
The Epoch Zero newsletter
What changed in ML architectures this week: new models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.