7.1 — Learning to Rank

Chapter 7 · Modern Statistical Learning in Practice

Prof. Xuhu Wan

Section 7.1 · Chapter 7 · Learning Statistics with Python

Learning to Rank

Modern Statistical Learning in Practice

Prof. Xuhu Wan

ISOM, HKUST Business School · 2026 Edition

Learning to Rank

In §4.5 you trained a boosting model to minimise squared error on next month’s return and then built a top-20 / bottom-20 portfolio from it. The portfolio never used the level of the prediction — only its order. You will show that a monotone transform of the prediction leaves the portfolio untouched while multiplying the MSE a hundredfold, then train three models that target the order directly and put all of them through the §4.5 walk-forward.

The same matrix, one new column

Standardise per month exactly as in §4.5 (median / std within the month, clipped at ±3). New this time: the target’s within-month percentile rank, centred at zero.

ret_next has mean 0.013 and std 0.082 with a range from −0.735 to +0.805 — a few meme months dominate any squared error. y_rank runs from −0.495 to +0.5 with std 0.289 in every month: the rank target has thrown away the level and kept the order.

Why does a top-K portfolio only need the order?

Take a prediction \(\hat y\) and replace it by \(3\tanh(50\hat y)\) — strictly increasing, very different values. What happens to the top-20 / bottom-20 portfolio and to the MSE?

  • Both change — the portfolio depends on the size of the predictions
  • Portfolio changes, MSE is unchanged
  • Portfolio is identical, MSE can change by orders of magnitude
  • Nothing changes — MSE is invariant to monotone transforms

Same 20 names long, same 20 short, same spread — and an MSE 110 times larger. A squared-error model spends its capacity on the level of returns, which is almost all noise (§6.2); a ranking model spends it on the order, which is all the portfolio uses.

Predict: does the rank IC see the transform?

The scoreboard of §4.5 was the rank IC — Spearman correlation between prediction and realised return across the month’s stocks. Before running: is it, like the portfolio, blind to the monotone transform?

spearmanr(p1, y)[0] and spearmanr(p2, y)[0] for October 2022, to 4 dp — the same number or different?

print(round(spearmanr(p1, y)[0], 4), round(spearmanr(p2, y)[0], 4))

-0.3316 -0.3316

Spearman −0.3316 for both; Pearson moves from −0.338 to −0.333. October 2022 was a month in which momentum lost — the high-momentum names fell hardest — and the rank IC records that with one number that no monotone transform can touch. It is the score that matches the trade, and the first column on every slide that follows.

The scoreboard: rank IC and the top-20 − bottom-20 spread

Same walk-forward as §4.5: test months from 2019-01, refit every 12 months on all earlier rows, two scores per month.

71 test months, 6 refits, 10 212 training rows for the first fit. Rank IC is the Spearman correlation between prediction and realised return across the 213 stocks in a month — a score that, like the portfolio, sees only the order. The spread’s Sharpe is \(\bar s / \sigma_s \cdot \sqrt{12}\), before costs.

Baseline: least squares on the raw return

A linear model with the MSE loss — the loss of every regression in Chapter 4.

Mean IC +0.001 (t = 0.06): the linear MSE model ranks no better than a coin, although its extremes earn a spread of +8.5 % a year (Sharpe 0.34). A one-sd move in momentum is worth +0.26 % a month, in volatility +0.26 %, in price-to-high −0.22 % — and last-month return gets nothing (−0.004): the loss, dominated by the ±80 % months, sees no reversal at all.

(a) Change the target: regress on the rank

Replacing ret_next by its within-month percentile changes…

  • Nothing — a linear model on ranks has the same order of predictions as a linear model on returns
  • The weighting of months: the ±80 % months no longer dominate the fit
  • The model class — ranks require a classifier
  • The features — they must be ranked too

IC −0.004 (t = −0.17), spread +5.0 % a year, Sharpe 0.24. Not better — with five price-based characteristics on 213 survivors there is little order to learn, whichever target you pick. Note the coefficient signs: last-month return negative (short-term reversal), 12-1 momentum positive — the two best-known cross-sectional anomalies, present but faint.

(b) Learn from pairs: the RankNet loss

Ranking is a statement about pairs: within month \(t\), stock \(i\) should score above stock \(j\) when \(r_{i} > r_{j}\). RankNet (Burges et al., 2005) models

\[P(i \succ j) = \sigma\big(f(x_i) - f(x_j)\big), \qquad \sigma(u) = \frac{1}{1 + e^{-u}},\]

and maximises the log-likelihood of the observed orderings. With a linear scorer \(f(x) = w^\top x\) the difference is \(w^\top (x_i - x_j)\): a logistic regression, without intercept, on pair differences, label \(\mathbb 1[r_i > r_j]\).

  • Pairs are formed within a month only — the model never compares October 2022 with March 2020.
  • 213 stocks give 22 578 pairs per month; 119 months give 2.7 million. We subsample 400 pairs per month with a fixed seed: the same \(w\) to two decimals, a hundredth of the runtime.
  • The score \(f\) is only ever used to sort — which is all §7.1 has asked of any model.

Fit the pairwise model walk-forward

IC −0.005 (t = −0.21), spread −1.8 % a year, Sharpe −0.09. The weights again say reversal (−0.031) and momentum (+0.026), and the model is about as good as the other two — that is, indistinguishable from zero. The pairwise loss is the right loss for a ranking problem; it cannot manufacture order that five characteristics on 213 stocks do not contain.

(c) The §4.5 boosting machine, on both targets

Same specification as §4.5 (max_depth=2, learning_rate=0.05, max_iter=200, min_samples_leaf=50); the only change is the target column.

The §4.5 number returns: IC −0.015 (t = −1.04) with the MSE loss. On the rank target the same trees give IC +0.017 (t = +0.98) and the best spread of the five, +9.3 % a year, Sharpe 0.64. The sign of the IC flipped with the target — which tells you the difference between the two is noise, not that ranks are magic.

Five models, one honest picture

Every mean IC lies within one standard error (0.02) of zero and the five cumulative spreads fan out from the same 2020 whipsaw. The ranking losses did what they promise — they changed which order the model learns: the MSE trees’ monthly ICs correlate only 0.31 with OLS and 0.20 with the pairwise model, while the three rank-based models move together (0.87–0.92) — but 71 months of a survivorship-biased file cannot tell 0.6 from 0. Report the protocol, not the best line.

Where this leads: preference-based asset pricing

The pairwise loss you just fitted is a preference model: the data are statements “\(i\) was better than \(j\) this month” and the score \(f(x)\) is a latent utility that rationalises them. Push the idea further and it becomes a research programme:

  • A ranking loss estimates the order of expected returns directly, which is what a long-short portfolio, a top-decile screen or a stochastic discount factor’s sort actually needs; squared error estimates a level nobody trades.
  • The same machinery scales from a linear \(w\) to the attention scorer of §4.5, and from pairs to listwise losses that weight the top of the order more heavily (the top-20 matter more than the middle 170).
  • Estimation, inference and portfolio construction can be done in one step, on one objective, instead of a regression followed by a sort — the theme of Prof. Wan’s current research on ranking-loss and preference-based asset-pricing models.

Note

Nothing in this slide changes the verdict of the previous one: on five characteristics and 213 survivors, no loss finds a reliable order. The loss decides what you estimate; the data decide whether there is anything to find.

Your turn: how concentrated should the portfolio be?

evaluate builds a top-K / bottom-K spread with K=20. Re-score the rank-target boosting predictions with K=10 (more concentrated) or K=40 (more diversified) and compare the Sharpe ratio with the K = 20 value of 0.64. Which direction improves the Sharpe, and is the change larger than the noise you saw across models?

K = 10: spread +20.7 % a year, Sharpe 1.10; K = 40: +8.9 %, Sharpe 0.75. A Sharpe of 1.1 from the ten most extreme names looks like a discovery — hold that thought for §7.6, where you will learn what the best of three values of K is worth. Over 71 months none of the three is separable from the others, or from zero.

What you discovered

  • A top-K portfolio uses only the order of predictions: a monotone transform left the top-20, bottom-20 and spread untouched while multiplying the MSE 110-fold.
  • Three ways to target the order — a within-month rank target, a pairwise logistic (RankNet) loss on subsampled pairs, and the §4.5 trees on ranks — all fit inside the §4.5 walk-forward without changing the protocol.
  • Rank IC: +0.001 (OLS), −0.004 (rank target), −0.005 (pairwise), −0.015 (trees, MSE), +0.017 (trees, rank) — every one within one standard error (0.02) of zero. Spread Sharpe from −0.09 to 0.64, none evidence.
  • The weights of every ranking model say reversal (last month negative) and momentum (12-1 positive) — the signs of the literature, at a strength 71 months cannot confirm.
  • Preference-based asset pricing takes the pairwise idea to its conclusion: estimate the order, the utility and the portfolio on one objective.

Next: §7.2 — stop predicting a number; predict an interval, and check that it keeps its promise.