← All posts
Machine Learning · Handwritten Notes · Note 1 of 8

MLE, MAP, and Bayesian Prediction

Expanded from MLE.pdf · View a handwritten page

We observe five coin tosses. Four are heads. What should we believe about the next toss? This one question takes us through likelihood, maximum likelihood estimation, priors, MAP, and Bayesian prediction. We will keep the observations fixed so that each new idea has something familiar to attach to.

Start with the observations

Our teaching data. H means heads and T means tails.
Toss12345
OutcomeHHTHH

Let $\theta$ be the coin's chance of heads. It is an unknown number between 0 and 1. For this model, tosses are independent once $\theta$ is fixed, and the same $\theta$ applies to every toss. A candidate $\theta=0.8$ gives each observed head probability 0.8 and the observed tail probability 0.2.

Probability of our particular sequence, if $\theta=0.8$:
0.8 × 0.8 × 0.2 × 0.8 × 0.8 = 0.08192.

Now keep the observed sequence fixed and vary the candidate parameter. This gives the likelihood: a score for how well each candidate explains the observations.

$$L(\theta)=\theta^4(1-\theta).$$
The data stay the same in every row.
Candidate $\theta$Likelihood of our sequence
0.50.03125
0.80.08192
0.90.06561

The 0.8 candidate scores best among these three. Moving to 0.9 helps explain the four heads but makes the observed tail harder to explain. Likelihood balances both. A likelihood value is not the probability that the parameter itself is correct.

If we record only the count “four heads in five tosses,” there are five possible positions for the tail. The binomial likelihood is therefore $5\theta^4(1-\theta)$. Multiplying every candidate's score by the same 5 leaves the best candidate unchanged.

Find the best likelihood

Maximum likelihood estimation, or MLE, selects the parameter with the largest likelihood. With $h$ observed heads and $t$ observed tails, where $h+t>0$, the likelihood of the observed sequence is:

$$L(\theta)=\theta^h(1-\theta)^t.$$

The counts $h$ and $t$ are fixed observations. We vary only $\theta$:

$$\hat\theta_{\mathrm{MLE}} =\underset{0\le\theta\le1}{\arg\max} \left[\theta^h(1-\theta)^t\right].$$

When both counts are positive, the maximum lies between 0 and 1. Taking the logarithm preserves the maximizer and turns the product into a sum:

$$\ell(\theta)=h\log\theta+t\log(1-\theta).$$

Differentiate with respect to $\theta$ and set the result to zero:

$$\ell'(\theta)=\frac{h}{\theta}-\frac{t}{1-\theta}=0.$$

Multiplying by $\theta(1-\theta)$ gives $h(1-\theta)=t\theta$. Collect the terms containing $\theta$ to get $h=\theta(h+t)$, then divide:

$$\hat\theta_{\mathrm{MLE}}=\frac{h}{h+t}.$$

The second derivative is negative when both counts are positive, so this stationary point is a maximum. If $t=0$, the maximum is at the endpoint $\theta=1$; if $h=0$, it is at $\theta=0$. Those confident endpoints are one reason we might want to incorporate information beyond a tiny sample.

Add a prior, then update it

A Bayesian model represents uncertainty about $\theta$ with a distribution. The prior describes that uncertainty before these observations. The posterior describes it after using them:

$$ \underbrace{p(\theta\mid D)}_{\text{posterior}} \propto \underbrace{p(D\mid\theta)}_{\text{likelihood}}\, \underbrace{p(\theta)}_{\text{prior}}. $$

Here $D$ means our data. The missing proportionality constant makes the posterior integrate to one. It is the same for every $\theta$, so we can ignore it while finding the posterior's highest point.

A Beta prior is convenient for a probability because it lives between 0 and 1. Its two positive parameters, $\alpha$ and $\beta$, control its shape:

$$p(\theta)\propto\theta^{\alpha-1}(1-\theta)^{\beta-1}.$$

Choose $\alpha=\beta=2$. This prior is symmetric and favors values near 0.5 over values near either endpoint. By comparison, Beta(1, 1) is flat: its density is equal throughout the interval. It does not concentrate around 0.5.

Multiplying the prior by the likelihood adds the exponents. Our four heads and one tail turn Beta(2, 2) into Beta(6, 3). In general:

$$\theta\mid D\sim\operatorname{Beta}(\alpha+h,\;\beta+t).$$

MAP chooses the posterior's highest point

Maximum a posteriori estimation, or MAP, selects the parameter where the posterior density is highest. With $h$ observed heads, $t$ observed tails, and a Beta$(\alpha,\beta)$ prior, the posterior is proportional to:

$$\theta^{h+\alpha-1}(1-\theta)^{t+\beta-1}.$$

The counts $h$ and $t$ are fixed observations; $\alpha$ and $\beta$ are fixed prior parameters. We vary only $\theta$:

$$\hat\theta_{\mathrm{MAP}} =\underset{0<\theta<1}{\arg\max} \left[\theta^{h+\alpha-1}(1-\theta)^{t+\beta-1}\right].$$

Taking the logarithm preserves the maximizer. Its terms that depend on $\theta$ are:

$$\begin{aligned} &(h+\alpha-1)\log\theta\\ &\quad +(t+\beta-1)\log(1-\theta). \end{aligned}$$

Differentiate with respect to $\theta$ and set the result to zero:

$$ \frac{h+\alpha-1}{\theta} -\frac{t+\beta-1}{1-\theta}=0. $$

Multiplying by $\theta(1-\theta)$ gives $(h+\alpha-1)(1-\theta)=(t+\beta-1)\theta$. Collect the terms containing $\theta$ and divide to obtain:

$$\hat\theta_{\mathrm{MAP}}= \frac{h+\alpha-1}{h+t+\alpha+\beta-2}.$$

This interior maximum exists when $h+\alpha>1$ and $t+\beta>1$; the log posterior is then strictly concave. Otherwise, check the boundary behavior or whether the density is flat. The offsets $\alpha-1$ and $\beta-1$ show how the prior changes the observed counts in the MAP formula.

A symmetric Beta(2,2) prior peaks at 0.5. The updated Beta(6,3) density peaks near 0.714; the likelihood peaks at 0.8.
The observations favor a heads-heavy coin. The prior moderates how far five tosses move our estimate. The likelihood is scaled only for this visual comparison.

Use MLE when you have ample data relative to the model's complexity and no strong prior information. It fits the model using the likelihood alone, so there is no prior to specify.

Use MAP when data are limited or useful prior knowledge is available. A sensible prior can stabilize estimates and reduce extreme fits; in regression, this becomes regularization. The trade-off is that the prior influences the answer. With enough informative data, a fixed prior often matters less.

Predict the next toss

We have a whole posterior distribution, so we can average predictions over it. A candidate $\theta=0.6$ predicts heads with probability 0.6; a candidate 0.9 predicts 0.9. Weight every candidate's prediction by its posterior density.

$$P(H_{\mathrm{next}}\mid D)= \int_0^1\theta\,p(\theta\mid D)\,d\theta.$$

For a Beta posterior, this integral is its mean. In our example:

Posterior predictive chance of heads
= (4 + 2) / (5 + 2 + 2) = 6/9 = 2/3.
Three answers to three related questions, using the same observations.
MethodResultWhat we calculated
MLE0.8000Best likelihood parameter
MAP0.7143Highest posterior-density parameter
Bayesian prediction0.6667Average next-toss prediction over the posterior

A Beta(1, 1) prior would make MAP equal MLE here, while its posterior predictive probability would be 5/7. A flat prior therefore does not make every Bayesian answer identical to MLE. As the number of tosses grows with the prior fixed, the observed counts increasingly dominate these estimates.

Check your understanding: what if both observed tosses are heads?

MLE is 1. With Beta(2, 2), the posterior is Beta(4, 2), MAP is 3/4, and the next-head probability is 4/6. Two observations have changed our belief without eliminating uncertainty about tails.

From the handwritten notes

This guide expands MLE.pdf with worked tables and intermediate steps. The PDF contains all the original pages.

View original page 3
Original handwritten page showing a Beta prior, Bayes rule, posterior exponents and the MAP estimator.
MLE.pdf, page 3: the prior-to-posterior calculation. The worked coin example above separates the posterior mode from the posterior predictive mean. Select the image to open the full-size scan.

Further reading:

What MLE and MAP maximize

The expressions name the quantities; MLE and MAP choose parameter values by maximizing them.
ExpressionNameWhat we do
$P(D\mid\theta)$LikelihoodMLE chooses the $\theta$ that maximizes it.
$P(\theta\mid D)$PosteriorMAP chooses the $\theta$ that maximizes it.

Think of the likelihood and posterior as curves over possible parameter values. The MLE and MAP estimates are the parameter values at their respective peaks.