ADSP Logo

How To Calculate The Trappiest Chess Openings Using The Lichess API

The definitive guide to ranking the best chess traps using stats

ChessOpening

In this blog post, we will use cold hard stats (and Python) to calculate the best chess traps! šŸš€

🪤 What’s a chess trap?

Let’s start with a definition:

A trap is a position where a player is likely to play a move that when followed by the optimal response, results in a position where the player is likely to lose.

This means that the quality of a trap can be judged by two scores:

šŸŽ² Probability

How likely is the player to play the next move in the trap sequence?

A good trap should have a high likelihood that the next move in sequence is commonly chosen.

😵 Potency

How likely is the player to lose after they have fallen into the trap?

A good trap should be deadly once your opponent has fallen into it!

šŸ“Š Data

We can calculate both of these quantities statistically, using theĀ Lichess opening explorer APIĀ to grab data from online Lichess games.

In the analysis below, I’m filtering on blitz, rapid and classical games between players rated between 1600–1800 — theĀ Python codebaseĀ that accompanies this blog allows you to choose your own filters.

šŸ‘©ā€šŸ”¬ An Example

Let’s start with an example of a classic chess trap — the main trap of the Stafford Gambit (1. e4 e5 2. Nf3 Nf6 3. Nxe5 Nc6 4. Nxc6 dxc6 5. d3 Bc5 6. Bg5).

Chess GIF

6. Bg5 has occurred 3,050 times and is fatal for white, as black can sacrifice the queen for a win. In fact, 80% of the games are now won by black after 6. Bg5.

But…

This still doesn’t reflect the true awfulness of the move 6. Bg5, because in some of these games, black didn’t find the optimal response (6. … Nxe4), offering up the queen in return for mate.

Chess1

If black plays the optimal 6. … Nxe4, the win percentage for black rises to 95.6%, with most games ending with the following mate.

šŸ‘Øā€šŸ« Calculating the trap score

Now let’s calculate the two trap statistics (probabilityĀ andĀ potency) for the 6. Bg5 line of the Stafford Gambit.

šŸŽ² Probability

To calculate theĀ probabilityĀ score for the 6. Bg5 line of the Stafford Gambit, we need to calculate the chance of the opponent falling into this line, right from the opening. Notice we can’t just use the fact that it’s occurred 3,050 times as a proxy for likelihood, because we only need to calculate the probability thatĀ whiteĀ follows the trappy line, as black is assumed to play for the trap.

So…

  1. e4 — played in 25,214,898 / 39,383,933 games = 64.0%
  2. Nf3 — played in 7,803,828 / 11,793,967 games = 66.2%
  3. Nxe5— played in 260,538 / 762,614 games = 34.2%
  4. Nxc6 — played in 54,005 / 64,162 games = 84.2%
  5. d3 — played in 20,723 / 53,550 games = 38.7%
  6. Bg5 — played in 3,050 / 20,865 games = 14.6%

Multiplying the probabilities together gives aĀ 0.69%Ā likelihood that you get this line — in other words, roughly once every 145 games.

Technically, the chance is fractionally greater than this as there are other move orders can lead to the same position on the board , but for this analysis we will disregard these obscure lines.

In order to compare lines of different lengths, we take the geometric mean of the probabilities, to give the average probability of the opponent playing the next required move in sequence. For example, for the 6 move Stafford Gambit line, the average probability per move is 0.69% ^ 1/6 =Ā 43.6%.

So…

Probability score = 43.6%Ā šŸš€

😵 Potency

TheĀ potencyĀ of the trap is the win percentage for the opposing side, given the optimal response is played. So the 6. Bg5 line of the Stafford Gambit, isĀ 95.6%Ā potent.

Notice that I’m not using an engine to calculate potency. The engine gives -4.4 after 6. … Nxe4, but this doesn’t reflect the true temptation of a 1600–1800 player to greedily grab the queen, not realising they are walking into mate.

So for this reason, we stick to using data from actual games. This also allows us to create a confidence interval around each potency score! The 95.6% win percentage for black is taken over 1,667 games, giving a 95%Ā confidence intervalĀ of betweenĀ 94.7%Ā andĀ 96.6%Ā potent.

Potency score = 95.6%Ā šŸš€

šŸŽÆ Trap score

We calculate the overallĀ trap scoreĀ by multiplying the probability and potency scores… 43.6% * 95.6% =Ā 41.72%

Trap score = 41.7%Ā šŸš€

Results

ChessOpening

How do other traps compare? Here’s the leaderboard so far šŸ‘‡

The Stafford Gambit reigns supreme! šŸš€ It is the perfect mixture of potent and probableā€¦šŸ˜€. It also hasĀ many other trappy sidelinesĀ that make it a particularly difficult opening to handle, if you don’t play accurately.

An interactive version of the dashboard is availableĀ here.

šŸ† Challenge

Can anyone find a sequence of moves that scores higher than 41.7%?

UPDATE: This different line of the Stafford Gambit scores 43.8% šŸŽ‰

1. e4 e5

2. Nf3 Nf6

3. Nxe5 Nc6

4. Nxc6 dxc6

5. Nc3 Bc5

6. Bc4 Ng4

7. O-O

To take the challenge, start byĀ cloning the repoĀ and following the instructions in the README šŸ‘.

šŸ™ Thanks

Ā 

Thanks to all the great chess content creators out there — here’s three of the best:

Ā Applied Data Science PartnersĀ is a London based consultancy that implements end-to-end data science solutions for businesses, delivering measurable value. If you’re looking to do more with your data, please get in touch via our website. Follow us onĀ LinkedInĀ for more AI and data science stories!