5.1 Clustering#

Clustering sorts rows into groups of similar rows without being told what the groups are. This section is taught in the same session as 5. The session 5 slides are on the chapter 5 page.2 and 5.3. Each month of the firm's data is one row, and the question is which months look alike.

▶ Open in Colab

Learning objectives

  • Say what unsupervised learning is, and why the output has no score against a right answer.

  • Run k-means by hand: assign each row to its nearest centre, move each centre to the middle of its rows, repeat.

  • Say why columns are put on one scale first, and why the number of groups is a judgement.

5.1.1 Learning without a label#

In chapter 4 each month had four features (the market’s, size’s, value’s and momentum’s returns) and a label, Deere’s return. Delete the label. What is left is a table of 24 rows and four columns, and a different kind of question: do some of these months resemble each other? Answering it is clustering: the model proposes groups, and every month goes in exactly one.

This is the first kind of unsupervised learning from section 4.1. Nothing marks a grouping as correct. You judge it by whether the groups are tight, whether they are stable, and above all whether they mean something to the person who will use them.

5.1.2 One scale for every column#

Distance is the idea underneath clustering: two months are alike when their rows are close. But a distance adds up the columns, so a column with big numbers dominates. Look at how much each column moves:

from ml_unsup import months, standardize
from finance_basics import MONTHS

X = months()                                  # market, size, value, momentum, in percent
z, means, spreads = standardize(X)
print("typical swing of each column (its spread):", [round(s, 1) for s in spreads])
print("first month before:", [round(v, 1) for v in X[0]])
print("first month after: ", [round(v, 1) for v in z[0]])

The market swings about 4 points a month and size about 2. Left alone, a size difference would count for half as much as a market difference just because of the units. Standardizing subtracts each column’s average and divides by its spread, so every column is measured in “typical swings” and counts equally. Nearly every method in this chapter starts here.

5.1.3 k-means#

k-means needs one choice from you, the number of groups, k. Then it repeats two steps:

  1. Assign. Give each month to the nearest group centre.

  2. Move. Put each centre at the average of the months now assigned to it.

It starts with some centres, and stops when no month changes group. kmeans does it:

from ml_unsup import months, standardize, kmeans, inertia
from finance_basics import MONTHS

X = months()
z, _, _ = standardize(X)
centers, labels = kmeans(z, 3)

for g in range(3):
    mine = [i for i in range(24) if labels[i] == g]
    avg = [sum(X[i][j] for i in mine) / len(mine) for j in range(4)]
    print(f"group {g + 1}: {len(mine)} months, average market/size/value/momentum:", [round(v, 1) for v in avg])
    print("   ", ", ".join(MONTHS[i] for i in mine))
print("tightness (lower is tighter):", round(inertia(z, centers, labels), 1))

Read the averages and name the groups yourself: the model returns only “group 1, 2, 3”. One group is a steady rally with strong momentum (the market up about 2.7 points, momentum up 2.4). One is a rally led by small and value stocks, with momentum falling. One is five months of sell-off, the market down about 5 points on average. Try it:

/// Group the months · k-means on the four benchmarks
The groups the model found (it was never shown Deere's return)
GroupMonthsAvg marketAvg Deere
Tightness: sum of squared distances to the group centres
Inertia for k = 1, 2, 3, 4, 5
Each point is one month (illustrative returns in percent, not market data). The model sees four numbers per month (market, size, value, momentum), each put on the same scale; the chart shows two of them. The group names in your head are yours: the model only returns the numbers 1, 2, 3.

Checkpoint.

Why are the columns standardized before k-means?

5.1.4 How many groups?#

k-means will give you whatever k you ask for, and the tightness score (the inertia: each month’s squared distance to its group centre, added up) always falls as k grows. At k equal to the number of months it is zero. So the score cannot choose k.

from ml_unsup import months, standardize, kmeans, inertia

z, _, _ = standardize(months())
for k in range(1, 6):
    c, l = kmeans(z, k)
    print(k, "groups: inertia", round(inertia(z, c, l), 1), " sizes", sorted([l.count(g) for g in range(k)], reverse=True))

The drop is 92 to 69, then to 49, 35 and 30. A textbook “elbow”, a sharp bend after which extra groups barely help, would show here as a clear change of slope, and there is only a mild one around 3 to 4. With 24 months that is the honest result. Choose k by asking whether you can name each group and whether it holds up: at 5 groups three of them have three or four months, which is too few to say anything about.

Two more cautions. The result depends on where k-means starts: shuffle the order of the months and the tightness moves (about 48 to 49) and a month or two changes group. And the groups describe these 24 months. They are not a forecast of the 25th.

Now check the groups against something the model never saw. Deere’s average return in each group:

from ml_unsup import months, standardize, kmeans

X = months()
de = months(("DE",))
z, _, _ = standardize(X)
_, labels = kmeans(z, 3)
for g in range(3):
    mine = [i for i in range(24) if labels[i] == g]
    print("group", g + 1, ": Deere averaged", round(sum(de[i][0] for i in mine) / len(mine), 1), "% over", len(mine), "months")

Deere averaged about +1.8, +2.7 and -7.7 across the three groups, though its return was never given to the model. That is the use of clustering at the firm: a regime label (“sell-off month”) you can attach to each month and then study. It is also why the next section asks how to describe these months with fewer numbers.

Checkpoint.

The inertia is lower at k = 5 than at k = 3. What does that tell you?

5.1.5 Exercise#

Open the Colab notebook and run section 1.

  1. Cluster the months using only market and momentum. Do the same five sell-off months stay together? Which month moves?

  2. Add Deere and Caterpillar as two more columns (months(NAMES)) and cluster again at k = 3. Does the sell-off group grow or shrink, and why would it?

  3. Run k = 3 from three different row orders (the notebook shows how). Which months change group? What would you tell a client about those months?

Further reading#

  • Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, An Introduction to Statistical Learning, chapter 12 (2021) — clustering and principal components, free to read.

  • scikit-learn, Clustering — the methods beyond k-means, and when each fits.