SVM, Naive Bayes & Probabilistic Models
Level 3: Production Paper (From-Scratch Derivations & Explain-Out-Loud)
Time limit: 45 minutes Total marks: 60
Answer all questions. Show full working. For code questions, write clean pseudocode or Python; minor syntax slips are not penalised if logic is correct.
Q1. SVM Margin Derivation (12 marks)
Consider a linearly separable binary classification problem with decision boundary .
(a) Derive from scratch the expression for the geometric margin (distance from a point to the hyperplane). (3)
(b) Show why, after fixing the scale of so that support vectors satisfy , the total margin width equals . (3)
(c) Write the full primal hard-margin optimisation problem and explain why we minimise rather than maximise directly. (3)
(d) State the soft-margin primal with slack variables and explain the role of . (3)
Q2. Kernel Trick — Explain Out Loud (10 marks)
(a) State precisely what the "kernel trick" allows you to avoid computing, and the property a function must satisfy to be a valid kernel. (3)
(b) Show explicitly that the polynomial kernel for corresponds to an inner product in a higher-dimensional feature space, and write out . (4)
(c) Contrast the effect of the RBF kernel bandwidth being very large vs very small on the decision boundary and on overfitting. (3)
Q3. Gaussian Naive Bayes From Scratch (12 marks)
You are given a training set for a Gaussian Naive Bayes classifier over one feature and two classes.
Class A: values Class B: values Priors equal.
(a) State the Naive Bayes conditional independence assumption and the general classification rule (MAP). (2)
(b) Compute the per-class MLE mean and variance for feature . (4)
(c) A new point arrives. Compute the Gaussian likelihood under each class and determine the predicted class (show the ratio). (4)
(d) Explain why GNB can still misclassify even when the true class-conditional densities are exactly Gaussian. (2)
Q4. Multinomial NB for Text + Laplace Smoothing (14 marks)
A spam filter is trained on 4 documents:
| Doc | Class | Words |
|---|---|---|
| 1 | spam | free free money |
| 2 | spam | free win |
| 3 | ham | meeting money |
| 4 | ham | meeting today |
Vocabulary free, money, win, meeting, today .
(a) Compute class priors , . (2)
(b) Using Laplace (add-1) smoothing, compute and . Show the smoothed formula. (4)
(c) Classify the test document "free money" — compute the unnormalised posterior score for each class and give the prediction. (6)
(d) Explain in one or two sentences why Laplace smoothing is essential here. (2)
Q5. Bernoulli vs Multinomial & Support Vectors (8 marks)
(a) Explain the key difference between the Bernoulli and Multinomial Naive Bayes event models for text (what each counts / represents). (4)
(b) In a trained soft-margin SVM, define which training points are the support vectors in terms of their Lagrange multipliers , and explain why removing a non-support-vector point does not change the boundary. (4)
Q6. Explain-Out-Loud: Hyperparameters (4 marks)
Describe how you would jointly tune and for an RBF-SVM, and describe the qualitative behaviour you expect at the extremes (high /high vs low /low ).
Answer keyMark scheme & solutions
Q1 (12 marks)
(a) The signed distance from point to hyperplane is . Multiplying by label gives the (positive for correct) geometric margin . (1 for signed distance formula, 1 for projection reasoning, 1 for factor)
(b) With canonical scaling, closest points (support vectors) satisfy . The distance of a support vector = . The margin is bounded by the two nearest classes, one on each side, so total width . (1 canonical constraint, 1 single-side distance, 1 doubling)
(c) Maximising is equivalent to minimising , which is equivalent to minimising ; the squared form is smooth, convex, differentiable (quadratic program), unlike the norm/reciprocal. (1 objective, 1 constraint, 1 convexity reasoning)
(d) trades off margin width vs training violations: large penalises misclassifications heavily → narrow margin, low bias/high variance; small allows more slack → wider margin, more regularisation. (2 formulation, 1 role of C)
Q2 (10 marks)
(a) The kernel trick lets us compute inner products in a high-dimensional feature space without ever computing explicitly. Valid kernel: must be symmetric and produce a positive semi-definite Gram matrix (Mercer's condition). (1 avoid explicit mapping, 1 symmetry, 1 PSD/Mercer)
(b) For , : . This equals with . (2 expansion, 2 correct incl )
(c) Large → narrow Gaussians, each point influences only its neighbourhood → highly wiggly boundary → overfitting. Small → wide Gaussians → smooth, nearly linear boundary → risk of underfitting. (1 large, 1 small, 1 overfit link)
Q3 (12 marks)
(a) Assumption: features are conditionally independent given the class. Rule: . (1 + 1)
(b) Class A : mean . MLE variance . Class B : mean , variance (by symmetry). (mean A 1, var A 1, mean B 1, var B 1)
(c) . , same for both, so compare exponents. Class A: , exponent . Class B: , exponent . Ratio . Priors equal → predict Class A. (likelihood A 1, likelihood B 1, ratio 1, decision 1)
(d) Even with true Gaussian densities, NB uses estimated (MLE) parameters from finite data and, more fundamentally, points near the decision boundary (Bayes-error region) can fall on the wrong side; overlapping distributions always incur irreducible Bayes error. (2)
Q4 (14 marks)
(a) 2 spam, 2 ham → . (2)
(b) Smoothed: , . Spam word tokens: free,free,money,free,win → ; count(free,spam)=3. . Ham tokens: meeting,money,meeting,today → ; count(free,ham)=0. . (formula 1, spam 1.5, ham 1.5)
(c) Need too. count(money,spam)=1 → . count(money,ham)=1 → . Spam score . Ham score . → predict SPAM. (each P 1, each score 1.5, decision 1)
(d) Without smoothing, would zero out the entire ham posterior for any document containing "free," making unseen words catastrophic. Laplace smoothing removes zero probabilities. (2)
Q5 (8 marks)
(a) Multinomial: models word counts/frequencies; a document is a bag of tokens, feature = number of occurrences. Bernoulli: models binary presence/absence of each vocabulary word; explicitly accounts for words that are absent via terms. (2 + 2)
(b) Support vectors are training points with ; they lie on or inside the margin (). Since , points with contribute nothing to , so removing them leaves the solution unchanged. (2 definition, 2 reasoning via w)
Q6 (4 marks)
Use grid/random search with cross-validation over a log-spaced grid of (e.g. , ), pick the pair maximising CV accuracy/F1. (2) High + high → very tight, overfit boundary. Low + low → smooth, underfit boundary. Best models balance both. (2)
[
{"claim":"GNB variance for class A {2,4,6} is 8/3","code":"import statistics as st; vals=[2,4,6]; m=sum(vals)/3; var=sum((v-m)**2 for v in vals)/3; result = sympy.simplify(var-Rational(8,3))==0"},
{"claim":"GNB likelihood ratio at x=5 equals exp(1.5)","code":"expA=-Rational(1,2)*(5-4)**2/(Rational(8,3)); expB=-Rational(1,2)*(5-8)**2/(Rational(8,3)); result = sympy.simplify((expA-expB)-Rational(3,2))==0"},
{"claim":"P(free|spam)=0.4 and P(free|ham)=1/9 with Laplace","code":"pfs=Rational(3+1,5+5); pfh=Rational(0+1,4+5); result = (pfs==Rational(2,5)) and (pfh==Rational(1,9))"},
{"claim":"Spam score 0.04 > ham score 1/81 for 'free money'","code":"spam=Rational(1,2)*Rational(4,10)*Rational(2,10); ham=Rational(1,2)*Rational(1,9)*Rational(2,9); result = (spam==Rational(1,25)) and (spam>ham)"}
]