Working on History struct. First test is passing.

This commit is contained in:
2022-06-12 23:11:13 +02:00
parent 5a7053fb5d
commit 4227617513
6 changed files with 241 additions and 18 deletions

View File

@@ -1,5 +1,203 @@
use std::collections::{HashMap, HashSet};
use crate::{utils, Agent, Batch, Gaussian, Player, N_INF};
pub struct History {
size: usize,
batches: Vec<Batch>,
agents: HashMap<String, Agent>,
mu: f64,
sigma: f64,
gamma: f64,
p_draw: f64,
time: bool,
}
impl History {
pub fn new(
composition: Vec<Vec<Vec<&str>>>,
results: Vec<Vec<u16>>,
times: Vec<u64>,
priors: HashMap<String, Player>,
mu: f64,
beta: f64,
sigma: f64,
gamma: f64,
p_draw: f64,
) -> Self {
let this_agent = composition
.iter()
.flat_map(|teams| teams.iter())
.flat_map(|team| team.iter())
.cloned()
.collect::<HashSet<_>>();
let agents = this_agent
.into_iter()
.map(|a| {
let player = priors
.get(a)
.cloned()
.unwrap_or_else(|| Player::new(Gaussian::new(mu, sigma), beta, gamma, N_INF));
(
a.to_string(),
Agent {
player,
message: N_INF,
last_time: f64::NEG_INFINITY,
},
)
})
.collect::<HashMap<_, _>>();
println!("{:#?}", agents);
let mut this = Self {
size: composition.len(),
batches: Vec::new(),
agents,
mu,
sigma,
gamma,
p_draw,
time: !times.is_empty(),
};
this.trueskill(composition, results, times);
this
}
fn trueskill(
&mut self,
composition: Vec<Vec<Vec<&str>>>,
results: Vec<Vec<u16>>,
times: Vec<u64>,
) {
let o = {
let mut o = utils::sortperm(&times);
o.reverse();
o
};
let o = o;
let mut i = 0;
while i < self.size {
let mut j = i + 1;
let t = times[o[i]];
while j < self.size && times[o[j]] == t {
j += 1;
}
let composition = (i..j)
.map(|e| composition[o[e]].clone())
.collect::<Vec<_>>();
let results = (i..j).map(|e| results[o[e]].clone()).collect::<Vec<_>>();
let b = Batch::new(
composition,
results,
t as f64,
self.agents.clone(),
self.p_draw,
);
self.batches.push(b.clone());
for a in b.skills.keys() {
let agent = self.agents.get_mut(a).unwrap();
agent.last_time = t as f64;
agent.message = b.forward_prior_out(a);
}
i = j;
}
}
fn iteration(&self) {
todo!()
}
fn convergence(&self) {
let epsilon = 1e-6;
let iterations = 30;
let verbose = true;
todo!()
}
fn learning_curves(&self) {
todo!()
}
fn log_evidence(&self) {
todo!()
}
}
#[cfg(test)]
mod tests {
use approx::assert_ulps_eq;
use crate::{Game, BETA, GAMMA, MU, P_DRAW, SIGMA};
use super::*;
#[test]
fn test_init() {
let composition = vec![
vec![vec!["a"], vec!["b"]],
vec![vec!["a"], vec!["c"]],
vec![vec!["b"], vec!["c"]],
];
let results = vec![vec![1, 0], vec![0, 1], vec![1, 0]];
let mut priors = HashMap::new();
for k in ["a", "b", "c"] {
let player = Player::new(
Gaussian::new(25.0, 25.0 / 3.0),
25.0 / 6.0,
0.15 * 25.0 / 3.0,
N_INF,
);
priors.insert(k.to_string(), player);
}
let h = History::new(
composition,
results,
vec![1, 2, 3],
priors,
MU,
BETA,
SIGMA,
GAMMA,
P_DRAW,
);
let p0 = h.batches[0].posteriors();
assert_ulps_eq!(p0["a"].mu(), 29.205220743876975, epsilon = 0.000001);
assert_ulps_eq!(p0["a"].sigma(), 7.194481422570443, epsilon = 0.000001);
let observed = h.batches[1].skills["a"].forward.sigma();
let gamma: f64 = 0.15 * 25.0 / 3.0;
let expected = (gamma.powi(2) + h.batches[0].posterior("a").sigma().powi(2)).sqrt();
assert_ulps_eq!(observed, expected, epsilon = 0.000001);
let observed = h.batches[1].posterior("a");
let p = Game::new(h.batches[1].within_priors(0), vec![0, 1], P_DRAW).posteriors();
let expected = p[0][0];
assert_ulps_eq!(observed.mu(), expected.mu(), epsilon = 0.000001);
assert_ulps_eq!(observed.sigma(), expected.sigma(), epsilon = 0.000001);
}
}