Skip to content

Calculate the probabilities of duplicated games in a set of chess games

Notifications You must be signed in to change notification settings

nilslindemann/Chessprob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

chessprob.py

This script transforms the formulas given in this answer on Math Stack Overflow to Python code.

The code was tested with Python 3.7.

The chessprob module has six functions, corresponding to the formulas in the above link. They calculate the probability that, in a (idealized) set of games ...

  • identical_any() - any two games are identical
  • identical_pov_player() - any of the games played by a specific player is identical to any of the other games played by other players
  • identical_pov_game() - a specific game is identical to any other game
  • identical_these_moves() - any two games are identical and have a specific move order
  • identical_these_moves_pov_player() - any of the games played by a specific player is identical to any of the games played by the other players and they have a specific move order
  • identical_these_moves_pov_game() - a specific game is identical to any other game, and they have a specific move order

There are two formulas for each case, one exact formula and an approximation formula. The exact formula gives a precise result but can not calculate bigger inputs in a reasonable time (See the description for the factorial function in the below glossary for the reason why). The approximation formula (the default) is precise up to around five positions after the first number which is not null, and it can calculate even very big inputs in no time.

See the example_usage.py for an example.

You can switch between precision and approximation mode by using set_options(exact=True) or set_options(exact=False).

How to use

Too long, didn´t read – See the example_usage.py.

The chessprob module behaves like a class.

If you want other that the default options, first use the set_options() function to set them. Use the print_options() function to print them.

These are the options you can set with set_options():

  • choices int: The amount of choices in a chess position. Defaults to 3 [1].
  • depth int: Alias ply. The amount of half moves per game. Defaults to 80, alias 40 moves [1].
  • players int: The amount of chess players. Defaults to 1.500.000.000. A guess of the amount of chess players in the history of chess.
  • games int: The amount of games every player plays. Defaults to 2500. A guess of the average amount of games played by the average chess player in his life.
  • exact bool: If the exact algorithms shall be used to calculate the different probabilities. Defaults to False (recommened).
  • precision int: Sets the amount of numbers after the comma. Defaults to 100.

An example is set_options(choices=3, depth=80, players=1_500_000_000, games=2_500, exact=False, precision=100) (these are also the default values).

[1] Number of Sensible Chess Games

Other Tools

The get_formulas() function returns the formulas of the identical_* functions you pass to it, as a list of lists of strings. For each function two formulas. The first is the exact and the second the approximation formula.

The mathify() function converts such a formula string to math syntax.

The populate() function replaces the variables in a formula string with the current values you have set (see the set_options() function).

The wolfram_open() function opens the default webbrowser and opens each formula string passed to it at wolframalpha.com, populated with the current values, where it will be evaluated. The wolfram engine can work with bigger numbers when using the exact formulas.

Again, see the example_usage.py for examples.

Glossary

Meaning of variables, operators and functions used in Eyeballfrogs Answer and in the chessprob module:

Variables:

  • N = CHOICES = the amount of move choices in a chess position. Set it with set_options(choices=<number>)
  • P = DEPTH = how many half moves a game lasts. 40 moves = 80 half moves. Set it with set_options(depth=<number>)
  • Np = POSSGAMES = N ^ P = all possible different chess games that can be played, depending on N and P. Automatically calculated.
  • Nc = PLAYERS = The amount of players who play games (originally computers). Set it with set_options(players=<number>)
  • Ng = GAMES = The amount of games each player plays. Set it with set_options(games=<number>)
  • Na = ALLGAMES = Nc * Ng / 2 = all games played by all players. Automatically calculated.

Operators:

  • + plus
  • - minus
  • * multiplication (left away in math notation, where A B means A * B)
  • / division (÷ in math notation)
  • ** power (^ in math notation)

Functions:

  • fac(number) The factorial function, eg. fac(5) = 1 * 2 * 3 * 4 * 5 = 120. Written (number)! in math notation. Some of the exact algorithms use it, which is why they are slow on big numbers. Eg. if we assume that 3 ^ 80 is the amount of all possible different chess games with sensible moves (already a number with 38 digits), then fac(3 ^ 80) is a number with 5577626172914152352366030303784576483327 digits (you can calculate the amount of digits but not the number itself).
  • exp(number) Returns e ^ number, where e is 2.718281... – Euler´s number. Most of the approximation functions use this function.

Credits

Many thanks go to to Eyeballfrog for providing the formulas.

Written by Nils-Hero Lindemann in 2019.08.12.

License

public domain

About

Calculate the probabilities of duplicated games in a set of chess games

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages