Level 4 — ApplicationTree-Based & Instance Methods

Tree-Based & Instance Methods

60 minutes60 marksprintable — key stays hidden on paper

Level: 4 — Application (novel/unseen problems, no hints) Time limit: 60 minutes Total marks: 60

Show all working. Round numerical answers to 4 decimal places unless stated otherwise. Use natural log base 2 for entropy where entropy is requested.


Question 1 — Splitting a Node (14 marks)

A binary classification node contains 10 training samples of the following class labels: [+,+,+,+,+,+,,,,][+, +, +, +, +, +, -, -, -, -]

A candidate feature XX (numeric) has values: X=[1,2,3,4,5,6,7,8,9,10]X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] aligned index-wise with the labels above.

You are considering a threshold split XtX \le t.

(a) Compute the entropy (base 2) and the Gini impurity of the parent node. (4)

(b) For the candidate threshold t=6.5t = 6.5 (i.e. split into X6.5X \le 6.5 vs X>6.5X > 6.5), compute the information gain and the weighted Gini of the split. (6)

(c) A colleague claims t=6.5t = 6.5 is the optimal threshold for this feature. Argue whether any threshold can achieve a purer split, and identify the best achievable weighted Gini for this feature. Justify with reasoning about the label ordering. (4)


Question 2 — AdaBoost Iteration (14 marks)

You run AdaBoost (SAMME / discrete, binary labels {1,+1}\{-1, +1\}) on 5 training points. All points start with equal weights. After training weak learner h1h_1, it misclassifies points 2 and 4 only.

(a) Compute the weighted error ε1\varepsilon_1 and the learner weight α1=12ln ⁣(1ε1ε1)\alpha_1 = \tfrac{1}{2}\ln\!\big(\tfrac{1-\varepsilon_1}{\varepsilon_1}\big). (4)

(b) Compute the updated, normalized weight distribution D2D_2 over the 5 points after the reweighting and normalization step. (6)

(c) Explain quantitatively (using your numbers) why the sum of misclassified-point weights after normalization equals exactly 0.50.5, and state what this implies for the next iteration. (4)


Question 3 — Out-of-Bag Estimation (10 marks)

You build a random forest by bootstrap sampling nn points (sampling with replacement, sample size nn).

(a) Derive the probability that a specific point is not selected in a single bootstrap sample of size nn, and give its limiting value as nn \to \infty. (4)

(b) For n=5n = 5, compute the exact probability a given point is out-of-bag for one tree, to 4 decimals. (3)

(c) A forest has 200 trees. On average, roughly how many trees will treat a given fixed training point as OOB? Explain how the OOB error is then computed for that point. (3)


Question 4 — KNN & Distance Metrics (12 marks)

Given training points with labels:

Point x1x_1 x2x_2 Label
A 1 1 Red
B 2 2 Red
C 4 4 Blue
D 5 1 Blue

Query point q=(3,2)q = (3, 2).

(a) Compute the Euclidean and Manhattan distances from qq to each of A, B, C, D. (6)

(b) Predict the label of qq under 1-NN and 3-NN using Euclidean distance. Show which neighbours are used. (4)

(c) Explain, referencing the curse of dimensionality, one concrete reason why the Euclidean-based prediction becomes unreliable if we appended 100 irrelevant noise features to this dataset. (2)


Question 5 — Gradient Boosting Concept (10 marks)

A gradient boosting regressor uses squared-error loss L(y,F)=12(yF)2L(y,F) = \tfrac{1}{2}(y-F)^2. The initial model is the constant F0=yˉF_0 = \bar{y}.

Training targets: y=[3,6,9,12]y = [3, 6, 9, 12].

(a) Compute F0F_0 and the pseudo-residuals ri=L/Fr_i = -\partial L / \partial F evaluated at F0F_0 for each point. (4)

(b) The first regression tree h1h_1 predicts a constant cc for the two left points (indices 1,2) and a constant dd for the two right points (indices 3,4), fitting the residuals by mean. With learning rate ν=0.5\nu = 0.5, give F1(xi)=F0+νh1(xi)F_1(x_i) = F_0 + \nu\, h_1(x_i) for all four points. (4)

(c) State why gradient boosting fits residuals (gradients) rather than re-weighting samples like AdaBoost. (2)

Answer keyMark scheme & solutions

Question 1 (14 marks)

Parent: 6 positives, 4 negatives, p+=0.6p_+ = 0.6, p=0.4p_- = 0.4.

(a) Entropy: H=0.6log20.60.4log20.4=0.60.7370+0.41.3219=0.9710H = -0.6\log_2 0.6 - 0.4\log_2 0.4 = 0.6\cdot0.7370 + 0.4\cdot1.3219 = 0.9710 (2 marks — correct formula 1, value 1)

Gini: G=10.620.42=10.360.16=0.48G = 1 - 0.6^2 - 0.4^2 = 1 - 0.36 - 0.16 = 0.48 (2 marks)

(b) Split at t=6.5t=6.5: left = indices 1–6 (all ++), 6 samples, pure. Right = indices 7–10 (all -), 4 samples, pure.

Left entropy =0= 0, right entropy =0= 0. Weighted child entropy =610(0)+410(0)=0= \tfrac{6}{10}(0) + \tfrac{4}{10}(0) = 0. Information gain =Hparent0=0.9710= H_{parent} - 0 = 0.9710. (3 marks)

Weighted Gini =610(0)+410(0)=0= \tfrac{6}{10}(0) + \tfrac{4}{10}(0) = 0. (3 marks)

(c) Because the labels are perfectly ordered (all ++ then all -) and XX is monotonically increasing, the threshold t=6.5t=6.5 separates the classes with zero impurity. No split can do better than weighted Gini =0= 0 (the theoretical minimum). So t=6.5t=6.5 IS optimal; the best achievable weighted Gini is 00. (4 marks: recognise perfect separation 2, state min Gini = 0 with justification 2)


Question 2 (14 marks)

Initial weights: D1(i)=0.2D_1(i) = 0.2 each. Misclassified: points 2, 4.

(a) ε1=0.2+0.2=0.4\varepsilon_1 = 0.2 + 0.2 = 0.4. (2 marks) α1=12ln10.40.4=12ln1.5=12(0.4055)=0.2027\alpha_1 = \tfrac12\ln\frac{1-0.4}{0.4} = \tfrac12\ln 1.5 = \tfrac12(0.4055) = 0.2027 (2 marks)

(b) Update rule: D2(i)D1(i)eα1yih(xi)D_2(i) \propto D_1(i)\,e^{-\alpha_1 y_i h(x_i)}; multiply misclassified by eα1e^{\alpha_1}, correct by eα1e^{-\alpha_1}.

eα1=e0.2027=1.2247e^{\alpha_1} = e^{0.2027} = 1.2247, eα1=0.8165e^{-\alpha_1} = 0.8165.

Unnormalized:

  • Correct points (1,3,5): 0.2×0.8165=0.16330.2 \times 0.8165 = 0.1633 each
  • Misclassified (2,4): 0.2×1.2247=0.24490.2 \times 1.2247 = 0.2449 each

Sum Z=3(0.1633)+2(0.2449)=0.4899+0.4899=0.9798Z = 3(0.1633) + 2(0.2449) = 0.4899 + 0.4899 = 0.9798.

Normalized:

  • Correct: 0.1633/0.9798=0.16670.1633/0.9798 = 0.1667 each
  • Misclassified: 0.2449/0.9798=0.25000.2449/0.9798 = 0.2500 each

D2=[0.1667, 0.2500, 0.1667, 0.2500, 0.1667]D_2 = [0.1667,\ 0.2500,\ 0.1667,\ 0.2500,\ 0.1667] (Update rule 2, unnormalized 2, normalized values 2)

(c) After normalization, weighted error of h1h_1 under D2D_2 = sum of misclassified weights =0.25+0.25=0.5= 0.25 + 0.25 = 0.5. This is a general AdaBoost property: the reweighting makes the current learner have exactly 50% weighted error on the new distribution (equivalent to random guessing), forcing the next learner to focus on the previously hard points. (numeric confirmation 2, interpretation 2)


Question 3 (10 marks)

(a) Probability a specific point NOT chosen in one draw =11/n= 1 - 1/n. Independent over nn draws: P(OOB)=(11n)nP(\text{OOB}) = \left(1 - \frac1n\right)^n Limit: limn(11/n)n=e10.3679\lim_{n\to\infty}(1-1/n)^n = e^{-1} \approx 0.3679. (formula 2, limit 2)

(b) n=5n=5: (11/5)5=0.85=0.327680.3277(1 - 1/5)^5 = 0.8^5 = 0.32768 \approx 0.3277. (3 marks)

(c) Fraction of trees for which a point is OOB 0.368\approx 0.368. For 200 trees: 200×0.36873.674200 \times 0.368 \approx 73.6 \approx 74 trees. OOB error for that point = aggregate prediction (majority vote / average) using only those ~74 trees that did not train on it; the overall OOB error averages this over all points, giving a validation-free generalization estimate. (count 2, computation of OOB error 1)


Question 4 (12 marks)

(a) Query q=(3,2)q=(3,2).

Euclidean:

  • A(1,1): 4+1=5=2.2361\sqrt{4+1}=\sqrt5=2.2361
  • B(2,2): 1+0=1.0000\sqrt{1+0}=1.0000
  • C(4,4): 1+4=5=2.2361\sqrt{1+4}=\sqrt5=2.2361
  • D(5,1): 4+1=5=2.2361\sqrt{4+1}=\sqrt5=2.2361

Manhattan:

  • A: 2+1=32+1=3
  • B: 1+0=11+0=1
  • C: 1+2=31+2=3
  • D: 2+1=32+1=3

(3 marks Euclidean, 3 marks Manhattan)

(b) 1-NN: nearest is B (dist 1.0) → Red. 3-NN: nearest three are B (1.0), then a tie among A, C, D (all 2.2361). Choosing any 3, e.g. {B, and two of A/C/D}. With B=Red, A=Red, then one Blue → 2 Red vs 1 Blue → Red. (Note tie must be broken; a reasonable tie-break like taking A and C gives {B:Red, A:Red, C:Blue} → Red.) Answer: 1-NN = Red, 3-NN = Red (with stated tie-breaking). (4 marks: 1-NN 2, 3-NN with tie note 2)

(c) With 100 noise features, Euclidean distances become dominated by noise-dimension variation; all pairwise distances concentrate (ratio of nearest to farthest → 1), so "nearest neighbour" is no longer meaningfully closer in the relevant features — predictions degrade. (2 marks)


Question 5 (10 marks)

(a) F0=yˉ=(3+6+9+12)/4=30/4=7.5F_0 = \bar y = (3+6+9+12)/4 = 30/4 = 7.5. (1) Pseudo-residuals ri=yiF0r_i = y_i - F_0: [37.5, 67.5, 97.5, 127.5]=[4.5, 1.5, 1.5, 4.5][3-7.5,\ 6-7.5,\ 9-7.5,\ 12-7.5] = [-4.5,\ -1.5,\ 1.5,\ 4.5] (3 marks)

(b) Left leaf (points 1,2): c=(4.5+1.5)/2=3c = (-4.5 + -1.5)/2 = -3. Right leaf (points 3,4): d=(1.5+4.5)/2=3d = (1.5 + 4.5)/2 = 3. F1=F0+0.5h1F_1 = F_0 + 0.5 h_1:

  • pts 1,2: 7.5+0.5(3)=6.07.5 + 0.5(-3) = 6.0
  • pts 3,4: 7.5+0.5(3)=9.07.5 + 0.5(3) = 9.0

F1=[6.0, 6.0, 9.0, 9.0]F_1 = [6.0,\ 6.0,\ 9.0,\ 9.0] (leaf values 2, F1F_1 2)

(c) Gradient boosting fits each new learner to the negative gradient (residuals) of the loss, performing functional gradient descent, so it directly reduces the loss for any differentiable loss. AdaBoost's reweighting is a special case for exponential loss; residual-fitting generalizes to arbitrary losses and continuous targets. (2 marks)

[
  {"claim":"Q1a parent entropy = 0.9710 and Gini = 0.48","code":"from sympy import log, Rational, N\np=Rational(6,10); q=Rational(4,10)\nH=-(p*log(p,2)+q*log(q,2)); G=1-p**2-q**2\nresult = (abs(float(H)-0.9710)<1e-3) and (float(G)==0.48)"},
  {"claim":"Q2 alpha1 = 0.2027 and D2 misclassified weight = 0.25","code":"import sympy as sp\neps=sp.Rational(2,5)\nalpha=sp.Rational(1,2)*sp.log((1-eps)/eps)\nc=sp.Rational(1,5)*sp.exp(-alpha); m=sp.Rational(1,5)*sp.exp(alpha)\nZ=3*c+2*m\nresult = (abs(float(alpha)-0.2027)<1e-3) and (abs(float(m/Z)-0.25)<1e-6)"},
  {"claim":"Q3b OOB prob for n=5 is 0.32768","code":"result = abs((1-1/5)**5 - 0.32768) < 1e-9"},
  {"claim":"Q5 residuals and F1 correct","code":"y=[3,6,9,12]; F0=sum(y)/4\nr=[v-F0 for v in y]\nc=(r[0]+r[1])/2; d=(r[2]+r[3])/2\nF1=[F0+0.5*c,F0+0.5*c,F0+0.5*d,F0+0.5*d]\nresult = (F0==7.5) and (r==[-4.5,-1.5,1.5,4.5]) and (F1==[6.0,6.0,9.0,9.0])"}
]