See how k-means assigns observations and moves group centres, why scaling and cluster count change the result, and what a fitted peer group can establish.
Clustering
Clustering groups observations by a chosen notion of similarity, without supplied target labels telling the algorithm which group each observation belongs to. A quant might use it to construct an exploratory peer universe: assets with similar measured risk, liquidity or return behaviour. The result depends on what was measured and how similarity was defined; a group is a research object to interpret, rather than an economic fact discovered beyond doubt.
Clustering is one task within unsupervised learning. It differs from classification, where observed class labels guide fitting. Calling a cluster “defensive stocks” after inspecting it does not turn that description into a ground-truth label or a forecast of future returns.
What k-means is trying to make small
K-means is a particular clustering method. Choose a cluster count k, represent each observation by a feature vector xi, and give each group a centre μj. Its within-cluster sum of squares is
J=j=1∑ki∈Cj∑∥xi−μj∥2,
where Cj contains the observations assigned to group j. This is a sum, rather than an average. In the scalar example below, its units are squared feature units. A centre is the mean of its group and need not be an observed data point.
The usual Lloyd procedure alternates two operations:
Assign: hold the centres fixed and send each observation to its nearest centre under Euclidean distance.
Recentre: hold the assignments fixed and replace each nonempty group's centre with its mean.
Each operation can lower J or leave it unchanged. That makes the procedure easy to audit, but it does not guarantee the globally best partition. Different starting centres can lead to different local solutions; comparing several starts by their final objective helps address that dependence. A real implementation also needs explicit rules for equal-distance ties, empty groups and stopping. The k-Means Clustering lesson develops the objective and alternating minimisation in more detail (membership; public preview).
Watch one update
Take four synthetic dimensionless scalar features: 0,1,9,10. These are invented observations, with no supplied target labels. Stipulate k=2 and start the centres at 0 and 10.
The nearest-centre assignment gives group A {0,1} and group B {9,10}. Before moving the centres, the squared distances sum to
Jassign=(0−0)2+(1−0)2+(9−10)2+(10−10)2=2.
Now update each centre to its group mean:
μA=20+1=0.5,μB=29+10=9.5.
Every observation lies 0.5 from its new centre, so
Jupdate=4(0.5)2=1.
Synthetic dimensionless features 0, 1, 9, 10 are assigned to initial centres 0 and 10, giving SSE 2. Moving centres to the group means 0.5 and 9.5 reduces SSE to 1 with the same memberships. Filled circles are observations; hollow squares are centres.
Original synthetic k-means step. The geometry and memberships stay fixed while the group representatives move; SSE is the sum of squared feature distances.
Repeating the assignment with centres 0.5 and 9.5 leaves the groups unchanged. Their means are unchanged too. Here the fit has settled. For this tiny dataset, enumerating every nonempty two-group partition also confirms that this partition has the smallest SSE. That independent check is possible because the example is small; the iterative algorithm itself supplied no general optimality certificate.
The choices that define the groups
Features and scaling choose what counts as close. If an asset is represented by volatility and turnover, Euclidean distance adds squared coordinate differences. Changing the numerical scale of one coordinate changes its weight: multiplying that coordinate by a multiplies its contribution to squared distance by a2. Subtracting each feature's mean and dividing by its sample standard deviation can put features on comparable scales; constant features need explicit handling. This standardization also makes a modelling choice about relative importance. A high-variance coordinate may contain useful signal or irrelevant measurement scale; blindly equalising it is no substitute for deciding what a peer group should mean.
The metric and algorithm choose the geometry. Ordinary k-means uses squared Euclidean distance and is suited to compact groups around means. Changing the distance while retaining the same mean-update rule generally changes the optimisation problem; the mean need not remain the correct representative. Hierarchical methods instead construct nested groupings under a linkage rule, while density methods such as DBSCAN can identify connected dense regions and leave observations labelled as noise. These methods answer different grouping questions, rather than supplying interchangeable versions of one universal partition.
The cluster count chooses the resolution. In the toy data, the best achievable k-means costs include:
Chosen groups
Centres for the illustrated optimum
SSE
k=1
5
82
k=2
0.5,9.5
1
k=4
0,1,9,10
0
The zero cost with a separate centre at every observation is easy to obtain and provides no useful compression. Minimising training SSE alone therefore cannot choose the cluster count. Separation measures, changes in the objective as k varies, sensitivity to features and starts, and usefulness for the stated research question can all inform the choice. None turns a chosen count into a uniquely correct economic taxonomy.
Using a peer group in research
After fitting, inspect what distinguishes the groups in the original units. Check whether a handful of extreme observations drives the result and whether modest changes in the estimation window or feature choices radically change membership. Group IDs have no inherent order: swapping the names A and B leaves the partition unchanged.
If those groups feed a historical strategy evaluation, fit scaling and centres only on observations available before the decision, then apply the declared rule to later observations. Refitting on the complete history can introduce look-ahead bias, even though fitting used no return labels. Finally, test any claim about subsequent returns separately: a compact historical group does not establish predictive value or remove shared correlation risk within a portfolio.
Sources
Stephen Boyd and Lieven Vandenberghe, Introduction to Applied Linear Algebra: Vectors, Matrices, and Least Squares, Chapter 4, especially §§4.1–4.3, pp. 69–76: clustering, group representatives, the objective and the k-means procedure. Their objective averages squared distances; this article explicitly uses the sum.
Scikit-learn developers, Clustering, sections on k-means, hierarchical clustering and DBSCAN: the methods' geometry and differing group definitions.
Scikit-learn developers, Common pitfalls: data leakage: fitting transformations on the permitted training observations before applying them to later data.