//! Shared pieces: rotation thresholds, training designs, the fitted direct h-step models, //! random numbers. use super::features::{self, LAGS, P}; use super::glm::{self, Design}; use super::panel::{Panel, C}; /// "In rotation" on channel c = at least tau[c] plays in the week: the thresholds fitted to DR's /// rotation lists if `airplay model calibrate` has run, else each channel's 75th percentile of /// non-zero artist-weeks (at least 3) over weeks < `upto`. pub fn thresholds(p: &Panel, upto: usize) -> [u32; C] { // Calibrated on DR's own rotation lists when available (`airplay model calibrate`). if let Some(t) = p.list_tau { return t; } thresholds_from_plays(p, upto) } pub fn thresholds_from_plays(p: &Panel, upto: usize) -> [u32; C] { let mut tau = [3u32; C]; for (c, t) in tau.iter_mut().enumerate() { let mut v: Vec = (0..p.a()).flat_map(|a| (0..upto).map(move |w| (a, w))).map(|(a, w)| p.y(a, c, w)).filter(|&y| y > 0.0).collect(); if v.is_empty() { continue; } v.sort_by(f64::total_cmp); *t = (v[v.len() * 3 / 4] as u32).max(3); } tau } pub fn mix(mut x: u64) -> u64 { x = x.wrapping_add(0x9E3779B97F4A7C15); x = (x ^ (x >> 30)).wrapping_mul(0xBF58476D1CE4E5B9); x = (x ^ (x >> 27)).wrapping_mul(0x94D049BB133111EB); x ^ (x >> 31) } /// Training rows for horizon h: every origin t with LAGS - 1 <= t and t + h <= last_target. /// Rows with no plays on the channel in the 12 weeks before the origin are mostly zeros; /// a fixed quarter of them is kept, weighted 4. pub fn design(p: &Panel, h: usize, last_target: usize, tau: &[u32; C]) -> (Design, Design) { let mut counts = Design::new(P); let mut rot = Design::new(P); let mut x = vec![0.0; P]; if last_target < LAGS - 1 + h { return (counts, rot); } for t in LAGS - 1..=last_target - h { for a in 0..p.a() { for c in 0..C { let quiet = (0..LAGS).all(|i| p.y(a, c, t - i) == 0.0); let w = if quiet { if mix((a as u64) << 32 ^ (c as u64) << 20 ^ t as u64) % 4 != 0 { continue; } 4.0 } else { 1.0 }; features::row(p, a, c, t, t + h, &mut x); let y = p.y(a, c, t + h); counts.push(&x, y, w); rot.push(&x, (y >= tau[c] as f64) as i32 as f64, w); } } } (counts, rot) } /// The direct h-step models fitted on one design. pub struct Direct { pub h: usize, pub nb: Vec, pub alpha: f64, pub logit: Vec, } impl Direct { pub fn fit(p: &Panel, h: usize, last_target: usize, tau: &[u32; C], with_logit: bool) -> Direct { let (counts, rot) = design(p, h, last_target, tau); let (nb, alpha) = glm::nb(&counts, 1.0); drop(counts); let logit = if with_logit { glm::logit(&rot, 1.0) } else { Vec::new() }; Direct { h, nb, alpha, logit } } /// (expected plays, P(rotation) from the NB, P(rotation) from the logit or NaN). pub fn predict(&self, p: &Panel, a: usize, c: usize, t: usize, tau: u32, x: &mut [f64]) -> (f64, f64, f64) { features::row(p, a, c, t, t + self.h, x); let mu = glm::dot(&self.nb, x).clamp(-30.0, 12.0).exp(); let pl = if self.logit.is_empty() { f64::NAN } else { glm::sigmoid(glm::dot(&self.logit, x)).clamp(1e-9, 1.0 - 1e-9) }; (mu, glm::nb_tail(mu, self.alpha, tau), pl) } } pub struct Rng(pub u64); impl Rng { pub fn u(&mut self) -> f64 { self.0 = mix(self.0); (self.0 >> 11) as f64 / (1u64 << 53) as f64 } pub fn normal(&mut self) -> f64 { let (u1, u2) = (self.u().max(1e-300), self.u()); (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos() } /// Gamma(shape k, scale 1) by Marsaglia–Tsang. pub fn gamma(&mut self, k: f64) -> f64 { if k < 1.0 { return self.gamma(k + 1.0) * self.u().max(1e-300).powf(1.0 / k); } let d = k - 1.0 / 3.0; let c = 1.0 / (9.0 * d).sqrt(); loop { let x = self.normal(); let v = (1.0 + c * x).powi(3); if v <= 0.0 { continue; } let u = self.u(); if u.ln() < 0.5 * x * x + d - d * v + d * v.ln() { return d * v; } } } pub fn poisson(&mut self, lambda: f64) -> f64 { if lambda <= 0.0 { return 0.0; } if lambda > 40.0 { return (lambda + lambda.sqrt() * self.normal()).round().max(0.0); } let l = (-lambda).exp(); let (mut k, mut prod) = (0.0, self.u()); while prod > l { k += 1.0; prod *= self.u(); } k } }