Skip to content

Instantly share code, notes, and snippets.

@rasmusab
Created June 2, 2015 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasmusab/b29bb53cfc3fe25f3f80 to your computer and use it in GitHub Desktop.
Save rasmusab/b29bb53cfc3fe25f3f80 to your computer and use it in GitHub Desktop.
Takes the matrix created by this script: https://gist.github.com/rasmusab/fb98cced046d4c675d74 and calculates some statistics and fits some models to it.
# Takes the matrix created by this script: https://gist.github.com/rasmusab/fb98cced046d4c675d74
# and calculates some statistics and fits some models to it. All this is pretty memory heavy so saving
# the interesting parts using saveRDS so the script only need to be run once.
set.seed(123)
games <- as.data.frame(readRDS("milionbase_matrix.rds"))
# Saving some info
n_games <- max(games[,"game_id"])
fullmoves_white <- games[ games[, "fullmoves"] < 100 & games[,"active_player"] == 1, "fullmoves"]
saveRDS(n_games, "n_games.Rdata")
saveRDS(fullmoves_white, "fullmoves_white.Rdata")
saveRDS(nrow(games), "n_positions.Rdata")
games <- games[games$result != 0, ] # remove draws
games <- games[sample(nrow(games), 1000000),]
games$white_win <- games$result == 1
fit <- glm(white_win ~ I(white_pawn - black_pawn) + I(white_knight - black_knight) +
I(white_bishop - black_bishop) + I(white_rook - black_rook) + I(white_queen - black_queen),
data = games, family="binomial")$coefficients
fit_bishops <- glm(white_win ~ I(white_pawn - black_pawn) + I(white_knight - black_knight) + I(white_bishop - black_bishop) +
I((white_bishop == 2) - (black_bishop == 2)) + I(white_rook - black_rook) + I(white_queen - black_queen),
data = games, family="binomial")$coefficients
# Split up by each 10th fullmove
fit_over_moves <- lapply(seq(10, 60, 10), function(i) {
games <- as.data.frame(readRDS("milionbase_matrix.rds"))
games <- games[games$result != 0 & games$fullmoves >= i - 10 & games$fullmoves <= i, ]
if(nrow(games) > 1000000) {
games <- games[sample(nrow(games), 1000000),]
}
games$white_win <- games$result == 1
glm(white_win ~ I(white_pawn - black_pawn) + I(white_knight - black_knight) + I(white_bishop - black_bishop) +
I((white_bishop == 2) - (black_bishop == 2)) + I(white_rook - black_rook) + I(white_queen - black_queen),
data = games, family="binomial")$coefficients
})
saveRDS(list(fit = fit, fit_bishops = fit_bishops, fit_over_moves = fit_over_moves), "chess_fits.Rdata")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment