❯j-devlog▊
KO
~/nav

// categories

// tags

[MMD]

ML Probability & Statistics Chapter 3: Sampling, MLE, and MAP

·13 min read·

In Chapter 2 we covered descriptive statistics and multivariate distributions. This post focuses on estimating a population from samples, along with the two workhorses of machine learning estimation: MLE and MAP.

This post is based on Week 2 of DeepLearning.AI's Mathematics for Machine Learning and Data Science — Probability & Statistics.

What You'll Learn#

  • Population vs. Sample — what they are and why the distinction matters
  • Sample mean, proportion, and variance — unbiased estimators
  • Law of Large Numbers — why more data means better estimates
  • Central Limit Theorem (CLT) — why any distribution converges to normal with enough samples
  • MLE — finding the parameters that best explain your data
  • MAP — combining MLE with prior information
  • Regularization — the connection between MAP and L2 regularization

Population and Sample#

Key Concepts#

TermDefinition
PopulationThe complete set of subjects under study
SampleA randomly drawn subset of the population
ParameterA characteristic of the population (μ\mu, σ2\sigma^2)
StatisticAn estimate computed from the sample (xˉ\bar{x}, s2s^2)

Why use samples? Measuring an entire population is often impossible or prohibitively expensive, so we use random samples to infer population characteristics.

Properties of a good sample (i.i.d.):

  • Independence: each observation does not influence the others
  • Identical distribution: all observations are drawn from the same distribution

Sample Mean#

xˉ=1n∑i=1nxi\bar{x} = \frac{1}{n} \sum_{i=1}^n x_i


  • An unbiased estimator of the population mean μ\mu
  • As the sample size grows, xˉ→μ\bar{x} \to \mu

Sample Variance#

s2=1n−1∑i=1n(xi−xˉ)2s^2 = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})^2


Why n−1n-1? (Bessel's Correction): Dividing by nn underestimates the population variance. Because one degree of freedom is consumed when estimating the mean, dividing by n−1n-1 yields an unbiased estimator.

The MLE variance estimator uses 1n\frac{1}{n} and is therefore biased. In practice, always use 1n−1\frac{1}{n-1} for sample variance.


Law of Large Numbers#

As the sample size nn grows, the sample mean converges to the population mean.


xˉn→n→∞μ\bar{x}_n \xrightarrow{n \to \infty} \mu


Example: When flipping a coin, the proportion of heads fluctuates wildly in the first few tosses (0.3, 0.7, …), but converges to 0.5 after thousands of trials.

ML implication: The more training data you have, the better your model can learn the true data distribution.


Central Limit Theorem (CLT)#

The Core Statement#

Regardless of the original distribution's shape, the sample mean of nn independent, identically distributed random variables X1,X2,…,XnX_1, X_2, \ldots, X_n converges to a normal distribution as nn grows large.


Xˉn∼N(μ,σ2n)(for large n)\bar{X}_n \sim N\left(\mu, \frac{\sigma^2}{n}\right) \quad \text{(for large } n\text{)}


Standardized form:


Zn=Xˉn−μσ/n→n→∞N(0,1)Z_n = \frac{\bar{X}_n - \mu}{\sigma / \sqrt{n}} \xrightarrow{n \to \infty} N(0, 1)

Key Points#

ItemValue
Minimum sample size for CLTTypically n≥30n \geq 30
If the original distribution is symmetricConverges with smaller nn
If the original distribution is skewedRequires larger nn

Why standardize?: The variance of the sample mean is σ2n\frac{\sigma^2}{n}, which changes with nn, making direct comparisons difficult.

Practical significance: No matter what distribution your data comes from, statistics computed from a sufficiently large sample follow a normal distribution. This is the theoretical foundation of confidence intervals and hypothesis testing.


Maximum Likelihood Estimation (MLE)#

Core Idea#

Find the parameters most likely to have generated the observed data.


θ^MLE=arg⁡max⁡θP(data∣θ)=arg⁡max⁡θL(θ)\hat{\theta}_{\text{MLE}} = \arg\max_\theta P(\text{data} \mid \theta) = \arg\max_\theta L(\theta)


We find the θ\theta that maximizes the likelihood of the observed data.

Bernoulli MLE Example#

Flip a coin 10 times, observe 8 heads:


L(p)=p8(1−p)2L(p) = p^8 (1-p)^2


Apply the log transformation (products → sums):


log⁡L(p)=8log⁡p+2log⁡(1−p)\log L(p) = 8\log p + 2\log(1-p)


Differentiate and set to zero:


dlog⁡Ldp=8p−21−p=0⇒p^=810=0.8\frac{d \log L}{dp} = \frac{8}{p} - \frac{2}{1-p} = 0 \quad \Rightarrow \quad \hat{p} = \frac{8}{10} = 0.8


Takeaway: The MLE estimate equals the observed frequency — an intuitively satisfying result.

Gaussian MLE#

Given nn samples x1,…,xnx_1, \ldots, x_n drawn from N(μ,σ2)N(\mu, \sigma^2):

MLE for the mean:


μ^MLE=1n∑i=1nxi=xˉ\hat{\mu}_{\text{MLE}} = \frac{1}{n} \sum_{i=1}^n x_i = \bar{x}


MLE for the variance:


σ^MLE2=1n∑i=1n(xi−xˉ)2\hat{\sigma}^2_{\text{MLE}} = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})^2

The MLE variance estimator uses 1n\frac{1}{n} and is biased (the sample variance s2s^2 uses 1n−1\frac{1}{n-1} and is unbiased).

Linear Regression and MLE#

For linear regression y=wx+b+ϵy = wx + b + \epsilon (where ϵ∼N(0,σ2)\epsilon \sim N(0, \sigma^2)):


P(yi∣xi,w,b)=N(wxi+b,σ2)P(y_i \mid x_i, w, b) = N(wx_i + b, \sigma^2)


Maximizing the likelihood with respect to ww and bb is equivalent to:


minimize∑i(yi−(wxi+b))2\text{minimize} \sum_i (y_i - (wx_i + b))^2


Takeaway: Under a normality assumption, MLE = Ordinary Least Squares.


Regularization#

The Overfitting Problem#

When a model is too complex, it overfits the training data and performs poorly on new data.

Solution: Add a penalty term for complexity.


new loss=original loss+λ⋅penalty\text{new loss} = \text{original loss} + \lambda \cdot \text{penalty}


L2 Regularization (Ridge):


penalty=∑jwj2(sum of squared coefficients)\text{penalty} = \sum_j w_j^2 \quad \text{(sum of squared coefficients)}


  • Larger coefficients incur larger penalties
  • Shrinks all coefficients toward zero (but never exactly to zero)

L1 Regularization (Lasso): Sum of absolute values of coefficients → drives some coefficients to exactly zero, enabling feature selection.

Hyperparameter λ\lambda: Controls the penalty strength. A larger λ\lambda produces a simpler model; a smaller λ\lambda stays closer to the unregularized model.


MAP and Bayesian Statistics#

Frequentist vs. Bayesian#

FrequentistBayesian
View of parametersFixed but unknown valuesRandom variables with uncertainty
Estimation methodMLEPrior + data → posterior
OutputPoint estimateDistribution

MAP (Maximum A Posteriori)#

MLE + Prior information:


θ^MAP=arg⁡max⁡θP(θ∣data)⏟posterior=arg⁡max⁡θP(data∣θ)⏟likelihood⋅P(θ)⏟prior\hat{\theta}_{\text{MAP}} = \arg\max_\theta \underbrace{P(\theta \mid \text{data})}_{\text{posterior}} = \arg\max_\theta \underbrace{P(\text{data} \mid \theta)}_{\text{likelihood}} \cdot \underbrace{P(\theta)}_{\text{prior}}


  • Uniform prior → MAP = MLE
  • Informative prior → prior knowledge is incorporated into the estimate

Connection Between MAP and L2 Regularization#

Assume a Gaussian prior N(0,τ2)N(0, \tau^2) on θ\theta:


log⁡P(θ)=−12τ2∑jθj2+const\log P(\theta) = -\frac{1}{2\tau^2} \sum_j \theta_j^2 + \text{const}


Maximizing MAP = minimizing the MLE loss + a 1τ2∑θj2\frac{1}{\tau^2} \sum \theta_j^2 penalty


⇔L2 Regularization (Ridge)\Leftrightarrow \text{L2 Regularization (Ridge)}

Conclusion: L2 regularization = MAP estimation with a Gaussian prior.

Updating the Prior (Prior → Posterior)#

The heart of Bayesian statistics: update the prior each time new data is observed.

Sequential updating: the previous posterior becomes the next prior.


P(θ∣D1)→new data D2P(θ∣D1,D2)P(\theta \mid D_1) \xrightarrow{\text{new data } D_2} P(\theta \mid D_1, D_2)


Whether you process data all at once or sequentially, the final posterior is the same.

MAP choice: Use the mode of the posterior distribution as a point estimate.


MLE vs. MAP Summary#

MLEMAP
Uses prior information❌✅
Equivalent toLeast SquaresL2/L1 Regularization
With little dataRisk of overfittingStabilized by prior
With lots of dataMLE ≈ MAPPrior influence diminishes
OutputPoint estimatePoint estimate (mode of posterior)

Key Takeaways#

ConceptSummary
Population/SampleWhole/subset; i.i.d. condition
Sample varianceUnbiased with 1n−1\frac{1}{n-1}
Law of Large Numbersn↑n \uparrow → sample mean → population mean
Central Limit Theoremn≥30n \geq 30 → sample mean ~ normal distribution
MLEMaximize likelihood = observed frequency / least squares
MAPMaximize likelihood × prior = MLE + regularization
L2 RegularizationEquivalent to MAP with a Gaussian prior


Quiz#

Q1. Why do we divide by n−1n-1 instead of nn when computing sample variance?

Show answer

Bessel's Correction: Estimating the mean from the sample consumes one degree of freedom.

Dividing by nn underestimates the population variance. Dividing by n−1n-1 removes this bias and yields an unbiased estimator.

Intuition: data points within a sample tend to cluster around the sample mean, making the spread appear smaller than it really is in the full population.


Q2. Which of the following statements about the Central Limit Theorem is correct?

  1. The original distribution must be normal
  2. When the sample size is large, the distribution of the sample mean approaches a normal distribution
  3. As the sample size increases, the population variance decreases
  4. The sample mean always equals the population mean
Show answer

Answer: 2

  • 1 ❌: CLT applies regardless of the original distribution's shape
  • 2 ✅: This is the core statement of the CLT
  • 3 ❌: The sample mean's variance shrinks to σ2/n\sigma^2/n; the population variance itself does not change
  • 4 ❌: The sample mean converges probabilistically to the population mean, but they are not always equal

Q3. You flip a coin 10 times and observe 7 heads. What is the MLE estimate of the probability pp of heads?

Show answer

L(p)=p7(1−p)3L(p) = p^7(1-p)^3


log⁡L(p)=7log⁡p+3log⁡(1−p)\log L(p) = 7\log p + 3\log(1-p)


dlog⁡Ldp=7p−31−p=0⇒p^=710=0.7\frac{d\log L}{dp} = \frac{7}{p} - \frac{3}{1-p} = 0 \quad \Rightarrow \quad \hat{p} = \frac{7}{10} = 0.7


The MLE estimate equals the observed frequency.


Q4. Explain the difference between MLE and MAP, and describe the connection to L2 regularization.

Show answer
  • MLE: maximizes the likelihood P(data∣θ)P(\text{data} \mid \theta) using data alone
  • MAP: also incorporates a prior P(θ)P(\theta) by maximizing P(data∣θ)⋅P(θ)P(\text{data} \mid \theta) \cdot P(\theta)

Assuming a Gaussian prior P(θ)∝e−λ∥θ∥2P(\theta) \propto e^{-\lambda \|\theta\|^2} on θ\theta:


MAP=arg⁡min⁡θ[−log⁡P(data∣θ)+λ∥θ∥2]\text{MAP} = \arg\min_\theta \left[ -\log P(\text{data} \mid \theta) + \lambda \|\theta\|^2 \right]


This is exactly the form of L2 regularization (Ridge).

→ L2 regularization = MAP estimation with a Gaussian prior


Q5. Which of the following correctly describes the difference between the Law of Large Numbers and the Central Limit Theorem?

  1. The LLN describes the shape of the sample mean's distribution; the CLT describes the rate of convergence
  2. The LLN states that the sample mean converges to the population mean; the CLT states that the distribution of the sample mean converges to a normal distribution
  3. The LLN applies only to continuous variables; the CLT applies only to discrete variables
Show answer

Answer: 2

  • Law of Large Numbers: as n→∞n \to \infty, Xˉn→μ\bar{X}_n \to \mu — the value of the sample mean approaches the population mean
  • CLT: for large nn, the distribution of Xˉn\bar{X}_n approaches N(μ,σ2/n)N(\mu, \sigma^2/n)

The two theorems describe different aspects: the LLN addresses convergence of the value, while the CLT describes the shape of the distribution it converges to.


The next post will cover Confidence Intervals and Hypothesis Testing — methods for quantifying the uncertainty in our estimates.

// Related Posts