Hypothesis Testing on Chess Openings

Hypothesis Testing on Chess Openings

Photo by sk on Unsplash


In the game of chess, an opening refers to the initial moves of a game. The term can refer to the initial moves by either side, white or black, but an opening by black may also be known as a defense. There are dozens of different openings, and hundreds of variants. The Oxford Companion to Chess lists 1, 327 named openings and variants. Chess players, including professionals, follow different opening moves as part of their strategy.

The goal of this study is to test if there is a relationship between the type of opening move and the outcome of the game.

About the game

For the unfamiliar, chess is a game played on a board of 64 squares by two players. One player is in control of the white pieces, and the other player is in control of the black pieces. The player controlling the white pieces is often simply referred to as “white” and the player controlling the black pieces is often simply referred to as “black”. Each player gets eight pawns, two knights, two bishops, two rooks, one queen and one king. The board below shows the starting position for standard chess.

The goal of chess is to trap the enemy king up to the point that it cannot avoid being captured. The white player always goes first followed by black. Each player would take turns moving 1 piece per turn. No one is allowed to “pass” on a turn. The game is won when one king is in ‘check’ and cannot avoid capture on the next player’s move; this is called checkmate. A game also can end when a player acknowledges his or her defeat by resigning. It is also possible for the game to end in a draw, that’s when a player is not in check but has no legal moves.

Data

To meet to the objective of this study, we have to collect chess games records which include the opening used and the outcome. These records can be seen from chessgames.com, an online chess database and community. Since the data needed for this study is not readily available from the said website, it has to be scraped.

Chessgames.com lists all openings on this page. For each opening type, all game records that used this opening were scraped and stored in a database. BeatifulSoup, a python library for pulling data from HTML and XML files, was used to scrape such data and were stored in a database using sqlite3, a module included in the Python standard library intended for working with SQLite databases.

The first 10 rows of the dataset is shown below.

opening wplayer bplayer wres bres move year event
0 A00 NN Lucena 0 1 26.0 1497.0 Salamanca
1 A00 Lucena Quintana 1 0 32.0 1515.0 Huesca
2 A00 Napoleon Bonaparte Madame De Remusat 1 0 14.0 1804.0 Chateau de Malmaison
3 A00 Amsterdam Rotterdam 1 0 34.0 1824.0 City Match
4 A00 Hyderabad Madras Chess Club 0 1 44.0 1828.0 City Match
5 A00 Kieseritzky H H Boncourt 0 1 30.0 1839.0 Match?
6 A00 Kieseritzky Saint Amant ½ ½ 50.0 1843.0 Paris
7 A00 Enschede Zutphen ½ ½ 58.0 1850.0 corr
8 A00 E Williams Wyvill 0 1 50.0 1851.0 London
9 A00 E Williams Harrwitz ½ ½ 25.0 1852.0 Harrwitz - Williams

Column description

  1. opening: code of the opening move according to http://www.chessgames.com/chessecohelp.html
  2. wplayer: name of the player controlling white pieces
  3. bplayer: name of the player controlling black pieces
  4. wres: game outcome for white player
  5. bres: game outcome for black player
  6. move: number of moves taken by white player before the game concludes
  7. year: year of the game
  8. event: name of the event

Exploratory Data Analysis

Scraped data contains 920,458 games. The earliest game on record dates back 1475, in 21 moves, this match was won by Castellvi over Vinyoles in Valencia. Approximately, a third of the matches resulted in a draw (311831). By barely looking at the winning percentages, it is evident that white enjoys the first-mover advantage, winning 38.2% (351,565) of matches while black only won 27.9% (257,062) of the time.

Figure below shows the top 10 most used openings. 3 most used openings are A07 (King's Indian Attack), A04 (Reti Opening), and D02 (Queen's Pawn Game) which were used in 26,295, 14,386, and 12,883 games respectively. Hover your mouse pointer on the figure to see the number of games for each of the most used openings. To see the actual names of the openings, please refer to this link.

But does the popularity of opening always mean winning? To answer this question, we have to look at the winning percentages of the openings. In this study, it is assumed that openings are used only by the white player thus win percentage is calculated as the number of times white won when using a particular opening divided by the number of games that opening was used in the entire dataset.

Please note that some openings only have a few games on record and some of them have 100% win percentage and should therefore be excluded. To filter the less used openings, only the ones with frequency above the 25th percentile were considered or openings which were used on at least 460 (25th percentile) games.

The figure below shows the 10 openings with the highest win percentage. D06 (Queen's Gambit Declined) had the highest win percentage at 62.3% followed by C37 (King's Gambit Accepted) with 56.8% and C62 (Ruy Lopez, Old Steinitz Defense) with 56.6%. The most popular opening, A07, ranked 7th with 51.2% win percentage.

Statistical Analysis

In this section, we will answer the objective of this study using statistical analysis. To see whether the type opening used is related to the outcome of the game, a test for independence using chi-square test for independence should be performed.

Chi-Square Test of Independence

The Chi-Square Test can be used to test of independence between two categorical variables. Specifically, it tests whether the frequencies of one categorical variable differ across levels of another categorical variable. In other words, it tests whether or not a statistically significant relationship exists between the two variables.

Null Hypothesis, $H_0$: The outcome of the game is independent of the type of opening used.

Significance level, $\alpha$: 0.05

If the chi-square test statistic is greater than the critical value, the null hypothesis not be accepted.

This test does not consider directionality, that is, it is not of relevance whether one variable is statistically greater than or less than the other variable. Rather, it will only test if one variable is related to the other, therefore, a two-sided test will be used.

To compute for the chi-square test statistic and critical value, chi2_contigency and chi2 modules from scipy.stats will be used respectively.

chi2_contigency function requires a contingency table. A contingency table is a table where columns and rows are the nominal categorical variables that needs to be tested, table values are the frequencies of occurence. In this problem, the variables are the outcome of the game and the openings, thus, the contingency table have game outcomes as rows, openings as columns and values are the frequencies of occurence of outcome-opening combinations.

Contingency Table

Shown below is a portion of the contigency table used in this problem.

A00 A01 A02 A03 A04 A05 A07 A08 A09 A10 ...
Win 2900 1210 741 405 5347 1032 13674 192 868 1899 ...
Lose 2588 1065 842 539 4082 667 6859 119 678 1866 ...

The computed chi-square test statistic using chi2_contingency is 9031.31. The formula for this computation is discussed here.

To compute the critical value, chi2.ppf requires the lower tail probability and the degree of freedom. Lower tail probability can be computed by subtracting the significance level from 1, which then results to 0.95. Degree of freedom, also known as the shape parameter, is equal to $(r-1)*(c-1)$ where $r$ and $c$ are the number of rows and columns respectively (2 and 496) and is computed to be 495. Using these values, the critical value can then be computed using the percent point function of chi2, which is equal to 547.87.

Since the chi-square test statistic (9031.31) is greather than the critical value (547.87), the null hypothesis is not accepted.

Conclusion

The results show that there is a significant relationship between the outcome of a game and the type of opening used based on Chi-Square Test for Interdependence.

Recommendation

This project worked under the assumption that opening moves can only be performed by the white player. To further refine the results of the study, openings by black should also be considered.

References

[1] https://en.wikipedia.org/wiki/Chess_opening
[2] https://www.statisticssolutions.com/chi-square-2/
[3] https://stattrek.com/chi-square-test/independence.aspx

Final Notes

This project was co-authored by Rosely Peña as part of our requirements in MS in Data Science’s Applied Computational Statistics course. If you wish to have a copy of the data and the code used in this project, you may send me a message via Linked in.