Machine Learning Techniques · IIT Madras BS Degree

Estimation: maximum likelihood and Bayesian updating

Notes for lectures 19 to 21. The data is now assumed to come from a probabilistic model with unknown parameters, and estimation means guessing those parameters from the data. Maximum likelihood picks the value that makes the observed data most likely, and the Bayesian approach turns a prior belief into a posterior with Bayes' theorem.

Part 8 of 18 Instructor Prof. Arun Rajkumar About 84 min of video

At a glance

  • Estimation assumes a probabilistic model generated the data, with some unknown parameters, and uses the data to guess them. The parameter is a compressed description of the data.
  • The usual hidden assumption is that observations are i.i.d., independent and identically distributed: one box, pressed \(n\) times.
  • Maximum likelihood (Fisher) picks the parameter under which the observed data is most likely. For coin tosses it gives the fraction of heads, \(\hat p_{\text{ML}} = \frac1n\sum_i x_i\); for a Gaussian with known variance, the sample mean.
  • For continuous data the likelihood is built from densities, since every single value has probability zero. The estimator depends on the model: a Laplace model gives the sample median instead of the mean.
  • Bayesian estimation writes a hunch as a prior \(P(\theta)\) and updates it with Bayes' theorem: posterior \(\propto\) likelihood \(\times\) prior.
  • A Beta\((\alpha, \beta)\) prior with coin-toss data gives a Beta\((\alpha + n_H,\ \beta + n_T)\) posterior. Its mean \((\alpha + n_H)/(\alpha + \beta + n)\) treats the prior as \(\alpha\) ghost heads and \(\beta\) ghost tails; the MAP estimate is its mode.

A probabilistic model for the data

So far the course has studied two kinds of unsupervised learning. Representation learning found good representations with PCA, for linear relationships between features (Part 3), and with kernel PCA for non-linear ones (Part 5). Clustering grouped the points with Lloyd's algorithm, K-means (Parts 6 and 7).

None of these methods assumed anything about how the data was produced. Estimation does. It assumes a probabilistic model: some random mechanism that generated the data. We are still in unsupervised learning and still have only data, but the methods move from deterministic procedures to probability. The lecture adds that the same tools will be useful for supervised learning later in the course.

The hypothesis, at a high level: 2:30

  • Some probabilistic mechanism generates the data.
  • Something about this mechanism is unknown to us.
  • Given the data, find (estimate) what is unknown.

The lecture's picture is a black box with a button on top. Inside is a coin that need not be fair: the chance of heads is some value \(p\), perhaps 0.7 or 0.9, which we do not know. Each press of the button flips the coin and shows the outcome, written 1 for heads and 0 for tails. Four presses might give 1, 0, 1, 1. These are our observations.

Keep two sides apart. The observations are what we see. The box is a story we assume in order to explain them. It may or may not be true of the real world; for now we make the assumption and work with it, and the lecture postpones the question of how good such assumptions are. Within the story, the bias \(p\) is the unknown parameter.

Assumed: hidden from us Observed: known to us p = ? coin with unknown bias p press the button n times 1 0 1 1 … x₁x₂x₃x₄ estimate: p̂ = fraction of 1s = 3/4 your own box p̂ = 3/4 press 1 1 0 … new data on demand p is the unknown parameter: one number that explains (compresses) all n tosses. The box is an assumption; it may or may not be true.
The generative story behind estimation. We assume a hidden mechanism with an unknown parameter produced the data. Estimation runs the arrow backwards: from the observations to a guess \(\hat p\). With the guess in hand we own a copy of the box and can generate new data whenever we like.

The parameter is a compressed representation of the data. A thousand 0/1 values are explained by one number: if we knew \(p\), we would know the mechanism that produced them. This is the course's comprehension is compression theme from Part 2 in a new form.

Goal of estimation 7:43

Observe some data. Assume a probabilistic model that generates it. Estimate the unknown parameters of that model using the data.

A first guess, and why it is only a guess

Twelve presses give

\[ 1,\ 0,\ 0,\ 1,\ 1,\ 0,\ 1,\ 1,\ 1,\ 1,\ 1,\ 1 \]

The model says each observation is a random variable \(x\) with \(P(x = 1) = p\) and \(P(x = 0) = 1 - p\). What would you guess for \(p\)? The natural guess counts heads. There are 9 ones and 3 zeros out of 12, so

\[ \hat p = \frac{9}{12} = \frac34 \]

With an estimate in hand we can build our own box, a coin with bias 3/4, and generate as many observations as we like 10:59. The data mattered only because it told us about the mechanism. Once the parameter is fixed, we no longer need the data.

But 3/4 is a guess. As soon as probabilities enter, we can never be fully sure we have the true \(p\). Which values could have produced these twelve tosses?

True \(p\)Could it give 9 heads in 12?Why
2/3YesNothing rules it out.
0.0001Yes, barelyThe chance is tiny but not zero. We would have had an extremely unlucky day.
0NoEvery toss would be tails, but we saw heads.
1NoEvery toss would be heads, but we saw tails.

So the only values we can rule out are 0 and 1 13:48. Every \(p\) strictly between them remains a contender, and still we chose 3/4. The lecture asks which assumptions went into that choice.

The hidden assumptions: independent and identically distributed

Write the observations as \(x_1, x_2, \dots, x_n\), each 0 or 1. Two assumptions are almost always made about data like this, usually without saying so.

Independent L19 · 16:41

Knowing one outcome does not change the probability of another:

\[ P(x_i = a \mid x_j = b) = P(x_i = a) \qquad \text{for all } i \ne j \]

If the third toss was heads, our uncertainty about the fourth toss is unchanged. The same holds when we condition on any set of the other observations.

Identically distributed L19 · 17:44

\[ P(x_i = 1) = P(x_j = 1) = p \qquad \text{for all } i, j \]

The same coin is flipped every time. There is one box and we press it \(n\) times. Two different coins tossed once each are still independent, because the first result says nothing about the second, but they are not identically distributed: their biases may differ.

One box, pressed n times p 1 0 1 1 x₁x₂x₃x₄ Independent and identically distributed same coin every time: P(xᵢ = 1) = p for every i Two different coins p₁ p₂ 1 0 P(x₁ = 1) = p₁ P(x₂ = 1) = p₂ Independent, not identically distributed the two biases p₁ and p₂ may differ
What identical distribution adds. On the left one law produces every observation, so a single \(p\) describes them all. On the right the tosses are still independent, but each comes from its own coin and needs its own parameter.

The standard abbreviation for the pair is i.i.d. Under these assumptions, is there a principled way to call one guess better than another? Can we argue that 2/3 beats 0.0001, or that 3/4 beats every other value? The fraction of heads is intuitive, but so far nothing more. Lecture 20 supplies the principle.

Take the four observations 1, 0, 1, 1. Put every candidate value of \(p\) from 0 to 1 on a horizontal axis and ask of each one: if this were the true bias, what is the chance of seeing exactly this data? Plot that chance against \(p\). The curve is 0 at \(p = 0\) and at \(p = 1\), which we already know cannot produce this data, and it peaks at \(p = 3/4\) 2:16. That is the same as the fraction of ones.

Fisher's principle of maximum likelihood turns this into a method. Among all the parameter values that could have generated the data, bet on the one under which the observed data is most probable. The idea is old, and the lecture treats it as a review for anyone who has met it in a statistics course.

Writing it down L20 · 3:56

The likelihood function is a function of the parameter, with the data held fixed:

\[ \begin{aligned} L(p;\, x_1, \dots, x_n) &= P(x_1, x_2, \dots, x_n;\, p) \\ &= \prod_{i=1}^n P(x_i;\, p) && \text{independence} \\ &= \prod_{i=1}^n p^{x_i}(1-p)^{1-x_i} && \text{the same } p \text{ for every toss} \end{aligned} \]

Here \(x_1, \dots, x_n\) are the specific observed values, so the first line is the joint probability that the first toss came out as \(x_1\), the second as \(x_2\), and so on, if the bias is \(p\). Independence splits it into a product, and identical distribution puts the same \(p\) in every factor 5:05.

The compact form \(p^{x_i}(1-p)^{1-x_i}\) covers both outcomes at once. If \(x_i = 1\) it is \(p^1(1-p)^0 = p\); if \(x_i = 0\) it is \(p^0(1-p)^1 = 1-p\). For 1, 0, 1, 1:

\[ L(p) = p \cdot (1-p) \cdot p \cdot p = p^3(1-p) \]

That is the curve described above. Explore it below.

3 1 0.400
ML estimate (heads ÷ n)–
L at your p–
L at p̂–
The likelihood of coin tosses. The curve is \(L(p) = p^{n_H}(1-p)^{n_T}\) for the counts you set. The default is the lecture's 1, 0, 1, 1, so \(L(p) = p^3(1-p)\); set 9 heads and 3 tails for the twelve tosses of L19. The peak always sits at the fraction of heads, and more tosses make it narrower. Tick the box to plot \(\log L\) instead: the shape changes, the location of the peak does not.

Maximizing the likelihood for coin tosses

A hat marks a guess. The maximum likelihood (ML) guess is the value of \(p\) that maximizes \(L\):

\[ \hat p_{\text{ML}} = \arg\max_{p}\; \prod_{i=1}^n p^{x_i}(1-p)^{1-x_i} \]

Products are awkward to differentiate and sums are easy, so maximize the logarithm instead. This is safe because \(\log\) is monotonically increasing. If \(L(\hat p) > L(p)\) for every other \(p\), then \(\log L(\hat p) > \log L(p)\) as well, so the maximizer does not move. The log turns the product into a sum:

\[ \hat p_{\text{ML}} = \arg\max_{p}\; \sum_{i=1}^n \Big[\, x_i \log p + (1 - x_i)\log(1-p) \,\Big] \]

This log-likelihood is a concave function of \(p\), so the maximum is where its derivative is zero 9:29. The lecture leaves the derivative as an exercise:

\[ \frac{d}{dp}\log L = \sum_{i=1}^n \Big[\frac{x_i}{p} - \frac{1-x_i}{1-p}\Big] = 0 \;\Longrightarrow\; (1-p)\sum_{i=1}^n x_i = p\sum_{i=1}^n (1-x_i) = p\Big(n - \sum_{i=1}^n x_i\Big) \]

Expanding the left side gives \(\sum x_i - p\sum x_i = pn - p\sum x_i\). The \(p\sum x_i\) terms cancel, leaving \(\sum x_i = pn\).

Result: ML estimate for coin tosses 10:33 \[ \hat p_{\text{ML}} = \frac1n\sum_{i=1}^n x_i = \frac{n_H}{n} \]

Each \(x_i\) is 0 or 1, so the sum counts the ones. The ML estimate is the fraction of heads, \(n_H\) heads out of \(n\) tosses.

Check it on 1, 0, 1, 1. The derivative of \(p^3(1-p) = p^3 - p^4\) is \(3p^2 - 4p^3 = p^2(3 - 4p)\), which is zero at \(p = 3/4\). For the twelve tosses of L19, \(9/12 = 3/4\) as well.

The intuitive guess now has a principle behind it: choose the \(p\) that makes the observed data most probable. It quietly relied on both assumptions 11:09, independence to split the joint probability into a product and identical distribution to use one \(p\) in every factor.

Real-valued data: densities replace probabilities

Now let each \(x_i \in \mathbb{R}\), for example the heights of 100 people. A coin box can only produce 0s and 1s, so it cannot explain this data. We need a different box, which means a different probabilistic model. The simplest choice is a Gaussian 12:50:

\[ x_i \sim \mathcal{N}(\mu, \sigma^2) \qquad \text{for all } i \]

To keep things simple, assume the variance \(\sigma^2\) is known and only the mean \(\mu\) is unknown 13:26. With real data we would not know the variance either, but the simplification makes the method clear.

First attempt: multiply probabilities L20 · 14:27

Copying the coin recipe gives \(L(\mu, \sigma^2;\, x_1, \dots, x_n) = P(x_1, \dots, x_n;\, \mu, \sigma^2) = \prod_i P(x_i;\, \mu, \sigma^2)\) by independence. Suppose the first height measured is 162.53 cm and we try \(\mu = 150\). What is the chance that a Gaussian with mean 150 produces exactly 162.53?

For a continuous distribution, probabilities belong to intervals. \(P(a \le X \le b)\) is the area under the density curve between \(a\) and \(b\) 15:32. A single value has zero width and so zero probability. Every factor of the product is 0, for every \(\mu\), however close to the data or far from it. A function that is 0 everywhere cannot tell one \(\mu\) from another, so there is nothing to maximize.

Fisher's fix: multiply densities

Fisher's proposal was to replace the probabilities by the probability density function (PDF). For independent data the joint density factorizes just as probabilities do:

\[ L(\mu, \sigma^2;\, x_1, \dots, x_n) = f(x_1, \dots, x_n;\, \mu, \sigma^2) = \prod_{i=1}^n f(x_i;\, \mu, \sigma^2) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi}\,\sigma}\, e^{-\frac{(x_i - \mu)^2}{2\sigma^2}} \]

Each factor is the height of the bell curve at \(x_i\) 18:45. It is not a probability, and it need not even lie between 0 and 1. But it is nonzero, and it is larger when \(\mu\) puts \(x_i\) near the peak, so it works as a proxy for the probability. This is also why the function is called a likelihood and not a probability: it is not strictly the probability of the data 19:47.

Probability: an area 130 150 170 μ = 150 x₁ = 162.53 shaded area = P(155 ≤ X ≤ 170) ≈ 0.29 a single value has zero width, so P(X = 162.53) = 0 whatever μ is Density: a height 130 150 170 0.039 0.018 μ = 150 μ = 160 x₁ = 162.53 heights at x₁ differ, so the density can rank μ = 160 above μ = 150 for this point
Why the Gaussian likelihood uses densities. Left: probability is area, so an interval has positive probability but the single value 162.53 has none, under any mean. Right: the density's height at 162.53 is positive and depends on \(\mu\), so it can rank candidate means. The value \(\sigma = 10\) is chosen for illustration.

The ML estimate of a Gaussian mean

Same drill as before: take the logarithm. It is the natural log, so it cancels the exponential:

\[ \log L(\mu, \sigma^2;\, x_1, \dots, x_n) = \sum_{i=1}^n \Big[\log\frac{1}{\sqrt{2\pi}\,\sigma} - \frac{(x_i-\mu)^2}{2\sigma^2}\Big] \]

We maximize over \(\mu\). The first term does not contain \(\mu\), so it can be dropped. Since \(\sigma^2\) is a known constant, the factor \(\frac{1}{2\sigma^2}\) does not change the maximizer either:

\[ \hat\mu_{\text{ML}} = \arg\max_{\mu}\; -\sum_{i=1}^n (x_i - \mu)^2 \]

The lecture leaves the remaining two steps as an exercise. Setting the derivative to zero gives \(2\sum_i (x_i - \mu) = 0\), so \(\sum_i x_i = n\mu\).

Result: ML estimate of a Gaussian mean \[ \hat\mu_{\text{ML}} = \frac1n\sum_{i=1}^n x_i \]

If the data is assumed Gaussian with unknown mean, the maximum likelihood guess for the mean is the sample mean.

The model decides the estimator

The sample mean may look like an obvious guess anyway, so why the machinery? Because the sample mean is the ML answer because we assumed a Gaussian. Suppose domain knowledge suggests a different model, say a Laplace distribution, which looks like a Gaussian with a sharper peak 22:32. Maximum likelihood then gives the sample median instead of the mean. The estimator is tied to the probabilistic model you assume, and the method adapts to that model because it works with the model's own density.

Standard context, not derived in the lecture: the Laplace density is proportional to \(e^{-|x - \mu|/b}\), so its log-likelihood is \(-\frac1b\sum_i |x_i - \mu|\) plus a constant. Maximizing it means minimizing the sum of absolute distances, which the median does.

Two models for real-valued data μ Laplace: sharper peak Gaussian same centre, same variance Same data, two ML estimates Gaussian model: μ̂ = sample mean = 5 Laplace model: μ̂ = sample median = 4 0 2 4 6 8 10 12 illustrative points: 2, 3, 4, 5, 11
Change the model, change the estimator. Left: Gaussian and Laplace densities with the same centre and variance. Right: for the same five illustrative points, maximum likelihood gives the mean (5) under the Gaussian model and the median (4) under the Laplace model.
  1. Maximum likelihood, for any model
  2. Assume a probabilistic model for the data, with unknown parameters \(\theta\).
  3. Write down its probability (discrete data) or its PDF (continuous data).
  4. Under i.i.d. data, the likelihood is the product over data points: \(L(\theta;\, x_1, \dots, x_n) = \prod_{i} f(x_i;\, \theta)\).
  5. Take the logarithm, turning the product into a sum.
  6. Maximize over \(\theta\), often by setting the derivative to zero. The maximizer is \(\hat\theta_{\text{ML}}\).

The method is simple and, more importantly, general. Once you have a model, you have an estimator. The resulting estimators need not be intuitive in advance. Here they matched intuition (the fraction of heads, the sample mean), which is reassuring, but the method still produces estimates when we have no intuition at all. That is why it is so popular in statistics and machine learning.

A statistics course would go on to prove good properties of the ML estimator, and in some well-defined senses show that no estimator can do better 24:41. This course takes it as a good estimator that gives reasonable guesses. The lecture ends with a question 25:53: is something besides the data often available in practice, and if so, can we do better than maximum likelihood?

A hunch before any data

Back to the coin box. Coin tosses are called Bernoulli trials: \(x_i \sim \text{Bernoulli}(p)\), meaning \(P(x_i = 1) = p\) for every \(i\). The setup is the same as before.

Now add one thing. Before you have seen any data, someone walks in and says, "I believe the bias p is somewhere close to 1." That is an English sentence, yet it carries information about the coin in the box. Other statements are possible, such as: \(p\) is either small or large, but certainly not near 0.5.

The lecture calls this domain knowledge, or a hunch, perhaps built from a practitioner's experience 3:50. It is a hunch about the parameter, not about the data, and it exists before a single data point is seen.

Maximum likelihood has no way to use it. Ask the ML method for a guess before any data arrives and it can say nothing, because it depends only on data 4:25.

Goal: codify a hunch about the parameters in precise mathematical form and build it into the estimation procedure. This leads to Bayesian modelling.

Treat the parameter as a random variable

The approach is to treat the unknown parameter as a random variable and encode the hunch as a probability distribution over its possible values. Values we believe more get more weight. The lecture sketches three examples 7:22:

  • \(p\) is close to 1: a density on \([0, 1]\) that peaks near 0.9.
  • \(p\) is small or large, not near 0.5: a U-shaped density on \([0, 1]\), high at both ends and low in the middle 8:34.
  • For a Gaussian mean \(\mu\): a bump around the value we think most likely.

In general, call the parameter \(\theta\). The hunch becomes a distribution \(P(\theta)\), the prior. When \(\theta\) is continuous, like \(p\) or \(\mu\), \(P(\theta)\) is a PDF whose support is the set of values \(\theta\) can take; for \(p\) that is \([0, 1]\). Its shape records our belief before we see data. To get the chance that \(\theta\) lies in some range, integrate the density over that range.

From prior to posterior with Bayes' theorem

Then the data arrives and the belief must be updated. Suppose we believed \(p \approx 0.9\), so we expected mostly heads, but 1000 tosses come out with 900 tails. The data contradicts the belief and should pull it away. If instead the data agrees with the belief, it should strengthen it and make us more confident.

The updated hunch is again a distribution, now conditioned on the data: \(P(\theta \mid \text{data})\). Moving from the prior \(P(\theta)\) to the posterior \(P(\theta \mid \text{data})\) is the Bayesian way of doing things 13:32. You have a hunch, you see data, you update the hunch.

Where Bayes comes in

Bayes' theorem from school says \(P(A \mid B) = P(B \mid A)\,P(A)/P(B)\). Take \(A\) to be the parameter \(\theta\) and \(B\) the data \(x_1, \dots, x_n\):

\[ \underbrace{P(\theta \mid x_1, \dots, x_n)}_{\text{posterior}} = \frac{\overbrace{P(x_1, \dots, x_n \mid \theta)}^{\text{likelihood}}\;\overbrace{P(\theta)}^{\text{prior}}}{\underbrace{P(x_1, \dots, x_n)}_{\text{evidence}}} \]

The first factor in the numerator is the chance of the data when the parameter is \(\theta\), which is the likelihood from Lecture 20 16:14. The denominator is called the evidence: the chance of observing the data at all. It does not depend on \(\theta\).

Bayesian update \[ P(\theta \mid \text{data}) \;\propto\; P(\text{data} \mid \theta)\; P(\theta) \qquad \text{posterior} \propto \text{likelihood} \times \text{prior} \]

Bayes' theorem is a multiplicative update: the prior belief is re-weighted by the likelihood.

The lecture's sketch

Start with a prior peaking at 0.9. Observe 10 tosses, 8 zeros and 2 ones. The likelihood alone points to \(2/10 = 0.2\) and the prior to 0.9, so the posterior lands somewhere between; the lecture's freehand sketch puts its peak near 0.27, a number made up for illustration. With the flipped data, nine 1s and one 0, data and hunch agree, and the posterior rises even more sharply at 0.9 19:09. Both cases are reproduced, with an actual prior, in the interactive figure further down.

A prior for a coin: the Beta distribution

For Bernoulli data the parameter is \(\theta = p\). How do we write a prior on it mathematically? We need a continuous distribution supported only on \([0, 1]\), since \(p\) is a probability. A Gaussian will not do, because it gives positive density to every real number. A good choice is the Beta distribution 20:16, which has two positive parameters \(\alpha\) and \(\beta\):

\[ f(p;\, \alpha, \beta) = \frac{p^{\alpha - 1}(1-p)^{\beta - 1}}{Z}, \qquad p \in [0, 1],\quad \alpha, \beta > 0 \]

\(Z\) is a normalizing constant that does not depend on \(p\) and makes the density integrate to 1. What matters for us is the functional form: a power of \(p\) times a power of \(1 - p\).

The two parameters make it very flexible:

  • \(\alpha = 2,\ \beta = 2\): the density is \(p(1-p)/Z\), symmetric and highest at 0.5. We believe the coin is roughly fair, but spread our bets.
  • \(\alpha = 2,\ \beta = 5\): small \(\alpha\) and large \(\beta\) push the mass toward 0. We believe \(p\) is closer to 0 than to 1.
  • \(\alpha = 0.5,\ \beta = 0.5\): U-shaped 23:38. The coin is biased one way or the other, but \(p\) is not near 0.5. This encodes the second hunch above.
0 0.5 1 1 2 3 α = 2, β = 2 shape p(1 − p), highest at 0.5 roughly fair coin, bets spread 0 0.5 1 α = 2, β = 5 small α, large β p believed closer to 0 0 0.5 1 α = β = 0.5 U-shaped: p small or large not near 0.5 (unbounded at ends)
Three Beta priors, the lecture's examples, drawn to scale. All three panels share one vertical scale; the U-shaped density grows without bound at 0 and 1, so it is cut at 3. A hunch that \(p\) is close to 1 needs large \(\alpha\) and small \(\beta\); the interactive below uses \(\alpha = 5.5,\ \beta = 1.5\), whose peak is at 0.9 (our choice, not the lecture's).

Beta prior, Bernoulli data, Beta posterior

Now apply posterior \(\propto\) likelihood \(\times\) prior. The likelihood is the Bernoulli likelihood from Lecture 20 and the prior is the Beta density:

\[ f(p \mid x_1, \dots, x_n) \;\propto\; \underbrace{\prod_{i=1}^n p^{x_i}(1-p)^{1-x_i}}_{\text{likelihood}} \;\times\; \underbrace{p^{\alpha-1}(1-p)^{\beta-1}}_{\text{prior}} \]

Both factors are a power of \(p\) times a power of \(1 - p\), so the exponents simply add 25:20:

\[ f(p \mid x_1, \dots, x_n) \;\propto\; p^{\,\sum_i x_i + \alpha - 1}\,(1-p)^{\,\sum_i (1 - x_i) + \beta - 1} \]

We write \(\propto\) because the evidence and the normalizing constant are left out. They are not needed, as the next step shows.

This has the same functional form as the prior. Any density on \([0, 1]\) of the form \(p^{a-1}(1-p)^{b-1}\) is a Beta density, so the posterior is a Beta distribution. Only its parameters have changed, and they now depend on both the prior's \(\alpha, \beta\) and the data. We never have to compute the posterior density point by point: knowing its two parameters gives us the whole function.

Result: Beta–Bernoulli update \[ \text{prior } \text{Beta}(\alpha, \beta) \;\xrightarrow{\ n_H \text{ heads},\ n_T \text{ tails}\ }\; \text{posterior } \text{Beta}(\alpha + n_H,\ \beta + n_T) \]

Here \(n_H = \sum_i x_i\) is the number of heads and \(n_T = \sum_i (1 - x_i)\) the number of tails, with \(n_H + n_T = n\).

This convenience comes from the pairing 27:44. Another prior on \([0, 1]\) would still give a posterior when multiplied by the Bernoulli likelihood, and it might be useful, but it need not have the same form as the prior. Standard name: a prior whose posterior stays in the same family is called a conjugate prior. The Beta is conjugate to the Bernoulli.

Turning the posterior into one number

Maximum likelihood uses only the counts: \(\hat p_{\text{ML}} = n_H/(n_H + n_T) = n_H/n\) 29:30. The Bayesian approach gives a whole distribution over \(p\). If we must commit to a single guess, there are several sensible choices.

The posterior mean

The expected value of a Beta\((\alpha, \beta)\) distribution is \(\alpha/(\alpha+\beta)\) 30:32. The posterior is Beta\((\alpha + n_H,\ \beta + n_T)\), so its mean is

\[ \hat p_{\text{mean}} = \frac{\alpha + n_H}{(\alpha + n_H) + (\beta + n_T)} = \frac{\alpha + n_H}{\alpha + \beta + n} \]

The prior as ghost data

Compare this with \(n_H/n\). The posterior mean is exactly what maximum likelihood would give if, besides the \(n\) real tosses, we had \(\alpha + \beta\) extra pseudo-observations, which the lecture calls ghost data: \(\alpha\) ghost heads and \(\beta\) ghost tails. A Beta prior acts as if someone had already done \(\alpha + \beta\) experiments. If someone tells us they did 100 experiments and saw 80 heads and 20 tails, we can encode that as the prior Beta(80, 20) 32:18. The posterior then takes these ghost samples into account along with the real ones.

Priorghost data, Beta(80, 20) 80 ghost heads 20 prior mean 80/100 = 0.80 + Datathe 12 tosses of L19 9 heads, 3 tails · ML estimate 9/12 = 0.75 = Posteriorpooled counts 80 + 9 = 89 heads 20 + 3 = 23 tails Beta(89, 23) mean 89/112 ≈ 0.795 Posterior mean = the ML estimate computed as if the 100 ghost tosses were real: (α + heads)/(α + β + n).
Conjugate updating is adding counts. Bars are drawn to scale by count; dashed outlines are ghost tosses, solid outlines real ones, tinted blocks heads. Combining the lecture's Beta(80, 20) prior with the twelve tosses of L19 gives Beta(89, 23). Its mean, 0.795, sits close to the prior's 0.80, because 100 ghost tosses outweigh 12 real ones.

Maximum a posteriori (MAP)

Maximum likelihood picks the maximizer of the likelihood. The maximum a posteriori (MAP) estimator picks the maximizer of the posterior density, its mode, written \(\hat p_{\text{MAP}}\). For the Beta posterior it differs slightly from the posterior mean. The lecture does not write the formula; the standard result is that for \(a, b > 1\) the mode of Beta\((a, b)\) is \((a-1)/(a+b-2)\), so

\[ \hat p_{\text{MAP}} = \frac{\alpha + n_H - 1}{\alpha + \beta + n - 2} \qquad (\alpha + n_H > 1,\ \beta + n_T > 1) \]

Because we hold an entire distribution, the choice is ours 33:58: take its mode (MAP), its mean, or even draw samples from it and average them. Until we choose, the posterior is a belief over every possible value of \(p\) that does not commit to any single one.

5.5 1.5 2 8
ML estimate–
Posterior mean–
MAP (posterior mode)–
Prior \(\times\) likelihood \(\to\) posterior, for a coin. The default prior Beta(5.5, 1.5) is our choice for a hunch that \(p\) is near 0.9 (its peak is at 0.9). With the lecture's 2 heads and 8 tails the posterior lands between hunch and data; with 9 heads and 1 tail it sharpens near 0.9. The likelihood is rescaled to area 1 so it can share the axis. Try \(\alpha = \beta = 0.5\) for the U-shaped hunch, or large \(\alpha\) and \(\beta\) to watch a confident prior resist the data.

Two ways to estimate, and what comes next

Maximum likelihoodBayesian
UsesData onlyA prior hunch and the data
The parameter isA fixed unknown valueA random variable with a distribution
MethodWrite down the likelihood and maximize itUpdate prior to posterior with Bayes' theorem
OutputA point estimate \(\hat\theta_{\text{ML}}\)A posterior distribution \(P(\theta \mid \text{data})\)
One numberThe maximizer itselfPosterior mean, MAP (mode), or another summary
Coin tosses\(n_H/n\)Beta\((\alpha + n_H,\ \beta + n_T)\), mean \(\frac{\alpha + n_H}{\alpha + \beta + n}\)

The lecture calls these basic statistical ideas. Next they are put to work on a specific unsupervised learning problem. K-means assumed no probabilistic model for the data. With a reasonable model, maximum likelihood gives a probabilistic counterpart of K-means clustering: the Gaussian mixture model of Part 9. The lecture raises the same question for representation learning.

Both principles come back in supervised learning: maximum likelihood in the probabilistic view of regression (Part 11), Gaussian priors and ridge regression (Part 12), and generative classifiers such as naive Bayes (Part 14).

Key terms

Estimation
Assuming a probabilistic model generated the data and using the data to guess its unknown parameters.
Probabilistic model
The assumed random mechanism (the box in the lecture's picture) behind the data, such as a coin with bias \(p\) or a Gaussian \(\mathcal{N}(\mu, \sigma^2)\).
i.i.d.
Independent (one outcome does not change the probability of another) and identically distributed (the same law, such as one \(p\), for every observation).
Likelihood function
\(L(\theta;\, x_1, \dots, x_n)\): the probability (or density) of the observed data, viewed as a function of the parameter.
Maximum likelihood estimator
\(\hat\theta_{\text{ML}} = \arg\max_\theta L(\theta;\, x_1, \dots, x_n)\), Fisher's principle. For coins, the fraction of heads.
Log-likelihood
\(\log L\). It has the same maximizer as \(L\) because log is increasing, and turns products into sums.
Bernoulli trial
A 0/1 outcome with \(P(x = 1) = p\): one coin toss.
Prior
\(P(\theta)\): a distribution over the parameter that encodes a hunch before any data is seen.
Posterior
\(P(\theta \mid \text{data}) \propto P(\text{data} \mid \theta)\,P(\theta)\): the hunch updated by the data.
Evidence
\(P(x_1, \dots, x_n)\), the denominator in Bayes' theorem. It does not depend on \(\theta\).
Beta distribution
Density \(\propto p^{\alpha-1}(1-p)^{\beta-1}\) on \([0, 1]\), with \(\alpha, \beta > 0\) and mean \(\alpha/(\alpha + \beta)\).
Conjugate prior
A prior whose posterior has the same form. Beta prior with Bernoulli data gives Beta\((\alpha + n_H, \beta + n_T)\).
Ghost data
Reading a Beta\((\alpha, \beta)\) prior as \(\alpha\) imaginary heads and \(\beta\) imaginary tails added to the real tosses.
MAP estimate
Maximum a posteriori: the value of \(\theta\) where the posterior density is highest (its mode).

Test yourself

Q1Ten tosses give 7 heads and 3 tails. What is \(\hat p_{\text{ML}}\), and which values of \(p\) are ruled out completely?

\(\hat p_{\text{ML}} = 7/10 = 0.7\), the fraction of heads. Only \(p = 0\) and \(p = 1\) are ruled out, since the data contains both heads and tails. Every other value has a nonzero chance of producing it.

Q2Write the likelihood of the data 0, 0, 1 and find where it peaks.

\(L(p) = (1-p)(1-p)\,p = p(1-p)^2\). Its derivative is \((1-p)^2 - 2p(1-p) = (1-p)(1-3p)\), which is zero at \(p = 1/3\) and at \(p = 1\), where \(L = 0\). So \(\hat p_{\text{ML}} = 1/3\), the fraction of ones.

Q3Why may we maximize \(\log L\) instead of \(L\)?

Log is monotonically increasing, so the \(p\) with the largest \(L\) also has the largest \(\log L\). The maximizer does not change, and the log turns the product over data points into a sum that is easy to differentiate.

Q4For Gaussian data, why does the product of probabilities \(\prod_i P(x_i;\, \mu, \sigma^2)\) fail as a likelihood?

Under a continuous distribution every single value has probability 0, so the product is 0 for every \(\mu\) and cannot rank one \(\mu\) above another. Fisher's fix uses the density \(f(x_i;\, \mu, \sigma^2)\), which is positive and larger when \(\mu\) is near \(x_i\).

Q5Heights 160, 165 and 180 cm. What is the ML estimate of the mean under a Gaussian model with known variance? Under a Laplace model?

Gaussian: the sample mean, \((160 + 165 + 180)/3 \approx 168.3\) cm. Laplace: the sample median, 165 cm. Same data, a different model, a different estimator.

Q6Prior Beta(2, 2), data 3 heads and 1 tail. Give the posterior, its mean, the MAP estimate and the ML estimate.

Posterior Beta\((2 + 3,\ 2 + 1)\) = Beta(5, 3). Mean \(5/8 = 0.625\). MAP \((5-1)/(5+3-2) = 4/6 \approx 0.667\). ML \(3/4 = 0.75\). The prior, centred on 0.5, pulls both Bayesian estimates toward 0.5.

Q7A colleague reports 100 earlier tosses with 80 heads. You encode this as a prior, then observe 2 heads and 8 tails. What is the posterior mean, and why does it barely move?

Prior Beta(80, 20), posterior Beta(82, 28), mean \(82/110 \approx 0.745\). The prior mean was 0.80 and the new data alone says 0.2. The prior counts as 100 ghost tosses against only 10 real ones, so it dominates.

Q8With no data at all, what does maximum likelihood give, and what does the Bayesian approach give?

Maximum likelihood gives nothing useful: with no observations the likelihood is 1 for every \(p\), so there is nothing to maximize. The Bayesian posterior is simply the prior, Beta\((\alpha + 0,\ \beta + 0)\), so the hunch is still there to use.