Initial commit.
This commit is contained in:
110
src/lib.rs
Normal file
110
src/lib.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
mod matrix;
|
||||
|
||||
use matrix::Matrix;
|
||||
|
||||
/// Default initial mean of ratings.
|
||||
const MU: f32 = 25.0;
|
||||
|
||||
/// Default initial standard deviation of ratings.
|
||||
const SIGMA: f32 = MU / 3.0;
|
||||
|
||||
/// Default distance that guarantees about 76% chance of winning.
|
||||
const BETA: f32 = SIGMA / 2.0;
|
||||
|
||||
/// Default dynamic factor.
|
||||
const TAU: f32 = SIGMA / 100.0;
|
||||
|
||||
/// Default draw probability of the game.
|
||||
const DRAW_PROBABILITY: f32 = 0.10;
|
||||
|
||||
/// A basis to check reliability of the result.
|
||||
const DELTA: f32 = 0.0001;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Rating {
|
||||
mu: f32,
|
||||
sigma: f32,
|
||||
}
|
||||
|
||||
impl Default for Rating {
|
||||
fn default() -> Rating {
|
||||
Rating {
|
||||
mu: MU,
|
||||
sigma: SIGMA,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn quality(rating_groups: &[&[Rating]]) -> f32 {
|
||||
let flatten_ratings = rating_groups.iter().flat_map(|group| group.iter()).collect::<Vec<_>>();
|
||||
let flatten_weights = vec![1.0; flatten_ratings.len()].into_boxed_slice();
|
||||
|
||||
let length = flatten_ratings.len();
|
||||
|
||||
let mut mean_matrix = Matrix::new(length, 1);
|
||||
|
||||
for (i, rating) in flatten_ratings.iter().enumerate() {
|
||||
mean_matrix[(i, 0)] = rating.mu;
|
||||
}
|
||||
|
||||
let mut variance_matrix = Matrix::new(length, length);
|
||||
|
||||
for (i, rating) in flatten_ratings.iter().enumerate() {
|
||||
variance_matrix[(i, i)] = rating.sigma.powi(2);
|
||||
}
|
||||
|
||||
let mut rotated_a_matrix = Matrix::new(rating_groups.len() - 1, length);
|
||||
|
||||
let mut t = 0;
|
||||
let mut x = 0;
|
||||
|
||||
for (row, group) in rating_groups.windows(2).enumerate() {
|
||||
let current = group[0];
|
||||
let next = group[1];
|
||||
|
||||
for n in t..t + current.len() {
|
||||
rotated_a_matrix[(row, n)] = flatten_weights[n];
|
||||
t += 1;
|
||||
x += 1;
|
||||
}
|
||||
|
||||
for n in x..x + next.len() {
|
||||
rotated_a_matrix[(row, n)] = -flatten_weights[n];
|
||||
x += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let a_matrix = rotated_a_matrix.transpose();
|
||||
|
||||
let _ata = BETA.powi(2) * &rotated_a_matrix * &a_matrix;
|
||||
let _atsa = &rotated_a_matrix * &variance_matrix * &a_matrix;
|
||||
let start = mean_matrix.transpose() * &a_matrix;
|
||||
let middle = &_ata + &_atsa;
|
||||
let end = &rotated_a_matrix * &mean_matrix;
|
||||
|
||||
let e_arg = (-0.5 * &start * &middle.inverse() * &end).determinant();
|
||||
let s_arg = _ata.determinant() / middle.determinant();
|
||||
|
||||
e_arg.exp() * s_arg.sqrt()
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_quality_1vs1() {
|
||||
let alice = Rating {
|
||||
mu: 25.0,
|
||||
sigma: SIGMA,
|
||||
};
|
||||
|
||||
let bob = Rating {
|
||||
mu: 30.0,
|
||||
sigma: SIGMA,
|
||||
};
|
||||
|
||||
assert_eq!(quality(&[&[alice], &[bob]]), 0.41614607);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user