It works!

This commit is contained in:
2018-10-24 11:12:34 +02:00
parent b39c446b37
commit be74c1eac7
4 changed files with 190 additions and 68 deletions

View File

@@ -7,14 +7,14 @@ mod math;
mod matrix;
use factor_graph::*;
use gaussian::Gaussian;
pub use gaussian::Gaussian;
use matrix::Matrix;
/// Default initial mean of ratings.
const MU: f64 = 25.0;
pub const MU: f64 = 25.0;
/// Default initial standard deviation of ratings.
const SIGMA: f64 = MU / 3.0;
pub const SIGMA: f64 = MU / 3.0;
/// Default distance that guarantees about 76% chance of winning.
const BETA: f64 = SIGMA / 2.0;
@@ -28,6 +28,22 @@ const DRAW_PROBABILITY: f64 = 0.10;
/// A basis to check reliability of the result.
const DELTA: f64 = 0.0001;
fn draw_margin(p: f64, beta: f64, total_players: f64) -> f64 {
math::icdf((p + 1.0) / 2.0) * total_players.sqrt() * beta
}
/*
Constants::Constants() {
double INITIAL_MU = 25.0;
double INITIAL_SIGMA = INITIAL_MU / 3.0;
double TOTAL_PLAYERS = 2.0;
this->BETA = INITIAL_SIGMA / 2.0;
this->EPSILON = draw_margin(0.1, this->BETA, TOTAL_PLAYERS);
this->GAMMA = INITIAL_SIGMA / 100.0;
}
*/
pub fn rate(rating_groups: &[&[Gaussian]]) {
let flatten_ratings = rating_groups
.iter()
@@ -62,7 +78,7 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
for (i, rating) in flatten_ratings.iter().enumerate() {
let variable = ss[i];
let gaussian = Gaussian::from_mean_variance(rating.mean(), (rating.variance() + tau_sqr).sqrt());
let gaussian = Gaussian::from_mu_sigma(rating.mu(), (rating.sigma().powi(2) + tau_sqr).sqrt());
skill.push(PriorFactor::new(
&mut variable_arena,
@@ -118,12 +134,14 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
let mut trunc = Vec::new();
let epsilon = draw_margin(0.1, BETA, 2.0);
for i in 0..size - 1 {
trunc.push(TruncateFactor::new(
&mut variable_arena,
factor_id,
ds[i],
DELTA,
epsilon,
false,
));
@@ -134,22 +152,6 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
factor.start(&mut variable_arena);
}
/*
println!("before:");
for i in 0..size {
let value = variable_arena
.get(ss[i])
.map(|variable| variable.get_value())
.unwrap();
let mu = value.mean();
let sigma = value.std_dev();
println!("* player={}, mu={}, sigma={}", i, mu, sigma);
}
*/
for factor in &skill_to_perf {
factor.update_value(&mut variable_arena);
}
@@ -181,7 +183,6 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
factor.update_mean(&mut variable_arena);
}
/*
println!("after:");
for i in 0..size {
@@ -190,12 +191,11 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
.map(|variable| variable.get_value())
.unwrap();
let mu = value.mean();
let sigma = value.std_dev();
let mu = value.mu();
let sigma = value.sigma();
println!("* player={}, mu={}, sigma={}", i, mu, sigma);
}
*/
}
pub fn quality(rating_groups: &[&[Gaussian]]) -> f64 {
@@ -211,13 +211,13 @@ pub fn quality(rating_groups: &[&[Gaussian]]) -> f64 {
let mut mean_matrix = Matrix::new(length, 1);
for (i, rating) in flatten_ratings.iter().enumerate() {
mean_matrix[(i, 0)] = rating.mean();
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.std_dev().powi(2);
variance_matrix[(i, i)] = rating.sigma().powi(2);
}
let mut rotated_a_matrix = Matrix::new(rating_groups.len() - 1, length);
@@ -261,18 +261,18 @@ mod tests {
#[test]
fn test_quality_1vs1() {
let alice = Gaussian::from_mean_std_dev(MU, SIGMA);
let bob = Gaussian::from_mean_std_dev(MU, SIGMA);
let alice = Gaussian::from_mu_sigma(MU, SIGMA);
let bob = Gaussian::from_mu_sigma(MU, SIGMA);
assert_eq!(quality(&[&[alice], &[bob]]), 0.4472135954999579);
}
#[test]
fn test_rate_1vs1() {
let alice = Gaussian::from_mean_std_dev(MU, SIGMA);
let bob = Gaussian::from_mean_std_dev(MU, SIGMA);
let chris = Gaussian::from_mean_std_dev(MU, SIGMA);
let darren = Gaussian::from_mean_std_dev(MU, SIGMA);
let alice = Gaussian::from_mu_sigma(MU, SIGMA);
let bob = Gaussian::from_mu_sigma(MU, SIGMA);
let chris = Gaussian::from_mu_sigma(MU, SIGMA);
let darren = Gaussian::from_mu_sigma(MU, SIGMA);
// println!("alice: {:?}", alice);
// println!("bob: {:?}", alice);