Level 3 — ProductionSVM, Naive Bayes & Probabilistic Models

SVM, Naive Bayes & Probabilistic Models

45 minutes60 marksprintable — key stays hidden on paper

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 wx+b=0\mathbf{w}^\top \mathbf{x} + b = 0.

(a) Derive from scratch the expression for the geometric margin (distance from a point xi\mathbf{x}_i to the hyperplane). (3)

(b) Show why, after fixing the scale of (w,b)(\mathbf{w}, b) so that support vectors satisfy yi(wxi+b)=1y_i(\mathbf{w}^\top\mathbf{x}_i + b) = 1, the total margin width equals 2w\frac{2}{\lVert\mathbf{w}\rVert}. (3)

(c) Write the full primal hard-margin optimisation problem and explain why we minimise 12w2\frac{1}{2}\lVert\mathbf{w}\rVert^2 rather than maximise 2w\frac{2}{\lVert\mathbf{w}\rVert} directly. (3)

(d) State the soft-margin primal with slack variables ξi\xi_i and explain the role of CC. (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 K(x,z)K(\mathbf{x}, \mathbf{z}) must satisfy to be a valid kernel. (3)

(b) Show explicitly that the polynomial kernel K(x,z)=(xz)2K(\mathbf{x}, \mathbf{z}) = (\mathbf{x}^\top \mathbf{z})^2 for x,zR2\mathbf{x}, \mathbf{z} \in \mathbb{R}^2 corresponds to an inner product in a higher-dimensional feature space, and write out ϕ(x)\phi(\mathbf{x}). (4)

(c) Contrast the effect of the RBF kernel bandwidth γ\gamma 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 xx and two classes.

Class A: xx values {2,4,6}\{2, 4, 6\} Class B: xx values {6,8,10}\{6, 8, 10\} 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 xx. (4)

(c) A new point x=5x = 5 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 V={V = \{ free, money, win, meeting, today }\}.

(a) Compute class priors P(spam)P(\text{spam}), P(ham)P(\text{ham}). (2)

(b) Using Laplace (add-1) smoothing, compute P(freespam)P(\text{free}\mid\text{spam}) and P(freeham)P(\text{free}\mid\text{ham}). 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 αi\alpha_i, 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 CC and γ\gamma for an RBF-SVM, and describe the qualitative behaviour you expect at the extremes (high CC/high γ\gamma vs low CC/low γ\gamma).


Answer keyMark scheme & solutions

Q1 (12 marks)

(a) The signed distance from point xi\mathbf{x}_i to hyperplane wx+b=0\mathbf{w}^\top\mathbf{x}+b=0 is r=wxi+bwr = \frac{\mathbf{w}^\top\mathbf{x}_i + b}{\lVert\mathbf{w}\rVert}. Multiplying by label yi{1,+1}y_i\in\{-1,+1\} gives the (positive for correct) geometric margin γi=yi(wxi+b)w\gamma_i = \frac{y_i(\mathbf{w}^\top\mathbf{x}_i+b)}{\lVert\mathbf{w}\rVert}. (1 for signed distance formula, 1 for projection reasoning, 1 for yiy_i factor)

(b) With canonical scaling, closest points (support vectors) satisfy yi(wxi+b)=1y_i(\mathbf{w}^\top\mathbf{x}_i+b)=1. The distance of a support vector = 1w\frac{1}{\lVert\mathbf{w}\rVert}. The margin is bounded by the two nearest classes, one on each side, so total width =2w=\frac{2}{\lVert\mathbf{w}\rVert}. (1 canonical constraint, 1 single-side distance, 1 doubling)

(c) minw,b 12w2s.t. yi(wxi+b)1 i.\min_{\mathbf{w},b}\ \tfrac{1}{2}\lVert\mathbf{w}\rVert^2 \quad \text{s.t. } y_i(\mathbf{w}^\top\mathbf{x}_i+b)\ge 1\ \forall i. Maximising 2w\frac{2}{\lVert\mathbf{w}\rVert} is equivalent to minimising w\lVert\mathbf{w}\rVert, which is equivalent to minimising 12w2\frac{1}{2}\lVert\mathbf{w}\rVert^2; the squared form is smooth, convex, differentiable (quadratic program), unlike the norm/reciprocal. (1 objective, 1 constraint, 1 convexity reasoning)

(d) min 12w2+Ciξis.t. yi(wxi+b)1ξi, ξi0.\min\ \tfrac{1}{2}\lVert\mathbf{w}\rVert^2 + C\sum_i \xi_i \quad \text{s.t. } y_i(\mathbf{w}^\top\mathbf{x}_i+b)\ge 1-\xi_i,\ \xi_i\ge0. CC trades off margin width vs training violations: large CC penalises misclassifications heavily → narrow margin, low bias/high variance; small CC 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 ϕ(x)ϕ(z)\phi(\mathbf{x})^\top\phi(\mathbf{z}) in a high-dimensional feature space without ever computing ϕ\phi explicitly. Valid kernel: KK 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 x=(x1,x2)\mathbf{x}=(x_1,x_2), z=(z1,z2)\mathbf{z}=(z_1,z_2): (xz)2=(x1z1+x2z2)2=x12z12+2x1x2z1z2+x22z22(\mathbf{x}^\top\mathbf{z})^2 = (x_1z_1+x_2z_2)^2 = x_1^2z_1^2 + 2x_1x_2z_1z_2 + x_2^2z_2^2. This equals ϕ(x)ϕ(z)\phi(\mathbf{x})^\top\phi(\mathbf{z}) with ϕ(x)=(x12, 2x1x2, x22)\phi(\mathbf{x}) = (x_1^2,\ \sqrt{2}\,x_1x_2,\ x_2^2). (2 expansion, 2 correct ϕ\phi incl 2\sqrt 2)

(c) Large γ\gamma → narrow Gaussians, each point influences only its neighbourhood → highly wiggly boundary → overfitting. Small γ\gamma → 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: y^=argmaxcP(c)jP(xjc)\hat y = \arg\max_c P(c)\prod_j P(x_j\mid c). (1 + 1)

(b) Class A {2,4,6}\{2,4,6\}: mean =4=4. MLE variance =(24)2+(0)+(64)23=4+0+43=832.667=\frac{(2-4)^2+(0)+(6-4)^2}{3}=\frac{4+0+4}{3}=\frac{8}{3}\approx2.667. Class B {6,8,10}\{6,8,10\}: mean =8=8, variance =832.667=\frac{8}{3}\approx2.667 (by symmetry). (mean A 1, var A 1, mean B 1, var B 1)

(c) x=5x=5. σ2=8/3\sigma^2=8/3, 2πσ2\sqrt{2\pi\sigma^2} same for both, so compare exponents. Class A: (54)2=1(5-4)^2=1, exponent =128/3=316=0.1875=-\frac{1}{2\cdot8/3}=-\frac{3}{16}=-0.1875. Class B: (58)2=9(5-8)^2=9, exponent =9316=2716=1.6875=-\frac{9\cdot3}{16}=-\frac{27}{16}=-1.6875. Ratio P(xA)P(xB)=exp(0.1875+1.6875)=exp(1.5)4.48\frac{P(x|A)}{P(x|B)}=\exp(-0.1875+1.6875)=\exp(1.5)\approx4.48. 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 → P(spam)=P(ham)=24=0.5P(\text{spam})=P(\text{ham})=\tfrac{2}{4}=0.5. (2)

(b) Smoothed: P(wc)=count(w,c)+1Nc+VP(w\mid c)=\frac{\text{count}(w,c)+1}{N_c+|V|}, V=5|V|=5. Spam word tokens: free,free,money,free,win → Nspam=5N_{spam}=5; count(free,spam)=3. P(freespam)=3+15+5=410=0.4P(\text{free}|\text{spam})=\frac{3+1}{5+5}=\frac{4}{10}=0.4. Ham tokens: meeting,money,meeting,today → Nham=4N_{ham}=4; count(free,ham)=0. P(freeham)=0+14+5=190.1111P(\text{free}|\text{ham})=\frac{0+1}{4+5}=\frac{1}{9}\approx0.1111. (formula 1, spam 1.5, ham 1.5)

(c) Need P(moneyc)P(\text{money}|c) too. count(money,spam)=1 → P(moneyspam)=1+15+5=210=0.2P(\text{money}|\text{spam})=\frac{1+1}{5+5}=\frac{2}{10}=0.2. count(money,ham)=1 → P(moneyham)=1+14+5=290.2222P(\text{money}|\text{ham})=\frac{1+1}{4+5}=\frac{2}{9}\approx0.2222. Spam score =0.5×0.4×0.2=0.04=0.5\times0.4\times0.2=0.04. Ham score =0.5×19×29=0.5×281=1810.012346=0.5\times\frac{1}{9}\times\frac{2}{9}=0.5\times\frac{2}{81}=\frac{1}{81}\approx0.012346. 0.04>0.01230.04 > 0.0123predict SPAM. (each P 1, each score 1.5, decision 1)

(d) Without smoothing, P(freeham)=0P(\text{free}|\text{ham})=0 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 (1P(wc))(1-P(w|c)) terms. (2 + 2)

(b) Support vectors are training points with αi>0\alpha_i > 0; they lie on or inside the margin (yi(wxi+b)1y_i(\mathbf{w}^\top\mathbf{x}_i+b)\le1). Since w=iαiyixi\mathbf{w}=\sum_i \alpha_i y_i \mathbf{x}_i, points with αi=0\alpha_i=0 contribute nothing to w\mathbf{w}, 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 (C,γ)(C,\gamma) (e.g. C{102..103}C\in\{10^{-2}..10^{3}\}, γ{104..101}\gamma\in\{10^{-4}..10^{1}\}), pick the pair maximising CV accuracy/F1. (2) High CC + high γ\gamma → very tight, overfit boundary. Low CC + low γ\gamma → 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)"}
]