SVM, Naive Bayes & Probabilistic Models
Level: 5 — Mastery (cross-domain: math + probability + coding) Time limit: 90 minutes Total marks: 60
Instructions: Answer all questions. Show full derivations. Use for math. Code may be written in Python (NumPy / scikit-learn style pseudocode acceptable but must be runnable in spirit).
Question 1 — SVM: Derive and Interpret the Margin (24 marks)
Consider a binary classification dataset with linearly separable points , , and a separating hyperplane .
(a) Starting from the geometry of the hyperplane, prove that the perpendicular distance from a point to the hyperplane is , and hence show that maximizing the margin is equivalent to minimizing subject to . (6)
(b) Write the primal soft-margin optimization problem with slack variables and penalty . Explain precisely, using the KKT conditions, what the value of the dual variable tells us about whether point is: (i) correctly classified beyond the margin, (ii) exactly on the margin, or (iii) inside the margin / misclassified. (8)
(c) Consider the tiny 1-D dataset: positive points at ; negative points at . Working by hand, find the maximum-margin separator , the margin width, and identify the support vectors. (6)
(d) Explain the kernel trick: why we can replace with without ever computing the feature map . For the polynomial kernel in 2-D, explicitly write the feature map it corresponds to. (4)
Question 2 — Naive Bayes: Derivation, Smoothing & Text (24 marks)
(a) State the Naive Bayes conditional-independence assumption formally, and derive the classification rule from Bayes' theorem. Explain why we can drop the denominator . (5)
(b) For a spam classifier trained on the token counts below (Multinomial NB), compute — with Laplace (add-1) smoothing — the class-conditional probabilities and classify the test document "free money".
| Class | free | money | meeting | total tokens |
|---|---|---|---|---|
| spam | 3 | 3 | 0 | 6 |
| ham | 1 | 1 | 4 | 6 |
Vocabulary size . Priors: . Show all smoothed probabilities and the final decision. (9)
(c) Explain why Laplace smoothing is necessary here — give the concrete failure that occurs without it, and state how the smoothing parameter interacts with as . (4)
(d) Write a short, correct Python function predict_log_proba(counts, priors, likelihoods) that classifies a document using log-probabilities (to avoid numerical underflow) for a Multinomial NB. Explain in one sentence why working in log-space is essential for long documents. (6)
Question 3 — Cross-Model Reasoning (12 marks)
(a) Gaussian Naive Bayes models each . Show that if all classes share the same covariance, the resulting decision boundary between two classes is linear, and relate this to how an RBF-kernel SVM differs (i.e., why RBF-SVM can be nonlinear). (6)
(b) Contrast the roles of the SVM hyperparameters and (RBF) with the effect of the smoothing parameter in Naive Bayes, framing all three in terms of the bias–variance tradeoff. State what happens at extreme values. (6)
Answer keyMark scheme & solutions
Question 1
(a) (6 marks)
- The hyperplane is ; is normal to it. For any point , decompose where lies on the hyperplane. (2)
- Substituting: , so , distance . (2)
- With canonical scaling so margin points satisfy , margin half-width ; maximizing minimizing s.t. . (2)
(b) (8 marks) Primal: s.t. . (3) KKT interpretation (complementary slackness , and ): (5, ~1.6 each)
- (i) : point beyond margin, , — not a support vector.
- (ii) : point exactly on the margin, , — free support vector.
- (iii) : point inside margin or misclassified, — bounded support vector.
(c) (6 marks)
- Nearest opposite points: positive , negative . Boundary midway at . (2)
- With canonical constraints and : subtract → , . Boundary . ✓ (2)
- Margin width (half-width 1). Support vectors: (+) and (−); are not. (2)
(d) (4 marks)
- The dual objective and decision function depend on data only through inner products ; replacing with gives the high-dim solution without materializing (Mercer kernel). (2)
- For : , so (2)
Question 2
(a) (5 marks)
- Assumption: features conditionally independent given class: . (2)
- Bayes: ; apply independence → . (2)
- is constant across classes, so it doesn't affect the . (1)
(b) (9 marks) Smoothed likelihood: , denom . (2)
- Spam: , . (2)
- Ham: , . (2)
- Score spam .
- Score ham . (2)
- Spam score > ham score → classify as SPAM. (1)
(c) (4 marks)
- Without smoothing, "meeting" absent in spam would give , zeroing the entire product regardless of other evidence — a single unseen word vetoes the class. (2)
- Add-: ; as with fixed counts, each probability uniform-ish/tiny, smoothing dominates and washes out evidence — must keep small relative to counts. (2)
(d) (6 marks)
import numpy as np
def predict_log_proba(counts, priors, likelihoods):
# counts: dict token->freq in doc
# priors: {class: P(class)}, likelihoods: {class: {token: P(token|class)}}
scores = {}
for c in priors:
s = np.log(priors[c])
for tok, n in counts.items():
s += n * np.log(likelihoods[c][tok]) # log of product = sum of logs
scores[c] = s
return max(scores, key=scores.get)(5) for correct log-space summation and argmax. One sentence (1): multiplying many probabilities underflows to 0 in floating point; summing logs stays representable.
Question 3
(a) (6 marks)
- . (2)
- With shared , the quadratic term cancels in the log-ratio between two classes, leaving a term linear in → linear boundary (this is LDA). (2)
- RBF-SVM maps to an infinite-dimensional feature space where linear separation corresponds to nonlinear boundaries in input space; controls the locality of that mapping, allowing curved decision regions. (2)
(b) (6 marks)
- : penalty on margin violations. Large → low bias, high variance (hard margin, overfits noise); small → high bias, wider soft margin. (2)
- (RBF): inverse kernel width. Large → very local, high variance/overfit; small → smooth, high bias. (2)
- (NB smoothing): large → pulls estimates toward uniform, high bias, low variance; → trusts sparse counts, low bias, high variance (zero-probability instability). (2)
[
{"claim":"1D SVM: w=1,b=-1 gives correct canonical constraints and margin width 2",
"code":"w,b=1,-1; c1=(w*2+b==1); c2=(w*0+b==-1); margin=Rational(2,abs(w)); result=bool(c1 and c2 and margin==2)"},
{"claim":"Multinomial NB spam score exceeds ham score for 'free money'",
"code":"ps=Rational(1,2)*Rational(4,9)*Rational(4,9); ph=Rational(1,2)*Rational(2,9)*Rational(2,9); result=bool(ps>ph and ps==Rational(8,81) and ph==Rational(2,81))"},
{"claim":"Laplace smoothed P(free|spam)=4/9 with add-1, V=3, total=6",
"code":"p=Rational(3+1,6+3); result=bool(p==Rational(4,9))"},
{"claim":"Poly kernel expansion (x.z+1)^2 has 6 monomial terms matching phi",
"code":"x1,x2,z1,z2=symbols('x1 x2 z1 z2'); K=expand((x1*z1+x2*z2+1)**2); phi=[1,sqrt(2)*x1,sqrt(2)*x2,x1**2,x2**2,sqrt(2)*x1*x2]; psi=[1,sqrt(2)*z1,sqrt(2)*z2,z1**2,z2**2,sqrt(2)*z1*z2]; dot=expand(sum(a*b for a,b in zip(phi,psi))); result=bool(simplify(dot-K)==0)"}
]