simu

Ice hockey final standings simulator
git clone https://git.inz.fi/simu
Log | Files | Refs

elo.rs (1022B)


      1 #[derive(Clone, Copy, Debug)]
      2 pub struct Elo(pub f64);
      3 
      4 impl Default for Elo {
      5     fn default() -> Self {
      6         Self(2000.)
      7     }
      8 }
      9 
     10 impl std::fmt::Display for Elo {
     11     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
     12         write!(fmt, "{:.0}", self.0)
     13     }
     14 }
     15 
     16 impl Elo {
     17     pub fn expected(self, other: Self, adv: f64) -> f64 {
     18         1. / (1. + (10_f64).powf((other.0 - self.0 - adv) / 400.0))
     19     }
     20 
     21     pub fn update(&mut self, other: &mut Self, k: f64, actual: f64, adv: f64) {
     22         let expected = self.expected(*other, adv);
     23         self.0 += k * (actual - expected);
     24         other.0 -= k * (actual - expected);
     25     }
     26 
     27     fn regress(&mut self, k: f64, factor: f64) {
     28         self.0 += (Self::default().0 - self.0) * k * factor;
     29     }
     30 
     31     pub fn simulated(&mut self, other: &mut Self, adv: f64, k: f64, regress: f64) -> f64 {
     32         let expected = self.expected(*other, adv);
     33         self.regress(k, regress);
     34         other.regress(k, regress);
     35         expected
     36     }
     37 }