1. Write an optimized version of the Elo’s rating function below
  2. Compare the original and optimized versions on this dataset
##  Elo
# INPUT
# games: matches
# zeta: logistic parameter
# k: update factor
# OUTPUT
# r: rating vector
elo_slow = function(games, z = 400, k = 25) {
  
  # number of players
  players = unique(c(games$White, games$Black))
  n = max(players)

  # number of games
  m = nrow(games)
  
  # old rating vector
  rold = rep(0, n)
  
  # new rating vector
  rnew = rep(0, n)
  
  for (i in 1:m) {

    # White player
    # compute update
    score = games[[i, "Score"]]
    spread = rold[games[[i, "White"]]] - rold[games[[i, "Black"]]]
    mu = 1 / (1 + 10^(-spread / z))
    update = k * (score - mu)
    # update rating
    rnew[games[[i,"White"]]] = rold[games[[i,"White"]]] + update
    
    # Black player
    # compute update
    score = 1 - games[[i, "Score"]]
    spread = rold[games[[i, "Black"]]] - rold[games[[i, "White"]]]
    mu = 1 / (1 + 10^(-spread / z))
    update = k * (score - mu)
    # update rating
    rnew[games[[i,"Black"]]] = rold[games[[i,"Black"]]] + update
    
    # update old ratings
    rold[games[[i,"White"]]] = rnew[games[[i,"White"]]]
    rold[games[[i,"Black"]]] = rnew[games[[i,"Black"]]]
  }
  return(rnew)
}