Context:FIT2086_MOC Β· Studio 3 Β· what you do with ΞΈ^ after fitting β predict, then score the prediction on data the fit never saw Β· the first appearance of train/test thinking that becomes cross-validation later in the unit Β· extends R Toolkit (Cheatsheet) Β· data: train.csv (n=10 heights), test.csv (n=106)
Problem it solves: fit a distribution to a small sample, turn it into probability statements about the population, and check whether those probabilities were any good.
Quick Revision
π― Trigger: βfit a model and predict P(β )β β estimate ΞΈ^ on train β pnorm with ΞΈ^ β compare against empirical proportions on test β score with the out-of-sample negative log-likelihood.
β οΈ Key Constraint: every estimate comes from train only; test is touched only for scoring. Re-estimating on test destroys the whole point of the exercise.
π§ Minimal Working Example
# 1. Fit: all three estimates in one list-returning functionmy_estimates <- function(X) { n = length(X) retval = list() retval$mu_ml = sum(X)/n # sample mean e2 = (X - retval$mu_ml)^2 # squared deviations, reused twice retval$var_ml = sum(e2)/n # divisor n β biased retval$var_u = sum(e2)/(n-1) # divisor n-1 β unbiased return(retval)}train <- read.csv("train.csv"); test <- read.csv("test.csv")est <- my_estimates(train$heights)# 2. Predict: plug the estimates into the family β pnorm takes the SD, not the variance1 - pnorm(1.7, est$mu_ml, sqrt(est$var_ml)) # P(X > 1.7)pnorm(1.5, est$mu_ml, sqrt(est$var_u)) # P(X < 1.5)pnorm(1.75, est$mu_ml, sqrt(est$var_u)) - pnorm(1.6, est$mu_ml, sqrt(est$var_u))# 3. Ground truth: the empirical proportion in the held-out datamean(test$heights > 1.7) # logical vector β TRUE=1, FALSE=0 β mean = proportion# 4. Score: negative log-likelihood of the HELD-OUT data under the fitted modelnorm_negloglike <- function(y, mu, sigma) { n = length(y) return( (n/2)*log(2*pi*sigma^2) + 1/2/sigma^2*sum((y-mu)^2) )}norm_negloglike(test$heights, est$mu_ml, sqrt(est$var_ml))norm_negloglike(test$heights, est$mu_ml, sqrt(est$var_u))
Expected output:ΞΌ^βMLβ=1.6597, Ο^ML2β=0.00799, Ο^u2β=0.00888 β the unbiased estimate is always the larger, by the factor nβ1nβ=910β.
π Predicted vs Empirical (fit on n=10, scored on n=106)
Event
ML Ο^ML2β
Unbiased Ο^u2β
Empirical (test)
Verdict
P(X>1.7)
0.326
0.334
0.307
ML closer β both overestimate
P(X<1.5)
0.037
0.045
0.067
unbiased closer β both underestimate
P(1.6<X<1.75)
0.592
0.568
0.533
unbiased closer
Predictive NLL on test
β864,540.8
β874,975.6
β
unbiased wins (lower is better, β1%)
Final extracted output: the Ο^u2β model fits the future data better overall, because a wider density spreads probability onto the tail values a n=10 sample never showed β but that same widening is what makes it overshoot P(X>1.7). With n large the two estimates converge and the distinction evaporates.
π Variations
Two densities on one plot β scatter the sample on the x-axis, then overlay both fitted pdfs to see the width difference:
Why the NLL, not just the three probabilities β the NLL scores the model at every observed value at once rather than at three hand-picked thresholds; low probability assigned to values that actually occur is punished by βlog of a small number being large.
βοΈ Practice
Practice 1: fit Poi(Ξ») to a count vector y_train by ML, then report the predicted P(Xβ₯3) and compare it with the empirical proportion in y_test.
Key move: discrete boundaries β P(Xβ₯3)=P(X>2), so pass 2, not 3; the plug-in step is identical in shape to the normal case, only the family changes.
Practice 2: write a one-line R expression for the predictive NLL of y under Poi(Ξ»^) and say why it is comparable across models but not across datasets.
Reference solution
-sum(dpois(y, lam, log = TRUE)) # log=TRUE avoids underflow on the product
Key move: the NLL scales with n and with the units of y, so only differences between models on the same held-out data are meaningful β an absolute NLL value means nothing on its own.
β οΈ Common Mistakes
π‘ Passing the variance where R wants the SD β pnorm(1.7, mu, est$var_ml) silently models N(ΞΌ,(Ο2)2); every call needs sqrt(...).
π‘ Reading a negative NLL as an error β for continuous data the density can exceed 1 (here Οβ0.09), so βlogp goes negative; only differences between models carry meaning.
π‘ Estimating on the test set β test exists only to stand in for the unseen population; fitting on it makes the score self-congratulatory and, in reality, you never have it.
π‘ Trusting a cat() label over its argument β the released studio3.solns.R prints the var_ml figure under the label β(unbiased)β and vice versa; read which estimate is actually passed to the function.
π‘ Concluding βunbiased is betterβ in general β it won this comparison at n=10; the ranking is a property of this dataset and sample size, and both estimates converge as n grows.