Estimators as Functions of Data#
An estimator is a function that maps a dataset to a guess about the unknown parameter . It is a random variable (the data are random), and we judge it by its sampling distribution — bias, variance, and MSE (Week 10).
Example estimators for the mean of a population:
- Sample mean: — standard choice.
- Sample median: robust to outliers, less efficient for normal data.
- Trimmed mean: discards extremes, a compromise.
The question is: which function of the data should we use? Maximum likelihood provides a principled answer: choose the that makes the observed data most probable.
Likelihood#
Given i.i.d. data , the likelihood function is the joint probability (density) viewed as a function of :
The log-likelihood is almost always easier to work with (sums are friendlier than products, and the log is monotonic so the maximiser is unchanged):
Critical distinction — likelihood vs probability:
- as a function of (with fixed) is a probability — it sums/integrates to 1 over .
- as a function of (with fixed) is the likelihood — it does not integrate to 1 over . "The likelihood of " is not "the probability that is true."
The MLEMaximum Likelihood Estimation is the value that maximises the likelihood:
Since is monotonic, maximising is equivalent to maximising .
Bernoulli MLE#
Suppose we observe i.i.d. Bernoulli trials with unknown success probability . The data are . Let be the number of successes.
The likelihood of given the data:
Log-likelihood:
Set derivative to zero:
The MLE for a Bernoulli probability is simply the sample proportion of successes — intuitively the most natural estimate.
Worked numeric example. 7 heads in 10 coin flips. Then . The log-likelihood at is:
At (fair coin hypothesis), , which is lower — the data favour over .
Gaussian MLE#
For i.i.d. data assumed to come from with both parameters unknown:
Joint density:
Log-likelihood:
MLE for : Differentiate with respect to (the first two terms do not involve ):
MLE for : Plug back and differentiate with respect to :
Note the denominator is , not . The MLE for variance is slightly biased downward (the version is unbiased — see Week 10). For large , the difference is negligible.
MLE and Training Objectives#
MLE provides a unified lens on many ML training objectives:
- Linear regression with Gaussian noise: Assuming , the negative log-likelihood is — exactly the mean squared error loss.
- Logistic regression: Binary labels modelled as Bernoulli with , the negative log-likelihood is the binary cross-entropy loss.
- Multi-class classification: Categorical likelihood gives the cross-entropy loss with softmax.
The deep connection (preview Week 11): Maximising likelihood is equivalent to minimising the KL divergence between the empirical distribution of the data and the model distribution . Training a generative model by MLE means: make the model's distribution as close as possible (in KL sense) to the data distribution.
Invariance of MLE: If is the MLE for , then is the MLE for for any one-to-one function . Example: the MLE for standard deviation is . This property does not hold for unbiased estimators in general.
You observe 15 heads in 25 independent coin flips. What is the MLE of the probability of heads? Enter as a decimal.
Knowledge check#
Why do we maximise log-likelihood instead of likelihood?
Browser lab#
Plot the log-likelihood of Bernoulli for a fixed dataset and mark the MLE.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(7)
n, p_true = 30, 0.65
data = rng.binomial(1, p_true, size=n)
k = data.sum()
def log_lik(p):
if p <= 0 or p >= 1:
return -np.inf
return k * np.log(p) + (n - k) * np.log(1 - p)
p_grid = np.linspace(0.01, 0.99, 200)
ll_vals = np.array([log_lik(p) for p in p_grid])
p_mle = k / n
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(p_grid, ll_vals, "tab:blue", linewidth=2, label="log-likelihood ℓ(p)")
ax.axvline(p_true, color="tab:green", linestyle="--", linewidth=1.5, label=f"True p={p_true}")
ax.axvline(p_mle, color="tab:red", linestyle="--", linewidth=1.5, label=f"MLE p̂={p_mle:.3f}")
ax.scatter([p_mle], [log_lik(p_mle)], color="tab:red", s=80, zorder=5)
ax.set_xlabel("p")
ax.set_ylabel("ℓ(p)")
ax.set_title(f"Log-likelihood of p — {k} heads in {n} flips")
ax.legend()
plt.tight_layout()
plt.show()
print(f"True p = {p_true}, observed k = {k}/{n}, MLE p̂ = {p_mle:.4f}")
print(f"ℓ(p̂) = {log_lik(p_mle):.3f}, ℓ(p_true) = {log_lik(p_true):.3f}")