Skip to content

Commit

Permalink
Merge pull request #8279 from ddugovic/rating-advantage
Browse files Browse the repository at this point in the history
Ratings - account for first player advantage #6818
  • Loading branch information
ornicar authored Mar 2, 2021
2 parents e09edd4 + 4136df6 commit 725704c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
33 changes: 31 additions & 2 deletions modules/rating/src/main/java/glicko2/Rating.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public class Rating {

private final double advantage;
private double rating;
private double ratingDeviation;
private double volatility;
Expand All @@ -31,17 +32,35 @@ public class Rating {
private double workingVolatility;

public Rating(double initRating, double initRatingDeviation, double initVolatility, int nbResults) {
this(initRating, initRatingDeviation, initVolatility, nbResults, null);
this(0.0d, initRating, initRatingDeviation, initVolatility, nbResults, null);
}

public Rating(double initRating, double initRatingDeviation, double initVolatility, int nbResults, DateTime lastRatingPeriodEndDate) {
this(0.0d, initRating, initRatingDeviation, initVolatility, nbResults, lastRatingPeriodEndDate);
}

public Rating(double advantage, double initRating, double initRatingDeviation, double initVolatility, int nbResults, DateTime lastRatingPeriodEndDate) {
this.advantage = advantage;
this.rating = initRating;
this.ratingDeviation = initRatingDeviation;
this.volatility = initVolatility;
this.numberOfResults = nbResults;
this.lastRatingPeriodEndDate = lastRatingPeriodEndDate;
}

public Rating withAdvantage(double advantage) {
return new Rating(advantage, rating, ratingDeviation, volatility, numberOfResults, lastRatingPeriodEndDate);
}

/**
* Return the skill advantage (first-player handicap) value.
*
* @return double
*/
public double getAdvantage() {
return this.advantage;
}

/**
* Return the average skill value of the player.
*
Expand All @@ -55,6 +74,16 @@ public void setRating(double rating) {
this.rating = rating;
}

/**
* Return the average skill value of the player scaled down
* to the scale used by the algorithm's internal workings.
*
* @return double
*/
public double getGlicko2RatingWithAdvantage() {
return RatingCalculator.convertRatingToGlicko2Scale(this.rating + advantage);
}

/**
* Return the average skill value of the player scaled down
* to the scale used by the algorithm's internal workings.
Expand All @@ -70,7 +99,7 @@ public double getGlicko2Rating() {
*
* @param double
*/
public void setGlicko2Rating(double rating) {
private void setGlicko2Rating(double rating) {
this.rating = RatingCalculator.convertRatingToOriginalGlickoScale(rating);
}

Expand Down
14 changes: 7 additions & 7 deletions modules/rating/src/main/java/glicko2/RatingCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RatingCalculator {
private final static double DEFAULT_TAU = 0.75;
private final static double MULTIPLIER = 173.7178;
private final static double CONVERGENCE_TOLERANCE = 0.000001;
private final static int ITERATION_MAX = 3000;
private final static int ITERATION_MAX = 1000;
private final static double DAYS_PER_MILLI = 1.0 / (1000 * 60 * 60 * 24);

private final double tau; // constrains volatility over time
Expand Down Expand Up @@ -254,11 +254,11 @@ private double v(Rating player, List<Result> results) {
for ( Result result: results ) {
v = v + (
( Math.pow( g(result.getOpponent(player).getGlicko2RatingDeviation()), 2) )
* E(player.getGlicko2Rating(),
result.getOpponent(player).getGlicko2Rating(),
* E(player.getGlicko2RatingWithAdvantage(),
result.getOpponent(player).getGlicko2RatingWithAdvantage(),
result.getOpponent(player).getGlicko2RatingDeviation())
* ( 1.0 - E(player.getGlicko2Rating(),
result.getOpponent(player).getGlicko2Rating(),
* ( 1.0 - E(player.getGlicko2RatingWithAdvantage(),
result.getOpponent(player).getGlicko2RatingWithAdvantage(),
result.getOpponent(player).getGlicko2RatingDeviation())
));
}
Expand Down Expand Up @@ -293,8 +293,8 @@ private double outcomeBasedRating(Rating player, List<Result> results) {
outcomeBasedRating = outcomeBasedRating
+ ( g(result.getOpponent(player).getGlicko2RatingDeviation())
* ( result.getScore(player) - E(
player.getGlicko2Rating(),
result.getOpponent(player).getGlicko2Rating(),
player.getGlicko2RatingWithAdvantage(),
result.getOpponent(player).getGlicko2RatingWithAdvantage(),
result.getOpponent(player).getGlicko2RatingDeviation() ))
);
}
Expand Down
6 changes: 3 additions & 3 deletions modules/round/src/main/PerfsUpdater.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ final class PerfsUpdater(
private def updateRatings(white: Rating, black: Rating, result: Glicko.Result): Unit = {
val results = new RatingPeriodResults()
result match {
case Glicko.Result.Draw => results.addDraw(white, black)
case Glicko.Result.Win => results.addResult(white, black)
case Glicko.Result.Loss => results.addResult(black, white)
case Glicko.Result.Draw => results.addDraw(white.withAdvantage(5), black.withAdvantage(-5))
case Glicko.Result.Win => results.addResult(white.withAdvantage(5), black.withAdvantage(-5))
case Glicko.Result.Loss => results.addResult(black.withAdvantage(-5), white.withAdvantage(5))
}
try {
Glicko.system.updateRatings(results, true)
Expand Down

0 comments on commit 725704c

Please sign in to comment.