Before this lesson:lesson 03 (Bernoulli, Poisson,
exponential, Gaussian and gamma distributions), lesson 04
(covariance matrices are positive semidefinite; the sample variance S2) and
lesson 06 (the law of large numbers). From other tracks:
gradients and Hessians (calculus 02,
calculus 03); the KL divergence
(information theory 03) is defined again in section 3.
Time: about 75 minutes, plus the notebook.
Training losses are likelihoods. The mean squared error is the negative log-likelihood (NLL) of a Gaussian
model. Binary and softmax cross-entropy are the NLLs of Bernoulli and categorical models. Minimising them is
maximum likelihood (information theory 04).
Error bars and comparisons. Bias, variance and MSE are the language for comparing estimators: why the sample
variance divides by n−1, why a regularised (shrunk) estimate can beat the MLE, why a model evaluated with few
random seeds looks more stable than it is.
Generalised linear models. Linear, logistic and Poisson regression are maximum likelihood in an exponential
family. This is why their losses are convex and why their gradients all look like "prediction minus target".
Science. Fitting a gamma distribution to rainfall amounts, an exponential distribution to the waiting times
between events, or a Poisson rate to counts of photons or earthquakes is maximum likelihood.
You flip a coin n=10 times and see k=7 heads. Model: each flip is xi∼Bernoulli(p), independent,
with xi=1 for heads. The probability of the observed sequence is
P(x1,…,xn∣p)=i=1∏npxi(1−p)1−xi=pk(1−p)n−k.
Read this formula in two ways.
As a probability: fix p and vary the data. For p=0.7, it gives the probability of each possible
sequence of 10 flips. These numbers sum to 1 over all 210 sequences.
As a likelihood: fix the data (7 heads) and vary p. This gives a curve over p∈[0,1]:
L(p)=p7(1−p)3.
The likelihoodL(θ) is the probability (or density) of the observed data, seen as a function of the
parameter θ. It is the same formula, with a different variable. The maximum likelihood estimate is the
value of θ where L is largest.
Some values: L(0.7)=0.77⋅0.33≈0.00222 and L(0.5)=0.510≈0.00098. So the data are
about 2.3 times more probable under p=0.7 than under a fair coin. The number 0.00222 itself has no
meaning on its own; only ratios of likelihoods do.
Interactive figure: loading…
Flip the coin and watch the curve. Press Flip 10 a few times. The blue flips are the
data. The top panel is the orange likelihood L(p), divided by its maximum so that the
peak is at 1; the bottom panel is the log-likelihood ℓ(p)−ℓ(p^). The yellow line
marks the MLE p^=k/n, the dashed grey line the true p of the coin. Notice that the peak gets narrower
as n grows: more data pin p down. The readout gives the curvature −ℓ′′(p^); lesson 09 turns it into
an error bar.
Why we take logs. The likelihood of i.i.d. data is a product of n numbers, often much smaller than 1. For
1000 flips with 700 heads, L(0.7) is about 10−265; with 2000 flips the product underflows to 0 in double precision.
The log-likelihoodℓ(θ)=logL(θ) turns the product into a sum,
ℓ(p)=klogp+(n−k)log(1−p),
and has the same maximiser, because log is increasing. Set the derivative to zero:
ℓ′(p)=pk−1−pn−k=0⟹k(1−p)=(n−k)p⟹p^=nk.
Check that it is a maximum: ℓ′′(p)=−p2k−(1−p)2n−k<0 for all p∈(0,1). At
p^=0.7,ℓ′′(p^)=−0.497−0.093≈−47.6, which equals −p^(1−p^)n=−0.2110.
The size of this second derivative is the curvature of the peak. It grows in proportion to n.
The likelihood is not a density in p. The area under L(p) is ∫01p7(1−p)3dp=11!7!3!=13201,
not 1. To get a probability distribution over p you need a prior and Bayes' rule; that is
lesson 10.
Quick check
A coin gives 3 heads in 4 flips. Which statement is correct?
Why.
The likelihood of the sequence is p3(1−p), and 0.753⋅0.25≈0.105,0.54=0.0625. Only ratios of likelihoods carry meaning. The area under L is ∫01p3(1−p)dp=1/20, not 1: the likelihood is not a density over p.
Think of −logp(xi) as the "cost" of point i. A point far from μ (measured in units of σ) costs a
lot. A large σ makes far points cheap, but it makes every point pay the term logσ. The MLE balances
the two.
Interactive figure: loading…
Move μ and σ to make the total cost small. The blue points are the data; the
orange curve is the Gaussian model, and each stem shows the density p(xi) of a point. The
magenta bars below are the costs −logp(xi); the readout adds them up to the negative
log-likelihood. Try a tiny σ: the points near μ become cheap, the far ones very expensive. Press
Jump to MLE and compare with your best guess.
The minimum of the total cost is at μ^=xˉ and σ^2=n1∑i(xi−xˉ)2
(section 3 proves it). In words: the fitted Gaussian has the same mean and the same average squared deviation
as the data.
Quick check
You fit N(μ,σ2) by maximum likelihood to the data 1,2,6. What are μ^ and σ^2?
Why.
xˉ=9/3=3. The deviations are −2,−1,3, with squares summing to 14. The MLE divides by n=3:14/3≈4.67. Dividing by n−1=2 gives the unbiased S2=7, which is not the MLE. The median 2 is the MLE of the centre of a Laplace distribution, not of a Gaussian.
Setting. A model is a family of PMFs or densities p(x∣θ), indexed by a parameterθ∈Θ (a number or a vector). The data x1,…,xn are i.i.d. from p(x∣θ) for one true
value of θ.
The likelihood is L(θ)=∏i=1np(xi∣θ), a function of θ for fixed data.
The log-likelihood is ℓ(θ)=∑i=1nlogp(xi∣θ).
The maximum likelihood estimate is θ^MLE=argmaxθ∈Θℓ(θ), when the maximum
exists.
In practice, set the gradient ∇ℓ(θ) to zero (the likelihood equations) and check that the
stationary point is a maximum: a negative second derivative, or a negative definite Hessian
(calculus 03). Write xˉ=n1∑ixi.
Data
ℓ(θ), up to constants
Solve ℓ′(θ)=0
MLE
Bernoulli(p)
nxˉlogp+n(1−xˉ)log(1−p)
pnxˉ=1−pn(1−xˉ)
p^=xˉ
Poisson(λ)
nxˉlogλ−nλ
λnxˉ−n=0
λ^=xˉ
Exponential(λ), rate λ
nlogλ−λnxˉ
λn−nxˉ=0
λ^=1/xˉ
N(μ,v) with v=σ2
−2nlogv−2v1∑i(xi−μ)2
see below
μ^=xˉ,v^=n1∑i(xi−xˉ)2
The second derivatives are negative in all four cases: −λ2nxˉ (Poisson, when xˉ>0),−λ2n (exponential), and −p2nxˉ−(1−p)2n(1−xˉ) (Bernoulli).
Gaussian, step by step. We maximise over μ and over the variance v=σ2 (not σ; the answer is
the same by invariance, below).
∂μℓ=v1∑i(xi−μ). It is zero when ∑ixi=nμ, so μ^=xˉ, for every v.
∂vℓ=−2vn+2v21∑i(xi−μ^)2. Multiply by 2v2:−nv+∑i(xi−xˉ)2=0,
so v^=n1∑i(xi−xˉ)2.
It is a maximum: for fixed v,ℓ is a downward parabola in μ. With μ=xˉ and D=∑i(xi−xˉ)2>0,
the function −2nlogv−2vD increases for v<D/n and decreases after, so v=D/n is its maximum. (If all xi
are equal, D=0 and ℓ→∞ as v→0: no MLE exists.)
Invariance of the MLE. Let ϕ=g(θ) for a one-to-one map g. The likelihood in the new parameter is
L~(ϕ)=L(g−1(ϕ)). It takes the same values, only relabelled, so its maximum is at
ϕ^=g(θ^). Examples: the MLE of σ is v^; the MLE of the log-odds log1−pp
is log1−p^p^; the MLE of the mean waiting time 1/λ is 1/λ^=xˉ.
An estimator is a rule θ^=θ^(X1,…,Xn) that turns data into a guess. Before the data are
drawn, θ^ is a random variable with its own distribution, the sampling distribution. For a true value
θ:
the bias is bias(θ^)=Eθ[θ^]−θ; the estimator is unbiased if the bias is 0
for every θ;
the variance is Varθ(θ^), and its square root is the standard error;
the mean squared error is MSE(θ^)=Eθ[(θ^−θ)2].
A dartboard is the usual picture: the bias is how far the centre of the darts is from the bull's-eye, the variance
is how spread out the darts are, and the MSE is the average squared distance to the bull's-eye.
Write m=E[θ^] and b=m−θ (a constant). Split the error:
θ^−θ=(θ^−m)+b.
Square: (θ^−θ)2=(θ^−m)2+2b(θ^−m)+b2.
Take expectations. E[θ^−m]=0, so the middle term vanishes, and
E[(θ^−θ)2]=Var(θ^)+b2.■
Example: three ways to estimate a variance. Let X1,…,Xn be i.i.d. N(μ,σ2) and
D=∑i(Xi−Xˉ)2. Lesson 04 showed E[D]=(n−1)σ2. For Gaussian data,
D/σ2 has a chi-squared distribution with n−1 degrees of freedom, so Var(D)=2(n−1)σ4.
Consider the estimators D/c:
Divide by
Name
Bias
Variance
MSE at n=10,σ2=1
c=n−1
S2, unbiased
0
n−12σ4
0.222
c=n
σ^MLE2
−nσ2
n22(n−1)σ4
0.190
c=n+1
−n+12σ2
(n+1)22(n−1)σ4
0.182
The bias of the MLE comes from E[D/n]=nn−1σ2. But the biased estimators have a smaller MSE:
they pay a little bias for a larger cut in variance. For Gaussian data, c=n+1 gives the smallest MSE of all
estimators of the form D/c. Unbiased is not the same as best.
Interactive figure: loading…
Throw darts: draw samples and compare three estimators. Each press of Draw 500 simulates 500 samples of size
n from N(0,1), so the true variance is σ2=1 (dashed orange line). Each
blue dot is one estimate; the black tick is the average of the dots in its row. The table
compares the simulated bias, variance and MSE with the exact formulas. Notice that the unbiased row is centred on 1
but has the widest spread. Then move n: all three rows shrink toward 1 (consistency), and their differences vanish.
Another bias: a nonlinear function. For exponential data, Xˉ is unbiased for the mean 1/λ. But
λ^=1/Xˉ is not unbiased for λ. The sum ∑iXi has a Gamma(n,λ) distribution
(lesson 03), and a short integral gives, for n≥3,
At n=10 and λ=1: mean 1.111, variance 0.154, MSE 0.167. The bias is upward, as Jensen's inequality
predicts: t↦1/t is convex, so E[1/Xˉ]≥1/E[Xˉ]=λ.
Consistency. A sequence of estimators θ^n (one for each sample size) is consistent if
θ^n→θ in probability for every θ:P(∣θ^n−θ∣>ε)→0 for every ε>0.
MSE → 0 is enough. By Markov's inequality applied to (θ^n−θ)2 (lesson 06),
P(∣θ^n−θ∣>ε)≤MSE(θ^n)/ε2→0.
Xˉ is consistent (law of large numbers). σ^MLE2 is biased but consistent: its bias −σ2/n
and its variance both go to 0. And 1/Xˉ is consistent for λ, because t↦1/t is continuous at
1/λ (continuous mapping).
Unbiased does not imply consistent: X1 alone is an unbiased estimate of the mean, but its spread never shrinks.
Under regularity conditions, the MLE is consistent. The KL view below explains why.
Quick check
For a coin with true p=0.6 and n=10 flips, compare the MLE p^=k/n with the silly estimator p~=0.5 that ignores the data. Which has the smaller MSE?
Why.
A constant has variance 0, so its MSE is its squared bias, (0.5−0.6)2=0.01. The MLE is unbiased with variance p(1−p)/n=0.24/10=0.024. At p=0.9 the order flips: 0.16 against 0.009. MSE comparisons depend on the true θ; no estimator wins everywhere.
The Kullback–Leibler divergence between distributions p and q is
KL(p∥q)=EX∼p[logq(X)p(X)]≥0,
with equality only if p=q (Gibbs' inequality, a consequence of Jensen's inequality;
information theory 03). Let pdata be the true distribution of the
data, and pθ the model.
By the law of large numbers, n1ℓ(θ)=n1∑ilogpθ(Xi)→Epdata[logpθ(X)].
Add and subtract logpdata:Epdata[logpθ(X)]=Epdata[logpdata(X)]−KL(pdata∥pθ).
The first term does not depend on θ. So maximising the expected log-likelihood is the same as minimising
KL(pdata∥pθ), and the same as minimising the cross-entropy−Epdata[logpθ(X)].
For finite n the same identity holds exactly with the empirical distributionp^n (mass 1/n on each data
point) in place of pdata: the MLE minimises the cross-entropy between p^n and pθ.
Three consequences:
Consistency. If the model is correct (pdata=pθ0) and identifiable (different θ give
different distributions), the KL is zero only at θ0. So the limit of n1ℓ has its unique maximum at
the truth. (Turning this into θ^n→θ0 needs a uniform law of large numbers or concavity.)
Misspecification. If no θ fits exactly, the MLE still converges, to the θ∗ that minimises
KL(pdata∥pθ): the closest model in this sense.
Losses are NLLs. The average negative log-likelihood is the training loss:
Model for the target y given input x
Negative log-likelihood of one example
Name of the loss
y∼N(f(x),σ2),σ fixed
2σ2(y−f(x))2+const
mean squared error
y∼Bernoulli(σ(z(x))), logit z
−ylogσ(z)−(1−y)log(1−σ(z))
binary cross-entropy
y∼Categorical(softmax(z(x)))
−logsoftmax(z)y
softmax cross-entropy
y∼N(μ(x),σ(x)2), both predicted
21logσ2+2σ2(y−μ)2+const
Gaussian NLL
Quick check
A regression network minimises the mean absolute error n1∑i∣yi−f(xi)∣. For which noise model is this maximum likelihood?
Why.
The Laplace NLL of one example is ∣y−f(x)∣/b+log(2b). With b fixed, minimising it is minimising the absolute error. The Gaussian NLL gives the squared error. Every loss of the form "a function of the residual" is the NLL of some noise model, as long as e−loss can be normalised.
The log-probability is linear in the datax, with the coefficient η=log1−pp, the log-odds. The
rest, log(1−p), does not depend on x. Solve for p:p=1+eηeη=σ(η), the sigmoid
(foundations 05). So log(1−p)=−log(1+eη), and
p(x∣η)=exp(ηx−A(η)),A(η)=log(1+eη).
Differentiate A:A′(η)=1+eηeη=p=E[X], and A′′(η)=p(1−p)=Var(X).
The derivatives of the normaliser are the mean and the variance. This is not a coincidence.
Definition. A family of densities (or PMFs) is an exponential family if it can be written as
A is convex. A covariance matrix is positive semidefinite (lesson 04), so ∇2A⪰0.
The log-likelihood is concave in η. For i.i.d. data,
ℓ(η)=∑ilogh(xi)+η⊤∑iT(xi)−nA(η): a linear function minus n times a convex one.
So any stationary point is a global maximum.
The MLE matches moments.∇ℓ(η)=∑iT(xi)−n∇A(η)=0 gives
Eη^[T(X)]=n1i=1∑nT(xi).
The fitted model reproduces the sample averages of the sufficient statistic. For the Gaussian, T=(x,x2): the
fitted model has the data's mean and mean square, which is exactly μ^=xˉ,σ^2=x2−xˉ2.
All four rows of the MLE table above are this rule.
Gamma: matching needs a computer. For the gamma, ∇A=(ψ(k)−logβ,k/β)=(E[logX],E[X]),
where ψ=Γ′/Γ is the digamma function. So the MLE solves
ψ(k^)−logβ^=n1i∑logxi,β^k^=xˉ.
Put β^=k^/xˉ into the first equation: logk^−ψ(k^)=logxˉ−n1∑ilogxi. The right
side is ≥0 by Jensen's inequality (log is concave). There is no closed form; Newton's method solves it in a few
steps (notebook exercise 3).
Interactive figure: loading…
Fit a gamma by matching two averages. The blue histogram is a data set of 200 positive
numbers; the orange curve is the gamma density with your shape k and rate β. The two
readout rows compare the model's E[logX] and E[X] with the data's averages of logx and x.
Move the sliders until both rows match, then press Jump to MLE: the MLE is the one point where both match, and
there the average log-likelihood is largest.
Sufficiency. A statistic T(X) is sufficient for θ if the conditional distribution of the data given
T(X) does not depend on θ. Once you know T, the rest of the data tells you nothing more about θ.
Theorem (factorisation, Fisher–Neyman).T is sufficient for θ if and only if
p(x∣θ)=g(T(x),θ)h(x) for some functions g and h.
Proof (discrete case)
(⇐) Fix x and let t=T(x). Then
Pθ(X=x∣T=t)=∑x′:T(x′)=tg(t,θ)h(x′)g(t,θ)h(x)=∑x′:T(x′)=th(x′)h(x),
which does not depend on θ.
(⇒) Take g(t,θ)=Pθ(T=t) and h(x)=P(X=x∣T=T(x)), which is free of θ by sufficiency. Then
p(x∣θ)=Pθ(T=T(x))P(X=x∣T=T(x))=g(T(x),θ)h(x).■
For i.i.d. data from an exponential family,
p(x1,…,xn∣η)=(∏ih(xi))exp(η⊤∑iT(xi)−nA(η)), so ∑iT(xi) (with n)
is sufficient. Its size d does not grow with n. Coin: the number of heads. Gaussian: (∑ixi,∑ixi2). Gamma:
(∑ilogxi,∑ixi). The same structure gives every exponential family a conjugate prior
(lesson 10).
Quick check
You fit a Poisson model to counts by maximum likelihood. Which equation does λ^ solve, according to the exponential-family theorem?
Why.
For the Poisson, T(x)=x, so the MLE matches E[X]=λ with xˉ. The Poisson variance is also λ, but the MLE does not use S2; if the sample variance is much larger than xˉ (overdispersion), the model is wrong, and the MLE still matches the mean. The natural parameter is logλ, and invariance gives η^=logxˉ, not xˉ.
Zero counts. A new web page variant has k=0 conversions in n=20 visits. The MLE is p^=0: "this page
never converts". In the natural parameter, η^=log10=−∞: the maximum is not attained. The same
happens for all-zero Poisson counts and for logistic regression on data that a line separates perfectly (the
weights grow without bound). A prior fixes this (lesson 10).
No unique maximum. If the model is not identifiable, a whole set of θ values gives the same likelihood.
Small samples. The MLE can be biased (the variance by (n−1)/n, the rate by n/(n−1)); the bias shrinks like 1/n.
An unbounded likelihood: Gaussian mixtures
Fit the mixture 21N(μ1,σ12)+21N(μ2,σ22) to data x1,…,xn with at least two
different values. Put μ1=x1 and let σ1→0. The density at x1 contains the term
22πσ11, which goes to infinity. Every other point keeps a density of at least
21N(xi;μ2,σ22)>0 from the second component. So L→∞: the "MLE" is a spike on one data
point, a useless model. In practice, EM for Gaussian mixtures needs a floor on the variances or a prior on them
(this is MAP estimation, lesson 10). The KL view does not apply here, because no θ achieves the supremum.
Answers to section 1. (1) Too small, by the factor (n−1)/n=0.9:E[σ^2]=0.9σ2. The 1/n
variance of 10 seeds is 10% low on average. Divide by n−1=9 for an unbiased estimate. (2) By invariance, the MLE of
θ=1/λ is 1/λ^=xˉ. But λ^=1/xˉ is biased upward: E[λ^]=n−1nλ.
The MLE keeps its form under a change of parameter; unbiasedness does not survive a nonlinear map.
(a) A kitchen scale. You weigh a 1 kg reference weight four times. The readings minus 1000 g are
x=(2.1,−0.3,3.0,0.4) g. Model: xi∼N(μ,σ2) i.i.d., where μ is the scale's offset and σ its noise.
Sufficient statistics.∑ixi=2.1−0.3+3.0+0.4=5.2 and
∑ixi2=4.41+0.09+9.00+0.16=13.66. For this model, nothing else in the data matters.
MLE of the offset.μ^=5.2/4=1.3 g.
MLE of the variance by moment matching on x2.μ^2+σ^2=13.66/4=3.415, so
σ^2=3.415−1.69=1.725 g².
Check with the deviations.xi−xˉ=(0.8,−1.6,1.7,−0.9). Their squares are 0.64,2.56,2.89,0.81, with sum
6.9. And 6.9/4=1.725 ✓.
Bias.E[σ^2]=43σ2: with only 4 readings, the MLE is 25% low on average. The unbiased estimate is
S2=6.9/3=2.3 g².
Invariance. The MLE of the noise standard deviation is σ^=1.725≈1.313 g.
(b) Waiting times. A help desk records the times between 5 calls: their sum is S=10 minutes. Model: i.i.d.
Exponential(λ).
ℓ(λ)=nlogλ−λS=5logλ−10λ.
ℓ′(λ)=5/λ−10=0 gives λ^=0.5 calls per minute. ℓ′′(λ)=−5/λ2<0: a maximum.
Invariance: the MLE of the mean waiting time is 1/λ^=2 minutes =xˉ.
Bias: E[λ^]=n−1nλ=1.25λ. With 5 observations, the rate estimate is 25% too high on average.
nn−1λ^=Sn−1=0.4 is unbiased.
(c) The coin in natural form. For k=7 heads in n=10 flips, the log-likelihood in the log-odds η is
ℓ(η)=7η−10log(1+eη).
ℓ′(η)=7−10σ(η)=0 gives σ(η^)=0.7: the model mean matches the sample mean.
η^=log0.30.7≈0.847, which is log1−p^p^ (invariance).
ℓ′′(η)=−10σ(η)(1−σ(η))=−2.1 at η^, negative for every η:ℓ is concave in η.
NumPy and SciPy check the worked example, the bias of the variance estimators and the gamma moment matching. The
notebook has these cells and the exercises.
importnumpyasnpfromscipyimportstats,special# (a) kitchen scale: MLE mean, MLE variance (ddof=0) and the unbiased S^2 (ddof=1)x=np.array([2.1,-0.3,3.0,0.4])print(x.mean(),x.var(),round(x.var(ddof=1),6))# 1.3 1.725 2.3# predict-first 1: 10 seeds; the 1/n estimate averages 0.9 * sigma^2rng=np.random.default_rng(1)runs=rng.normal(0.80,0.02,size=(200_000,10))# accuracy 0.80, sd 0.02print(round(runs.var(axis=1).mean()/0.02**2,3))# 0.898 (exact: 0.9)# bias of 1/xbar for exponential data, n = 10, lambda = 1 (exact: mean 1.111, MSE 0.167)lam_hat=1/rng.exponential(1.0,size=(400_000,10)).mean(axis=1)print(round(lam_hat.mean(),3),round(((lam_hat-1)**2).mean(),3))# 1.111 0.166# gamma MLE = moment matching on T = (log x, x); NumPy and SciPy use scale = 1 / rateg=np.random.default_rng(3).gamma(shape=2.0,scale=1/0.5,size=200)k_hat,_,scale_hat=stats.gamma.fit(g,floc=0)b_hat=1/scale_hatprint(np.isclose(special.digamma(k_hat)-np.log(b_hat),np.log(g).mean()))# Trueprint(np.isclose(k_hat/b_hat,g.mean()))# True
The printed Monte Carlo values move a little with the seed; their errors are about ±0.002 here. The exact
values come from the formulas of section 3.
Every supervised loss. Mean squared error, binary and softmax cross-entropy are Gaussian, Bernoulli and categorical
NLLs. For the categorical, the logits z are the natural parameters, A(z)=log∑kezk and
∇A=softmax(z). So the gradient of the loss with respect to the logits is "softmax minus one-hot", the
rule of calculus 06.
Generalised linear models. With the canonical link, ηi=w⊤xi, and the NLL is
∑i[A(w⊤xi)−yiw⊤xi]+const: convex in w, with gradient
∑i(y^i−yi)xi, where y^i=A′(w⊤xi). Linear, logistic and Poisson regression have the same form.
Language models. Pretraining minimises the average NLL of the next token: the MLE of a categorical model.
Perplexity is eaverage NLL.
Models that predict their own uncertainty. Heteroscedastic regression, probabilistic weather models and
surrogates of simulators predict a mean and a variance and minimise the Gaussian NLL.
Science. Gamma fits of rainfall amounts (the Standardized Precipitation Index), exponential fits of the magnitudes
of earthquakes above a threshold (Aki's estimator of the Gutenberg–Richter b-value is an exponential MLE), Poisson
rates of photon counts in astronomy.
Evaluation. Reporting the variance of a metric over a handful of seeds or folds with the 1/n formula
underestimates it; with 3 seeds the factor is 2/3.
Treating the likelihood as a probability of θ.L(θ) is the probability of the data. It does not integrate to 1
over θ, and "the probability that p=0.7" needs a prior (lesson 10).
Thinking unbiased means best. Compare MSEs. The 1/n and 1/(n+1) variance estimators beat the unbiased S2
in MSE for Gaussian data.
Expecting unbiasedness to survive a transformation.S2 is unbiased for σ2, but S is biased for σ
(Jensen). The MLE does survive transformations (invariance).
Differentiating A in the wrong parameters.∇A=E[T] holds for the natural parameter η, not for
(μ,σ) or (k,β).
Mixing up rate and scale. These lessons write the exponential and gamma with the rate. NumPy and SciPy use the
scale 1/β.
Assuming the MLE always exists. It does not for all-zero counts, separable data in logistic regression, constant
data in a Gaussian or gamma fit, or Gaussian mixtures.
Maximising the likelihood with a product. Multiply 1000 probabilities and you get 0 in floating point. Always sum
log-probabilities.
Write the exponential distribution in exponential-family form, and get its mean and variance from A.Answerp(x)=λe−λx=exp(−λx+logλ) on x≥0. So η=−λ,T(x)=x,h(x)=1 on x≥0, and A(η)=−logλ=−log(−η). Then A′(η)=−1/η=1/λ=E[X] and A′′(η)=1/η2=1/λ2=Var(X).
A website gets x=(5,7,4,6,3) sign-ups in five hours, modelled as i.i.d. Poisson(λ). Give the MLE, check that it is a
maximum, and give the MLE of P(no sign-up in an hour)=e−λ.Answerℓ(λ)=25logλ−5λ+const, so ℓ′(λ)=25/λ−5=0 gives λ^=5.ℓ′′(λ)=−25/λ2<0: a maximum. By invariance, the MLE of e−λ is e−5≈0.0067.
S2 is unbiased for σ2. Is S unbiased for σ?AnswerNo. The square root is strictly concave, so by Jensen E[S]<E[S2]=σ (strict, because S is not constant). For Gaussian data with n=5,E[S]≈0.94σ.
Which of these estimators of a mean are consistent? (a) X1; (b) Xˉ+5/n; (c) Xˉ for data from a Cauchy
distribution centred at m, as an estimator of m.Answer(a) No: it is unbiased, but its distribution does not change with n. (b) Yes: the bias 5/n→0 and the variance σ2/n→0, so the MSE goes to 0. (c) No: the mean of n i.i.d. Cauchy variables has the same Cauchy distribution for every n. The Cauchy has no mean, so the law of large numbers does not apply. The sample median is consistent for m.
Show that the binary cross-entropy of a logistic model, −∑i[yilogσ(zi)+(1−yi)log(1−σ(zi))], is the
negative Bernoulli log-likelihood, and give its derivative with respect to one logit zi.AnswerThe Bernoulli PMF with pi=σ(zi) is piyi(1−pi)1−yi; its log is yilogpi+(1−yi)log(1−pi). Sum and negate: that is the loss. In natural form, the term is −(yizi−log(1+ezi)), so the derivative is σ(zi)−yi: prediction minus target.
What are the sufficient statistics of a gamma sample? Why does a gamma fit fail for constant data?AnswerT(x)=(logx,x), so (∑ilogxi,∑ixi), or the geometric and the arithmetic mean. The shape equation is logk−ψ(k)=logxˉ−logx. For constant data the right side is 0 (the two means are equal), and logk−ψ(k)>0 for every k>0, decreasing to 0 only as k→∞. So no MLE exists: the fit wants a spike of infinite shape.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
What is the likelihood L(θ)?
The probability (or density) of the observed data as a function of the parameter: L(θ)=∏ip(xi∣θ) for i.i.d. data. It is not a density over θ.
MLE of a Bernoulli p from k successes in n trials?
p^=k/n, from ℓ′(p)=k/p−(n−k)/(1−p)=0.
Gaussian MLEs of μ and σ2?
μ^=xˉ and σ^2=n1∑i(xi−xˉ)2 (divide by n, not n−1).
State the bias–variance decomposition of the MSE.
E[(θ^−θ)2]=(Eθ^−θ)2+Var(θ^); the cross term vanishes because E[θ^−Eθ^]=0.
What is E[σ^MLE2] for n Gaussian observations?
nn−1σ2. For n=10 it is 0.9σ2.
What does consistency of an estimator mean, and what is a sufficient condition?
θ^n→θ in probability for every θ.MSE(θ^n)→0 is enough (Markov's inequality).
Why does maximum likelihood minimise a KL divergence?
Epdata[logpθ]=−H(pdata)−KL(pdata∥pθ), and the entropy term does not depend on θ.
Write the general exponential-family form.
p(x∣η)=h(x)exp(η⊤T(x)−A(η)): natural parameter η, sufficient statistic T, base measure h, log-partition A.
What are ∇A(η) and ∇2A(η) in an exponential family?
∇A=Eη[T(X)] and ∇2A=Covη[T(X)]. So A is convex and the MLE matches the mean of T.
What is the invariance property of the MLE?
If ϕ=g(θ) with g one-to-one, then ϕ^MLE=g(θ^MLE). Example: the MLE of 1/λ is 1/λ^.
"Derive the MLE of a Gaussian. Why does the unbiased variance divide by n−1?" — μ^=xˉ and σ^2=n1∑i(xi−xˉ)2;
because E∑i(Xi−Xˉ)2=(n−1)σ2: the deviations are measured from Xˉ, which is fitted to the same data.
"Mean squared error and cross-entropy: which likelihoods?" — Gaussian with a fixed variance; Bernoulli or categorical.
Minimising them is maximum likelihood, which minimises KL(pdata∥pθ).
"Is an unbiased estimator always better?" — No. MSE=bias2+variance; shrinkage (ridge, MAP, James–Stein) trades bias for
variance.
"Why is the logistic-regression loss convex?" — It is an exponential-family NLL with a natural parameter linear in w:
the convex A(w⊤xi) minus a linear term.
"Your logistic regression weights blow up. Why?" — The data are linearly separable, so the likelihood keeps increasing
as ∥w∥→∞; the MLE does not exist. Add L2 regularisation (a prior).
"What is a sufficient statistic? Give one for a Gaussian." — A summary that carries all the information about θ;(∑ixi,∑ixi2).