-
Notifications
You must be signed in to change notification settings - Fork 20
/
simple_elo_mmr.rs
140 lines (128 loc) · 5.29 KB
/
simple_elo_mmr.rs
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
//! This version has fewer features and optimizations than elo_mmr.rs, more
//! closely matching the pseudocode in https://arxiv.org/abs/2101.00400
use super::{Player, Rating, RatingSystem, TanhTerm, SECS_PER_DAY};
use crate::data_processing::ContestRatingParams;
use crate::numerical::solve_newton;
use rayon::prelude::*;
fn eval_less(term: &TanhTerm, x: f64) -> (f64, f64) {
let (val, val_prime) = term.base_values(x);
(val - term.w_out, val_prime)
}
fn eval_grea(term: &TanhTerm, x: f64) -> (f64, f64) {
let (val, val_prime) = term.base_values(x);
(val + term.w_out, val_prime)
}
fn eval_equal(term: &TanhTerm, x: f64, mul: f64) -> (f64, f64) {
let (val, val_prime) = term.base_values(x);
(mul * val, mul * val_prime)
}
#[derive(Debug)]
pub struct SimpleEloMMR {
// the weight of each new contest
pub weight_limit: f64,
// weight multipliers (less than one) to apply on first few contests
pub noob_delay: Vec<f64>,
// each contest participation adds an amount of drift such that, in the absence of
// much time passing, the limiting skill uncertainty's square approaches this value
pub sig_limit: f64,
// additional variance per day, from a drift that's continuous in time
pub drift_per_day: f64,
// whether to count ties as half a win plus half a loss
pub split_ties: bool,
// maximum number of recent contests to store, must be at least 1
pub history_len: usize,
// maximum number of opponents and recent events to use, as a compute-saving approximation
pub transfer_speed: f64,
}
impl Default for SimpleEloMMR {
fn default() -> Self {
Self {
weight_limit: 0.2,
noob_delay: vec![],
sig_limit: 80.,
drift_per_day: 0.,
split_ties: false,
history_len: usize::MAX,
transfer_speed: 1.,
}
}
}
impl SimpleEloMMR {
fn compute_weight(&self, mut contest_weight: f64, n: usize) -> f64 {
contest_weight *= self.weight_limit;
if let Some(delay_factor) = self.noob_delay.get(n) {
contest_weight *= delay_factor;
}
contest_weight
}
fn compute_sig_perf(&self, weight: f64) -> f64 {
let discrete_perf = (1. + 1. / weight) * self.sig_limit * self.sig_limit;
let continuous_perf = self.drift_per_day / weight;
(discrete_perf + continuous_perf).sqrt()
}
fn compute_sig_drift(&self, weight: f64, delta_secs: f64) -> f64 {
let discrete_drift = weight * self.sig_limit * self.sig_limit;
let continuous_drift = self.drift_per_day * delta_secs / SECS_PER_DAY;
(discrete_drift + continuous_drift).sqrt()
}
}
impl RatingSystem for SimpleEloMMR {
fn individual_update(&self, params: ContestRatingParams, player: &mut Player, mu_perf: f64) {
let weight = self.compute_weight(params.weight, player.times_played_excl());
let sig_perf = self.compute_sig_perf(weight);
let sig_drift = self.compute_sig_drift(weight, player.delta_time as f64);
player.add_noise_best(sig_drift, self.transfer_speed);
player.update_rating_with_logistic(
Rating {
mu: mu_perf,
sig: sig_perf,
},
self.history_len,
);
}
fn round_update(
&self,
params: ContestRatingParams,
mut standings: Vec<(&mut Player, usize, usize)>,
) {
// Update ratings due to waiting period between contests,
// then use it to create Gaussian terms for the Q-function.
// The rank must also be stored in order to determine if it's a win,
// loss, or tie term.
let tanh_terms: Vec<TanhTerm> = standings
.par_iter_mut()
.map(|(player, _, _)| {
let weight = self.compute_weight(params.weight, player.times_played_excl());
let sig_perf = self.compute_sig_perf(weight);
let sig_drift = self.compute_sig_drift(weight, player.delta_time as f64);
player.add_noise_best(sig_drift, self.transfer_speed);
player.approx_posterior.with_noise(sig_perf).into()
})
.collect();
let mul = if self.split_ties { 1. } else { 2. };
// The computational bottleneck: update ratings based on contest performance
standings.into_par_iter().for_each(|(player, lo, hi)| {
let bounds = (-6000.0, 9000.0);
let f = |x| {
let itr1 = tanh_terms[0..lo].iter().map(|term| eval_less(term, x));
let itr2 = tanh_terms[lo..=hi]
.iter()
.map(|term| eval_equal(term, x, mul));
let itr3 = tanh_terms[hi + 1..].iter().map(|term| eval_grea(term, x));
itr1.chain(itr2)
.chain(itr3)
.fold((0., 0.), |(s, sp), (v, vp)| (s + v, sp + vp))
};
let mu_perf = solve_newton(bounds, f).min(params.perf_ceiling);
let weight = self.compute_weight(params.weight, player.times_played_excl());
let sig_perf = self.compute_sig_perf(weight);
player.update_rating_with_logistic(
Rating {
mu: mu_perf,
sig: sig_perf,
},
self.history_len,
);
});
}
}