Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Created November 28, 2023 20:06
Show Gist options
  • Save thatcosmonaut/a608e4c6c123ebd7d88d011e7a600c6a to your computer and use it in GitHub Desktop.
Save thatcosmonaut/a608e4c6c123ebd7d88d011e7a600c6a to your computer and use it in GitHub Desktop.
Glicko-2 C#
// An implementation of the Glicko-2 system
// http://www.glicko.net/glicko/glicko2.pdf
// Copyright (c) 2023 Evan "cosmonaut" Hemsley
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
// Note that this implementation assumes a rating period of 1
// This simplifies rating updates in arbitrary online matchmaking
public static class Glicko2
{
// Algorithmic constants, don't touch
const double ScaleConstant = 173.7178;
const double ConvergenceTolerance = 0.000001;
// Can modify defaults to suit your rating system's needs
const double DefaultRating = 1500;
const double DefaultDeviation = 350;
const double DefaultVolatility = 0.06;
// Constrains changes in volatility over time
// Reasonable choices are values between 0.3 and 1.2
const double Tau = 0.5;
public readonly record struct Player(
double Rating,
double Deviation,
double Volatility
) {
public static Player Unranked => new Player(DefaultRating, DefaultDeviation, DefaultVolatility);
// The lower bound of the rating interval
public double LowestRating => Rating - (Deviation * 2);
// The upper bound of the rating interval
public double HighestRating => Rating + (Deviation * 2);
// The percentage confidence that the interval contains the true rating
public double Confidence => 1 - Volatility;
}
// Score is 1 for player win, 0 for player loss
public static Player UpdateRating(
Player playerRating,
Player opponentRating,
double score
) {
var (scaledRating, scaledDeviation) = Scale(playerRating.Rating, playerRating.Deviation);
var (opponentScaledRating, opponentScaledDeviation) = Scale(opponentRating.Rating, opponentRating.Deviation);
var estimatedVariance = EstimatedVariance(scaledRating, opponentScaledRating, opponentScaledDeviation);
var estimatedImprovement = EstimatedImprovement(estimatedVariance, score, scaledRating, opponentScaledRating, opponentScaledDeviation);
var updatedVolatility = Volatility(playerRating.Volatility, scaledDeviation, estimatedVariance, estimatedImprovement);
var updatedDeviation = RatingDeviation(updatedVolatility, scaledDeviation);
var (updatedScaledRating, updatedScaledDeviation) =
NewRating(scaledRating, updatedDeviation, estimatedVariance, score, opponentScaledRating, opponentScaledDeviation);
var (rating, deviation) = Unscale(updatedScaledRating, updatedScaledDeviation);
return new Player(rating, deviation, updatedVolatility);
}
private static (double, double) Scale(
double rating,
double deviation
) {
var mu = (rating - DefaultRating) / ScaleConstant;
var phi = deviation / ScaleConstant;
return (mu, phi);
}
private static (double, double) Unscale(
double mu,
double phi
) {
var rating = ScaleConstant * mu + DefaultRating;
var deviation = ScaleConstant * phi;
return (rating, deviation);
}
private static double G(double phi) => 1 / Math.Sqrt(1 + 3 * Math.Pow(phi, 2) / Math.Pow(Math.PI, 2));
private static double E(double mu, double opponentMu, double opponentPhi) => 1 / (1 + Math.Exp(-G(opponentPhi) * (mu - opponentMu)));
private static double EstimatedVariance(double scaledRating, double opponentScaledRating, double opponentScaledDeviation)
{
var gPhi = G(opponentScaledDeviation);
var eValue = E(scaledRating, opponentScaledRating, opponentScaledDeviation);
return 1 / (gPhi * gPhi * eValue * (1 - eValue));
}
private static double EstimatedImprovement(double estimatedVariance, double score, double scaledRating, double opponentScaledRating, double opponentScaledDeviation)
{
return estimatedVariance * G(opponentScaledDeviation) * (score - E(scaledRating, opponentScaledRating, opponentScaledDeviation));
}
private static double VolatilityFunction(double x, double scaledDeviation, double estimatedVariance, double estimatedImprovement, double a)
{
var phi2 = scaledDeviation * scaledDeviation;
var d2 = estimatedImprovement * estimatedImprovement;
var ex = Math.Exp(x);
var a2 = phi2 + estimatedVariance + ex;
var p2 = (x - a) / (Tau * Tau);
var p1 = (ex * (d2 - phi2 - estimatedVariance - ex)) / (2 * a2 * a2);
return p1 - p2;
}
private static double Volatility(double currentVolatility, double scaledDeviation, double estimatedVariance, double estimatedImprovement)
{
var a = Math.Log(currentVolatility * currentVolatility);
var originalA = a;
double b;
if (estimatedImprovement * estimatedImprovement > scaledDeviation * scaledDeviation + estimatedVariance)
{
b = Math.Log(estimatedImprovement * estimatedImprovement - scaledDeviation * scaledDeviation - estimatedVariance);
}
else
{
var k = 1;
var x = a - k * Tau;
while (VolatilityFunction(x, scaledDeviation, estimatedVariance, estimatedImprovement, originalA) < 0)
{
k += 1;
}
b = a - k * Tau;
}
var fa = VolatilityFunction(a, scaledDeviation, estimatedVariance, estimatedImprovement, originalA);
var fb = VolatilityFunction(b, scaledDeviation, estimatedVariance, estimatedImprovement, originalA);
while (Math.Abs(b - a) > ConvergenceTolerance)
{
var c = a + (a - b) * fa / (fb - fa);
var fc = VolatilityFunction(c, scaledDeviation, estimatedVariance, estimatedImprovement, originalA);
if (fc * fb <= 0)
{
a = b;
fa = fb;
}
else
{
fa /= 2;
}
b = c;
fb = fc;
}
return Math.Exp(a / 2);
}
private static double RatingDeviation(double updatedVolatility, double scaledDeviation)
{
return Math.Sqrt(updatedVolatility * updatedVolatility + scaledDeviation * scaledDeviation);
}
private static (double, double) NewRating(
double scaledRating,
double updatedDeviation,
double estimatedVariance,
double score,
double opponentScaledRating,
double opponentScaledDeviation
) {
var phiPrime = 1 / Math.Sqrt(1 / (updatedDeviation * updatedDeviation) + (1 / estimatedVariance));
var muPrime = scaledRating + phiPrime * phiPrime * (G(opponentScaledDeviation) * (score - E(scaledRating, opponentScaledRating, opponentScaledDeviation)));
return (muPrime, phiPrime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment