Probability & Statistic in Machine Learning(III)
- 發佈時間
Sampling and Point Estimation#
Population and Sample#
Population is the entire group we actually care about — for example, every score every student in the world could ever produce on a given test. Sample is the subset we actually get to observe — for example, the 30 scores from this year's midterm in one classroom. We never get to see the population directly; we only see a sample of it, and we use statistics computed from that sample (like the sample mean) to estimate properties of the population (like the true population mean) that we can never directly measure.
Key idea: every dataset we work with in machine learning is a sample, not the population. We are always trying to infer properties of the true underlying data-generating process (the population) from a finite dataset (the sample).
-
Sample Mean — estimates the population mean
-
Sample Proportion — estimates a population proportion
-
Sample Variance — estimates the population variance , but needs a small correction to be unbiased
Why does the sample variance divide by instead of ?
The population variance is defined using the true population mean :
But with only a sample, we don't know — we can only use the sample mean instead. This substitution is exactly where the bias comes from: is computed from the same data it's being compared against, so it always sits a little closer to the sample points than the true would, making the sum of squared deviations come out slightly too small.
Concrete example. Suppose the population has values: .
Population mean: . Population variance: .
Now draw a sample of values (with replacement) and compute the variance two ways. For two points , the sum of squared deviations from the sample mean conveniently simplifies to :
Sample Sample mean Divide by Divide by 3 2 1.0 2.0 5 18 9.0 18.0 6 8 4.0 8.0 7 2 1.0 2.0 Any single sample is a noisy estimate either way. But average the estimate over all 16 equally likely ordered pairs you could draw (with replacement) from this population:
Dividing by recovers the true population variance exactly, on average. Dividing by systematically underestimates it — which is exactly why the sample variance formula uses , a correction known as Bessel's correction:
Law of Large Numbers#
The Law of Large Numbers (LLN) says that as we collect more samples, the sample mean converges to the true population mean:
Intuitively: any individual sample is noisy, but that noise averages out as grows. This is exactly why "more data" tends to make machine learning estimates more reliable — a model trained on a larger sample sees a mean/behavior that's closer to the true population behavior.
CLT#
The Central Limit Theorem (CLT) goes a step further: regardless of the shape of the population distribution, the sample mean becomes approximately normally distributed as grows large:
This is what lets us build confidence intervals and hypothesis tests around a sample mean even when we don't know the true shape of the population distribution — we only need its mean and variance to exist.
Point Estimations#
A point estimate is a single numerical value based on sample data that is used to approximate an unknown parameter of a population or model.
MLE (Maximum Likelihood Estimation)#
Likelihood: — given the prior/condition , what is the probability of observing event ?
So MLE is trying to find the parameter value under which the probability of observing the event we actually saw is maximized.
The blue curve above is the likelihood: for every possible value of the parameter (the x-axis), it shows how likely it is — computed directly from the formula — that this parameter would have produced the orange sampled data shown below. Notice the curve peaks near , which lines up almost exactly with where the sampled data actually came from.
The core idea behind the likelihood function is: "Given the data we've already observed, if we assume the population parameter is , how likely (in terms of probability density) is it that this exact data would occur together?"
1. Probability Density of a Single Data Point#
Suppose we draw a single data point . If we guess that this data comes from a normal distribution with mean and standard deviation , then the probability density of this point occurring (the height of the curve at that point) is:
2. The Joint Probability of Multiple Independent Points (the Likelihood Itself)#
In practice we usually draw many points (for example, points in the figure above). Because each draw is an independent event, the total probability of drawing all points together is simply the product of each point's probability density.
This gives us the likelihood function :
Written with the product notation :
3. Computing It in Practice: Taking the Log (Log-Likelihood)#
You've probably noticed the problem: multiplying hundreds of probability values that are each less than 1 makes the product shrink to an astronomically small number (e.g., ), which easily causes numerical underflow on a computer. On top of that, a product of many terms is awkward to differentiate when we want to find its maximum.
To fix this, statisticians take the natural log ( or ) of both sides, since logarithms turn multiplication into addition:
Expanding this using log rules, the log-likelihood of the normal distribution becomes:
This is exactly the formula behind the blue parabola plotted in the middle panel of the demo above!
4. A Worked Example#
Suppose we only have 3 data points: , and we know . We want to test two different guesses for the mean ( and ) and see which one gives a higher likelihood.
- Guess 1: The sum of squared distances from each point to 0 is . Plugging this into the formula, the value subtracted in the last term is .
- Guess 2: The sum of squared distances from each point to 2 is . Plugging this into the formula, the value subtracted in the last term is .
Since the formula has a negative sign in front, subtracting less means the overall is larger!
- The log-likelihood computed for is much larger than for .
- This matches intuition: since the data is clustered around 1, 2, 3, guessing the mean is 2 is clearly closer to reality than guessing 0.
This is exactly how a computer finds the MLE in practice: it plugs in many different values of behind the scenes until it finds the that maximizes this function (the peak of the parabola) — that's the computational process behind Maximum Likelihood Estimation (MLE).
MLE Regression#
First, take a look at the following picture — this is a 2-dimensional regression problem. For every , we assume the corresponding is drawn from a Gaussian distribution centered on the regression line. In other words, the line doesn't predict exactly — it predicts the mean of a normal distribution that is sampled from, so we can calculate a probability for observing any particular .
For a single point , the probability of observing given the line's prediction is:
For all five points in the picture, assuming each is drawn independently, the joint probability is the product of each individual probability:
Viewed as a function of the line's parameters and , this joint probability is exactly the likelihood:
Just like before, take the log to turn the product into a sum:
Now let's take off the constant — since doesn't depend on or at all, maximizing is the same as maximizing what's left:
And if we flip the sign and change the problem into a minimization (dropping the positive constant , which doesn't change where the optimum is):
This is the least-squares error we already know! In other words, ordinary least-squares regression is exactly the MLE solution under the assumption that the noise around the line is Gaussian.
Here comes a very interesting question, though: what does a probability actually represent? Two schools of thought answer this differently.
Frequentists v.s Bayesians#
Frequentists
- Probabilities represent the long-run frequency of events
- Built around the concept of likelihood
- Goal: find the model that most likely generated the observed data
Bayesians
- Probabilities represent a degree of belief (or certainty)
- Built around the concept of a prior
- Goal: update prior beliefs based on new observations
Maximum Posteriori (MAP)#
Bayes' rule connects the prior, the likelihood, and the posterior:
Applying this to parameter estimation, the posterior is the probability of the parameter given the data we observed, and Maximum a Posteriori (MAP) estimation finds the value of that maximizes it:
The only difference from MLE is the extra term — the prior belief about before seeing any data. When the prior is flat (uniform, no preference for any value of ), MAP reduces exactly to MLE.
Regularization#
Consider what happens when we bring a prior into the regression problem above. Recall that MLE regression reduces to minimizing the sum of squared errors. If we now put a Gaussian prior on the weights — i.e., we believe before seeing any data that the weights should be small and centered around 0 — then MAP estimation adds a penalty term to that objective:
This is exactly L2 regularization (ridge regression): a Gaussian prior on the weights, expressed through Bayes' rule, becomes the familiar penalty term. (A Laplace prior on the weights would instead give L1 regularization / Lasso.) This is the deep connection between regularization and Bayesian priors — regularizing a model is mathematically the same as encoding a prior belief about what the parameters should look like.
A discrete example of Bayesian updating
Let be the "true" probability of heads for a coin:
- if the coin is fair
- if the coin is biased
Our prior belief about which one we have, before flipping anything, is:
We flip the coin once and observe heads (). We want the posterior — our updated belief after seeing this evidence. By Bayes' rule:
So after a single head, our belief that the coin is fair drops from 75% to about 65.2% — and this posterior now becomes the new prior for the next flip.
If we keep flipping and keep getting heads, the posterior keeps updating and converges toward the coin actually being biased (since a run of heads is far more consistent with than ):
| Flips so far (all heads) | Posterior | Posterior |
|---|---|---|
| 0 (prior) | 0.750 | 0.250 |
| 1 | 0.652 | 0.348 |
| 2 | 0.540 | 0.460 |
| 3 | 0.423 | 0.577 |
| 4 | 0.314 | 0.686 |
| 5 | 0.222 | 0.778 |
As more evidence comes in, the belief that the coin is fair steadily shrinks toward 0, while the belief that it's biased grows toward 1. This convergence, driven purely by repeatedly applying Bayes' rule, is the core mechanic behind all Bayesian updating.
