1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const CONVERGENCE_TOLERANCE: f64 = 0.000001;

#[derive(Clone, Copy, Debug)]
pub struct Glicko2Player {
    pub rating: f64,
    pub rating_deviation: f64,
    pub volatility: f64,
}

#[derive(Clone, Copy, Debug)]
pub struct GlickoPlayer {
    pub rating: f64,
    pub rating_deviation: f64,
}

#[derive(Clone, Copy, Debug)]
pub struct GameResult {
    // GLICKO2
    opponent_rating: f64,
    opponent_rating_deviation: f64,
    score: Score,
}

impl GameResult {
    pub fn win<T: Into<Glicko2Player>>(player: T) -> GameResult {
        let player: Glicko2Player = player.into();
        GameResult {
            opponent_rating: player.rating,
            opponent_rating_deviation: player.rating_deviation,
            score: Score::Win,
        }
    }

    pub fn loss<T: Into<Glicko2Player>>(player: T) -> GameResult {
        let player: Glicko2Player = player.into();
        GameResult {
            opponent_rating: player.rating,
            opponent_rating_deviation: player.rating_deviation,
            score: Score::Loss,
        }
    }

    pub fn draw<T: Into<Glicko2Player>>(player: T) -> GameResult {
        let player: Glicko2Player = player.into();
        GameResult {
            opponent_rating: player.rating,
            opponent_rating_deviation: player.rating_deviation,
            score: Score::Draw,
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum Score {
    Win,
    Loss,
    Draw,
}

impl Into<f64> for Score {
    fn into(self) -> f64 {
        match self {
            Score::Win => 1.0,
            Score::Draw => 0.5,
            Score::Loss => 0.0,
        }
    }
}

impl From<GlickoPlayer> for Glicko2Player {
    fn from(player: GlickoPlayer) -> Glicko2Player {
        Glicko2Player {
            rating: (player.rating - 1500.0) / 173.7178,
            rating_deviation: player.rating_deviation / 173.7178,
            volatility: 0.06,
        }
    }
}

impl From<Glicko2Player> for GlickoPlayer {
    fn from(player: Glicko2Player) -> GlickoPlayer {
        GlickoPlayer {
            rating: player.rating * 173.7178 + 1500.0,
            rating_deviation: player.rating_deviation * 173.7178,
        }
    }
}

impl Glicko2Player {
    pub fn unrated() -> Glicko2Player {
        Glicko2Player::from(GlickoPlayer {
            rating: 1500.0,
            rating_deviation: 350.0,
        })
    }
}

impl GlickoPlayer {
    pub fn unrated() -> GlickoPlayer {
        GlickoPlayer {
            rating: 1500.0,
            rating_deviation: 350.0,
        }
    }
}

// The rest is best read with a copy of the glicko2 example PDF;
// I've tried to keep naming somewhat consistent
// http://www.glicko.net/glicko/glicko2.pdf

fn g(rating_deviation: f64) -> f64 {
    use std::f64::consts::PI;
    let denom = 1.0 + ((3.0 * rating_deviation * rating_deviation) / (PI * PI));
    1.0 / denom.sqrt()
}

fn e(rating: f64, other_rating: f64, other_rating_deviation: f64) -> f64 {
    let base = -1.0 * g(other_rating_deviation) * (rating - other_rating);
    1.0 / (1.0 + base.exp())
}

fn f(x: f64, delta: f64, rating_deviation: f64, v: f64, volatility: f64, sys_constant: f64) -> f64 {
    let fraction_one = {
        let numer =
            x.exp() * ((delta * delta) - (rating_deviation * rating_deviation) - v - x.exp());
        let denom = 2.0 * (rating_deviation * rating_deviation + v + x.exp())
            * (rating_deviation * rating_deviation + v + x.exp());
        numer / denom
    };
    let fraction_two = {
        let numer = x - (volatility * volatility).ln();
        let denom = sys_constant * sys_constant;
        numer / denom
    };
    fraction_one - fraction_two
}

pub fn new_rating<T: Into<Glicko2Player> + From<Glicko2Player>>(
    player: T,
    results: &[GameResult],
    sys_constant: f64,
) -> T {
    let player: Glicko2Player = player.into();
    if !results.is_empty() {
        let v: f64 = {
            let mut sum = 0.0;
            for result in results {
                let mut p =
                    g(result.opponent_rating_deviation) * g(result.opponent_rating_deviation);
                p *= e(
                    player.rating,
                    result.opponent_rating,
                    result.opponent_rating_deviation,
                );
                p *= 1.0
                    - e(
                        player.rating,
                        result.opponent_rating,
                        result.opponent_rating_deviation,
                    );
                sum += p;
            }
            1.0 / sum
        };
        let delta = {
            let mut sum = 0.0;
            for result in results {
                let mut p = g(result.opponent_rating_deviation);
                let score: f64 = result.score.into();
                p *= score
                    - e(
                        player.rating,
                        result.opponent_rating,
                        result.opponent_rating_deviation,
                    );;
                sum += p;
            }
            v * sum
        };
        let new_volatility = {
            let mut a = (player.volatility * player.volatility).ln();
            let delta_squared = delta * delta;
            let rd_squared = player.rating_deviation * player.rating_deviation;
            let mut b = if delta_squared > rd_squared + v {
                delta_squared - rd_squared - v
            } else {
                let mut k = 1.0;
                while f(
                    a - k * sys_constant,
                    delta,
                    player.rating_deviation,
                    v,
                    player.volatility,
                    sys_constant,
                ) < 0.0
                {
                    k += 1.0;
                }
                a - k * sys_constant
            };
            let mut fa = f(
                a,
                delta,
                player.rating_deviation,
                v,
                player.volatility,
                sys_constant,
            );
            let mut fb = f(
                b,
                delta,
                player.rating_deviation,
                v,
                player.volatility,
                sys_constant,
            );
            while (b - a).abs() > CONVERGENCE_TOLERANCE {
                // a
                let c = a + ((a - b) * fa / (fb - fa));
                let fc = f(
                    c,
                    delta,
                    player.rating_deviation,
                    v,
                    player.volatility,
                    sys_constant,
                );
                // b
                if fc * fb < 0.0 {
                    a = b;
                    fa = fb;
                } else {
                    fa /= 2.0;
                }
                // c
                b = c;
                fb = fc;
                // d (while loop)
            }
            (a / 2.0).exp()
        };
        let new_pre_rd = ((player.rating_deviation * player.rating_deviation)
            + (new_volatility * new_volatility))
            .sqrt();
        let new_rd = {
            let subexpr_1 = 1.0 / (new_pre_rd * new_pre_rd);
            let subexpr_2 = 1.0 / v;
            1.0 / (subexpr_1 + subexpr_2).sqrt()
        };
        let new_rating = {
            let mut sum = 0.0;
            for result in results {
                let mut p = g(result.opponent_rating_deviation);
                let score: f64 = result.score.into();
                p *= score
                    - e(
                        player.rating,
                        result.opponent_rating,
                        result.opponent_rating_deviation,
                    );
                sum += p;
            }
            player.rating + ((new_rd * new_rd) * sum)
        };
        Glicko2Player {
            rating: new_rating,
            rating_deviation: new_rd,
            volatility: new_volatility,
        }.into()
    } else {
        let new_rd = ((player.rating_deviation * player.rating_deviation)
            + (player.volatility * player.volatility))
            .sqrt();
        Glicko2Player {
            rating: player.rating,
            rating_deviation: new_rd,
            volatility: player.volatility,
        }.into()
    }
}

#[cfg(test)]
mod tests {
    extern crate approx;
    use self::approx::*;
    use super::*;

    #[test]
    fn test_rating_update() {
        let example_player = Glicko2Player::from(GlickoPlayer {
            rating: 1500.0,
            rating_deviation: 200.0,
        });
        let mut results: [GameResult; 3] = unsafe { ::std::mem::uninitialized() };
        results[0] = GameResult::win(GlickoPlayer {
            rating: 1400.0,
            rating_deviation: 30.0,
        });
        results[1] = GameResult::loss(GlickoPlayer {
            rating: 1550.0,
            rating_deviation: 100.0,
        });
        results[2] = GameResult::loss(GlickoPlayer {
            rating: 1700.0,
            rating_deviation: 300.0,
        });
        let new_player = new_rating(example_player, &results, 0.5);
        assert!(
            Relative::new(&new_player.rating, &-0.2069)
                .epsilon(0.0001)
                .eq()
        );
        assert!(
            Relative::new(&new_player.rating_deviation, &0.8722)
                .epsilon(0.0001)
                .eq()
        );
        assert!(
            Relative::new(&new_player.volatility, &0.05999)
                .epsilon(0.0001)
                .eq()
        );
    }

    #[test]
    fn test_glicko_glicko2_conversions() {
        let example_player = GlickoPlayer {
            rating: 1500.0,
            rating_deviation: 200.0,
        };
        let glicko2_player = Glicko2Player::from(example_player);
        assert!(
            Relative::new(&glicko2_player.rating, &0.0)
                .epsilon(0.0001)
                .eq()
        );
        assert!(
            Relative::new(&glicko2_player.rating_deviation, &1.1513)
                .epsilon(0.0001)
                .eq()
        );
        assert!(
            Relative::new(&glicko2_player.volatility, &0.06)
                .epsilon(0.0001)
                .eq()
        );
        let glicko_player = GlickoPlayer::from(glicko2_player);
        assert!(
            Relative::new(&glicko_player.rating, &1500.0)
                .epsilon(0.0001)
                .eq()
        );
        assert!(
            Relative::new(&glicko_player.rating_deviation, &200.0)
                .epsilon(0.0001)
                .eq()
        );
    }
}