Exploring chess games

MP 171: A first foray into the massive Lichess database.

I've been getting back into chess since moving to North Carolina, and as part of that journey I've been wanting to analyze games in ways that weren't really possible when I was last playing chess about 25 years ago. There are a bunch of resources in the chess world that simply didn't exist back then, and I've been looking forward to exploring them more.

One of these resources is the Lichess games database. If you haven't heard of it, Lichess is an open platform for playing online chess. It started as a hobby project, and it's remained an open-source, free platform for playing. One of the many interesting things it offers is a set of downloads comprising a huge number of games that have been played on the site. This is an incredible resource for looking at patterns in how people play, across a wide range of skill levels and time controls.

I've been really busy these past few weeks so I won't be showing any code in this post, but I will share some interesting visualizations that have come out of my first couple hours examining this database.

Material advantages, and the question of when to resign

When two experienced chess players play a game, it's customary to resign when you've reached a position where there are no reasonable attacking chances left, and it's clear that your opponent knows enough to win with the pieces and position they have on the board.

The question of when exactly you should resign is an interesting one though. If you resign too early when you're new to chess, you won't gain the experience of playing in weaker positions. Chess is also a game where the winner is often the person who made the second to last mistake. If there's a chance your opponent could mess up and lose their advantage, you should keep playing. That's almost always the case at lower levels, so most people who are still working through the lower levels of a chess playing pool should almost never resign.

I don't have a whole lot of time to play these days so I mostly play blitz games, where the games are over in about ten minutes. I play longer games when I can though, and I was surprised to see people resigning as soon as they'd lost a single piece, such as a knight or a bishop. This started happening in slightly longer time controls, where a game could take 20 to 30 minutes. I keep playing in these situations for two reasons. I want to get better at playing defensively, and I know that until I reach truly expert levels my opponents have a fair chance of accidentally dropping a piece just like I did, putting the game back into even territory.

This led me to a hypothesis. In chess, a player has a material advantage if the pieces they currently have on the board are stronger than their opponent's pieces. I'd guess that if you tracked the material advantage of each player throughout a chess game, you'd see more swings in who has an advantage at lower levels than you would at higher levels.

The Lichess database makes it possible to find out if ideas like these are accurate or not, so let's see if this idea holds in practice.

Material advantage in a single game

I wrote a bit of very messy code that looks through about 100,000 recent games, and picks a subset of games that fit certain criteria. It then calculates and plots the material advantage at every move in the game.

Here's a plot from a game between two players, with ratings between 800 and 1000:

Material advantage by move, for a single chess game. White got an advantage around move 25, but then lost the advantage by the end of the game.

Material advantage is calculated based on the strength of each chess piece. A pawn is worth 1 point, bishops and knights are worth 3 points, rooks are worth 5 points, and queens are worth 9 points. Nobody counts points to see who won a game, and these values aren't exact, but they're a good basic measure of the relative strength of each piece.

A single player's move in chess is called a ply, and each move is made up of two plies. In this plot, each person's total points are added up, and the difference at each ply is plotted. Positive numbers mean the player with the white pieces has a material advantage, and negative numbers mean the player with the black pieces has an advantage. This game lasted just over 100 plies, which means it lasted around 50 moves.

The spikes in the plot are a result of trades. For example, the first spike points down, which means Black captured one of White's pieces. But the material advantage disappeared on the next ply, meaning White was able to capture an equivalent piece back. This happened a number of times; the large spike is a queen trade. But shortly after the queen trade, White got a lasting advantage. At some point, however, they lost their advantage and the game ended with Black ahead in material.

Here's the same kind of plot, for two players with ratings between 2200 and 2400:

A game between much stronger players. Notice that the overall material advantage is much lower, in this case never more than 4 points either way.

This might look like a volatile game, but notice the scale of the y-axis. In the previous game White had a +8 advantage at one point. In this game, neither player ever held a material advantage of more than 4 points. They traded some pawns, and at one point a rook was traded for a knight. There were some more captures, and then Black found a checkmate.

This brings up an interesting aspect of higher-level chess games. White had an advantage in material in this game, but Black won the game. In higher-level games, good players will often give up some material in order to build a stronger attack with a subset of their pieces.

Looking at many games

Enough about individual games! Let's see what these patterns look like across a wide range of games. All these games were played at rapid time controls, which usually takes about 20-30 minutes per game. In these kinds of time controls, games end in a reasonable amount of time, but people are able to think long enough to play pretty decent moves, reflective of their skill levels, most of the time.

Rating band: 800-1000

Here's a random set of 250 games where both players have ratings between 800 and 1000:

This is what I expected to see. There's a huge variation in how much of a material advantage players build up. Sometimes the stronger player just sweeps a whole bunch of their opponent's pieces off the board. There are quite a few games where players build up 10- to 20-point advantages. There are two games where the players built up material advantages of more than 30 points. You never need this much of an advantage to win a game, but newer players are still learning how to use their pieces effectively. Sometimes they don't see any way of proceeding except to keep taking pieces. That's fine! It's part of how you learn to use your pieces well.

The biggest spikes come from players advancing a pawn up the board until they promote it to a queen, or another stronger piece. Some of these larger advantages come from promoting multiple pawns in a single game. Even in the games that don't see a huge material advantage, there's a lot of swings back and forth of who has the advantage.

The longest game here was just under 100 moves, and most games are over by around 40-50 moves.

Rating band: 1000-1200

Here's what the same plot looks like, for players in the 1000-1200 rating band:

This shows similar patterns. I like how every plot starts at 0, and branches out in both directions. In this band, it takes a little longer for players to start building an advantage. At this level, people are a little more aware of opening traps, and less likely to fall for them. People are still accumulating significant material advantages. The longest game here was over 100 moves.

Rating band: 1200-1400

Here you can see people starting to get a handle on the opening. Most of the early spikes are just trades. There's only one game that got to a 30-point advantage, and most games end with not much more than a 10-point advantage. Again, there was one game that went over 100 moves.

Rating band: 1400-1600

We're starting to see much more consistent and even play in this band. Very few people are getting a lasting advantage over 10 points until around move 50.

Rating band: 1600-1800

This is quite interesting! Most games are tightly grouped in the 10-point advantage zone. There were a few games, however, where one player ran away early.

Rating band: 1800-2000

This is what I was expecting to see for higher level games. No one is really running away with a game. Most spikes are just trades, where neither playing is gaining a significant material advantage. There are a number of games that went longer than 60 moves. The few games with big spikes that turn into advantages are clearly endgames where someone promoted a pawn to a queen.

Rating band: 2000-2200

This is the highest rating band where the player pool is big enough to find 250 games to analyze quickly. This is a bit more of the same, with a couple games where someone ran away with a material advantage. There are a lot more games that last well beyond 50 moves.

If you go back and compare this to the plot for the 800-1000 rating band, it shows a world of difference in how people play. The games are much more even, and people don't need much of a material advantage to play for a win.

Conclusions

This is just my first foray into analyzing a database of chess games, but there are already some interesting takeaways. First of all, it's helpful to recognize the volatility in games at various rating levels. If you're playing in the lower or intermediate bands, recognize how often these material swings happen, and consider playing on when you get down in material. People make mistakes in games all the time, and the only way to learn how to take advantage of your opponent's mistakes is to keep playing when there's any chance you might be able to turn things around.

I also love this kind of work. It's really interesting to dig into how chess games are represented from a data science perspective. It makes me look forward to analyzing other aspects of large sets of games, and my own games as well. For example, one well-known advantage in chess is having two bishops when your opponent has one or no bishops. I want to find all the games I've played where I had a bishop pair and my opponent did not, and see if I was able to use this advantage well. I know there are existing tools that let me look for that, and I'll use those tools at some point, but I'm also curious to mine for that data myself. There's a good chance that if I do, I'll discover some other patterns that existing tools won't let me find.

In the next chess-focused post, I'll share some of the code for this data exploration work. I'll also share a version of these plots where you can click on any line in the plot, and see the actual game that the line represents. It's a really fun way to see what these patterns look like in actual games.

If you're curious about chess and have your own questions that might be answered by this kind of data exploration, please reach out and share your ideas and questions.