Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Non-parametric methods: Parzen windows and nearest neighbour

Parzen windows and nearest-neighbour are non-parametric density-estimation and classification methods that estimate class-conditional densities from data without assuming a parametric form; useful for small to medium datasets, prototype-free classifiers, and as baseline methods in pattern-recognitio

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: where an engineer uses this.

Non-parametric methods like Parzen windows and nearest-neighbour (NN) let an engineer build classifiers and density estimates directly from samples when a parametric model (Gaussian, exponential, etc.) is not trusted. They are used for prototype-based classification, anomaly detection, and as baseline methods when feature distributions are unknown or multi-modal.

Key ideas

Explain from first principles, define terms, state assumptions and derivations.

  • Pattern: a measured vector x ∈ R^d describing an object. Units depend on the feature (e.g., meters, pixels).
  • Density estimation: given i.i.d. samples x_1,...,x_n drawn from unknown probability density p(x), estimate p(x) without assuming a parametric form.
  • Non-parametric: a method is non-parametric if it does not assume a fixed finite-dimensional parameter vector for p(x); complexity can grow with n.

Parzen windows (kernel density estimation)

  • Define a kernel function K(u): a real-valued function on R^d with the properties: (1) K(u) ≥ 0, (2) ∫_{R^d} K(u) du = 1, (3) typically symmetric K(u)=K(-u). K is dimensionless. Examples: Gaussian, uniform, Epanechnikov.
  • Define window width (bandwidth) h > 0. For multivariate, often a scalar h and scaled kernel K((x-x_i)/h) or a matrix H for anisotropic scaling.
  • The Parzen window density estimator at x is the average contribution of kernels centred at each sample: p_hat_n(x) = (1/n) * sum_{i=1..n} (1/h^d) * K((x - x_i)/h) Here d is the data dimension. The factor (1/h^d) ensures correct scaling so p_hat integrates to 1 if K integrates to 1.
  • Assumptions for consistency (convergence to true p(x)): if h = h_n depends on n, then require
    • h_n -> 0 as n -> infinity (windows shrink), and
    • n * h_n^d -> infinity (enough samples fall inside shrinking windows). Under mild smoothness conditions on p(x) and K, p_hat_n(x) -> p(x) in mean-square or almost surely.
  • Bias and variance (informal): bias decreases with smaller h, variance decreases with larger h; choose h to trade bias vs variance. In d dimensions the variance scales roughly like 1/(n h^d) so high d makes density estimation hard (the curse of dimensionality).

Nearest-neighbour density and classification

  • k-nearest-neighbour (k-NN) density estimate: for a query x, find the smallest volume V that contains k sample points (from n total). Then p_hat_kNN(x) = k / (n * V) Intuition: density ≈ number of points per volume. For Euclidean distance using a d-dimensional ball of radius r, V = c_d r^d with c_d = π^{d/2} / Γ(d/2 + 1) (Γ is the gamma function).
  • For classification, build class-conditional densities by using only samples from class C_j (n_j samples): p_hat(x | C_j) = k_j / (n_j * V_j) where k_j neighbours from class j lie in volume V_j (often using same V across classes means counting k_j inside that V).
  • Alternatively the k-NN classifier works without explicit density: find k nearest labelled samples of x and assign the majority class among them. This is a non-parametric classifier that stores all samples.
  • Consistency conditions for k-NN classifier/density: choose k = k_n such that
    • k_n -> infinity and k_n / n -> 0 as n -> infinity. Then the k-NN posterior estimates and classification error approach the Bayes-optimal under mild conditions.

Using non-parametric estimates in Bayes decision rule

  • With class priors P(C_j) (estimated by n_j/n) and class-conditional estimates p_hat(x|C_j), the estimated posterior is P_hat(C_j | x) = p_hat(x | C_j) * P_hat(C_j) / p_hat(x) and the Bayes decision picks the class with maximum posterior. In practice for classification we can pick the class maximizing p_hat(x|C_j) * P_hat(C_j) since p_hat(x) is common.

What to draw in exam diagrams (describe exactly)

  • Parzen: draw 1D horizontal axis x, plot sample points x_i as small vertical ticks; at a chosen x draw small kernel bumps centered at each sample and one thicker curve which is p_hat(x) (sum of bumps). Label h and show individual kernel K((x-x_i)/h).
  • k-NN: draw 2D scatter of labelled points for two classes, mark query x, draw a circle (or square) centred at x that just reaches the k-th nearest sample; label radius r and the k neighbours inside. Indicate k and V = area of circle (or volume in d-dim).

Formulas

Each formula on its own line followed by symbol meanings, SI units and when it applies.

p_hat_n(x) = (1/n) * sum_{i=1..n} (1/h^d) * K((x - x_i)/h)

  • p_hat_n(x): Parzen (kernel) density estimate at x, unit = 1/(unit of x)^d. n: number of samples (dimensionless). h: window width, same units as x. d: data dimension (dimensionless). K: kernel function, dimensionless, integrates to 1. Applies for kernel density estimation with scalar bandwidth.

p_hat_kNN(x) = k / (n * V)

  • p_hat_kNN(x): k-NN density estimate at x, unit = 1/(unit of x)^d. k: number of neighbours (dimensionless). n: total samples. V: volume of region containing k neighbours, units = (unit of x)^d. Applies when V is the smallest volume containing k points.

P_hat(C_j) = n_j / n (class prior estimate)

  • P_hat(C_j): estimated prior probability of class j (dimensionless). n_j: samples in class j. Applies when classes sampled iid.

P_hat(C_j | x) ∝ p_hat(x | C_j) * P_hat(C_j)

  • Posterior proportionality used for decision; normalize by p_hat(x) if needed.

Consistency conditions (stated as limits):

h_n -> 0 and n * h_n^d -> infinity (for Parzen consistency)

k_n -> infinity and k_n / n -> 0 (for k-NN consistency)

Worked examples

Two exam-standard problems with steps and units.

Example 1 — 1D Parzen with Gaussian kernel (numerical) Data: x_1 = -1.0 m, x_2 = 0.0 m, x_3 = 1.0 m (n=3). Kernel: 1D Gaussian K(u) = (1/sqrt(2π)) e^{-u^2/2} (dimensionless). Choose h = 0.5 m. Estimate p_hat(0.0 m). Steps:

  1. Formula: p_hat_n(x) = (1/n) * sum_{i=1..n} (1/h) * K((x-x_i)/h).
  2. Compute u_i = (0 - x_i)/h: u_1 = (0 - (-1))/0.5 = 2.0, u_2 = 0, u_3 = (0 - 1)/0.5 = -2.0.
  3. K(u) values: K(0) = 1/sqrt(2π) ≈ 0.39894; K(2) = (1/sqrt(2π)) e^{-2^2/2} = 0.39894 * e^{-2} ≈ 0.39894 * 0.13534 ≈ 0.05399.
  4. Each term (1/h) * K(u_i): for u=2: (1/0.5)*0.05399 = 2 * 0.05399 = 0.10798. For u=0: (1/0.5)*0.39894 = 0.79788.
  5. Sum = 0.10798 + 0.79788 + 0.10798 = 1.01384.
  6. Multiply by 1/n = 1/3: p_hat(0) ≈ 1.01384 / 3 = 0.33795 1/m. Answer: p_hat(0.0 m) ≈ 0.338 per metre.

Example 2 — 3-NN classification in 2D (theory + numeric counts) Data: Two classes A and B. Given query x, its 3 nearest neighbours are: 2 points from class A and 1 point from class B. Priors estimated: P_hat(A) = 0.6, P_hat(B) = 0.4 (from training set). Using k-NN majority rule, class assigned is A (2 of 3). Using density-style estimate on equal volume V around x: P_hat(A|x) ∝ (2/(n_A * V)) * P_hat(A) and P_hat(B|x) ∝ (1/(n_B * V)) * P_hat(B); if n_A and n_B are similar and priors are accounted for, class A still has the larger posterior. Model exam answer: use majority voting for full marks, show k counts and tie-breaking rule if needed. Answer: Assign class A.

Common mistakes

  • Not normalizing kernel K (use K that integrates to 1) or using a non-normalized kernel without correcting constant.
  • Ignoring dimensionality: using same h independent of d leads to poor estimates; variance scales like 1/(n h^d).
  • Using Euclidean distance without feature scaling; distances in different units distort NN results.
  • Using k too small (high variance) or too large (high bias) without cross-validation.
  • For classification, incorrectly dividing by total sample n when class-conditional estimates must use n_j for each class unless using same-volume comparisons.

For your exam

Kinds of questions and what full-marks answers include.

  • Definitions: define Parzen window, kernel, bandwidth, k-NN, consistency. Full marks need the properties (kernel integrates to 1, h->0, n h^d -> ∞, etc.).
  • Short notes: explain bias-variance trade-off, curse of dimensionality, kernel choices (Gaussian, uniform, Epanechnikov). Full marks: mention scaling of variance ~1/(n h^d) and qualitative bias behaviour. Also mention bandwidth selection methods (cross-validation, plug-in); for a 1D Gaussian kernel, Silverman's rule gives h ≈ 1.06 * sigma * n^{-1/5}.
  • Derivations: derive Parzen estimator form and show normalization if K normalized; show conditions for consistency. Full marks: show (1/h^d) factor and explain limits h_n->0 & n h_n^d->∞.
  • Numericals: compute p_hat at a point for given small dataset and kernels. Full marks: show all steps, compute u=(x-x_i)/h, K(u), (1/h^d)K, sum, divide by n, include units.
  • Programs: implement k-NN classifier or Parzen estimator; show sample input/output and note computational cost O(n) per query. Full marks: correct code, explanation of distance metric and bandwidth/k choice.
  • Diagrams: draw kernels summing in 1D or circle enclosing k neighbours in 2D; label axes, sample points, h, r, k, V.

Quick check

Three short questions with answers.

  1. What is the Parzen window factor that ensures density integrates to 1? (Answer in one symbol)
  2. State two conditions on h_n and n for Parzen consistency.
  3. In k-NN classification, what happens if k = 1 in the limit n -> infinity (qualitative)?

Answers: 1) 1/h^d. 2) h_n -> 0 and n * h_n^d -> infinity. 3) 1-NN is not generally consistent; its asymptotic error is bounded above by about twice the Bayes error (more precisely ≤ 2 R*(1−R*)); to achieve consistency use k = k_n with k_n -> ∞ and k_n/n -> 0.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?