Solution (source code)

= Solution

A weighted nearest-neighbours classifier predicts one when
$$
\sum_{i=1}^kw_iY_{(i)}\geq\frac12,
\qquad w_i\geq0,\quad\sum_iw_i=1,
$$
where neighbours are distance ordered. Under the usual smooth-density and smooth-regression assumptions, asymptotically optimal weights downweight distant neighbours, for example normalized positive parts of $1-(i/k)^{2/p}$. The optimal weighted-nearest-neighbour theorem gives smaller leading asymptotic regret than equal weights.