04 - Parzen Windows

Pattern Recognition and Machine Learning, Lab 4

Michal Neoral

neoramic@fel.cvut.cz

16 October 2026

Test first

Test now

  • Bayesian and non-Bayesian tasks
  • Calculator only: no phone, laptop or notes
  • Write your name on every sheet

Recap

What do we know about the classes?

Bayesian task

  • \(\cp(\cx \mid \ck)\) known
  • \(\cp(\ck)\) known
  • \(\cW(\ck, \cd)\) known

We know everything.

Non-Bayesian tasks

  • \(\cp(\cx \mid \ck)\) known
  • \(\cp(\ck)\) unknown
  • \(\cW(\ck, \cd)\) unknown

Neyman-Pearson, minimax, Wald

Today

  • \(\cp(\cx \mid \ck)\) unknown
  • only samples \(\cx_1, \dots, \cx_N\)

We have to estimate the density.

today

Once we have the estimate \(\cph(\cx \mid \ck)\), we plug it into the Bayesian strategy as before.

Probability density estimation

Each class is estimated separately, from its own samples: we drop \(\ck\) and write \(\cp(\cx)\) instead of \(\cp(\cx \mid \ck)\).

How can we estimate \(\cp(\cx)\) from samples?

Parametric

Known form (e.g. normal), unknown parameters.

MLE, MAP, Bayesian estimation

next lab

Non-parametric

Any shape of the density.

histogram, \(k\)-NN, Parzen windows (kernel density estimation)

today

Mixtures

Combine simple densities.

mixture of Gaussians, neural networks

Histogram

The histogram as a density estimate

The true density p(x), a mixture of two normal distributions

Twelve samples drawn from the true density

Histogram with bins of width 1.5 and counts 0, 3, 3, 0, 5, 1

The histogram compared with the true density, one bin in a dense region is empty

The true density \(\cp(\cx)\) exists, but we do not know it.

We only get \(N = 12\) samples drawn from it: \(\cx_i \sim \cp(\cx)\).

Bins of width \(w_b\), \(N_b\) samples in bin \(b\). What is the height of the bin?

Compared with the true \(\cp(\cx)\): one bin in a dense region is empty.

Derivation: the height of a bin

The bin contains \(N_b\) of the \(N\) samples: \[ P(\cx \in \text{bin } b) \approx \frac{N_b}{N} \]

Density is probability per unit length: \[ \cph_b = \frac{N_b}{N \cdot w_b} \]

The estimate integrates to one: \[ \sum_b \cph_b \, w_b = \sum_b \frac{N_b}{N} = 1 \]

Histogram with bins of width 1.5

Here: \(\cph_b = \frac{5}{12 \cdot 1.5} = 0.28\) for the highest bin.

\(\cph_b\) is a height, not a probability: 6 of 12 samples in a bin of width 0.25 give \(\cph_b = 2\)

Histogram: pros and cons

Histograms of the same 200 samples with bin widths 0.1, 0.6 and 3

Pros

  • Simple, easy to visualise
  • We do not need to keep the samples \(\cx_i\)

Cons

  • Step-like, bins can be empty
  • How to choose \(w_b\)?
  • \(D > 2\): \(B^D\) bins, mostly empty

Towards Parzen windows

More dimensions: \(\;\cph(\cx) = \frac{K}{N V}\)

k-NN: circles around two query points reaching their third nearest neighbour; the circles have different volumes

Parzen: circles of a fixed size around the same query points containing different numbers of samples

Both methods with squares instead of circles

The histogram in \(D\) dimensions: \(K\) samples in a volume \(V\) around \(\cx\).

Fix \(K\), find the smallest \(V\) containing \(K\) samples: \(k\)-nearest neighbours.

Fix \(V\), count the samples \(K\) inside: Parzen windows.

The volume can be a ball or a hypercube: the cube is easy to count in.

Counting the samples in a window

Window function: the unit hypercube centred at the origin \[ \varphi(\mathbf{u}) = \begin{cases} 1 & \text{if } |u^j| \le \tfrac{1}{2} \text{ for all } j = 1, \dots, D \\ 0 & \text{otherwise} \end{cases} \]

\(\varphi\big(\frac{\cx - \cx_i}{\ch}\big) = 1\) iff \(\cx_i\) lies in the hypercube of side \(\ch\) around \(\cx\), so \(V = \ch^D\) and \[ K = \sum_{i=1}^{N} \varphi\Big(\frac{\cx - \cx_i}{\ch}\Big) \]

\[ \cph_\ch(\cx) = \frac{K}{N V} = \frac{1}{N} \sum_{i=1}^{N} \underbrace{\frac{1}{\ch^D}\, \varphi\Big(\frac{\cx - \cx_i}{\ch}\Big)}_{K_\ch(\cx, \cx_i)} \]

A sum of kernels

Six samples on the x axis

A box around the first sample

Boxes around the first two samples

Boxes around the first three samples

A box of width h and height 1 over N h around every sample

The sum of the boxes is a step function

A small Gaussian around the first sample

Small Gaussians around the first two samples

Small Gaussians around the first three samples

A small Gaussian around every sample

The sum of the Gaussians is a smooth curve

\(N = 6\) samples \(\cx_i\).

Around every sample a box of width \(\ch\) and height \(\frac{1}{N\ch}\) (area \(\frac{1}{N}\)).

\(\cph_\ch(\cx)\) is their sum: still step-like, but no fixed bins.

A Gaussian kernel around every sample, again with area \(\frac{1}{N}\).

Their sum is a smooth estimate.

Kernels \(K_\text{type}(\cx, \cx_i)\)

Three kernels around a sample x_i: box of width h, triangle reaching h to each side and a Gaussian with standard deviation h

\[ K_\text{box}(\cx, \cx_i) = \begin{cases} \frac{1}{\ch} & \text{if } |\cx - \cx_i| \le \frac{\ch}{2} \\ 0 & \text{otherwise} \end{cases} \]

\[ \int K_\text{box}(\cx, \cx_i)\, d\cx = \ch \cdot \frac{1}{\ch} = 1 \]

\[ K_\text{tri}(\cx, \cx_i) = \begin{cases} \frac{1}{\ch} \Big(1 - \frac{|\cx - \cx_i|}{\ch}\Big) & \text{if } |\cx - \cx_i| \le \ch \\ 0 & \text{otherwise} \end{cases} \]

\[ \int K_\text{tri}(\cx, \cx_i)\, d\cx = \frac{1}{2} \cdot 2\ch \cdot \frac{1}{\ch} = 1 \]

\[ K_\text{Gauss}(\cx, \cx_i) = \frac{1}{\sqrt{2\pi}\, \ch}\, e^{-\frac{(\cx - \cx_i)^2}{2\ch^2}} \]

\[ \int K_\text{Gauss}(\cx, \cx_i)\, d\cx = 1 \]

What makes a kernel?

Which properties must \(K_\text{type}(\cx, \cx_i)\) have, so that the estimate \(\cph_\ch(\cx)\) is a density?

For all \(\cx\) and \(\cx_i\): \[ K_\text{type}(\cx, \cx_i) \ge 0, \qquad \int K_\text{type}(\cx, \cx_i)\, d\cx = 1 \]

\[ \int \cph_\ch(\cx)\, d\cx = \int \frac{1}{N} \sum_{i=1}^{N} K_\text{type}(\cx, \cx_i)\, d\cx = \frac{1}{N} \sum_{i=1}^{N} \int K_\text{type}(\cx, \cx_i)\, d\cx = \frac{1}{N} \cdot N = 1 \]

Usually also symmetric: \[ K_\text{type}(\cx, \cx_i) = K_\text{type}(\cx_i, \cx) \]

Assignment: \(K_\text{Gauss}(\cx, \cx_i)\) = norm.pdf(x, loc=x_i, scale=h)

Example: estimate by hand

Five samples at -1, 0, 0, 1.5 and 2 and the point x = 1

Samples: \(\;-1,\ 0,\ 0,\ 1.5,\ 2 \quad (N = 5)\)

1. Box kernel, \(\ch = 1\): what is \(\cph_\ch(1)\)?

2. The same for \(\ch = 2\).

3. Which \(\ch\) gives a larger estimate far from all samples, e.g. at \(\cx = 5\)?

Derivation: estimate by hand

Five samples and the point x = 1

A window of width 1 around x = 1 contains the sample 1.5

A window of width 2 around x = 1 contains the samples 0, 0, 1.5 and 2

\(\ch = 1\): window \([0.5, 1.5]\), \(K = 1\) \[ \cph_1(1) = \frac{1}{5 \cdot 1} = 0.2 \]

\(\ch = 2\): window \([0, 2]\), \(K = 4\) \[ \cph_2(1) = \frac{4}{5 \cdot 2} = 0.4 \]

Gaussian kernel, \(\ch = 1\): \[ \cph_1(1) = \frac{1}{5} \sum_{i=1}^{5} \mathcal{N}(1 - \cx_i;\, 0, 1) \approx 0.226 \]

The unit test of parzen in the assignment.

Parzen windows in 2D

ball kernel with a small h and the samples only

ball kernel with a small h placed on the first sample

ball kernel with a small h placed on two samples

ball kernel with a small h placed on three samples

ball kernel with a small h placed on every sample: the density estimate

ball kernel with a large h and the samples only

ball kernel with a large h placed on the first sample

ball kernel with a large h placed on two samples

ball kernel with a large h placed on three samples

ball kernel with a large h placed on every sample: the density estimate

Gaussian kernel with a small h and the samples only

Gaussian kernel with a small h placed on the first sample

Gaussian kernel with a small h placed on two samples

Gaussian kernel with a small h placed on three samples

Gaussian kernel with a small h placed on every sample: the density estimate

Gaussian kernel with a large h and the samples only

Gaussian kernel with a large h placed on the first sample

Gaussian kernel with a large h placed on two samples

Gaussian kernel with a large h placed on three samples

Gaussian kernel with a large h placed on every sample: the density estimate

Ball kernel, small \(\ch\) (radius). Put it on every sample and average.

Small \(\ch\): a separate disc around every sample, gaps in between.

Ball kernel, large \(\ch\).

Large \(\ch\): the discs overlap, but the estimate is still step-like.

Gaussian kernel, small \(\ch = \sigma\).

Small \(\ch\): a separate peak around every sample.

Gaussian kernel, large \(\ch = \sigma\).

Large \(\ch\): the kernels merge into a smooth density with two clusters.

Parzen windows in 2D: try it

Parzen windows: try it

Choosing the bandwidth \(h\)

The bandwidth controls the smoothness

Kernel density estimates for h = 0.08, 0.5 and 2 compared with the true density

Can we choose \(\ch\) by maximising the likelihood of the training samples?

No: for \(\ch \to 0\) every sample gets its own spike and \(\cph_\ch(\cx_i) \to \infty\). We need unseen data.

Cross-validation

Ten-fold cross-validation: in split j the part S_j is the validation set and the rest is the training set

\[ \cX = \bigcup_{j=1}^{J} S_j, \quad S_i \cap S_j = \emptyset, \qquad \cX^j_{\mathrm{tst}} = S_j, \quad \cX^j_{\mathrm{trn}} = \cX \setminus S_j \]

Log-likelihood on the validation set

For every split \(j\) and bandwidth \(\ch\): \[ L^j(\ch) = \sum_{\cx \in \cX^j_{\mathrm{tst}}} \log \cph_\ch\big(\cx \,;\, \cX^j_{\mathrm{trn}}\big) \]

\[ L(\ch) = \frac{1}{J} \sum_{j=1}^{J} L^j(\ch) \] \[ \ch^* = \arg\max_{\ch} L(\ch) \]

Why the logarithm?

The validation log-likelihood L(h) with its maximum at h* = 0.38

The log-likelihood of the training data keeps growing as h decreases

The assignment

Assignment: Parzen windows

Part 1: estimate

parzen(x, trn_x, h)

Gaussian kernel, x can be a vector.

Part 2: choose \(\ch\)

compute_Lh(...)

10-fold cross-validation, fminbound gives \(\ch^*\) for each class.

Part 3: classify

classify_bayes_parzen(...)

Bayes with 0/1 loss, priors \(\cp(\cA) = \frac{N_\cA}{N_\cA + N_\cC}\).

parzen is evaluated for all \(\cx\) and all training samples at once: build a \(M \times N\) matrix of kernel values, then average over \(N\).

Take-away for the lab

  • Without \(\cp(\cx \mid \ck)\) we estimate it from samples, each class separately
  • In general \(\cph(\cx) = \frac{K}{N V}\): \(k\)-NN fixes \(K\), Parzen fixes \(V\)
  • Parzen: \(\cph_\ch(\cx) = \frac{1}{N} \sum_i K_\ch(\cx, \cx_i)\), the kernel is a density
  • The bandwidth \(\ch\) matters more than the kernel shape
  • Choose \(\ch\) by cross-validation: maximise the log-likelihood of unseen data