//! New adds: the chance that an artist gets a (new) song into rotation, rather than the chance //! of being played at all. //! //! The DR artist pages count plays per artist, not per song, so a song-level add isn't visible. //! The proxy used here: //! //! add on channel c in week w the artist is in rotation on c in w (plays >= tau_c) after at //! least GAP weeks out of rotation on c //! add on any DR channel the artist enters rotation on some channel after at least GAP //! weeks out of rotation on every channel (closer to "a new song") //! //! The target is an add within the window of W weeks that starts h weeks after the origin //! (weeks t+h+1 .. t+h+W): "if a window opened h weeks from now, would the artist get an add in it?" //! An artist with a song in rotation now has to drop out first, which is how it usually works: //! in the week of song-level plays we have, only 3-12% of the artists in rotation on a station //! had two songs in rotation at once. Re-entries of an old song also count as adds; the parsed //! rotation lists (song level) will separate them. use super::common::{mix, Rng}; use super::features::{self, LAGS, P}; use super::glm::{self, Design}; use super::calib::Calib; use super::panel::{group, Panel, C, GROUPS}; use anyhow::Result; use std::fmt::Write as _; pub const GAP: usize = 4; pub const W: usize = 8; /// Per-channel rows: the panel features plus these. /// Wave covariates: the chance of a next wave peaks 13–25 weeks after the last one and at /// 0.5–1.25x the artist's usual gap (25 years of Airplay Top-20: weekly hazard 1.7% at 13–25 /// weeks vs 0.5% before and 0.3–0.4% after a year). Appended after the release block. pub const WAVE: usize = 6; /// Feature blocks switched off for an ablation run ("rel", "wave"). pub static WITHOUT: std::sync::OnceLock> = std::sync::OnceLock::new(); fn off(block: &str) -> bool { WITHOUT.get().is_some_and(|v| v.iter().any(|b| b == block)) } pub const WAVE_NAMES: &[&str] = &["DR: weeks since last add 13-25", "DR: last add 26+ weeks ago", "DR: no add in the panel yet", "Airplay Top-20: last new song 13-25 weeks ago", "Airplay Top-20: 0.5-1.25x own usual gap", "Airplay Top-20: last new song 2+ years ago"]; /// Weeks since the artist's last add on any DR channel (as of week t; None = none in the panel), /// and the IFPI wave position. pub fn wave_feats(p: &Panel, r: &Rot, a: usize, t: usize, shift: usize) -> [f64; WAVE] { if off("wave") { return [0.0; WAVE]; } // An add in week w is only confirmed in week w + 1 (the surge rule reads the following week), // so as of week t only adds up to t - 1 are known. let since = (1..=t).find(|&i| r.add_any(a, t - i)).map(|s| s + shift); let ch = p.chart(a, t); let f = |b: bool| b as i32 as f64; let air = ch.air_since as f64 + shift as f64; let known = ch.air_since != u16::MAX; let ratio_ok = known && ch.air_gap > 0 && { let x = air / ch.air_gap as f64; (0.5..=1.25).contains(&x) }; [f(since.is_some_and(|s| (13..=25).contains(&s))), f(since.is_some_and(|s| s >= 26)), f(since.is_none()), f(known && (13.0..=25.0).contains(&air)), f(ratio_ok), f(known && air >= 104.0)] } /// What the forecast assumes about the window: None = what actually happened (training and /// backtests), Some = a scenario (the ranking, the what-if). /// `win`: the window length in weeks (the add must come within it); `shift`: how many weeks after /// the origin the release is assumed, so time-since features (last release, last add, lists, NMF) /// are measured at the release week rather than today. #[derive(Clone, Copy)] pub struct Scenario { pub release: Option, pub nmf: Option, pub shift: usize, pub win: usize } impl Scenario { pub const ACTUAL: Scenario = Scenario { release: None, nmf: None, shift: 0, win: W }; pub fn actual(win: usize) -> Scenario { Scenario { win, ..Self::ACTUAL } } pub fn new(release: bool, nmf: bool) -> Scenario { Scenario { release: Some(release), nmf: Some(nmf), shift: 0, win: W } } pub fn at(self, shift: usize) -> Scenario { Scenario { shift, ..self } } pub fn window(self, win: usize) -> Scenario { Scenario { win, ..self } } } /// New Music Friday Denmark: placed in the window (and in its top 10), placements in the past /// year, and the share of the artist's releases over two years that were placed. pub const NMF: usize = 4; pub const NMF_NAMES: &[&str] = &["NMF Denmark in the window", "NMF Denmark top 10 in the window", "log NMF Denmark placements last 52w", "share of releases on NMF Denmark (2 years)"]; fn window(p: &Panel, t: usize, h: usize, win: usize) -> (chrono::NaiveDate, chrono::NaiveDate) { let w0 = p.weeks[t] + chrono::Duration::weeks(h as i64 + 1); (w0, w0 + chrono::Duration::weeks(win as i64)) } /// A release in week t0+h+1 is "too soon" if the artist's latest release (as of t0) is less than /// CLEAN_WEEKS before it: the release-event model only learned from releases with a clear gap. pub fn too_soon(p: &Panel, a: usize, t0: usize, h: usize) -> bool { let Some(dates) = p.releases[a].as_ref() else { return false }; let end_t = p.weeks[t0] + chrono::Duration::days(6); let start = p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1); dates.iter().rev().find(|d| **d <= end_t).is_some_and(|d| (start - *d).num_days() < 7 * CLEAN_WEEKS as i64) } /// Did the artist release something (Deezer discography) in DR week w (Monday to Sunday)? pub fn released_in_week(p: &Panel, a: usize, w: usize) -> bool { let Some(dates) = p.releases[a].as_ref() else { return false }; let start = p.weeks[0] + chrono::Duration::weeks(w as i64); dates.iter().any(|d| *d >= start && *d < start + chrono::Duration::days(7)) } /// Weeks since the artist's latest release as of week t, in bins: <6, 6–11, 12–25, 26–51, 52+, none. pub fn since_bin(p: &Panel, a: usize, t: usize) -> usize { let Some(dates) = p.releases[a].as_ref() else { return 5 }; let end_t = p.weeks[t] + chrono::Duration::days(6); match dates.iter().rev().find(|d| **d <= end_t).map(|d| (end_t - *d).num_days() / 7) { None => 5, Some(w) if w < 6 => 0, Some(w) if w < 12 => 1, Some(w) if w < 26 => 2, Some(w) if w < 52 => 3, Some(_) => 4, } } /// Does the artist release something in the window weeks t+h+1 ..= t+h+win? pub fn releases_in(p: &Panel, a: usize, t: usize, h: usize, win: usize) -> bool { let Some(dates) = p.releases[a].as_ref() else { return false }; let (w0, w1) = window(p, t, h, win); dates.iter().any(|d| *d >= w0 && *d < w1) } /// A clean release event in week w: a release that week and none in the 6 weeks before it, so an /// add that follows can't be the previous song's (DR's counts are per artist, not per song). pub const CLEAN_WEEKS: usize = 6; pub fn clean_release(p: &Panel, a: usize, w: usize) -> bool { released_in_week(p, a, w) && (w.saturating_sub(CLEAN_WEEKS)..w).all(|k| !released_in_week(p, a, k)) } /// P(an artist's release is placed on NMF Denmark), from the past two years (shrunk). pub fn p_nmf(p: &Panel, a: usize, t: usize) -> f64 { let end_t = p.weeks[t] + chrono::Duration::days(6); let placed = p.nmf_dk[a].iter().filter(|(d, _)| *d <= end_t && (end_t - *d).num_days() <= 730).count() as f64; let rels = p.releases[a].as_ref().map(|v| v.iter().filter(|d| **d <= end_t && (end_t - **d).num_days() <= 730).count()).unwrap_or(0) as f64; ((placed + 0.1) / (rels + 1.0)).clamp(0.0, 0.95) } /// NMF Denmark position as a score: 1 at #1, falling with the log of the position to 0 at #120 /// (a placement without a known position counts as #40). Used instead of "top 10" when the /// AIRPLAY_NMF_POS environment variable is set (for the backtest comparison). fn pos_score(pos: Option) -> f64 { (1.0 - (pos.unwrap_or(40).max(1) as f64).ln() / 120f64.ln()).clamp(0.0, 1.0) } fn nmf_pos() -> bool { static V: std::sync::OnceLock = std::sync::OnceLock::new(); *V.get_or_init(|| std::env::var("AIRPLAY_NMF_POS").is_ok()) } pub fn nmf_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario) -> [f64; NMF] { if off("nmf") { return [0.0; NMF]; } let end_t = p.weeks[t] + chrono::Duration::days(6); let asof = end_t + chrono::Duration::weeks(sc.shift as i64); let (w0, w1) = window(p, t, h, sc.win); let placed = p.nmf_dk[a].iter().filter(|(d, _)| *d >= w0 && *d < w1); let (in_win, top10) = match sc.nmf { None => { let v: Vec<_> = placed.collect(); (!v.is_empty(), v.iter().any(|(_, pos)| pos.is_some_and(|x| x <= 10))) } Some(false) => (false, false), // scenario "placed": top 10 as often as the artist's past placements were Some(true) => (true, false), }; let past: Vec<&(chrono::NaiveDate, Option)> = p.nmf_dk[a].iter().filter(|(d, _)| *d <= end_t).collect(); let top10_share = if past.is_empty() { 0.0 } else { past.iter().filter(|(_, pos)| pos.is_some_and(|x| x <= 10)).count() as f64 / past.len() as f64 }; let last_year = past.iter().filter(|(d, _)| (asof - *d).num_days() <= 364).count() as f64; let f = |b: bool| b as i32 as f64; if nmf_pos() { let best = p.nmf_dk[a].iter().filter(|(d, _)| *d >= w0 && *d < w1).map(|(_, pos)| pos_score(*pos)).fold(0.0, f64::max); let past_mean = if past.is_empty() { 0.0 } else { past.iter().map(|(_, pos)| pos_score(*pos)).sum::() / past.len() as f64 }; let score = match sc.nmf { None => best, Some(false) => 0.0, Some(true) => past_mean }; return [f(in_win), score, last_year.ln_1p(), p_nmf(p, a, t)]; } [f(in_win), if sc.nmf == Some(true) { top10_share } else { f(top10) }, last_year.ln_1p(), p_nmf(p, a, t)] } /// Long-history covariates: DR's 2012–2021 archive and DR's real rotation lists before week t. pub const LONG: usize = 5; pub const LONG_NAMES: &[&str] = &["log archive plays per 100 sampled hours (2012-21)", "log DR lists with the artist before t (2023-)", "DR list recency 26w", "DR lists known before t at all", "quiet former favourite (4+ weeks on DR 2019-24, out of rotation 26w)"]; /// Not in rotation anywhere in the 26 weeks to t (as far as the panel goes back). fn quiet(r: &Rot, a: usize, t: usize) -> bool { (t.saturating_sub(25)..=t).all(|w| !r.any(a, w)) } pub fn long_feats(p: &Panel, r: &Rot, a: usize, t: usize, _h: usize, sc: Scenario) -> [f64; LONG] { if off("long") { return [0.0; LONG]; } let shift = sc.shift; // The comeback case: a former favourite (archive pages 2019-24, before the panel) out of // rotation for half a year. One bounded flag: a continuous favourite score next to its // interaction with "quiet" fitted two large opposing coefficients on one panel year. let fav = !off("fav") && p.fav_weeks[a] >= 4.0 && quiet(r, a, t); let end_t = p.weeks[t] + chrono::Duration::days(6); // A list published on a Friday is known from then. let before: Vec<&chrono::NaiveDate> = p.listed_on[a].iter().filter(|d| **d <= end_t).collect(); let since = before.last().map(|d| (end_t - **d).num_days() as f64 / 7.0 + shift as f64); [p.archive_rate[a].ln_1p(), (before.len() as f64).ln_1p(), since.map(|s| (-s / 26.0).exp()).unwrap_or(0.0), (!before.is_empty()) as i32 as f64, fav as i32 as f64] } /// Season: the mean log season index over the window's weeks (new Airplay Top-20 songs by week of /// the year, 2002–2024). pub const SEASON: usize = 1; pub const SEASON_NAMES: &[&str] = &["season index of the window (log, IFPI 2002-24)"]; pub fn season_feat(p: &Panel, t: usize, h: usize, win: usize) -> [f64; SEASON] { if off("season") { return [0.0]; } let start = p.weeks[t] + chrono::Duration::weeks(h as i64 + 1); let v: f64 = (0..win).map(|k| { let d = start + chrono::Duration::weeks(k as i64); p.season[chrono::Datelike::iso_week(&d).week() as usize].ln() }).sum::() / win as f64; [v] } /// Channel affinity and genre fit (per-channel rows only): the share of the artist's DR plays that /// were on this station group (all-time top tracks, minus panel plays after week t), the log of /// those plays, "never played here", and how much DR plays the artist's genre on this group. pub const AFF: usize = 4; pub const AFF_NAMES: &[&str] = &["share of the artist's DR plays on this station (before t)", "log DR plays on this station (before t)", "never played on this station (before t)", "genre fit for this station"]; pub fn aff_feats(p: &Panel, a: usize, c: usize, t: usize) -> [f64; AFF] { if off("aff") { return [0.0; AFF]; } // Plays after week t (the future, as seen from t) taken out of the all-time counts. let after = |g: usize| -> f64 { let cs: Vec = (0..C).filter(|&k| group(k) == g).collect(); let n = cs.len() as f64; (t + 1..p.w()).map(|w| cs.iter().map(|&k| p.y(a, k, w)).sum::() / n).sum() }; let before: [f64; GROUPS] = std::array::from_fn(|g| (p.alltime[a][g] - after(g)).max(0.0)); let tot: f64 = before.iter().sum(); let g = group(c); [if tot > 0.0 { before[g] / tot } else { 0.0 }, before[g].ln_1p(), (before[g] == 0.0) as i32 as f64, p.genre_fit[a][g]] } /// Release covariates (Deezer discography), appended to both row kinds. pub const REL: usize = 6; pub const REL_NAMES: &[&str] = &["discography known", "release recency 4w", "release recency 12w", "log releases last 52w", "releases in the window", "releases in window x not in rotation now"]; /// Weeks since the latest release, releases in the past year, and whether the artist releases in /// the window t+h+1 ..= t+h+W (the actual one when `rel` is None: training and backtests; else the /// scenario "releases / doesn't release in the window"). pub fn rel_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario, in_rot_now: bool) -> [f64; REL] { let rel = sc.release; if off("rel") { return [0.0; REL]; } let Some(dates) = p.releases[a].as_ref() else { return [0.0; REL] }; let end_t = p.weeks[t] + chrono::Duration::days(6); let asof = end_t + chrono::Duration::weeks(sc.shift as i64); let since = dates.iter().rev().find(|d| **d <= end_t).map(|d| (asof - *d).num_days() as f64 / 7.0); let last_year = dates.iter().filter(|d| **d <= end_t && (asof - **d).num_days() <= 364).count() as f64; let (w0, w1) = window(p, t, h, sc.win); let in_window = rel.unwrap_or_else(|| dates.iter().any(|d| *d >= w0 && *d < w1)); let f = |b: bool| b as i32 as f64; [1.0, since.map(|s| (-s / 4.0).exp()).unwrap_or(0.0), since.map(|s| (-s / 12.0).exp()).unwrap_or(0.0), last_year.ln_1p(), f(in_window), f(in_window && !in_rot_now)] } /// P(the artist releases something in an 8-week window), from their release rate over two years /// (shrunk towards one release a year for artists with little history). pub fn p_release(p: &Panel, a: usize, t: usize) -> Option { let dates = p.releases[a].as_ref()?; let end_t = p.weeks[t] + chrono::Duration::days(6); let n = dates.iter().filter(|d| **d <= end_t && (end_t - **d).num_days() <= 730).count() as f64; let per_year = (n + 1.0) / 3.0; Some(1.0 - (-per_year * W as f64 / 52.0).exp()) } pub const EXTRA: &[&str] = &["in rotation on channel now", "in rotation anywhere now", "station groups in rotation now (/5)", "run on channel = 1 week", "run = 2 weeks", "run = 3-5 weeks", "run >= 6 weeks", "near threshold (>= half of tau, not in rotation)", "plays / tau (capped 3, /3)", "weeks since in rotation on channel (/12)"]; pub const PC: usize = P + 10 + REL + WAVE + LONG + NMF + SEASON + AFF; /// Artist-level ("any channel") rows. pub const ANY_NAMES: &[&str] = &[ "intercept", "log DR plays t", "log DR plays 4w", "log DR plays 12w", "log max DR plays 12w", "in rotation anywhere now", "station groups in rotation now (/5)", "current run anywhere (/12)", "weeks since in rotation anywhere (/12)", "Track Top-40 score", "Airplay Top-20 score", "chart entry recency 6w", "chart entry recency 26w", "log Track Top-40 hits", "log Airplay Top-20 hits", "Danish", "group", "female", "career years (/50)", "debut unknown", "Christmas in window", ]; pub const PA: usize = 21 + REL + WAVE + LONG + NMF + SEASON; /// Positions of the chart covariates (for the what-if): (t40 score, air score, recency 6w, recency 26w). pub const CHART_IDX_CHANNEL: [usize; 4] = [10, 11, 12, 13]; pub const CHART_IDX_ANY: [usize; 4] = [9, 10, 11, 12]; pub struct Rot { nw: usize, pub tau: [u32; C], rot: Vec, // [a][c][w] any: Vec, // [a][w] adds: Vec, // [a][c][w] adds_any: Vec, // [a][w] } /// Which add definition: "onset" = entering rotation after GAP+ weeks out (the original); "surge" = /// onset, or plays jumping well above the artist's own 8-week baseline and staying up the next /// week (catches new songs of artists who never leave rotation). Default: surge. pub static ADD_RULE: std::sync::OnceLock = std::sync::OnceLock::new(); fn surge_rule() -> bool { ADD_RULE.get().is_none_or(|r| r != "onset") } /// Surge-rule constants: [floor (× τ), jump over the baseline (× τ), jump (× baseline), held the /// next week (× τ and × baseline)]. Tuned against DR's lists (`airplay model addcheck --tune`). pub const SURGE_DEFAULT: [f64; 4] = [0.6, 0.5, 0.5, 0.25]; pub static SURGE: std::sync::OnceLock<[f64; 4]> = std::sync::OnceLock::new(); impl Rot { pub fn new(p: &Panel, tau: &[u32; C]) -> Rot { Self::new_with(p, tau, *SURGE.get().unwrap_or(&SURGE_DEFAULT)) } pub fn new_with(p: &Panel, tau: &[u32; C], k: [f64; 4]) -> Rot { let nw = p.w(); let mut rot = vec![false; p.a() * C * nw]; let mut any = vec![false; p.a() * nw]; for a in 0..p.a() { for c in 0..C { for w in 0..nw { let r = p.y(a, c, w) >= tau[c] as f64; rot[(a * C + c) * nw + w] = r; if r { any[a * nw + w] = true; } } } } let mut adds = vec![false; p.a() * C * nw]; let mut adds_any = vec![false; p.a() * nw]; let surge = surge_rule(); for a in 0..p.a() { for c in 0..C { let on = |w: usize| rot[(a * C + c) * nw + w]; let t = tau[c] as f64; for w in GAP..nw { let onset = on(w) && (1..=GAP).all(|k| !on(w - k)); let jump = surge && w >= 8 && w + 1 < nw && { let mut b: Vec = (w - 8..w).map(|k| p.y(a, c, k)).collect(); b.sort_by(f64::total_cmp); let base = b[4]; let (y0, y1) = (p.y(a, c, w), p.y(a, c, w + 1)); y0 >= (k[0] * t).max(base + (k[1] * t).max(k[2] * base)) && y1 >= (k[0] * t).max(base + (k[3] * t).max(k[3] * base)) }; if (onset || jump) && (1..=GAP).all(|k| !adds[(a * C + c) * nw + w - k]) { adds[(a * C + c) * nw + w] = true; } } } for w in GAP..nw { let classic = any[a * nw + w] && (1..=GAP).all(|k| !any[a * nw + w - k]); let some = if surge { (0..C).any(|c| adds[(a * C + c) * nw + w]) } else { classic }; if some && (1..=GAP).all(|k| !adds_any[a * nw + w - k]) { adds_any[a * nw + w] = true; } } } Rot { nw, tau: *tau, rot, any, adds, adds_any } } pub fn on(&self, a: usize, c: usize, w: usize) -> bool { self.rot[(a * C + c) * self.nw + w] } pub fn any(&self, a: usize, w: usize) -> bool { self.any[a * self.nw + w] } pub fn add(&self, a: usize, c: usize, w: usize) -> bool { w < self.nw && self.adds[(a * C + c) * self.nw + w] } pub fn add_any(&self, a: usize, w: usize) -> bool { w < self.nw && self.adds_any[a * self.nw + w] } /// An add in weeks t+h+1 ..= t+h+W (all inside the panel). pub fn add_in_window(&self, a: usize, c: usize, t: usize, h: usize) -> bool { self.add_in(a, c, t, h, W) } pub fn add_any_in_window(&self, a: usize, t: usize, h: usize) -> bool { self.add_any_in(a, t, h, W) } pub fn add_in(&self, a: usize, c: usize, t: usize, h: usize, win: usize) -> bool { (t + h + 1..=t + h + win).any(|w| self.add(a, c, w)) } pub fn add_any_in(&self, a: usize, t: usize, h: usize, win: usize) -> bool { (t + h + 1..=t + h + win).any(|w| self.add_any(a, w)) } } /// Station groups (P4 once) where the artist is in rotation in week t. fn groups_on(r: &Rot, a: usize, t: usize) -> f64 { let mut on = [false; GROUPS]; for c in 0..C { if r.on(a, c, t) { on[group(c)] = true; } } on.iter().filter(|x| **x).count() as f64 } fn tau_of(_p: &Panel, r: &Rot, _a: usize, c: usize, _t: usize) -> f64 { r.tau[c] as f64 } fn run_and_since(flag: impl Fn(usize) -> bool, t: usize) -> (f64, f64) { let run = (0..LAGS).take_while(|&i| flag(t - i)).count() as f64 / LAGS as f64; let since = (0..LAGS).find(|&i| flag(t - i)).unwrap_or(LAGS) as f64 / LAGS as f64; (run, since) } fn christmas_in_window(p: &Panel, t: usize, h: usize, win: usize) -> bool { (t + h + 1..=t + h + win).any(|w| features::christmas(p, w)) } pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, sc: Scenario, out: &mut [f64]) { features::row(p, a, c, t, t + h + 1, &mut out[..P]); out[21] = christmas_in_window(p, t, h, sc.win) as i32 as f64; let n_on = groups_on(r, a, t); let (run, since) = run_and_since(|w| r.on(a, c, w), t); let weeks = (run * LAGS as f64).round() as usize; let on = r.on(a, c, t); let ratio = p.y(a, c, t) / tau_of(p, r, a, c, t); let f = |b: bool| b as i32 as f64; out[P..P + 10].copy_from_slice(&[f(on), f(r.any(a, t)), n_on / GROUPS as f64, f(weeks == 1), f(weeks == 2), f((3..=5).contains(&weeks)), f(weeks >= 6), f(!on && ratio >= 0.5), ratio.min(3.0) / 3.0, since]); out[P + 10..P + 10 + REL].copy_from_slice(&rel_feats(p, a, t, h, sc, on)); out[P + 10 + REL..P + 10 + REL + WAVE].copy_from_slice(&wave_feats(p, r, a, t, sc.shift)); out[P + 10 + REL + WAVE..P + 10 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, r, a, t, h, sc)); out[P + 10 + REL + WAVE + LONG..PC - SEASON - AFF].copy_from_slice(&nmf_feats(p, a, t, h, sc)); out[PC - SEASON - AFF..PC - AFF].copy_from_slice(&season_feat(p, t, h, sc.win)); out[PC - AFF..PC].copy_from_slice(&aff_feats(p, a, c, t)); } pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, out: &mut [f64]) { let tot = |w: usize| p.group_total(a, w, None); let mean = |n: usize| (0..n).map(|i| tot(t - i)).sum::() / n as f64; let max12 = (0..LAGS).map(|i| tot(t - i)).fold(0.0, f64::max); let n_on = groups_on(r, a, t); let (run, since) = run_and_since(|w| r.any(a, w), t); let ch = p.chart(a, t); let art = &p.artists[a]; let recency = |s: f64| if ch.weeks_since_entry == u16::MAX { 0.0 } else { (-(ch.weeks_since_entry as f64) / s).exp() }; let year = chrono::Datelike::year(&p.weeks[t]); let v = [ 1.0, tot(t).ln_1p(), mean(4).ln_1p(), mean(12).ln_1p(), max12.ln_1p(), r.any(a, t) as i32 as f64, n_on / GROUPS as f64, run, since, if ch.t40_rank > 0 { (41.0 - ch.t40_rank as f64) / 40.0 } else { 0.0 }, if ch.air_rank > 0 { (21.0 - ch.air_rank as f64) / 20.0 } else { 0.0 }, recency(6.0), recency(26.0), (ch.t40_hits as f64).ln_1p(), (ch.air_hits as f64).ln_1p(), art.danish as i32 as f64, art.group as i32 as f64, art.female as i32 as f64, art.debut.map(|d| ((year - d).clamp(0, 50)) as f64 / 50.0).unwrap_or(0.0), art.debut.is_none() as i32 as f64, christmas_in_window(p, t, h, sc.win) as i32 as f64, ]; out[..21].copy_from_slice(&v); out[21..21 + REL].copy_from_slice(&rel_feats(p, a, t, h, sc, r.any(a, t))); out[21 + REL..21 + REL + WAVE].copy_from_slice(&wave_feats(p, r, a, t, sc.shift)); out[21 + REL + WAVE..21 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, r, a, t, h, sc)); out[21 + REL + WAVE + LONG..PA - SEASON].copy_from_slice(&nmf_feats(p, a, t, h, sc)); out[PA - SEASON..PA].copy_from_slice(&season_feat(p, t, h, sc.win)); } /// Logit models for one h, trained on origins whose whole window is observed by `last_week`. pub struct AddModel { pub h: usize, /// Window length in weeks (the add must come within it). pub win: usize, pub channel: Vec, pub any: Vec, /// Platt recalibration (a, b): p' = sigmoid(a + b * logit(p)); identity when there wasn't /// enough history to fit it honestly. pub platt_channel: (f64, f64), pub platt_any: (f64, f64), /// Recalibration fitted on pooled out-of-sample forecasts (replaces the Platt pair when set). pub calib_channel: Option, pub calib_any: Option, } /// Recent origins used to fit the recalibration. const CAL_ORIGINS: usize = 6; fn logit(p: f64) -> f64 { let p = p.clamp(1e-6, 1.0 - 1e-6); (p / (1.0 - p)).ln() } fn platt(ab: (f64, f64), p: f64) -> f64 { glm::sigmoid(ab.0 + ab.1 * logit(p)).clamp(1e-6, 1.0 - 1e-6) } fn origins(h: usize, last_week: usize) -> std::ops::RangeInclusive { origins_w(h, last_week, W) } fn origins_w(h: usize, last_week: usize, win: usize) -> std::ops::RangeInclusive { // The label's last week needs the following week too (surge rule): the window must close at // least one week before `last_week`. let hi = last_week.saturating_sub(h + win + 1); LAGS - 1..=hi } impl AddModel { /// The logits on every window closed by `last_week`, then a recalibration: an inner model fitted /// on windows closed before the last CAL_ORIGINS origins predicts those origins, and a Platt fit /// maps its probabilities to what happened. (The raw fits were overconfident above ~0.4.) pub fn fit(p: &Panel, r: &Rot, h: usize, last_week: usize) -> AddModel { Self::fit_with(p, r, h, last_week, false, W) } /// The release-event model: "if the artist releases a new song in week t+1, will DR add it /// within 8 weeks?" Trained only on artist-weeks where the artist did release in week t+1, with /// the artist's state the week before (earlier releases and NMF placements count as history; /// the new song's own NMF placement is the scenario variable). /// As `fit_events`, for a window of `win` weeks (6-origin recalibration; used in backtests). pub fn fit_events_win(p: &Panel, r: &Rot, last_week: usize, win: usize) -> AddModel { Self::fit_with(p, r, 0, last_week, true, win) } /// The site's release-event model (8-week window, pooled recalibration). pub fn fit_events_site(p: &Panel, r: &Rot, last_week: usize) -> AddModel { Self::fit_events_pooled(p, r, last_week, W) } /// The release-event model for a window of `win` weeks. pub fn fit_events_w(p: &Panel, r: &Rot, last_week: usize, win: usize) -> AddModel { Self::fit_events_pooled(p, r, last_week, win) } /// The release-event model recalibrated on pooled out-of-sample forecasts: a rolling refit (every /// 4 weeks) predicts each later week's clean releases from releases whose outcome was already /// known; Platt per station and isotonic for any station are fitted on all those forecasts /// (the combination that scored best in the backtest), then attached to the model fitted on /// everything known by `last_week`. pub fn fit_events_pooled(p: &Panel, r: &Rot, last_week: usize, win: usize) -> AddModel { let (mut pc_pairs, mut pa_pairs) = (Vec::new(), Vec::new()); let first = LAGS - 1 + win + 1 + 8; let last = last_week.saturating_sub(win + 1); let mut model: Option = None; for t0 in first..=last { if (t0 - first) % 4 == 0 || model.is_none() { model = Some(Self::fit_raw(p, r, 0, t0, true, win)); } let m = model.as_ref().unwrap(); for a in 0..p.a() { if !p.known(a, t0) || !clean_release(p, a, t0 + 1) { continue; } let q = p_nmf(p, a, t0); let sc = |b: bool| Scenario::new(true, b).window(win); let (rcn, ran) = m.predict_raw(p, r, a, t0, 0, sc(true)); let (rcr, rar) = m.predict_raw(p, r, a, t0, 0, sc(false)); pa_pairs.push((q * ran + (1.0 - q) * rar, r.add_any_in(a, t0, 0, win))); for c in 0..C { pc_pairs.push((q * rcn[c] + (1.0 - q) * rcr[c], r.add_in(a, c, t0, 0, win))); } } } let mut m = Self::fit_raw(p, r, 0, last_week, true, win); m.calib_channel = Some(Calib::platt(&pc_pairs)); m.calib_any = Some(Calib::isotonic(&pa_pairs)); m } fn fit_with(p: &Panel, r: &Rot, h: usize, last_week: usize, events: bool, win: usize) -> AddModel { let mut m = Self::fit_raw(p, r, h, last_week, events, win); let hi = last_week.saturating_sub(h + win + 1); if hi + 1 >= CAL_ORIGINS + LAGS { let c0 = hi + 1 - CAL_ORIGINS; // the inner model may only use windows that closed before the first calibration origin if c0 >= LAGS - 1 + h + win + 4 { let inner = Self::fit_raw(p, r, h, c0, events, win); let (mut dc, mut da) = (Design::new(2), Design::new(2)); for t in c0..=hi { for a in 0..p.a() { if !p.known(a, t) { continue; } if events && !clean_release(p, a, t + 1) { continue; } let (pc, pa) = inner.predict(p, r, a, t, Scenario::actual(win), None); for c in 0..C { if pc[c] < 1e-4 && !r.add_in(a, c, t, h, win) { continue; } // certain zeros carry no information dc.push(&[1.0, logit(pc[c])], r.add_in(a, c, t, h, win) as i32 as f64, 1.0); } da.push(&[1.0, logit(pa)], r.add_any_in(a, t, h, win) as i32 as f64, 1.0); } } let (bc, ba) = (glm::logit(&dc, 0.0), glm::logit(&da, 0.0)); // keep the map monotone and sane if bc[1] > 0.2 { m.platt_channel = (bc[0], bc[1]); } if ba[1] > 0.2 { m.platt_any = (ba[0], ba[1]); } } } m } fn fit_raw(p: &Panel, r: &Rot, h: usize, last_week: usize, events: bool, win: usize) -> AddModel { let (mut dc, mut da) = (Design::new(PC), Design::new(PA)); let (mut x, mut xa) = (vec![0.0; PC], vec![0.0; PA]); let sc = Scenario::actual(win); for t in origins_w(h, last_week, win) { for a in 0..p.a() { // Only artists knowable at the origin (no survivorship from later discovery). if !p.known(a, t) { continue; } if events && !clean_release(p, a, t + 1) { continue; } for c in 0..C { // Channels where the artist hasn't been heard for 12 weeks: keep a quarter, weighted 4. let quiet = (0..LAGS).all(|i| p.y(a, c, t - i) == 0.0); let wgt = if quiet && !events { if mix((a as u64) << 32 ^ (c as u64) << 20 ^ t as u64 ^ 0xadd) % 4 != 0 { continue; } 4.0 } else { 1.0 }; row_channel(p, r, a, c, t, h, sc, &mut x); dc.push(&x, r.add_in(a, c, t, h, win) as i32 as f64, wgt); } row_any(p, r, a, t, h, sc, &mut xa); da.push(&xa, r.add_any_in(a, t, h, win) as i32 as f64, 1.0); } } AddModel { h, win, channel: glm::logit(&dc, 1.0), any: glm::logit(&da, 1.0), platt_channel: (0.0, 1.0), platt_any: (0.0, 1.0), calib_channel: None, calib_any: None } } /// (P(add) per channel, P(add on any channel)) from origin t. /// `sc`: what the window holds (Scenario::ACTUAL in backtests). pub fn predict(&self, p: &Panel, r: &Rot, a: usize, t: usize, sc: Scenario, chart: Option<&dyn Fn(&mut [f64], [usize; 4])>) -> ([f64; C], f64) { self.predict_at(p, r, a, t, self.h, sc, chart) } /// As `predict`, for a window starting h weeks after t (the release-event model: a release in /// week t+h+1, with the time-since features moved forward by `sc.shift`). pub fn predict_at(&self, p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, chart: Option<&dyn Fn(&mut [f64], [usize; 4])>) -> ([f64; C], f64) { self.predict_full(p, r, a, t, h, sc, chart, false) } /// The logits' own probabilities, without recalibration or the any-channel cap. pub fn predict_raw(&self, p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario) -> ([f64; C], f64) { self.predict_full(p, r, a, t, h, sc, None, true) } #[allow(clippy::too_many_arguments)] fn predict_full(&self, p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, chart: Option<&dyn Fn(&mut [f64], [usize; 4])>, raw: bool) -> ([f64; C], f64) { let sc = sc.window(self.win); let mut x = vec![0.0; PC]; let mut pc = [0.0; C]; for c in 0..C { row_channel(p, r, a, c, t, h, sc, &mut x); if let Some(f) = chart { f(&mut x, CHART_IDX_CHANNEL); } let p0 = glm::sigmoid(glm::dot(&self.channel, &x)).clamp(1e-6, 1.0 - 1e-6); pc[c] = if raw { p0 } else { self.calib_channel.as_ref().map(|k| k.apply(p0)).unwrap_or_else(|| platt(self.platt_channel, p0)) }; } let mut xa = vec![0.0; PA]; row_any(p, r, a, t, h, sc, &mut xa); if let Some(f) = chart { f(&mut xa, CHART_IDX_ANY); } // Coherence: "any station" can't exceed what the stations allow together (P4 counted once, // as its best region): at most 1 - prod(1 - p) over the station groups. let mut best = [0.0f64; GROUPS]; for c in 0..C { best[group(c)] = best[group(c)].max(pc[c]); } let cap = 1.0 - best.iter().map(|p| 1.0 - p).product::(); let p0 = glm::sigmoid(glm::dot(&self.any, &xa)).clamp(1e-6, 1.0 - 1e-6); if raw { return (pc, p0); } let pa = self.calib_any.as_ref().map(|k| k.apply(p0)).unwrap_or_else(|| platt(self.platt_any, p0)); (pc, pa.min(cap.max(1e-6))) } } /// Sets the chart covariates at `idx` (t40 score, air score, recency 6w, recency 26w). pub fn set_chart(x: &mut [f64], idx: [usize; 4], entry: Option>, t40: Option, air: Option) { if let Some(e) = entry { let (r6, r26) = match e { Some(w) => ((-w / 6.0).exp(), (-w / 26.0).exp()), None => (0.0, 0.0) }; x[idx[2]] = r6; x[idx[3]] = r26; } if let Some(r) = t40 { x[idx[0]] = if (1.0..=40.0).contains(&r) { (41.0 - r) / 40.0 } else { 0.0 }; } if let Some(r) = air { x[idx[1]] = if (1.0..=20.0).contains(&r) { (21.0 - r) / 20.0 } else { 0.0 }; } } // ---------------------------------------------------------------- backtest fn ll(p: f64, y: bool) -> f64 { -(if y { p } else { 1.0 - p }).clamp(1e-9, 1.0).ln() } /// ROC-AUC (Mann-Whitney): the chance a random add is ranked above a random non-add; ties count half. fn roc_auc(mut v: Vec<(f64, bool)>) -> f64 { v.sort_by(|a, b| a.0.total_cmp(&b.0)); let (mut i, mut rank_sum, n_pos) = (0, 0.0, v.iter().filter(|x| x.1).count() as f64); let n_neg = v.len() as f64 - n_pos; while i < v.len() { let mut j = i; while j < v.len() && v[j].0 == v[i].0 { j += 1; } let avg = (i + j + 1) as f64 / 2.0; // ranks i+1 ..= j rank_sum += v[i..j].iter().filter(|x| x.1).count() as f64 * avg; i = j; } if n_pos == 0.0 || n_neg == 0.0 { return f64::NAN; } (rank_sum - n_pos * (n_pos + 1.0) / 2.0) / (n_pos * n_neg) } fn average_precision(mut v: Vec<(f64, bool)>) -> f64 { v.sort_by(|a, b| b.0.total_cmp(&a.0)); let (mut hits, mut sum) = (0.0, 0.0); for (i, (_, y)) in v.iter().enumerate() { if *y { hits += 1.0; sum += hits / (i as f64 + 1.0); } } if hits > 0.0 { sum / hits } else { f64::NAN } } fn skill(model: &[f64], base: &[f64]) -> (f64, f64, f64) { let point = 1.0 - model.iter().sum::() / base.iter().sum::(); let n = model.len(); // Blocks of 8 weeks: neighbouring origins share most of an 8-week target window. let block = 8.min(n); let mut rng = Rng(7); let mut d: Vec = (0..2000).map(|_| { let (mut lm, mut lb, mut k) = (0.0, 0.0, 0); while k < n { let s = (rng.u() * (n - block + 1) as f64) as usize; for i in s..s + block { if k < n { lm += model[i]; lb += base[i]; k += 1; } } } 1.0 - lm / lb }).collect(); d.sort_by(f64::total_cmp); (point, d[100], d[1899]) } /// Scores of several models over walk-forward origins. struct Scores { names: Vec<&'static str>, wk: Vec>, // per origin, per model: summed log-loss pairs: Vec>, // per model calib: Vec<[[f64; 3]; 10]>, // per model: [sum p, hits, n] per decile n: f64, hits: f64, } impl Scores { fn new(names: Vec<&'static str>) -> Scores { let k = names.len(); Scores { names, wk: Vec::new(), pairs: vec![Vec::new(); k], calib: vec![[[0.0; 3]; 10]; k], n: 0.0, hits: 0.0 } } fn start_origin(&mut self) { self.wk.push(vec![0.0; self.names.len()]); } fn add(&mut self, preds: &[f64], y: bool) { let w = self.wk.last_mut().unwrap(); for (k, p) in preds.iter().enumerate() { let p = p.clamp(1e-6, 1.0 - 1e-6); w[k] += ll(p, y); self.pairs[k].push((p, y)); let d = ((p * 10.0) as usize).min(9); self.calib[k][d][0] += p; self.calib[k][d][1] += y as i32 as f64; self.calib[k][d][2] += 1.0; } self.n += 1.0; self.hits += y as i32 as f64; } fn col(&self, k: usize) -> Vec { self.wk.iter().map(|w| w[k]).collect() } fn logloss(&self, k: usize) -> f64 { self.col(k).iter().sum::() / self.n.max(1.0) } fn prauc(&self, k: usize) -> f64 { average_precision(self.pairs[k].clone()) } fn rocauc(&self, k: usize) -> f64 { roc_auc(self.pairs[k].clone()) } fn calib_table(&self, md: &mut String, ks: &[usize]) -> Result<()> { write!(md, "| Predicted |")?; for &k in ks { write!(md, " {}: rows / mean predicted / observed |", self.names[k])?; } writeln!(md, "\n|---|{}", ks.iter().map(|_| "---|").collect::())?; for d in 0..10 { if ks.iter().all(|&k| self.calib[k][d][2] == 0.0) { continue; } write!(md, "| {:.1}–{:.1} |", d as f64 / 10.0, (d + 1) as f64 / 10.0)?; for &k in ks { let c = self.calib[k][d]; if c[2] > 0.0 { write!(md, " {} / {:.3} / {:.3} |", c[2], c[0] / c[2], c[1] / c[2])?; } else { write!(md, " — |")?; } } writeln!(md)?; } Ok(()) } } /// Pooled out-of-sample forecasts for recalibration: (origin, raw prediction, outcome). struct Pool(Vec<(usize, f64, bool)>); impl Pool { /// Platt and isotonic fitted on forecasts whose outcome was known by `t0` (origin + gap <= t0). fn fit(&self, t0: usize, gap: usize) -> (Calib, Calib) { let pairs: Vec<(f64, bool)> = self.0.iter().filter(|(o, _, _)| o + gap <= t0).map(|(_, p, y)| (*p, *y)).collect(); (Calib::platt(&pairs), Calib::isotonic(&pairs)) } } /// The models scored in both backtests (index: meaning). const SCORED: [&str; 8] = ["rate by in-rotation-now", "rate by last-12-weeks level", "release timing only", "model, calibrated on the last 6 origins (pre-release)", "model, raw (pre-release)", "model + pooled Platt (pre-release)", "model + pooled isotonic (pre-release)", "model, NMF known (post-release)"]; fn table(md: &mut String, s_c: &Scores, s_a: &Scores, rows: &mut Vec<(String, usize, String, f64)>, h: usize, prefix: &str) -> Result<()> { writeln!(md, "| Model | Log-loss, per channel | PR-AUC, per channel | ROC-AUC, per channel | Log-loss, any channel | PR-AUC, any channel | ROC-AUC, any channel |\n|---|---|---|---|---|---|---|")?; for k in 0..SCORED.len() { let (lc, pc, la, pa) = (s_c.logloss(k), s_c.prauc(k), s_a.logloss(k), s_a.prauc(k)); let (rc, ra) = (s_c.rocauc(k), s_a.rocauc(k)); writeln!(md, "| {} | {lc:.4} | {pc:.3} | {rc:.3} | {la:.4} | {pa:.3} | {ra:.3} |", SCORED[k])?; for (m, v) in [("logloss_ch", lc), ("prauc_ch", pc), ("rocauc_ch", rc), ("logloss_any", la), ("prauc_any", pa), ("rocauc_any", ra)] { rows.push((SCORED[k].to_string(), h, format!("{prefix}{m}"), v)); } } writeln!(md, "\nSkill (1 − log-loss ÷ baseline log-loss; 90% interval from a bootstrap over blocks of 8 forecast weeks):\n")?; writeln!(md, "| Model | Per channel vs last-12-weeks | Per channel vs release timing | Any channel vs last-12-weeks | Any channel vs release timing |\n|---|---|---|---|---|")?; for k in [3usize, 5, 6] { let f = |s: &Scores, b: usize| { let x = skill(&s.col(k), &s.col(b)); format!("{:+.3} ({:+.3} to {:+.3})", x.0, x.1, x.2) }; writeln!(md, "| {} | {} | {} | {} | {} |", SCORED[k], f(s_c, 1), f(s_c, 2), f(s_a, 1), f(s_a, 2))?; rows.push((SCORED[k].to_string(), h, format!("{prefix}skill_any_vs_level"), skill(&s_a.col(k), &s_a.col(1)).0)); rows.push((SCORED[k].to_string(), h, format!("{prefix}skill_ch_vs_level"), skill(&s_c.col(k), &s_c.col(1)).0)); } writeln!(md, "\nCalibration, any channel:\n")?; s_a.calib_table(md, &[3, 5, 6])?; writeln!(md, "\nCalibration, per channel:\n")?; s_c.calib_table(md, &[3, 5, 6])?; writeln!(md)?; Ok(()) } /// Release-timing-only rates: per station (and any) by whether the artist releases in the window /// and weeks since their last release. [key][hits, n] struct Timing { ch: Vec<[f64; 2]>, any: Vec<[f64; 2]> } impl Timing { fn new() -> Timing { Timing { ch: vec![[1.0, 50.0]; C * 12], any: vec![[1.0, 20.0]; 12] } } fn key(p: &Panel, a: usize, t: usize, h: usize, win: usize) -> usize { releases_in(p, a, t, h, win) as usize * 6 + since_bin(p, a, t) } } /// Walk-forward: at each origin t0 the models see only windows that had closed by t0. pub fn backtest(p: &Panel, tau: &[u32; C], horizons: &[usize], start: usize, md: &mut String) -> Result> { let r = Rot::new(p, tau); let mut rows = Vec::new(); writeln!(md, "# Backtest: new adds\n")?; writeln!(md, "An add = the artist enters rotation (plays ≥ τ) after at least {GAP} weeks out of rotation, or their plays jump well above their own 8-week baseline and stay up; per channel, and on any DR channel. Target: an add within the {W} weeks that start h weeks after the forecast origin. Origins from week {}; each fit uses only windows closed (a week before) its origin; only artists knowable at the origin. Pre-release rows blend the window's NMF placement from the artist's history; release timing in the window is a scenario input.\n", start + 1)?; for &h in horizons { if p.w() < start + h + W + 2 { continue; } let last_origin = p.w() - 1 - h - W - 1; let (mut s_c, mut s_a) = (Scores::new(SCORED.to_vec()), Scores::new(SCORED.to_vec())); let (mut pool_c, mut pool_a) = (Pool(Vec::new()), Pool(Vec::new())); let lvl = |m12: f64| ((m12.ln_1p() / 0.5) as usize).min(7); for t0 in start..=last_origin { let m = AddModel::fit(p, &r, h, t0); // Baselines from the same closed windows. let (mut by_rot, mut by_lvl, mut any_rot, mut any_lvl) = ([[1.0f64, 2.0]; 2], [[1.0f64, 2.0]; 8], [[1.0f64, 2.0]; 2], [[1.0f64, 2.0]; 8]); let mut tm = Timing::new(); for t in origins(h, t0) { for a in 0..p.a() { if !p.known(a, t) { continue; } let key = Timing::key(p, a, t, h, W); let mut tot = 0.0; for c in 0..C { let y = r.add_in_window(a, c, t, h) as i32 as f64; let on = r.on(a, c, t) as usize; by_rot[on][0] += y; by_rot[on][1] += 1.0; let m12 = (0..LAGS).map(|i| p.y(a, c, t - i)).sum::() / LAGS as f64; let b = lvl(m12); by_lvl[b][0] += y; by_lvl[b][1] += 1.0; tm.ch[c * 12 + key][0] += y; tm.ch[c * 12 + key][1] += 1.0; tot += m12; } let y = r.add_any_in_window(a, t, h) as i32 as f64; let on = r.any(a, t) as usize; any_rot[on][0] += y; any_rot[on][1] += 1.0; let b = lvl(tot); any_lvl[b][0] += y; any_lvl[b][1] += 1.0; tm.any[key][0] += y; tm.any[key][1] += 1.0; } } let gap = h + W + 1; let ((pl_c, iso_c), (pl_a, iso_a)) = (pool_c.fit(t0, gap), pool_a.fit(t0, gap)); s_c.start_origin(); s_a.start_origin(); for a in 0..p.a() { if !p.known(a, t0) { continue; } let q = p_nmf(p, a, t0); let nmf = |b: bool| Scenario { nmf: Some(b), ..Scenario::ACTUAL }; let (pcn, pan) = m.predict(p, &r, a, t0, nmf(true), None); let (pcr, par) = m.predict(p, &r, a, t0, nmf(false), None); let (rcn, ran) = m.predict_raw(p, &r, a, t0, h, nmf(true)); let (rcr, rar) = m.predict_raw(p, &r, a, t0, h, nmf(false)); let (pcx, pax) = m.predict(p, &r, a, t0, Scenario::ACTUAL, None); let key = Timing::key(p, a, t0, h, W); let mut tot = 0.0; for c in 0..C { let y = r.add_in_window(a, c, t0, h); let on = r.on(a, c, t0) as usize; let m12 = (0..LAGS).map(|i| p.y(a, c, t0 - i)).sum::() / LAGS as f64; tot += m12; let raw = q * rcn[c] + (1.0 - q) * rcr[c]; let tmc = tm.ch[c * 12 + key]; s_c.add(&[by_rot[on][0] / by_rot[on][1], by_lvl[lvl(m12)][0] / by_lvl[lvl(m12)][1], tmc[0] / tmc[1], q * pcn[c] + (1.0 - q) * pcr[c], raw, pl_c.apply(raw), iso_c.apply(raw), pcx[c]], y); pool_c.0.push((t0, raw, y)); } let y = r.add_any_in_window(a, t0, h); let on = r.any(a, t0) as usize; let raw = q * ran + (1.0 - q) * rar; let tma = tm.any[key]; s_a.add(&[any_rot[on][0] / any_rot[on][1], any_lvl[lvl(tot)][0] / any_lvl[lvl(tot)][1], tma[0] / tma[1], q * pan + (1.0 - q) * par, raw, pl_a.apply(raw), iso_a.apply(raw), pax], y); pool_a.0.push((t0, raw, y)); } eprintln!("adds backtest h={h}: origin {} done", p.weeks[t0]); } writeln!(md, "## Window starting h = {h} weeks ahead ({} forecast origins)\n", s_c.wk.len())?; writeln!(md, "Base rates: per channel {:.2}% of artist-channels get an add in the window; on any channel {:.1}% of artists.\n", 100.0 * s_c.hits / s_c.n.max(1.0), 100.0 * s_a.hits / s_a.n.max(1.0))?; table(md, &s_c, &s_a, &mut rows, h, "add_")?; if s_c.wk.len() < 8 { writeln!(md, "Fewer than 8 forecast origins: no verdict at this horizon (doc rule).\n")?; } } let m = AddModel::fit(p, &r, 0, p.w() - 1); writeln!(md, "## Coefficients, window starting now (h = 0), all closed windows\n\n| Any-channel feature | Coefficient |\n|---|---|")?; for (n, b) in ANY_NAMES.iter().chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; } writeln!(md, "\n| Per-channel feature | Coefficient |\n|---|---|")?; for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).chain(AFF_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; } writeln!(md)?; Ok(rows) } /// Walk-forward test of the release-event model: at each origin t0 the model sees only events /// whose window had closed (a week before t0), and scores the releases of week t0+1. Pre-release /// rows blend the new song's NMF placement from the artist's history (as on the site). pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, win: usize, md: &mut String) -> Result> { let r = Rot::new(p, tau); let mut rows = Vec::new(); let (mut s_c, mut s_a) = (Scores::new(SCORED.to_vec()), Scores::new(SCORED.to_vec())); let (mut pool_c, mut pool_a) = (Pool(Vec::new()), Pool(Vec::new())); let lvl = |m: f64| ((m.ln_1p() / 0.5) as usize).min(7); let level = |a: usize, t: usize| (0..LAGS).map(|i| p.group_total(a, t - i, None)).sum::() / LAGS as f64; // Release events by quiet artists (out of rotation 26 weeks): former favourites (4+ weeks on DR 2019-24) or not. let mut favs: Vec<(f64, f64, bool)> = Vec::new(); let mut quiet_other: Vec<(f64, f64, bool)> = Vec::new(); for t0 in start..p.w().saturating_sub(win + 1) { let m = AddModel::fit_events_win(p, &r, t0, win); // Baselines from the training events. Release timing only: by weeks since the previous release. let (mut by_rot, mut by_lvl, mut cr, mut cl) = ([[1.0f64, 2.0]; 2], [[1.0f64, 2.0]; 8], [[1.0f64, 2.0]; 2], [[1.0f64, 2.0]; 8]); let mut tm = Timing::new(); for t in origins_w(0, t0, win) { for a in 0..p.a() { if !p.known(a, t) || !clean_release(p, a, t + 1) { continue; } let key = since_bin(p, a, t); let y = r.add_any_in(a, t, 0, win) as i32 as f64; let on = r.any(a, t) as usize; by_rot[on][0] += y; by_rot[on][1] += 1.0; let b = lvl(level(a, t)); by_lvl[b][0] += y; by_lvl[b][1] += 1.0; tm.any[key][0] += y; tm.any[key][1] += 1.0; for c in 0..C { let yc = r.add_in(a, c, t, 0, win) as i32 as f64; let onc = r.on(a, c, t) as usize; cr[onc][0] += yc; cr[onc][1] += 1.0; let bc = lvl((0..LAGS).map(|i| p.y(a, c, t - i)).sum::() / LAGS as f64); cl[bc][0] += yc; cl[bc][1] += 1.0; tm.ch[c * 12 + key][0] += yc; tm.ch[c * 12 + key][1] += 1.0; } } } let gap = win + 1; let ((pl_c, iso_c), (pl_a, iso_a)) = (pool_c.fit(t0, gap), pool_a.fit(t0, gap)); let mut started = false; for a in 0..p.a() { if !p.known(a, t0) || !clean_release(p, a, t0 + 1) { continue; } if !started { s_c.start_origin(); s_a.start_origin(); started = true; } let q = p_nmf(p, a, t0); let (pcn, pan) = m.predict(p, &r, a, t0, Scenario::new(true, true).window(win), None); let (pcr, par) = m.predict(p, &r, a, t0, Scenario::new(true, false).window(win), None); let (rcn, ran) = m.predict_raw(p, &r, a, t0, 0, Scenario::new(true, true).window(win)); let (rcr, rar) = m.predict_raw(p, &r, a, t0, 0, Scenario::new(true, false).window(win)); let (pcx, pax) = m.predict(p, &r, a, t0, Scenario::actual(win), None); let key = since_bin(p, a, t0); let y = r.add_any_in(a, t0, 0, win); let on = r.any(a, t0) as usize; let raw = q * ran + (1.0 - q) * rar; let tma = tm.any[key]; s_a.add(&[by_rot[on][0] / by_rot[on][1], by_lvl[lvl(level(a, t0))][0] / by_lvl[lvl(level(a, t0))][1], tma[0] / tma[1], q * pan + (1.0 - q) * par, raw, pl_a.apply(raw), iso_a.apply(raw), pax], y); pool_a.0.push((t0, raw, y)); if quiet(&r, a, t0) { if p.fav_weeks[a] >= 4.0 { favs.push((raw, iso_a.apply(raw), y)); } else { quiet_other.push((raw, iso_a.apply(raw), y)); } } for c in 0..C { let yc = r.add_in(a, c, t0, 0, win); let onc = r.on(a, c, t0) as usize; let bc = lvl((0..LAGS).map(|i| p.y(a, c, t0 - i)).sum::() / LAGS as f64); let rawc = q * rcn[c] + (1.0 - q) * rcr[c]; let tmc = tm.ch[c * 12 + key]; s_c.add(&[cr[onc][0] / cr[onc][1], cl[bc][0] / cl[bc][1], tmc[0] / tmc[1], q * pcn[c] + (1.0 - q) * pcr[c], rawc, pl_c.apply(rawc), iso_c.apply(rawc), pcx[c]], yc); pool_c.0.push((t0, rawc, yc)); } } eprintln!("event backtest: origin {} done", p.weeks[t0]); } writeln!(md, "# Backtest: if the artist releases a new song, will DR add it?\n")?; writeln!(md, "Events: an artist knowable at the time releases something (Deezer) in week t0+1, with no other release in the {CLEAN_WEEKS} weeks before (so an add can't be the previous song's); target: a DR add (entering rotation after {GAP}+ weeks out, or plays jumping well above the artist's own 8-week baseline and staying up) within {win} week(s) from the release week. The model knows the artist's state the week before; pre-release rows blend the new song's NMF Denmark placement from the artist's history. \"Release timing only\" = the add rate by weeks since the artist's previous release. {} release events scored, {} ({:.1}%) got an add on some DR channel.\n", s_a.n, s_a.hits, 100.0 * s_a.hits / s_a.n.max(1.0))?; table(md, &s_c, &s_a, &mut rows, win, "event_")?; writeln!(md, "\nQuiet artists (out of rotation everywhere for 26 weeks) releasing, any channel:\n\n| Group | Events | Adds | Rate | Mean predicted (raw) | Mean predicted (isotonic) | Log-loss (isotonic) | PR-AUC (raw) |\n|---|---|---|---|---|---|---|---|")?; for (name, v) in [("former favourites (4+ weeks on DR 2019-24)", &favs), ("other quiet artists", &quiet_other)] { let n = v.len().max(1) as f64; let hits = v.iter().filter(|x| x.2).count(); writeln!(md, "| {name} | {} | {hits} | {:.3} | {:.3} | {:.3} | {:.4} | {:.3} |", v.len(), hits as f64 / n, v.iter().map(|x| x.0).sum::() / n, v.iter().map(|x| x.1).sum::() / n, v.iter().map(|x| ll(x.1.clamp(1e-6, 1.0 - 1e-6), x.2)).sum::() / n, average_precision(v.iter().map(|x| (x.0, x.2)).collect()))?; } writeln!(md)?; let m = AddModel::fit_events_win(p, &r, p.w() - 1, win); writeln!(md, "| Any-channel feature (all events) | Coefficient |\n|---|---|")?; for (n, b) in ANY_NAMES.iter().chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; } Ok(rows) }