K-Means Clustering
The default way to split unlabelled data into k groups — guess k centres, assign every point to its nearest, move each centre to the middle of what it caught, repeat. Fast and useful, but it needs you to pick k and it can land in the wrong answer.
Prerequisites: Variance, Intuitively
Sometimes nobody hands you labels. You have five hundred tickers and a matrix of returns, and the question is not "predict tomorrow" but "which of these behave like each other?" There is no right answer written down anywhere — you are looking for structure that is already in the data. That is clustering, and k-means is the method almost everyone reaches for first.
The cleanest way to picture it: you have to place ice-cream vans in a town so that everybody's walk to the nearest van is as short as possible. Park the vans somewhere. Every resident walks to whichever is closest, which carves the town into territories. Now look at each van's customers and move the van to the middle of that crowd. Some residents are now closer to a different van, so they switch, so the middles move again. Keep going until nobody switches. That loop is the algorithm.
What it is actually minimising
Write for the position of cluster 's centre (its centroid) and for the set of points assigned to it. K-means minimises the total squared distance from points to their own centre:
In plain English: add up how far every point sits from the middle of its own group, squaring so that a far-flung point hurts much more than two mildly off ones. Lower is tighter. The two steps of the loop each reduce this number and neither can ever increase it, which is why the algorithm always stops — but only at a local minimum, not necessarily the best one.
Worked example 1: one full pass
Six numbers: . Ask for and start the centres at and .
Assign. Point is away from the first centre and from the second, so it joins the first. Same for (distances and ) and (distances and ). Point is from the first and from the second, so it joins the second, along with and . Groups: and .
Move. The new centres are the group means, and .
Assign again. Nothing switches — is from the left centre and from the right — so the algorithm has converged. The final WCSS is
Worked example 2: why you cannot just minimise WCSS
Run the same six numbers with . The best split is , , , giving . With , take for . With every point is its own cluster and WCSS is exactly .
So the objective falls every time you add a cluster and bottoms out at a useless answer. You cannot choose by minimising the thing k-means minimises — you have to look at where the curve stops falling steeply and read off the bend.
K-means answers "given exactly groups, where should the centres go?" It never answers "how many groups are there?" and it never says "actually these points belong to no group." Every point gets assigned, including the outliers.
What this means in practice
For assets, the natural features are not prices but co-movements: convert a correlation into a distance and cluster on that, which is the standard route to sector-like groupings that ignore the official taxonomy (Clustering for Portfolios, Correlation Clustering). Because the objective is squared distance, feature scale is destiny — mixing a variable in percent with one in basis points hands the whole clustering to the larger-numbered one. Standardise first.
Two more practicalities. Run it several times from different starts and keep the lowest WCSS, since the algorithm only finds a local minimum; better still, seed with K-Means++ Initialization, which spreads the initial centres apart and usually fixes it. And validate with something outside the objective — an elbow, a silhouette score, or plain stability under resampling (Choosing the Number of Clusters).
It is worth knowing why the loop is guaranteed to stop. Each step is a partial minimisation of the same quantity: reassigning a point to a nearer centre can only shrink WCSS, and moving a centre to the mean of its members is precisely the position that minimises squared distance to them. Since WCSS never rises and there are only finitely many ways to split points into groups, the algorithm must halt. What it cannot promise is that the resting place is the global best — finding that is a genuinely hard combinatorial problem, which is why restarts matter.
Squared Euclidean distance is a strong assumption in disguise: it makes k-means look for round, similarly sized blobs. Give it two long parallel bands, or one dense cluster beside a sparse one, and it will slice straight through the real structure while reporting a perfectly good WCSS. When the shapes are irregular, reach for density-based or model-based clustering instead (Gaussian Mixture Models).
Related concepts
Practice in interviews
Further reading
- MacQueen, Some Methods for Classification and Analysis of Multivariate Observations (1967)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (Ch. 14)
- López de Prado, Machine Learning for Asset Managers (Ch. 4)