Skip to content

Instantly share code, notes, and snippets.

@mathzero
Created April 29, 2020 08:30
Show Gist options
  • Save mathzero/82c0bd9a3b5feffefbb7216199c9d0f4 to your computer and use it in GitHub Desktop.
Save mathzero/82c0bd9a3b5feffefbb7216199c9d0f4 to your computer and use it in GitHub Desktop.
Elo score calculator functions
### Functions for calculating Elo probabilities and scores ###
# Uses the weighted Elo formula created by 538, with a K of 250, a shape of 0.4 and an offset of 5
# https://fivethirtyeight.com/features/how-were-forecasting-the-2016-us-open/
### Define a function that calculates probabilities on the basis of Elo score inputs
#p1elo is p1's current Elo score
#p2elo is p2's current Elo score
Elo.prob <- function(p1elo,p2elo){
p1exp <- (1+(10^((p2elo-p1elo)/400)))^(-1)
return(p1exp) # returns the probability of p1 winning the match
}
### Define a function that calculates new Elo scores on the basis of previous scores, probabilities and match result
# p1elo - previous Elo score, p1
# score - zero (lost match) or one (won match)
# p1exp - expected match outcome (value between zero and one, determined by previous formula)
# numGames_ID1 - number of matches played by p1. Elo scores are affected less by any given match when a player has played many matches
Elo.newscore <- function(p1elo,score,p1exp,numGames_ID1){
newscore <- p1elo + 250/((5+numGames_ID1)^0.4)*(score-p1exp)
return(newscore)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment