The Claude Quant PlaybookDownload ebookDownload ebook

k-nearest neighbours

To predict at a new point, find the k most similar past examples and let them vote. Small k follows the data closely; large k averages over a wider area.

Build a trading system with Claude.Download ebook

01The formula

the predicted chance of class 1 at the point x
how many neighbours vote
the k training points closest to x
the label of neighbour i: 1 or 0

02See it move

03Where it is used

04Why traders use it

It needs no formula for the relationship, only a sense of what 'similar' means. It is also the clearest picture of the bias–variance trade-off: k is a single dial between the two.

05Worked example

Suppose the true chance of class 1 near x is 70%. With k = 15 neighbours the vote has a standard error of 11.8%, and the majority picks the wrong class 5.0% of the time. On 200 training points the model behaves as if it had about 13.3 free parameters.

06How it is built

  1. Choose a distance, for example Euclidean distance between standardised features.
  2. For a new point , find the nearest training points .
  3. Average their labels: ; predict class 1 when .
  4. The vote is a binomial average, so its standard error is : more neighbours, less variance, but from a wider and less relevant neighbourhood.

07The common mistake

Forgetting to scale the features. If one feature is in dollars and another in fractions, distance is decided almost entirely by the dollars.

08In Python and LaTeX

from scipy.stats import binom
import numpy as np
k, q = 15, 0.7
print(round(np.sqrt(q * (1 - q) / k), 4), round(binom.cdf(k // 2, k, q), 4))  # std error, chance the vote is wrong
\hat p(x) = \frac{1}{k}\sum_{i \in N_k(x)} y_i

09Where it fits

Rather not code the maths yourself? Describe your trading idea and let AI build the bot.

Build a bot with AgenKit

The worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.