Level 5 — MasteryUnsupervised Learning

Unsupervised Learning

90 minutes60 marksprintable — key stays hidden on paper

Time limit: 90 minutes
Total marks: 60
Instructions: Answer all three questions. Show all derivations. Partial credit awarded for correct method. Calculators/derivations by hand; code answers may be pseudocode or NumPy-style.


Question 1 — K-Means, PCA and their hidden equivalence (24 marks)

(a) Prove that the Lloyd (K-Means) update rule is a coordinate-descent minimization of the within-cluster sum of squares objective J({ck},{μk})=i=1nk=1Krikxiμk2,J(\{c_k\},\{\mu_k\}) = \sum_{i=1}^{n}\sum_{k=1}^{K} r_{ik}\,\lVert x_i-\mu_k\rVert^2, where rik{0,1}r_{ik}\in\{0,1\} is a one-hot assignment. Specifically, show that (i) with μk\mu_k fixed, the optimal assignment is nearest-centroid, and (ii) with assignments fixed, the optimal μk\mu_k is the cluster mean. Argue that JJ is monotonically non-increasing and the algorithm terminates. (8)

(b) Consider the following 1-D dataset (already mean-centered): x={3,1,1,3}x = \{-3,-1,\,1,\,3\}. Run K-Means with K=2K=2 starting from centroids μ1=3, μ2=1\mu_1=-3,\ \mu_2=-1. Show every iteration (assignment + update) until convergence, and give the final centroids and the final value of JJ. (6)

(c) PCA on the same data: treat xx as four samples of a single feature. Compute the sample variance (using divisor nn) and state the first principal component's explained-variance ratio. Then explain, with reference to the "spectral relaxation" of K-Means, why the top principal component provides a continuous relaxation of the optimal 2-cluster indicator vector. (6)

(d) State one dataset geometry where K-Means provably fails but DBSCAN succeeds, and justify in one or two sentences using each algorithm's objective/assumptions. (4)


Question 2 — Gaussian Mixtures, EM, and the physics of soft assignment (22 marks)

(a) Write the complete-data log-likelihood for a KK-component GMM and derive the E-step responsibility γik=πkN(xiμk,Σk)jπjN(xiμj,Σj).\gamma_{ik} = \frac{\pi_k\,\mathcal N(x_i\mid\mu_k,\Sigma_k)}{\sum_{j}\pi_j\,\mathcal N(x_i\mid\mu_j,\Sigma_j)}. Then derive the M-step update for μk\mu_k by maximizing the expected complete-data log-likelihood. (8)

(b) Show that in the limit Σk=σ2I\Sigma_k=\sigma^2 I with σ0\sigma\to 0 (and equal priors), the GMM responsibilities become hard 0/1 nearest-centroid assignments, recovering K-Means. Use the softmax form of γik\gamma_{ik} to justify this. Draw the thermodynamic analogy: identify σ2\sigma^2 as a "temperature." (8)

(c) A 1-D two-component GMM has μ1=0, μ2=2\mu_1=0,\ \mu_2=2, both σ2=1\sigma^2=1, equal priors. Compute the responsibility γi1\gamma_{i1} (probability of component 1) for a point at x=1x=1. Give the numeric value. (6)


Question 3 — Build & analyze: DBSCAN + anomaly scoring (14 marks)

(a) Write clean pseudocode for DBSCAN given parameters ε\varepsilon and minPts. Clearly distinguish core, border, and noise points, and ensure each point is visited once. State the time complexity with and without a spatial index. (7)

(b) For the 1-D points {1,2,3,10,11,50}\{1,2,3,10,11,50\} with ε=1.5\varepsilon=1.5 and minPts=2=2, list the resulting clusters and noise points, identifying which points are core. (4)

(c) Briefly contrast DBSCAN-based anomaly detection with a Gaussian (parametric) anomaly detector for a dataset consisting of two dense elongated bands plus scattered outliers — which is more appropriate and why? (3)

Answer keyMark scheme & solutions

Question 1

(a) [8]

  • (i) Fixed μk\mu_k: JJ decouples per point ii; minimizing krikxiμk2\sum_k r_{ik}\lVert x_i-\mu_k\rVert^2 subject to one-hot rir_i picks k=argminkxiμk2k^*=\arg\min_k\lVert x_i-\mu_k\rVert^2. (2) Hence nearest-centroid assignment is optimal. (1)
  • (ii) Fixed assignments: J/μk=2irik(xiμk)=0μk=irikxiirik\partial J/\partial\mu_k = -2\sum_i r_{ik}(x_i-\mu_k)=0 \Rightarrow \mu_k=\frac{\sum_i r_{ik}x_i}{\sum_i r_{ik}}, the cluster mean. (3) Second derivative 2irik>02\sum_i r_{ik}>0 ⇒ minimum. (1)
  • Each step is exact minimization over one block ⇒ JJ non-increasing; J0J\ge0 bounded below and finite number of assignment configurations ⇒ convergence in finite steps. (1)

(b) [6]

  • Iter 1 assign: distances to μ1=3,μ2=1\mu_1=-3,\mu_2=-1. 3μ1; 1μ2; 1μ2-3\to\mu_1;\ -1\to\mu_2;\ 1\to\mu_2 (dist 4<2? no: |1-(-3)|=4,|1-(-1)|=2 →μ2\mu_2); 3μ23\to\mu_2 (|3+3|=6,|3+1|=4). Clusters: {3},{1,1,3}\{-3\},\{-1,1,3\}. (2)
  • Update: μ1=3, μ2=(1+1+3)/3=1\mu_1=-3,\ \mu_2=(-1+1+3)/3=1. (1)
  • Iter 2 assign: μ1=3,μ2=1\mu_1=-3,\mu_2=1. 3μ1-3\to\mu_1; 1-1: |−1+3|=2,|−1−1|=2 tie → assign μ1\mu_1 (or μ2\mu_2); resolving to nearest with tie-break to lower index: 1μ1-1\to\mu_1; 1μ21\to\mu_2;3μ23\to\mu_2. Clusters {3,1},{1,3}\{-3,-1\},\{1,3\}. (1)
  • Update: μ1=2, μ2=2\mu_1=-2,\ \mu_2=2. Reassign confirms stable. Final centroids 2,2-2,2. (1)
  • J=(3+2)2+(1+2)2+(12)2+(32)2=1+1+1+1=4J=(-3+2)^2+(-1+2)^2+(1-2)^2+(3-2)^2=1+1+1+1=4. (1)

(c) [6]

  • Mean =0=0; variance =14(9+1+1+9)=5=\frac{1}{4}(9+1+1+9)=5. (2)
  • Single feature ⇒ PC1 explains 100% of variance, ratio =1=1. (2)
  • Spectral relaxation: relaxing the discrete cluster indicator to be real-valued and orthogonal to 1\mathbf 1, the optimal continuous solution maximizing between-cluster variance is the top eigenvector of the (centered) covariance/Gram matrix = PC1. Sign of PC1 projection then gives a natural 2-cluster split; thus PCA relaxes the NP-hard K-Means assignment. (2)

(d) [4]

  • Non-convex / concentric-ring (or two-moons) geometry. (2) K-Means minimizes squared distance to a single centroid ⇒ assumes convex isotropic (spherical) clusters and will slice rings incorrectly; DBSCAN groups by density-connectivity so it follows arbitrary shapes and labels sparse points as noise. (2)

Question 2

(a) [8]

  • Complete-data LL: logik[πkN(xiμk,Σk)]zik=ikzik[logπk+logN()]\log\prod_i\prod_k[\pi_k\mathcal N(x_i|\mu_k,\Sigma_k)]^{z_{ik}}=\sum_i\sum_k z_{ik}[\log\pi_k+\log\mathcal N(\cdots)]. (2)
  • E-step: γik=E[zik]=P(zik=1xi)=\gamma_{ik}=E[z_{ik}]=P(z_{ik}=1|x_i)= posterior via Bayes = stated formula. (2)
  • M-step μk\mu_k: maximize Q=iγiklogNQ=\sum_i\gamma_{ik}\log\mathcal N; /μk\partial/\partial\mu_k of 12(xiμk)TΣk1(xiμk)-\tfrac12(x_i-\mu_k)^T\Sigma_k^{-1}(x_i-\mu_k) gives iγikΣk1(xiμk)=0μk=iγikxiiγik\sum_i\gamma_{ik}\Sigma_k^{-1}(x_i-\mu_k)=0\Rightarrow \mu_k=\frac{\sum_i\gamma_{ik}x_i}{\sum_i\gamma_{ik}}. (4)

(b) [8]

  • With Σ=σ2I\Sigma=\sigma^2 I, equal π\pi: γik=exp(xiμk2/2σ2)jexp(xiμj2/2σ2)\gamma_{ik}=\frac{\exp(-\lVert x_i-\mu_k\rVert^2/2\sigma^2)}{\sum_j\exp(-\lVert x_i-\mu_j\rVert^2/2\sigma^2)}. (3) As σ0\sigma\to0 the softmax sharpens: the term with smallest xiμk2\lVert x_i-\mu_k\rVert^2 dominates → γik1\gamma_{ik}\to1 for nearest centroid, 0 else = hard assignment = K-Means. (3) Analogy: σ2\sigma^2 \leftrightarrow temperature TT; σ0\sigma\to0 is the zero-temperature limit where the Boltzmann/softmax distribution collapses to the ground state (nearest centroid). (2)

(c) [6]

  • N(10,1)e1/2\mathcal N(1|0,1)\propto e^{-1/2}, N(12,1)e1/2\mathcal N(1|2,1)\propto e^{-1/2} (both distance 1). (3) Equal priors ⇒ γi1=e1/2e1/2+e1/2=0.5\gamma_{i1}=\frac{e^{-1/2}}{e^{-1/2}+e^{-1/2}}=0.5. (3)

Question 3

(a) [7]

DBSCAN(D, eps, minPts):
  label = {} (undefined for all)
  C = 0
  for p in D:
    if label[p] defined: continue
    N = rangeQuery(D, p, eps)      # neighbors within eps (incl p)
    if |N| < minPts:
      label[p] = NOISE; continue    # may become border later
    C += 1; label[p] = C
    Seed = N \ {p}
    while Seed not empty:
      q = Seed.pop()
      if label[q]==NOISE: label[q]=C   # border point
      if label[q] defined: continue
      label[q]=C
      Nq = rangeQuery(D,q,eps)
      if |Nq| >= minPts: Seed += Nq    # q is core, expand
  return label
  • Core: \ge minPts neighbors in ε\varepsilon. Border: non-core but in a core's neighborhood. Noise: neither. (3 correctness, 2 core/border/noise distinction)
  • Complexity: O(n2)O(n^2) naive; O(nlogn)O(n\log n) with a spatial index (kd-tree/ball-tree) for low dimensions. (2)

(b) [4]

  • ε=1.5\varepsilon=1.5, neighbors: 1↔2, 2↔3, 3↔2 → each of 1,2,3 has ≥2 pts incl self ⇒ all core; cluster A={1,2,3}. (1.5)
  • 10↔11 within 1.5 ⇒ each core; cluster B={10,11}. (1.5)
  • 50 isolated ⇒ noise. (1)

(c) [3]

  • Two elongated (non-spherical) bands ⇒ a single Gaussian per band mismatches shape and a global Gaussian anomaly detector flags band tails as anomalies. DBSCAN (density) captures arbitrary band shapes and cleanly labels scattered points as noise/outliers ⇒ more appropriate. (3)
[
  {"claim":"Q1b final J = 4 with centroids -2,2","code":"pts=[-3,-1,1,3]; c1=[-3,-1]; c2=[1,3]; m1=sum(c1)/2; m2=sum(c2)/2; J=sum((x-m1)**2 for x in c1)+sum((x-m2)**2 for x in c2); result = (m1==-2 and m2==2 and J==4)"},
  {"claim":"Q1c variance (divisor n) = 5","code":"x=[-3,-1,1,3]; n=len(x); mu=sum(x)/n; var=sum((xi-mu)**2 for xi in x)/n; result = (var==5)"},
  {"claim":"Q2c responsibility gamma=0.5 for x=1","code":"import sympy as sp; f=lambda x,mu: sp.exp(-(x-mu)**2/2); g=f(1,0)/(f(1,0)+f(1,2)); result = (sp.simplify(g)==sp.Rational(1,2))"},
  {"claim":"Q3b cluster A={1,2,3} all core with eps=1.5 minPts=2","code":"P=[1,2,3,10,11,50]; eps=1.5; A=[p for p in P if abs(p-1)<=eps or abs(p-2)<=eps or abs(p-3)<=eps]; core1=len([q for q in P if abs(q-1)<=eps])>=2; core2=len([q for q in P if abs(q-2)<=eps])>=2; result = (core1 and core2 and set([1,2,3]).issubset(set(A)))"}
]