diff --git a/src/factor_graph.rs b/src/factor_graph.rs index 124c366..7022c16 100644 --- a/src/factor_graph.rs +++ b/src/factor_graph.rs @@ -113,7 +113,7 @@ pub struct LikelihoodFactor { id: usize, mean: VariableId, value: VariableId, - variance: f32, + variance: f64, } impl LikelihoodFactor { @@ -122,7 +122,7 @@ impl LikelihoodFactor { id: usize, mean: VariableId, value: VariableId, - variance: f32, + variance: f64, ) -> LikelihoodFactor { if let Some(variable) = variable_arena.get_mut(mean) { variable.attach_factor(id); @@ -183,7 +183,7 @@ pub struct SumFactor { id: usize, sum: VariableId, terms: Vec, - coeffs: Vec, + coeffs: Vec, } impl SumFactor { @@ -192,7 +192,7 @@ impl SumFactor { id: usize, sum: VariableId, terms: Vec, - coeffs: Vec, + coeffs: Vec, ) -> SumFactor { if let Some(variable) = variable_arena.get_mut(sum) { variable.attach_factor(id); @@ -218,7 +218,7 @@ impl SumFactor { variable: VariableId, y: Vec, fy: Vec, - a: &Vec, + a: &Vec, ) { let size = a.len(); @@ -313,21 +313,21 @@ impl SumFactor { } } -fn v_win(t: f32, e: f32) -> f32 { +fn v_win(t: f64, e: f64) -> f64 { math::pdf(t - e) / math::cdf(t - e) } -fn w_win(t: f32, e: f32) -> f32 { +fn w_win(t: f64, e: f64) -> f64 { let vwin = v_win(t, e); vwin * (vwin + t - e) } -fn v_draw(t: f32, e: f32) -> f32 { +fn v_draw(t: f64, e: f64) -> f64 { (math::pdf(-e - t) - math::pdf(e - t)) / (math::cdf(e - t) - math::cdf(-e - t)) } -fn w_draw(t: f32, e: f32) -> f32 { +fn w_draw(t: f64, e: f64) -> f64 { let vdraw = v_draw(t, e); let n = (vdraw * vdraw) + ((e - t) * math::pdf(e - t) + (e + t) * math::pdf(e + t)); let d = math::cdf(e - t) - math::cdf(-e - t); @@ -338,7 +338,7 @@ fn w_draw(t: f32, e: f32) -> f32 { pub struct TruncateFactor { id: usize, variable: VariableId, - epsilon: f32, + epsilon: f64, draw: bool, } @@ -347,7 +347,7 @@ impl TruncateFactor { variable_arena: &mut VariableArena, id: usize, variable: VariableId, - epsilon: f32, + epsilon: f64, draw: bool, ) -> TruncateFactor { if let Some(variable) = variable_arena.get_mut(variable) { diff --git a/src/gaussian.rs b/src/gaussian.rs index e5f3542..b046a7d 100644 --- a/src/gaussian.rs +++ b/src/gaussian.rs @@ -2,8 +2,8 @@ use std::ops; #[derive(Clone, Copy)] pub struct Gaussian { - pub pi: f32, - pub tau: f32, + pub pi: f64, + pub tau: f64, } impl Gaussian { @@ -11,17 +11,17 @@ impl Gaussian { Gaussian::with_pi_tau(0.0, 0.0) } - pub fn with_pi_tau(pi: f32, tau: f32) -> Gaussian { + pub fn with_pi_tau(pi: f64, tau: f64) -> Gaussian { Gaussian { pi, tau } } - pub fn with_mu_sigma(mu: f32, sigma: f32) -> Gaussian { + pub fn with_mu_sigma(mu: f64, sigma: f64) -> Gaussian { let pi = 1.0 / sigma.powi(2); Gaussian::with_pi_tau(pi, pi * mu) } - pub fn mu(&self) -> f32 { + pub fn mu(&self) -> f64 { if self.pi == 0.0 { 0.0 } else { @@ -29,7 +29,7 @@ impl Gaussian { } } - pub fn sigma(&self) -> f32 { + pub fn sigma(&self) -> f64 { (1.0 / self.pi).sqrt() } } diff --git a/src/lib.rs b/src/lib.rs index 5519acf..57f1b90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,27 +8,27 @@ use gaussian::Gaussian; use matrix::Matrix; /// Default initial mean of ratings. -const MU: f32 = 25.0; +const MU: f64 = 25.0; /// Default initial standard deviation of ratings. -const SIGMA: f32 = MU / 3.0; +const SIGMA: f64 = MU / 3.0; /// Default distance that guarantees about 76% chance of winning. -const BETA: f32 = SIGMA / 2.0; +const BETA: f64 = SIGMA / 2.0; /// Default dynamic factor. -const TAU: f32 = SIGMA / 100.0; +const TAU: f64 = SIGMA / 100.0; /// Default draw probability of the game. -const DRAW_PROBABILITY: f32 = 0.10; +const DRAW_PROBABILITY: f64 = 0.10; /// A basis to check reliability of the result. -const DELTA: f32 = 0.0001; +const DELTA: f64 = 0.0001; #[derive(Debug, PartialEq)] pub struct Rating { - pub mu: f32, - pub sigma: f32, + pub mu: f64, + pub sigma: f64, } impl Default for Rating { @@ -191,7 +191,7 @@ fn rate(rating_groups: &[&[Rating]]) { } } -fn quality(rating_groups: &[&[Rating]]) -> f32 { +fn quality(rating_groups: &[&[Rating]]) -> f64 { let flatten_ratings = rating_groups .iter() .flat_map(|group| group.iter()) diff --git a/src/math.rs b/src/math.rs index d96a404..1986b8e 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,6 +1,6 @@ -use std::f32; +use std::f64; -fn erfc(x: f32) -> f32 { +fn erfc(x: f64) -> f64 { let z = x.abs(); let t = 1.0 / (1.0 + z / 2.0); let r = t @@ -21,10 +21,10 @@ fn erfc(x: f32) -> f32 { } } -pub fn cdf(x: f32) -> f32 { - 0.5 * erfc(-x / 2.0f32.sqrt()) +pub fn cdf(x: f64) -> f64 { + 0.5 * erfc(-x / 2.0f64.sqrt()) } -pub fn pdf(x: f32) -> f32 { - 1.0 / (2.0 * f32::consts::PI).sqrt() * (-((x / 1.0).powi(2) / 2.0)).exp() +pub fn pdf(x: f64) -> f64 { + 1.0 / (2.0 * f64::consts::PI).sqrt() * (-((x / 1.0).powi(2) / 2.0)).exp() } diff --git a/src/matrix.rs b/src/matrix.rs index 5b1df8c..4448ec2 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,6 +1,6 @@ use std::ops; -fn det(m: &[f32], x: usize) -> f32 { +fn det(m: &[f64], x: usize) -> f64 { if x == 1 { m[0] } else if x == 2 { @@ -17,7 +17,7 @@ fn det(m: &[f32], x: usize) -> f32 { .map(|(_, v)| *v) .collect::>(); - d += (-1.0f32).powi(n as i32) * m[n] * det(&ms, x - 1); + d += (-1.0f64).powi(n as i32) * m[n] * det(&ms, x - 1); } d @@ -26,7 +26,7 @@ fn det(m: &[f32], x: usize) -> f32 { #[derive(Clone, Debug)] pub struct Matrix { - data: Box<[f32]>, + data: Box<[f64]>, height: usize, width: usize, } @@ -80,7 +80,7 @@ impl Matrix { matrix } - pub fn determinant(&self) -> f32 { + pub fn determinant(&self) -> f64 { debug_assert!(self.width == self.height); det(&self.data, self.width) @@ -122,7 +122,7 @@ impl Matrix { } impl ops::Index<(usize, usize)> for Matrix { - type Output = f32; + type Output = f64; fn index(&self, pos: (usize, usize)) -> &Self::Output { &self.data[(self.width * pos.0) + pos.1] @@ -135,7 +135,7 @@ impl ops::IndexMut<(usize, usize)> for Matrix { } } -impl<'a> ops::Mul<&'a Matrix> for f32 { +impl<'a> ops::Mul<&'a Matrix> for f64 { type Output = Matrix; fn mul(self, rhs: &'a Matrix) -> Matrix {