# Probing Stockfish: reactions, piece values, forced advantage and the formula wheel

Part of [`research/chess-proof`](../README.md). Scripts are in the parent folder, results in `../data/`, figures in `../figures/`.

## Stockfish's reactions: does a move matter to it?

`stockfish_probe.py` asks Stockfish 16 (depth 14) two things in 817 positions from the graded games: what it would
play if the side to move passed (its plan), and how it replies to each of the 25 913 legal moves. A move is ignored if
the reply is the plan, and changes the reply otherwise. `probe_analysis.py` compares that with each move's quality
from the depth-12 scores (good = lichess accuracy 90+):

| moves | share of all moves | good | share of best moves |
|---|---|---|---|
| ignored (Stockfish carries on with its plan) | 35% | 17.4% | 22.8% |
| change the reply, and the evaluation is no worse than passing | 26% | **49.2%** | **76.4%** |
| change the reply, and the evaluation is worse than passing (punished) | 38% | 1.3% | 0.8% |

A reaction alone says little: moves that change the reply are good 20.8% of the time, ignored ones 17.4%. Blunders
change Stockfish's reply most of all, because it stops to take what was hung. With the direction added, the picture
is sharp: moves that make Stockfish change course without costing anything hold three quarters of the best moves, in
a quarter of the moves. The direction comes from Stockfish's own evaluation, though, so this is a way of reading
Stockfish, not a replacement for it. And ignored moves still hold 23% of the best moves, so cancelling them would be a
mistake: a quiet improving move often leaves Stockfish's plan unchanged.

Reverse-engineering what Stockfish reacts to: a logistic model on the 16 named facts predicts "changes the reply" on
held-out positions with AUC 0.78. The largest weights per standard deviation are putting a piece en prise (+1.16),
another piece already en prise (−0.61), rescuing a piece (+0.47), then piece activity, check and capture (about
+0.27 each). Stockfish's plan is usually to take what is loose. The moves that change it are the ones that hang a
piece, save one, or capture. If something is already hanging, Stockfish keeps its plan to take it. That is the same
material logic the seven-fact rule found.

## What is a piece worth where it stands? Occlusion probing of Stockfish

`piece_value.py` treats Stockfish 16 as a black box and removes one piece at a time. It evaluates each of 1 193
positions from the graded games at depth 12 (hash cleared before every evaluation), then again with each non-king piece
taken off: 20 832 pieces in all. A piece's value is what its owner loses when it disappears. Stockfish reports scores on
a win-probability scale, so `piece_patterns.py` normalizes every value by the median pawn of the same phase: opening
(non-pawn material ≥ 50 of 62), middlegame (25–49) or endgame. Each piece also carries named "photon" facts: rays cast
along its lines of movement give mobility and x-ray reach, and there are attackers, defenders, targets, king-zone
attacks, centrality, advance, king distances, pins, phase, and pawn, file and bishop-pair facts.

| piece | opening | middlegame | endgame |
|---|---|---|---|
| knight | 3.4 [3.1–3.7] | 3.5 [3.1–3.8] | 3.9 [3.4–4.3] |
| bishop | 3.7 [3.4–3.9] | 3.7 [3.2–4.0] | 4.3 [3.8–4.6] |
| rook | 3.9 [3.6–4.2] | 4.2 [3.8–4.5] | 5.0 [4.6–5.4] |
| queen | 5.0 [4.8–5.2] | 4.9 [4.6–5.4] | 7.6 [6.0–11.1] |

Medians, with the 25th–75th percentile in brackets, in median pawns of the phase. The ordering is the classical one:
the bishop is a little ahead of the knight in every phase, and rooks gain as the board empties. The queen's value is
squeezed because the scale saturates: once removing a piece already loses the game, removing a bigger one cannot cost
much more.

Not all pieces are equal. 3.2% of pawns are liabilities (their side is better off without them), 18% are worth less
than half a median pawn, and 21% more than one and a half. Knights and bishops are steadier: about 96% lie within half
to one and a half of their median. `piece_heatmaps.png` shows three positions. On the left is photon control, White's
attacks minus Black's on each square with x-rays at half weight; on the right, each piece's value where it stands. In
the endgame shown, Black's rook on d2 is worth 9.4 pawns and White's bishop on e2 0.2.

**What the facts explain: little.** The values are real: re-measured at depth 16 on 529 pieces, they correlate with
depth 12 at 0.98 (pawns), 0.96 (rooks), 0.91 (bishops), 0.78 (knights) and 0.67 (queens). Yet the photon facts predict
them poorly on held-out positions: R² 0.00 (knights), 0.04 (pawns), 0.07 (bishops), 0.11 (queens), 0.13 (rooks). The
phase helps most, for rooks and queens. Adding how decided the game already is does not help. A two-layer network on
the same facts does no better: it overfits, giving negative R² for three of the five pieces and 0.15 for pawns. So what
a piece is worth to Stockfish where it stands is stable but not local. It depends on concrete play across the whole
board, which counts of rays, attackers and defenders do not capture. That is why Stockfish needs a large network and a
deep search.

## The shortest forced advantage, with the least counterplay

**The question.** In how few moves can a side force a material gain that no defence prevents? And among the shortest
ways, which leaves the defender the least counterplay? `advantage_prover.py` answers both exactly, within a depth limit.

**Definitions.** Material counts pawn 1, knight 3, bishop 3, rook 5, queen 9. The settled material of a position is the
balance once every capture has played out (`qsearch`). The side to move may stand pat unless it is in check; in check
every evasion is searched, and captures, promotions and checkmate (−100) are followed for up to 12 plies. A forced gain
of T within n moves is a strategy: one attacker move per turn and an answer to every legal defender reply. After one of
its own moves, either the defender is checkmated or the attacker's settled material is at least T above the root's.
Stalemate is no gain. Because checks are part of settling, a forking check that wins a rook next move counts as one move.
The search is exhaustive except on the attacker's last move. There only captures, promotions and checks are tried, which
is exact: after a quiet move the defender can stand pat. `verify_proof.verify_advantage` re-checks every line on the
D0RE engine with its own capture search. Draw claims and the 50-move rule are ignored, since only material is at stake.

**Counterplay**, computed in Tally (`../tally/chess/chess_forced_advantage.tly`). At each leaf of a line, with the
defender to move, three named counts are recorded: its checking moves, its captures, and the squares next to the
attacker's king it attacks. A leaf's risk is 4·checks + 2·captures + 1·king_zone. A line's risk is its worst leaf,
because the defender chooses where the line ends. Tally picks the least-risk line among the shortest ones, earliest on
ties, and Python must agree exactly.

**Results** (`advantage_benchmark.py`, gain of at least 1 pawn, up to 3 moves). There are two sets of positions. The
first is 100 real blunders from the probe: a non-capture that cost the mover 2+ pawns, with the punisher to move. The
second is 40 random positions whose shortest forced gain takes 2 or 3 moves.

| | 1 move | 2 moves | 3 moves | none within 3 |
|---|---|---|---|---|
| after a real blunder (100) | 97 | 0 | 0 | 3 |
| random, multi-move (40) | 0 | 27 | 13 | 0 |

All 355 shortest lines pass the D0RE engine. Real blunders almost always leave something to take at once; the three
exceptions lose to something other than material within 3 moves. In 16 of the 40 multi-move positions there is more than
one shortest way, up to 19. Over all positions, the least-counterplay line differs from the first one found in 32, and
it removes 224 risk points in total.

**A forced material gain is not a forced win.** Stockfish 16 (depth 14) rates 123 of the 137 gaining sides as winning
(+3 or more), but 5 below +1. In `5K2/2P5/5p2/8/8/3k4/3p4/4r3 w` White's forced gain is c8=Q, yet Stockfish gives
White −5.00: Black's d-pawn promotes too, and Black's attack wins. Material is one coordinate of a position, not its
value.

**The starting position.** The same exhaustive search finds no forced gain of even one pawn for White within 11 plies:

| White moves | plies | forced gain | prover calls | time |
|---|---|---|---|---|
| 4 | 7 | no | 8 225 | 1.8 s |
| 5 | 9 | no | 122 651 | 35 s |
| 6 | 11 | no | 1 624 787 | 710 s |

So the starting position has no shortcut through material either. Even if it had, a forced material gain would not
settle the game, as the example above shows.

## The chess formula wheel

`chess_wheel.svg` is the electrical engineer's formula wheel rebuilt for chess. Material, Time, Space and Safety stand
where V, I, R and P stand. The centre is the Value of the starting position, which stays unknown, between −1 and +1.
Each of the twelve outer entries is read from a result file in this directory and tagged:

- **proved**: an exhaustive search re-checked on the D0RE engine, or a short argument;
- **measured**: held-out data;
- **defined**: a stated choice;
- **cited**: used as given.

The wheel's numeric claims are re-checked by `../tally/chess/chess_wheel.tly`: bishop over knight in every phase,
rooks gaining in the endgame, the 13- and 11-ply starting bounds, 12 of 12 games at 90%+ with 5 logical moves, and
forced material that is not a forced win. Unlike Ohm's law, none of these entries is an exact law. Chess has no closed
formula, only proofs and measurements.

![The chess formula wheel](../figures/chess_wheel.png)
