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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! BBT is an implementation of a skill-rating system similar to Elo, Glicko or
//! TrueSkill. It follows `Algorithm 1` from the paper [A Bayesian Approximation
//! Method for Online Ranking][ABAMOR].
//!
//! [ABAMOR]: http://jmlr.csail.mit.edu/papers/volume12/weng11a/weng11a.pdf
//!
//! ## Usage
//!
//! As a first step, you need to instantiate a Rater:
//!
//! ```rust
//! let rater = bbt::Rater::new(25.0/6.0);
//! ```
//!
//! The new() function takes one parameter, β. This parameter describes how much
//! randomness (variance in outcomes) your game has. For example, a game like
//! Hearthstone is much more luck-based than chess and should have a higher
//! variance; you may need to experiment to see which value has the highest
//! predictive power.
//!
//! ### Two-player games (e.g. Chess)
//!
//! BBT has a convenience function for two-player games that returns the new
//! ratings for the two players after a game. In the example, p1 wins against
//! p2:
//!
//! ```rust
//! let rater = bbt::Rater::default();
//!
//! let p1 = bbt::Rating::default();
//! let p2 = bbt::Rating::default();
//!
//! let (new_p1, new_p2) = rater.duel(p1, p2, bbt::Outcome::Win);
//! ```
//!
//! The `bbt::Outcome` enum can take on the values `Win`, `Loss` and `Draw`.
//!
//! ### Multiplayer games
//!
//! Games with more than two players will have to use the general
//! `update_ratings` method. It takes a vector of teams and a vector of ranks,
//! with each team being a vector of player ratings. If no error occurs, the
//! method returns a vector of the same form as the input with updated ratings.
//!
//! #### Example 1: Racing Game
//!
//! In a racing game without teams, each player is represented as a "team" of
//! one, and since there are usually no ties in a racing game, the list of ranks
//! contains no duplicates:
//!
//! ```rust
//! let rater = bbt::Rater::default();
//!
//! let p1 = bbt::Rating::default();
//! let p2 = bbt::Rating::default();
//! let p3 = bbt::Rating::default();
//! let p4 = bbt::Rating::default();
//! let p5 = bbt::Rating::default();
//! let p6 = bbt::Rating::default();
//!
//! let new_ratings = rater.update_ratings(vec![vec![p1], vec![p2], vec![p3],
//!                                             vec![p4], vec![p5], vec![p6]],
//!                                        vec![1, 2, 3, 4, 5, 6]).unwrap();
//! ```
//!
//! In the example, the first player places first, the second player second, and
//! so on.
//!
//! #### Example 2: Tied Teams
//!
//! Let's say you have a hypothetical game with four teams and two players per
//! team.
//!
//! | Team 1 | Team 2  | Team 3 | Team 4 |
//! | ------ | ------- | ------ | ------ |
//! | Alice  | Charlie | Eve    | Gabe   |
//! | Bob    | Dave    | Fred   | Henry  |
//!
//! If Team 1 wins, and Team 2 and 3 draw for second place and Team 4 loses, you
//! can call the `update_ratings` function as follows:
//!
//! ```rust
//! let rater = bbt::Rater::default();
//!
//! let alice   = bbt::Rating::default();
//! let bob     = bbt::Rating::default();
//! let charlie = bbt::Rating::default();
//! let dave    = bbt::Rating::default();
//! let eve     = bbt::Rating::default();
//! let fred    = bbt::Rating::default();
//! let gabe    = bbt::Rating::default();
//! let henry   = bbt::Rating::default();
//!
//! let new_ratings = rater.update_ratings(vec![vec![alice, bob],
//!                                             vec![charlie, dave],
//!                                             vec![eve, fred],
//!                                             vec![gabe, henry]],
//!                                        vec![1, 2, 2, 4]).unwrap();
//! ```
//!
//! The second vector assigns a rank to the teams given in the first vector.
//! Team 1 placed first, teams 2 and 3 tie for second place and team 4 comes in
//! fourth.
//!
//! ## Rating scale
//!
//! The default rating scale follows TrueSkill's convention of ranks from 0 to 50.
//! You should be able to use a different scale by specifying the middle of that
//! scale in `Rating::new()`. For example, to use a more traditional scale of 0 to
//! 3000, you can initialize ratings with `Rating::new(1500.0, 1500.0/3.0)`. You'll
//! also need to adjust the β-value of the Rater instance accordingly:
//! `Rater::new(1500.0/6.0)`.

#[cfg(feature = "serde")]
extern crate serde;

#[cfg(feature = "serde")]
mod serialization;

use std::fmt;

/// Rater is used to calculate rating updates given the β-parameter.
pub struct Rater {
    beta_sq: f64,
}

impl Rater {
    /// This method instantiates a new rater with the given β-parameter.
    pub fn new(beta: f64) -> Rater {
        Rater {
            beta_sq: beta * beta,
        }
    }
}

impl Default for Rater {
    /// This method instantiates a new rater the default β-parameter of 25.0/6.0
    /// used in the paper.
    fn default() -> Rater {
        Rater::new(25.0 / 6.0)
    }
}

impl Rater {
    /// This method takes a vector of teams, with each team being a vector of
    /// player ratings, and a vector ranks of the same size that specifies the
    /// order in which the team finished a game. It returns either
    /// `Err(error_message)` if the input is incorrect or
    /// `Ok(Vec<Vec<Rating>>)`. The returned vector is an updated version of
    /// the `teams` vector that was passed into the function.
    pub fn update_ratings(
        &self,
        teams: Vec<Vec<Rating>>,
        ranks: Vec<usize>,
    ) -> Result<Vec<Vec<Rating>>, &'static str> {
        if teams.len() != ranks.len() {
            return Err("`teams` and `ranks` vectors must be of the same length");
        }

        let mut team_mu = Vec::with_capacity(teams.len());
        let mut team_sigma_sq = Vec::with_capacity(teams.len());
        let mut team_omega = Vec::with_capacity(teams.len());
        let mut team_delta = Vec::with_capacity(teams.len());

        for _ in 0..teams.len() {
            team_mu.push(0.0);
            team_sigma_sq.push(0.0);
            team_omega.push(0.0);
            team_delta.push(0.0);
        }

        ////////////////////////////////////////////////////////////////////////
        // Step 1 - Collect Team skill and variance ////////////////////////////
        ////////////////////////////////////////////////////////////////////////

        for (team_idx, team) in teams.iter().enumerate() {
            if team.is_empty() {
                return Err("At least one of the teams contains no players");
            }

            for player in team.iter() {
                team_mu[team_idx] += player.mu;
                team_sigma_sq[team_idx] += player.sigma_sq;
            }
        }

        ////////////////////////////////////////////////////////////////////////
        // Step 2 - Compute Team Omega and Delta ///////////////////////////////
        ////////////////////////////////////////////////////////////////////////

        for (team_idx, _) in teams.iter().enumerate() {
            for (team2_idx, _) in teams.iter().enumerate() {
                if team_idx == team2_idx {
                    continue;
                }

                let c = (team_sigma_sq[team_idx] + team_sigma_sq[team2_idx] + 2.0 * self.beta_sq)
                    .sqrt();
                let e1 = (team_mu[team_idx] / c).exp();
                let e2 = (team_mu[team2_idx] / c).exp();
                let piq = e1 / (e1 + e2);
                let pqi = e2 / (e1 + e2);
                let ri = ranks[team_idx];
                let rq = ranks[team2_idx];
                let s = if rq > ri {
                    1.0
                } else if rq == ri {
                    0.5
                } else {
                    0.0
                };
                let delta = (team_sigma_sq[team_idx] / c) * (s - piq);
                let gamma = team_sigma_sq[team_idx].sqrt() / c;
                let eta = gamma * (team_sigma_sq[team_idx] / (c * c)) * piq * pqi;

                team_omega[team_idx] += delta;
                team_delta[team_idx] += eta;
            }
        }

        ////////////////////////////////////////////////////////////////////////
        // Step 3 - Individual skill update ////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////

        let mut result = Vec::with_capacity(teams.len());

        for (team_idx, team) in teams.iter().enumerate() {
            let mut team_result = Vec::with_capacity(team.len());

            for player in team.iter() {
                let new_mu =
                    player.mu + (player.sigma_sq / team_sigma_sq[team_idx]) * team_omega[team_idx];
                let mut sigma_adj =
                    1.0 - (player.sigma_sq / team_sigma_sq[team_idx]) * team_delta[team_idx];
                if sigma_adj < 0.0001 {
                    sigma_adj = 0.0001;
                }
                let new_sigma_sq = player.sigma_sq * sigma_adj;

                team_result.push(Rating {
                    mu: new_mu,
                    sigma: new_sigma_sq.sqrt(),
                    sigma_sq: new_sigma_sq,
                });
            }

            result.push(team_result);
        }

        Ok(result)
    }

    /// This method calculates the new ratings for two players after a
    /// head-to-head duel. The outcome is from the first player `p1`'s
    /// perspective, i.e. `Win` if the first player won, `Loss` if the second
    /// player won and `Draw` if neither player won.
    pub fn duel(&self, p1: Rating, p2: Rating, outcome: Outcome) -> (Rating, Rating) {
        let teams = vec![vec![p1], vec![p2]];
        let ranks = match outcome {
            Outcome::Win => vec![1, 2],
            Outcome::Loss => vec![2, 1],
            Outcome::Draw => vec![1, 1],
        };

        let result = self.update_ratings(teams, ranks).unwrap();

        (result[0][0].clone(), result[1][0].clone())
    }
}

/// Outcome represents the outcome of a head-to-head duel between two players.
#[derive(Clone, Copy)]
pub enum Outcome {
    /// The first player won the game
    Win,

    /// The first player lost the game
    Loss,

    /// Neither player won
    Draw,
}

/// Rating represents the skill of a player.
#[derive(PartialEq, Clone)]
pub struct Rating {
    mu: f64,
    sigma: f64,
    sigma_sq: f64,
}

impl Default for Rating {
    /// Instantiates a Rating with the default values of mu=25.0 and sigma=25.0/3.0
    fn default() -> Rating {
        Rating {
            mu: 25.0,
            sigma: 25.0 / 3.0,
            sigma_sq: f64::powf(25.0 / 3.0, 2.0),
        }
    }
}

impl PartialOrd for Rating {
    fn partial_cmp(&self, other: &Rating) -> Option<std::cmp::Ordering> {
        (self.mu - 3.0 * self.sigma).partial_cmp(&(other.mu - 3.0 * other.sigma))
    }
}

impl fmt::Display for Rating {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let cons_est = self.mu - 3.0 * self.sigma;
        if cons_est < 0.0 {
            write!(f, "0.0")
        } else {
            write!(f, "{}", cons_est)
        }
    }
}

impl fmt::Debug for Rating {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}±{}", self.mu, 3.0 * self.sigma)
    }
}

impl Rating {
    pub fn new(mu: f64, sigma: f64) -> Rating {
        Rating {
            mu,
            sigma,
            sigma_sq: sigma.powf(2.0),
        }
    }

    /// Returns the estimated skill of the player.
    pub fn mu(&self) -> f64 {
        self.mu
    }

    /// Returns the variance on the estimate of the player's skill.
    pub fn sigma(&self) -> f64 {
        self.sigma
    }
}

#[cfg(test)]
mod test {

    #[test]
    fn can_instantiate_ratings() {
        let default_rating = ::Rating::default();
        let new_rating = ::Rating::new(25.0, 25.0 / 3.0);
        assert_eq!(default_rating, new_rating)
    }

    #[test]
    fn two_player_duel_win_loss() {
        let p1 = ::Rating::default();
        let p2 = ::Rating::default();

        let rater = ::Rater::default();
        let new_rs = rater
            .update_ratings(vec![vec![p1], vec![p2]], vec![0, 1])
            .unwrap();

        assert!((new_rs[0][0].mu - 27.63523138).abs() < 1.0 / 100000000.0);
        assert!((new_rs[0][0].sigma - 8.0655063).abs() < 1.0 / 1000000.0);
        assert!((new_rs[1][0].mu - 22.36476861).abs() < 1.0 / 100000000.0);
        assert!((new_rs[1][0].sigma - 8.0655063).abs() < 1.0 / 1000000.0);
    }

    #[test]
    fn two_player_duel_tie() {
        let p1 = ::Rating::default();
        let p2 = ::Rating::default();

        let rater = ::Rater::default();
        let (new_p1, new_p2) = rater.duel(p1, p2, ::Outcome::Draw);

        assert_eq!(new_p1.mu, 25.0);
        assert_eq!(new_p2.mu, 25.0);
        assert!((new_p1.sigma - 8.0655063).abs() < 1.0 / 1000000.0);
        assert!((new_p2.sigma - 8.0655063).abs() < 1.0 / 1000000.0);
    }

    #[test]
    fn four_player_race() {
        let p1 = ::Rating::default();
        let p2 = ::Rating::default();
        let p3 = ::Rating::default();
        let p4 = ::Rating::default();

        let rater = ::Rater::default();
        let teams = vec![vec![p1], vec![p2], vec![p3], vec![p4]];
        let ranks = vec![1, 2, 3, 4];

        let new_ratings = rater.update_ratings(teams, ranks).unwrap();

        assert!((new_ratings[0][0].mu - 32.9056941).abs() < 1.0 / 10000000.0);
        assert!((new_ratings[1][0].mu - 27.6352313).abs() < 1.0 / 10000000.0);
        assert!((new_ratings[2][0].mu - 22.3647686).abs() < 1.0 / 10000000.0);
        assert!((new_ratings[3][0].mu - 17.0943058).abs() < 1.0 / 10000000.0);

        assert!((new_ratings[0][0].sigma - 7.50121906).abs() < 1.0 / 1000000.0);
        assert!((new_ratings[1][0].sigma - 7.50121906).abs() < 1.0 / 1000000.0);
        assert!((new_ratings[2][0].sigma - 7.50121906).abs() < 1.0 / 1000000.0);
        assert!((new_ratings[3][0].sigma - 7.50121906).abs() < 1.0 / 1000000.0);
    }
}