Run XGBoost and LightGBM on the same 17,075 rows, with the same splits, and on the size of next month's move their rank correlations are 0.2092 and 0.2097: a difference of 0.0005, and they pick 97% of the same stocks. On the return itself they are 0.0137 and 0.0182, and neither is different from zero. This topic builds the comparison and the test that tells you which of these differences matter.
The same question for both
Where we are. The XGBoost skill ended with a ranker judged the way a research team would judge it. LightGBM has now been built piece by piece: leaf-wise trees, histograms, categories, weights, tuning, ranking and interpretation. The last question is the practical one: *for this problem, does it matter which library I use?* To answer it fairly, give both libraries the same features, the same training and test months, matching tree sizes and the same score, and then compare the scores, not the libraries.
Matching settings is the first step. A tree of depth 3 can have up to 8 leaves, so XGBoost gets max_depth=3 and LightGBM num_leaves=8, max_depth=3; both get 50 trees, a learning rate of 0.05 and one thread. They are still different models: they bin the features differently, regularise differently, and break ties differently. Worked example 1. *Problem:* how different are two models with matching settings on the same data? *Plan:* fit both on the same 500 rows of a curved relationship and compare their predictions. *Steps:* below.
rng = np.random.default_rng(0)n = 500X = pd.DataFrame({"a": rng.normal(size=n), "b": rng.normal(size=n)})y = np.sin(2 * X["a"]) + 0.3 * X["b"] + rng.normal(0, 0.3, n)from_xgb = make_model("xgb").fit(X, y).predict(X)1from_lgb = make_model("lgb").fit(X, y).predict(X)2print(round(float(np.corrcoef(from_xgb, from_lgb)[0, 1]), 4), round(float(np.abs(from_xgb - from_lgb).max()), 3))print(round(float(np.sqrt(np.mean((y - from_xgb) ** 2))), 4), round(float(np.sqrt(np.mean((y - from_lgb) ** 2))), 4))
0.9916 0.666 0.319 0.3429
- 1The same settings in spirit: depth 3, 50 trees, learning rate 0.05.
- 2Two libraries, one function to create each.
*Check:* the two sets of predictions correlate at 0.9916, but the largest difference on one row is 0.666, and the training errors differ (0.319 for XGBoost, 0.343 for LightGBM). *What to notice:* nearly the same model, not the same model. Any comparison on real data has to ask whether such differences matter for the decision, which depends on noise.
One validation, two libraries
The validation is the one of the XGBoost capstone: walk forward in time. The months are cut into blocks; fold i trains on the blocks before it (minus a one-month embargo) and predicts block i. Every prediction is made by a model that never saw that month or the one before it. The function is the same for both libraries, with only the model changing.
- 1FeaturesSix features per stock and month, ranked among the stocks of the month so that the level of the market drops out.
- 2Folds4 expanding folds with an embargo of 1 month; the first block of months is only ever used for training.
- 3ScoresOne prediction per test row from
make_model(kind). - 4Monthly ICThe rank correlation between the scores and the target among the stocks of each month: 244 numbers per model.
Is a difference a difference?
Each model now has 244 monthly rank correlations. How do we say whether one is better? Take the mean of each and compare? The two series share most of their noise: in a month when stocks are unpredictable, both models are bad. The right test works on the difference month by month, d = IC_a − IC_b, and asks whether its mean is far from 0 in units of its own standard error.
Worked example 2. *Problem:* two series of 60 monthly scores share a large common component (a hard month is hard for both): a is better than b by 0.01 on average, with a little noise of its own. Does an unpaired comparison see it, and a paired one? *Plan:* compute the difference of the means over its unpaired standard error, then paired_difference. *Steps:* below.
rng = np.random.default_rng(0)dates = pd.date_range("2010-01-31", periods=60, freq="ME")shared = rng.normal(0, 0.06, 60)a = pd.Series(0.03 + shared + rng.normal(0, 0.01, 60), index=dates)b = pd.Series(0.02 + shared + rng.normal(0, 0.01, 60), index=dates)unpaired = (a.mean() - b.mean()) / np.sqrt(a.var(ddof=1) / 60 + b.var(ddof=1) / 60)1print(round(a.mean() - b.mean(), 4), round(unpaired, 2))print({k: round(v, 4) for k, v in paired_difference(a, b).items()})2
0.0115 1.16
{'n': 60, 'mean': 0.0115, 'std': 0.0143, 't_stat': 6.2609, 'share_a_better': 0.8}- 1Treating the two series as independent: the common noise inflates the standard error.
- 2Testing the monthly difference removes the common noise.
*Check:* the difference of the means is 0.0115. Treated as independent samples, its t-statistic is 1.16: not significant. Tested month by month, the same data give t = 6.26, and a beats b in 80 % of the months. *What to notice:* nothing changed except the question. When two results come from the same months, always test the pairs: it is the more powerful and the more honest test.
Monthly scores of a market are not independent from month to month (volatility clusters), so the t-statistic overstates the evidence a little. It is still the right tool for ranking differences from large to small, and for saying that a difference of 1.4 is not worth a decision.
Do they pick the same stocks?
A last pair of questions that an IC does not answer: do the two models rank the stocks in the same order, and if you bought the top fifth of each, would you hold the same names? Two numbers answer them. Rank agreement is the average over the months of the rank correlation between the two models' scores. Top overlap is the share of the top fifth that both pick.
Worked example 3. *Problem:* five stocks in one month. Model A ranks them a, b, c, d, e (scores 5, 4, 3, 2, 1). Model B scores them 5, 1, 4, 2, 3. What are the agreement and the overlap of the top 40 %? *Plan:* the ranks of B are a, c, e, d, b; the top two of A are {a, b}, of B {a, c}. *Steps:* below.
tiny = pd.DataFrame({"date": pd.to_datetime(["2020-01-31"] * 5), "ticker": list("abcde")})print(rank_agreement(tiny, [5, 4, 3, 2, 1], [5, 1, 4, 2, 3]))print(top_overlap(tiny, [5, 4, 3, 2, 1], [5, 1, 4, 2, 3], 0.4))
0.3 0.5
*Check:* the Spearman correlation is 0.3 and the overlap 0.5: of the two picks, one is shared. Two random models would share, on average, the fraction picked (0.4); two identical models share all. *What to notice:* an agreement of 0.3 looks small and an overlap of 50 % looks large. Always read both against the random level and the perfect level.
- FunctionQuestion it answers
- make_model(kind)What are the two models?depth 3, 50 trees
- walk_forward_scores(panel, columns, target, kind)What would each model have predicted, never having seen the month?NaN for the first block
- summarise(series)How good and how reliable is one model?ddof = 1
- paired_difference(a, b)Is one really better than the other?only the common dates
- rank_agreement(panel, a, b)Do they rank the stocks alike?inside each date
- top_overlap(panel, a, b, share)Do they pick the same stocks?k at least 1
The verdict
Worked example 4. *Problem:* run the whole comparison on the real prices, twice: the target is the return of the next month (fwd, where the features know almost nothing) and its size (move, where they know a lot). *Plan:* study builds the panel, normalises the features month by month, walks forward, scores both libraries, and prints the summaries, the paired difference and the two similarity numbers. *Steps:* below.
out = study(load_close())print("panel:", out["rows"], "rows,", out["dates"], "months")for target in ("fwd", "move"): r = out[target] print(target, round(r["xgb"]["mean"], 4), round(r["xgb"]["t_stat"], 2), "|", round(r["lgb"]["mean"], 4), round(r["lgb"]["t_stat"], 2), "|", round(r["difference"]["mean"], 4), round(r["difference"]["t_stat"], 2), "|", round(r["agreement"], 3), round(r["overlap"], 3))
panel: 17075 rows, 307 months fwd 0.0137 1.22 | 0.0182 1.59 | -0.0044 -1.44 | 0.936 0.868 move 0.2092 16.13 | 0.2097 16.27 | -0.0005 -0.69 | 0.997 0.97
*Check:* the panel has 17,075 rows over 307 months, and each model is judged on 244 months. The return of the next month: XGBoost has a rank correlation of 0.0137 (t 1.22) and LightGBM 0.0182 (t 1.59): neither is distinguishable from 0. Their difference, -0.0044, has t -1.44: not significant either. They agree on the order of the stocks at 0.936 and pick 86.8 % of the same top fifth. The size of the move: 0.2092 (t 16.13) and 0.2097 (t 16.27): a strong signal in both. The difference is -0.0005 (t -0.69), they agree at 0.997 and pick 97.0 % of the same stocks. *What to notice:* where there is a signal, two good libraries find the same one and the choice does not matter. Where there is none, each follows its own noise: 0.936 agreement and 87 % overlap are the sound of two models reading the same random numbers differently, and a small difference between their ICs is not a reason to prefer either.
On a problem like this one, choose by what the rest of the pipeline needs: the speed on big data (LightGBM's histograms and leaf-wise growth), native categorical columns (LightGBM), monotone constraints and a mature GPU path (XGBoost), the format of the trained model you must ship. Accuracy will rarely decide it, and when it seems to, run the paired test before believing it.
What you will build
The lab follows the lecture. Task 1 makes the two models. Task 2 is the walk-forward scoring. Task 3 summarises a series of monthly scores. Task 4 is the paired test of the difference. Tasks 5 and 6 measure how much the two models agree on the ranking and on the top fifth. Task 7 runs the comparison on the real prices, for the return of the next month and for its size.
That ends the LightGBM skill. You have grown trees leaf-wise, built histograms and their subtraction, cut categories, weighted a rare event, tuned without fooling yourself, ranked with lambdarank, read a model four ways and compared it with XGBoost on one panel. Next in this part of the course: the alpha research skills, which turn models like these into signals that can be tested, combined and traded.
Lab · what's graded
7 tasks · 100 pts- The two models10 pts
- Walk-forward scores20 pts
- Summary of monthly scores5 pts
- The paired test15 pts
- Rank agreement10 pts
- Top overlap10 pts
- XGBoost against LightGBM on the panel30 pts