Exercises — Chapter 31: Chess Database Analytics

Difficulty: ⭐ recall and concepts · ⭐⭐ apply to a position or a query · ⭐⭐⭐ analyse, extend code, judge · ⭐⭐⭐⭐ open-ended research.

All numbers in the solutions come from the chapter's scripts (code/out_*.txt); every engine evaluation is Stockfish 19, White's point of view, at the stated depth.


Part A — Concepts ⭐

A1. The chapter title promises "10 million games". Give the exact number of games in the book's data snapshot, broken down by source, and say what the rest of the "10 million" consists of.

Answer

4,471,751 games: 4,000,000 Lichess games, 373,918 broadcast (OTB relay) rows and 97,833 PGN Mentor master games. The 6,193,530 Lichess puzzles bring the record count to 10,665,281 — but puzzles are positions, not games.

A2. Why does df.white_title != "BOT" silently remove almost every human game, and what should you use instead?

Answer

The title column is a nullable string; untitled players have a missing value, and comparing missing with "BOT" yields missing, which a boolean filter treats as False — the row is dropped. Use chessdata.is_bot_game(df) (it fills missing values first) and print the row count after the filter.

A3. Define the rating-adjusted score. In all Lichess games the Scotch Game scored 54.6% with an Elo-expected score of 50.4%. What is its rating-adjusted score, and what part of White's advantage is still inside that number?

Answer

Adj = actual score − mean Elo-expected score, in points: 54.6 − 50.4 = +4.2. It still contains White's first-move edge (White's average adj in balanced Lichess games is +1.4 to +2.5 by band), so compare openings with each other, or subtract the band average as in Section 31.9.

A4. When White was rated more than 100 points higher, White scored 68.5%; when Black was, White scored 34.2%. Explain in two sentences why opening statistics in this book use balanced games.

Answer

Rating gaps move the score far more than any opening does, so unbalanced games add a huge source of variation unrelated to the opening — and openings are not chosen equally often by the stronger and weaker side. Restricting to |rating difference| ≤ 100 removes most of that variation before you compare.

A5. The Lichess move-text subset contains all server-analysed games and one in eight of the rest. What weights make it represent the population, and what is the Kish effective sample size used for?

Answer

Weight 1 for analysed games and 8 for the others (the weights sum to 3,939,477 against a population of 3,938,632). Kish n = (Σw)²/Σw² is the number of equally weighted games the weighted sample is worth; use it inside interval formulas (853,303 rows → n_eff 542,092).

A6. What is the difference between a move-order query and a position query? Give the transposition share the chapter found for the QGD Exchange position on Lichess.

Answer

A move-order query matches the exact sequence of moves; a position query matches every game that reaches the same position at the same ply, including transpositions. For 1.d4 d5 2.c4 e6 3.Nc3 Nf6 4.cxd5 exd5, 70.6% of the 1,320 Lichess games (932) arrived by another order.

A7. Knights stand on f3 at move 15 in 33.4% of OTB side-games, with owner_adj +0.1. Why is the heat map of frequency not a heat map of quality?

Answer

Frequency reflects opening choice (f3 is the knight's developing square in most openings). Quality must be measured by an outcome, such as the owner's rating-adjusted score — and across squares the two are not positively related (Spearman −0.12 OTB, −0.33 Lichess).

A8. Name the statistical trap in "players whose clock fell below 10% of their base time scored 16–18 points below expectation, so low clocks cause losses".

Answer

Reverse causality (outcome contamination): bad, long, hard positions consume time, and losses on time count as both low-clocked and lost. Use predictors fixed before the outcome and control for the position.

A9. What is split-half replication, and what does it not test?

Answer

Randomly assign games to two halves, compute every cell in both, and count cells that are significant with the same sign in both. It tests whether a finding is noise; it does not test causation, because confounding is present in both halves.


Part B — Positions ⭐⭐

B1. Analyze this position — the Scotch, Black to move. In balanced Lichess games that reached this position, Black's most common move was 4...Nxd4 (43.0% of weighted replies), after which White scored 56.9%. Evaluate 4...Nxd4 5.Qxd4 and find Black's best move.

Scotch after 4.Nxd4

FEN: r1bqkbnr/pppp1ppp/2n5/8/3NP3/8/PPP2PPP/RNBQKB1R b KQkq - 0 4

Solution

Stockfish 19, MultiPV 3 at depth 20: 4...Nf6 +0.08, 4...Qf6 +0.17, 4...Bc5 +0.29 (depth 24: 4...Nf6 +0.09). After 4...Nxd4 5.Qxd4 the engine gives +0.76 (d20) / +0.62 (d24). The capture trades Black's only developed piece and centralises White's queen without loss of time; 4...Nf6 develops with an attack on e4. The database's "best opening" result rests partly on this one popular inaccuracy — it appears in only 3.5% of OTB games.

B2. Analyze this position — the Italian, Black to move. Lichess players chose 3...Nf6 (34.5%), 3...Bc5 (30.5%) and 3...h6 (13.3%). Rank the three by engine evaluation and say whether the database shows a practical difference.

Italian after 3.Bc4

FEN: r1bqkbnr/pppp1ppp/2n5/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3

Solution

Stockfish 19 MultiPV at depth 20: 3...Bc5 +0.23, 3...Nf6 +0.23, 3...Be7 +0.44; at depth 24 3...Bc5 +0.11. 3...h6 is not in the top three. In the database White scored 52.3% [51.1–53.5] after 3...Nf6, 51.1% [49.8–52.4] after 3...Bc5 and 51.9% [49.9–53.9] after 3...h6 — overlapping intervals, so no practical difference at club level. 3...h6 is a wasted tempo that strong players drop (0.8% of OTB replies) without it costing amateurs measurably.

B3. Build the plan — the London set-up, Black to move. Lichess players most often chose 3...e6 (28.2%); OTB players and the masters explorer most often 3...c5 (47.9% OTB; 5,489 of 9,246 masters games). What does the engine prefer, and what does 3...c5 aim for?

London set-up after 3.Bf4

FEN: rnbqkb1r/ppp1pppp/5n2/3p4/3P1B2/5N2/PPP1PPPP/RN1QKB1R b KQkq - 3 3

Solution

Stockfish 19 MultiPV at depth 20: 3...c5 +0.05, 3...e6 +0.12, 3...Bf5 +0.23 (depth 24: 3...c5 +0.05). 3...c5 attacks d4 at once, before White's c3/e3 chain is complete, and prepares ...Nc6 and ...Qb6 against b2 — the main line in the engine's PV is 4.e3 e6 5.Nbd2 Nc6 6.Bb5 Qb6. The plan is pressure on d4 and b2, the London's two soft spots, instead of passive development.

B4. Build the plan — QGD Exchange, White to move. OTB players chose 5.Bg5 in 94.6% of games (White 58.1%). Confirm the move with the engine and outline White's long-term plan in this structure.

QGD Exchange after 4...exd5

FEN: rnbqkb1r/ppp2ppp/5n2/3p4/3P4/2N5/PP2PPPP/R1BQKBNR w KQkq - 0 5

Solution

Stockfish 19 MultiPV at depth 20: 5.Bg5 +0.27, 5.Nf3 +0.14, 5.Bf4 +0.12 (depth 24: 5.Bg5 +0.21, PV 5...Be7 6.e3 O-O 7.Bd3 h6 8.Bh4). The structure (once Black plays ...c6) is the Carlsbad: White develops with e3, Bd3, Nge2 or Nf3, Qc2, castles, and then plays the minority attack b4–b5 to create a weak c-pawn — the plan from Chapter 2's Smyslov–Keres. In OTB data the attack itself was played in 9.1% of 11,934 White-owned Carlsbad games.

B5. Endgame technique — Fedoseev–Harikrishna, Polanica-Zdrój 2026, Black to move. White is "a pawn up" by every database definition. Find Black's move and explain why the label is misleading.

Fedoseev–Harikrishna after 30.Rxd5

FEN: 8/p4pk1/5rp1/3R3p/4r3/1P4P1/P4PKP/2R5 b - - 0 30

Solution

30...Re2! Stockfish 19 MultiPV at depth 20: 30...Re2 +0.03, 30...Ra6 +1.04, 30...Re7 +1.04; depth 24: 30...Re2 −0.06 with 31.Rf1 Rxa2. The game went 30...Re2 31.Rf1 Rxa2 and was drawn. The a2 pawn was loose and the rook reaches the second rank with tempo on f2; passive moves (…Ra6, …Re7) give White a real extra pawn (+1.04). The database counted a pawn that was already lost.

B6. Build the plan — Carlsen–Firouzja, Norway Chess 2026, White to move. White is a pawn up after 32...Qxf8; Black has four minutes left. Compare the engine's top moves; what happens after the careless 33.Kh1?

Carlsen–Firouzja after 32...Qxf8

FEN: 5qk1/2p4p/1b2p3/8/1P2P1p1/2PQ2P1/3N2KP/8 w - - 0 33

Solution

Stockfish 19 MultiPV at depth 20: 33.Qe2 +1.63, 33.Qf1 +1.37, 33.Kh1 +0.00 (PV 33...Qf2 34.Qd8+ Kf7 35.Qd7+ Kf8 36.Qd8+ — the engine's line ends in repeated checks). Depth 24: 33.Qe2 +1.82. Carlsen played 33.Qe2 (+1.71 after it at depth 20). The plan: keep the queen covering f2 and the second rank so Black's queen and bishop find no checks, then advance the queenside majority and target g4. 33.Kh1 lets Black's queen into f2 with checks — the pawn is worthless if the king is not safe.

B7. Find the move — Carlsen–Firouzja, White to move after 38...Qc3. Black threatens ...Qxb4. What does White play?

Carlsen–Firouzja after 38...Qc3

FEN: 5k2/8/1bp1p3/8/1PP1P1p1/2q3P1/4Q1KN/8 w - - 4 39

Solution

39.Nxg4! Stockfish 19 MultiPV at depth 20: 39.Nxg4 +1.89, 39.e5 +1.85, 39.b5 +0.82; depth 24 39.Nxg4 +2.29. White gives the b4 pawn for the g4 pawn and a knight heading for e5, where it eyes c6, f7 and g6 next to Black's king. Firouzja, with under two minutes, took on b4 (39...Qxb4, the game's largest swing at depth 16: best was 39...Ke7).

B8. Find the tactic — Carlsen–Firouzja, White to move after 46...Ke7.

Carlsen–Firouzja after 46...Ke7

FEN: 7Q/4k3/2pqp3/4N3/2PbP3/6PK/8/8 w - - 14 47

Solution

47.Nxc6+! Qxc6 48.Qxd4 — the knight check deflects the queen from the d4 bishop, and White emerges in a queen ending with three pawns (c4, e4, g3) against one. Stockfish 19 MultiPV at depth 20: 47.Nxc6+ +5.12, 47.Qh7+ +5.05, 47.Qg7+ +5.05; at depth 24 the checking line 47.Qh7+ scores higher (+6.88) — several moves win. After the game's 47.Nxc6+ the engine gives +5.92 (depth 20). Carlsen chose the move that converts into a simple technical ending, a practical choice with the opponent nearly out of time.


Part C — Data & Code ⭐⭐–⭐⭐⭐

C1. Database research ⭐⭐ — a filter cascade for one speed. Adapt code/01_inventory.py to run the Lichess funnel for blitz only. Output: a table with one row per filter (games left, White score, draw rate, mean average rating). Expected shape: five rows; then add a sixth row for "balanced, both players 1800–2000". Which filter changes the draw rate most?

Hint

Use db.load_lichess(speeds=["blitz"], balanced=None, exclude_bots=False) and apply the bot, balanced and rating filters yourself, printing len(df) after each step. Before running, write down which filter you expect to move the draw rate most; draw rates rise with rating in the reference tables (Appendix H), so the rating filter is the natural suspect.

C2. Database research ⭐⭐⭐ — next-move tables by band. Extend code/04_position_queries.py so that for each query the next-move table is printed per rating band (cells with Kish n ≥ 100 only). Output: a long table with columns band, move, games, share, White %, interval. Use it to answer: in which band does 4...Nxd4 in the Scotch stop being Black's most common reply?

Hint for C2

Group the hits frame by ["band", "next_move"] and call db.summarise(..., weight="w") on each group; compute the share within band from the summed weights, not from row counts (analysed games are over-represented). Print only cells with n_eff >= 100. Expect most bands below 2000 to have enough Scotch games; above 2000 the cells thin out quickly — say where your answer stops being supported by data rather than forcing a conclusion.

C3. Engine analysis ⭐⭐⭐ — grade the estimators again. In code/02_sample_weights.py, add three quantities that exist in the 4M table and in the subset: the share of games that end by Time forfeit within each speed, the mean Lichess rating of White, and the share of games with an average rating ≥ 2200. Expected output: the same four-column table plus error columns. Does weighting still recover the truth to within 0.3%?

C4. Database research ⭐⭐ — two pawns up. Modify code/06_endgame_conversion.py to study exactly two pawns up with equal pieces. Output: wins % by band under the entry and settled definitions, plus the median engine eval by band. Compare with the reference tables (OTB classical, two pawns up: 81.8% wins overall, 78.5% at 2400+). Does the composition effect (lower median eval at higher ratings) still appear?

C5. Database research ⭐⭐⭐ — replicate the Black defences. Using opening_compare.DEFENCES (Chapter 22's ECO groups) and database.split_half, run a split-half test of Black's rating-adjusted score by defence × band on balanced Lichess games (value: −adj_white, so positive = good for Black). Output: cells, significant in A, replicated in B, r(A,B). Which defence × band cells survive?

Hint for C3 and C5

C3: a ratio within speed needs the weights inside each speed group, np.average(x, weights=w) per group — not a pooled weighted mean. The 0.3% benchmark came from whole-population quantities; small subgroups will be noisier, so report the Kish n next to each estimate.

C5: build the value column first (-(adj_white - band_average_adj)), then call split_half(df, ["defence", "band"], value=..., min_n=500). With eight defences the table is small, so also report the full-data interval for each cell; a cell can replicate and still be tiny.

C6. Database research ⭐⭐⭐ — the increment effect. Repeat the time-usage quintile study of code/07_time_usage.py for 180+2 instead of 180+0. Expected output: five quintile rows with median share used, score and adj (weighted). Hypothesis to test: with a 2-second increment, spending a lot of time early costs less. State the result with intervals.

Hint for C6

With increment, "share of base used by move 15" should be computed as (base + 14 × inc − clock after move 15) / base, and stated as your definition. Keep the eval-controlled comparison too — the raw gradient alone will not distinguish "slow players were already worse" from "time spent costs".


Part D — Judgment & Transfer ⭐⭐⭐

D1. What changes at 1200 vs 2000? A 1200-rated player asks whether to switch from 1...e5 to the Sicilian because "the database says it scores better". Using the chapter's band results, give advice for a 1200 player and for a 2000 player.

Model answer

For Black the Sicilian outscores 1...e5 in every Lichess band below 2000 (e.g. 50.4% vs 47.5% below 1200; reference tables), and White's rating-adjusted score against the Sicilian is near zero below 1600 (−0.4 below 1200). So at 1200 the switch is supported by the data — but the effect is two to three points, and the reason may be who plays it and what White players at that level know. At 2000 the difference is gone (48.7% vs 48.5% at 2000–2199) and White's adj against the Sicilian is +1.5. Advice: at 1200 choose by what you will study and enjoy, knowing the Sicilian is at least not worse in practice; at 2000 the decision must be made on structures and preparation, not database percentages.

D2. Engine vs database. The engine says the Scotch after 4.Nxd4 is +0.08; the database says White scores 53.5% from it on Lichess and 55.8% OTB. Is the engine "wrong"? Explain what each number measures.

Model answer

Neither is wrong. The engine evaluates best play from the position; the database measures the results of the moves people actually play. White's practical score comes from Black's choices — on Lichess the most common reply, 4...Nxd4, is an inaccuracy (+0.76 at depth 20 after 5.Qxd4). An opening can be objectively equal and practically favourable; the database tells you where, the engine tells you why.

D3. Which pool for Grace? Grace (FIDE ~1850) prepares for a classical OTB event. She has the Lichess sample and the OTB broadcast set. Which pool should she use for (a) the most common replies to her main line, (b) the score of a sharp sideline, (c) how often opponents fall into a known trap? Justify each.

Model answer

(a) The OTB pool in her band — move frequencies differ by pool (3...h6 in the Italian is 13.3% of Lichess replies but 0.8% OTB), and her opponents are OTB players. (b) Both, with intervals — sharp sidelines are thin in any one band (the Najdorf 6.Be3 has one OTB band with ≥100 games), so pool bands and say so. (c) Both: Lichess for the large sample at a similar rating, the OTB pool for the rate among her actual kind of opponent — and if they differ, trust the OTB rate, however small its sample, for planning a classical game.

D4. About you or about everyone? Dev's Italian scores 67.9% [56.2–77.6] in 70 games at the 3.Bc4 position; the database at his level scores 52.7% [50.5–54.8]. Write the honest two-sentence interpretation, and say what evidence would change it.

Model answer

Dev's Italian results are clearly above what players at his level get from the position (the intervals do not overlap), so it is a real strength of his record — though 70 games still leave a wide interval and part of the edge may be luck. A replication — his next 70 Italian games scoring well above 52.7% again — or evidence of why (opponents' typical mistakes he punishes, from his own next-move tables) would make it a stable strength rather than a good run.


Part M — Mixed / Interleaved ⭐⭐–⭐⭐⭐

M1. (with Chapter 2 — pawn structure) ⭐⭐. Use game_features.struct_20 and the QGD Exchange query: of the games that reached the QGD Exchange position (Section 31.4), what share carries the Carlsbad:white label at ply 20? Why is the structure label a more stable unit than the move order?

M2. (with Chapter 13 — practical decisions) ⭐⭐⭐. Chapter 13 found that from level positions at move 25, OTB players who later fell below five minutes scored 44.0% vs 54.3%. Section 31.8 found a 3+0 effect of early time use even from equal evaluations. Design one query that tests whether the same players are involved: do 3+0 players in the slowest early quintile also fall below 10% of their clock more often? Report the cross-tabulation.

M3. (with Chapter 20 — building a repertoire) ⭐⭐. Recompute Dev's Najdorf 6.Be3 comparison with outpost.repertoire.wilson_interval and outpost.database.summarise. Do the two modules give the same interval for his 19 movetext games? If not, why?

M4. (with Chapter 22 — openings for Black) ⭐⭐⭐. Chapter 22 found that over the board the Nimzo-Indian scores best for Black among 1.d4 defences (48.1%) and the QGD worst (44.5%). Repeat that comparison with rating-adjusted scores (Section 31.3). Does the ranking survive adjustment?

What to look for in M1–M4

M1: join the query hits to game_features on game_id and count struct_20 containing Carlsbad:white. The answer will be below 100%, because Black does not always recapture into the c6/d5 chain (…c6 may never come, or the c-pawn moves to c5) — the position query fixes the moves, the label fixes the skeleton, and the plans follow the skeleton. M2: a two-by-two table (slowest quintile yes/no × clock below 10% yes/no) with weighted shares and a chi-square or a difference of proportions with an interval. M3: the two Wilson functions should agree for unweighted data; if they differ, check whether one treats draws as half-points out of n and the other rounds points, or uses a different z. M4: use db.load_otb(balanced=True) with Chapter 22's ECO groups; report Black's adj (−adj_white) with intervals and see whether the Nimzo/QGD gap is larger or smaller than 3.6 raw points once the players' ratings are accounted for.

M5. (with Chapter 30 — the Elo rating system) ⭐⭐⭐. The rating-adjusted score relies on the Elo expected score. The calibration tables behind Chapter 30 show that observed scores at a 200-point gap are 72.2% (OTB classical) and 68.0% (Lichess blitz) against a formula value of 76.0%. How would a systematic over-prediction for large gaps bias the rating-adjusted score of openings played mostly in unbalanced games (such as the OTB Slav, mean gap +47)? Which direction is the bias?

Answer to M5

If the formula expects too much for the stronger side, games where White is much stronger will show a negative adj even when the opening is neutral, so openings that the stronger side chooses (the OTB Slav, QGD) are biased downward by the adjustment and openings chosen by the weaker side biased upward. With balanced games the bias is small because the gaps are small — another reason to compare openings in balanced games.


Part E — Research & Extension ⭐⭐⭐⭐

E1. A full month. The Lichess sample is 34.2 hours. Download one complete month of the Lichess open database, build a prefix index with the same san_prefix function, and repeat Section 31.9's split-half test by week. Do the opening × band findings replicate across weeks? What changes with weekday/weekend mixes?

E2. Middlegame position search. The prefix index answers opening questions only. Design a position index for middlegame positions — for example, all positions with an isolated d-pawn for White at plies 20–40 — using positions_sample or a replay over lichess_moves. Report how often the same middlegame position (not structure) recurs across games, and what that implies for "position statistics" beyond the opening.

E3. Time of day and quality. Using the analysed Lichess games, test whether accuracy (book definition) varies by UTC hour after controlling for rating band and speed. Report effect sizes with intervals and discuss the selection problem (who requests analysis at 03:00 UTC?).