From 79a76b316b4887661dfda0efaae7c57916a9dd20 Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Wed, 27 Oct 2021 09:23:23 +0200 Subject: [PATCH] Fixes, upgrade, and stuff --- Cargo.toml | 4 ++-- README.md | 7 +++++++ examples/nba-history.rs | 25 +++++++++++++------------ src/model/binary.rs | 2 +- src/model/difference.rs | 2 +- src/model/ternary.rs | 2 +- src/observation/ordinal.rs | 4 ++-- tests/kickscore-basics.rs | 6 +++--- tests/nba-history.rs | 25 +++++++++++++------------ 9 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 README.md diff --git a/Cargo.toml b/Cargo.toml index 5fc43b6..6a7d3d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ ndarray = { version = "0.15", features = ["approx", "blas"] } [dev-dependencies] approx = "0.4" -rand = "0.8" -time = "0.2" blas-src = { version = "0.8", features = ["openblas"] } openblas-src = { version = "0.10", features = ["cblas", "system"] } +rand = "0.8" +time = { version = "0.3", features = ["parsing", "macros"] } diff --git a/README.md b/README.md new file mode 100644 index 0000000..df3ec38 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Kickscore + +Rust port of kickscore. + +## Todo + +- [ ] Add all kernels diff --git a/examples/nba-history.rs b/examples/nba-history.rs index ff005e4..75a6554 100644 --- a/examples/nba-history.rs +++ b/examples/nba-history.rs @@ -6,7 +6,7 @@ use std::fs; use std::io::{self, BufRead}; use kickscore as ks; -use time::Date; +use time::{macros::date, Date}; fn main() -> Result<(), Box> { let reader = fs::File::open("data/nba.csv").map(io::BufReader::new)?; @@ -14,7 +14,8 @@ fn main() -> Result<(), Box> { let mut teams = HashSet::new(); let mut observations = Vec::new(); - let cutoff = time::date!(2019 - 06 - 01); + let cutoff = date!(2019 - 06 - 01); + let format = time::format_description::parse("[year]-[month]-[day]").unwrap(); for line in reader.lines().skip(1) { let line = line?; @@ -22,7 +23,7 @@ fn main() -> Result<(), Box> { assert!(data.len() == 5); - let t = Date::parse(data[0], "%F")?; + let t = Date::parse(data[0], &format)?; if t > cutoff { break; @@ -73,9 +74,9 @@ fn main() -> Result<(), Box> { println!("Probability that CHI beats BOS..."); let (p_win, _) = model.probabilities( - &[&"CHI"], - &[&"BOS"], - time::date!(1996 - 01 - 01) + &["CHI"], + &["BOS"], + date!(1996 - 01 - 01) .midnight() .assume_utc() .unix_timestamp() as f64, @@ -83,9 +84,9 @@ fn main() -> Result<(), Box> { println!(" ... in 1996: {:.2}%", 100.0 * p_win); let (p_win, _) = model.probabilities( - &[&"CHI"], - &[&"BOS"], - time::date!(2001 - 01 - 01) + &["CHI"], + &["BOS"], + date!(2001 - 01 - 01) .midnight() .assume_utc() .unix_timestamp() as f64, @@ -93,9 +94,9 @@ fn main() -> Result<(), Box> { println!(" ... in 2001: {:.2}%", 100.0 * p_win); let (p_win, _) = model.probabilities( - &[&"CHI"], - &[&"BOS"], - time::date!(2020 - 01 - 01) + &["CHI"], + &["BOS"], + date!(2020 - 01 - 01) .midnight() .assume_utc() .unix_timestamp() as f64, diff --git a/src/model/binary.rs b/src/model/binary.rs index c1704ea..def22db 100644 --- a/src/model/binary.rs +++ b/src/model/binary.rs @@ -154,7 +154,7 @@ impl BinaryModel { fn process_items(&self, items: &[&str], sign: f64) -> Vec<(usize, f64)> { items .iter() - .map(|key| (self.storage.get_id(&key), sign)) + .map(|key| (self.storage.get_id(key), sign)) .collect() } } diff --git a/src/model/difference.rs b/src/model/difference.rs index 4fdfff7..de72f45 100644 --- a/src/model/difference.rs +++ b/src/model/difference.rs @@ -94,7 +94,7 @@ impl DifferenceModel { fn process_items(&self, items: &[&str], sign: f64) -> Vec<(usize, f64)> { items .iter() - .map(|key| (self.storage.get_id(&key), sign)) + .map(|key| (self.storage.get_id(key), sign)) .collect() } } diff --git a/src/model/ternary.rs b/src/model/ternary.rs index bd23669..7a2ff9a 100644 --- a/src/model/ternary.rs +++ b/src/model/ternary.rs @@ -187,7 +187,7 @@ impl TernaryModel { fn process_items(&self, items: &[&str], sign: f64) -> Vec<(usize, f64)> { items .iter() - .map(|key| (self.storage.get_id(&key), sign)) + .map(|key| (self.storage.get_id(key), sign)) .collect() } } diff --git a/src/observation/ordinal.rs b/src/observation/ordinal.rs index 9d15bc2..5bb7496 100644 --- a/src/observation/ordinal.rs +++ b/src/observation/ordinal.rs @@ -149,7 +149,7 @@ impl ProbitWinObservation { } pub fn probability(storage: &Storage, elems: &[(usize, f64)], t: f64, margin: f64) -> f64 { - let (m, v) = f_params(&elems, t, &storage); + let (m, v) = f_params(elems, t, storage); let (logpart, _, _) = mm_probit_win(m - margin, v); logpart.exp() @@ -188,7 +188,7 @@ impl ProbitTieObservation { } pub fn probability(storage: &Storage, elems: &[(usize, f64)], t: f64, margin: f64) -> f64 { - let (m, v) = f_params(&elems, t, &storage); + let (m, v) = f_params(elems, t, storage); let (logpart, _, _) = mm_probit_tie(m, v, margin); logpart.exp() diff --git a/tests/kickscore-basics.rs b/tests/kickscore-basics.rs index 2836b18..6ea33fd 100644 --- a/tests/kickscore-basics.rs +++ b/tests/kickscore-basics.rs @@ -31,15 +31,15 @@ fn kickscore_basic() { model.fit(); - let (p_win, _p_los) = model.probabilities(&[&"Jerry"], &[&"Tom"], 4.0); + let (p_win, _p_los) = model.probabilities(&["Jerry"], &["Tom"], 4.0); assert_abs_diff_eq!(p_win, 0.7299975928462964, epsilon = f64::EPSILON); - let (p_win, _p_los) = model.probabilities(&[&"Jerry"], &[&"Tom"], 2.0); + let (p_win, _p_los) = model.probabilities(&["Jerry"], &["Tom"], 2.0); assert_abs_diff_eq!(p_win, 0.4455095363120037, epsilon = f64::EPSILON); - let (p_win, _p_los) = model.probabilities(&[&"Jerry"], &[&"Tom"], -1.0); + let (p_win, _p_los) = model.probabilities(&["Jerry"], &["Tom"], -1.0); assert_abs_diff_eq!(p_win, 0.9037560799725326, epsilon = f64::EPSILON); } diff --git a/tests/nba-history.rs b/tests/nba-history.rs index f32a126..c70795b 100644 --- a/tests/nba-history.rs +++ b/tests/nba-history.rs @@ -7,7 +7,7 @@ use std::io::{self, BufRead}; use approx::assert_abs_diff_eq; use kickscore as ks; -use time::Date; +use time::{macros::date, Date}; #[test] fn nba_history() { @@ -18,7 +18,8 @@ fn nba_history() { let mut teams = HashSet::new(); let mut observations = Vec::new(); - let cutoff = time::date!(2019 - 06 - 01); + let cutoff = date!(2019 - 06 - 01); + let format = time::format_description::parse("[year]-[month]-[day]").unwrap(); for line in reader.lines().skip(1) { let line = line.unwrap(); @@ -26,7 +27,7 @@ fn nba_history() { assert!(data.len() == 5); - let t = Date::parse(data[0], "%F").unwrap(); + let t = Date::parse(data[0], &format).unwrap(); if t > cutoff { break; @@ -75,9 +76,9 @@ fn nba_history() { model.fit(); let (p_win, _) = model.probabilities( - &[&"CHI"], - &[&"BOS"], - time::date!(1996 - 01 - 01) + &["CHI"], + &["BOS"], + date!(1996 - 01 - 01) .midnight() .assume_utc() .unix_timestamp() as f64, @@ -86,9 +87,9 @@ fn nba_history() { assert_abs_diff_eq!(p_win, 0.9002599772490479, epsilon = f64::EPSILON); let (p_win, _) = model.probabilities( - &[&"CHI"], - &[&"BOS"], - time::date!(2001 - 01 - 01) + &["CHI"], + &["BOS"], + date!(2001 - 01 - 01) .midnight() .assume_utc() .unix_timestamp() as f64, @@ -97,9 +98,9 @@ fn nba_history() { assert_abs_diff_eq!(p_win, 0.22837870685441986, epsilon = f64::EPSILON); let (p_win, _) = model.probabilities( - &[&"CHI"], - &[&"BOS"], - time::date!(2020 - 01 - 01) + &["CHI"], + &["BOS"], + date!(2020 - 01 - 01) .midnight() .assume_utc() .unix_timestamp() as f64,