From 369994469296de25fe92db182b958046f4dc7d4b Mon Sep 17 00:00:00 2001 From: moritsbw-cpu Date: Sun, 15 Nov 2026 12:24:01 +0000 Subject: [PATCH] Release-week calendar: adjustable window (2-12 weeks, one release-event model each), time-since features moved to the release week, 'too soon' instead of special cases, season index (IFPI 2002-24) in the window; site: Best week tab, live ranking for any release week and window, single new-song view Co-Authored-By: Claude Opus 5.5 --- docs/BACKTEST-EVENTS.md | 73 +++++++++--------- src/model/adds.rs | 159 +++++++++++++++++++++++++--------------- src/model/panel.rs | 28 ++++++- src/model/rank.rs | 25 ++++--- src/web.rs | 102 ++++++++++++++++++++++---- src/web/index.html | 136 ++++++++++++++++++++++++++-------- 6 files changed, 369 insertions(+), 154 deletions(-) diff --git a/docs/BACKTEST-EVENTS.md b/docs/BACKTEST-EVENTS.md index 04a8090..215512a 100644 --- a/docs/BACKTEST-EVENTS.md +++ b/docs/BACKTEST-EVENTS.md @@ -6,50 +6,51 @@ Events: an artist in the panel releases something (Deezer) in week t0+1, with no |---|---|---|---|---| | rate by in-rotation-now | 0.1791 | 0.042 | 0.0572 | 0.011 | | rate by last-12-weeks level | 0.1604 | 0.116 | 0.0434 | 0.123 | -| event model | 0.1587 | 0.180 | 0.0384 | 0.286 | +| event model | 0.1584 | 0.179 | 0.0383 | 0.286 | -Skill of the event model, any channel: 0.114 (0.058 to 0.183) vs in-rotation-now, 0.011 (-0.024 to 0.059) vs last-12-weeks level. +Skill of the event model, any channel: 0.116 (0.062 to 0.184) vs in-rotation-now, 0.013 (-0.020 to 0.060) vs last-12-weeks level. | Any-channel feature (all events) | Coefficient | |---|---| -| intercept | -5.518 | -| log DR plays t | +0.493 | -| log DR plays 4w | +0.865 | -| log DR plays 12w | -0.826 | -| log max DR plays 12w | +0.444 | -| in rotation anywhere now | -1.361 | -| station groups in rotation now (/5) | -0.501 | -| current run anywhere (/12) | -1.509 | -| weeks since in rotation anywhere (/12) | +0.122 | -| Track Top-40 score | -0.479 | -| Airplay Top-20 score | -0.431 | -| chart entry recency 6w | +0.327 | -| chart entry recency 26w | -0.115 | -| log Track Top-40 hits | -0.306 | -| log Airplay Top-20 hits | -0.103 | -| Danish | +0.754 | -| group | -0.006 | +| intercept | -5.514 | +| log DR plays t | +0.487 | +| log DR plays 4w | +0.863 | +| log DR plays 12w | -0.819 | +| log max DR plays 12w | +0.447 | +| in rotation anywhere now | -1.356 | +| station groups in rotation now (/5) | -0.502 | +| current run anywhere (/12) | -1.525 | +| weeks since in rotation anywhere (/12) | +0.129 | +| Track Top-40 score | -0.489 | +| Airplay Top-20 score | -0.429 | +| chart entry recency 6w | +0.330 | +| chart entry recency 26w | -0.124 | +| log Track Top-40 hits | -0.307 | +| log Airplay Top-20 hits | -0.100 | +| Danish | +0.758 | +| group | -0.007 | | female | +0.124 | -| career years (/50) | +0.532 | -| debut unknown | +0.601 | +| career years (/50) | +0.528 | +| debut unknown | +0.596 | | Christmas in window | +0.000 | | discography known | -0.000 | -| release recency 4w | -0.131 | -| release recency 12w | -0.869 | -| log releases last 52w | -0.172 | +| release recency 4w | -0.124 | +| release recency 12w | -0.866 | +| log releases last 52w | -0.175 | | releases in the window | -0.000 | -| releases in window x not in rotation now | +1.361 | -| DR: weeks since last add 13-25 | +0.133 | -| DR: last add 26+ weeks ago | +0.694 | -| DR: no add in the panel yet | -0.625 | -| Airplay Top-20: last new song 13-25 weeks ago | +0.397 | -| Airplay Top-20: 0.5-1.25x own usual gap | -0.500 | +| releases in window x not in rotation now | +1.356 | +| DR: weeks since last add 13-25 | +0.144 | +| DR: last add 26+ weeks ago | +0.659 | +| DR: no add in the panel yet | -0.622 | +| Airplay Top-20: last new song 13-25 weeks ago | +0.399 | +| Airplay Top-20: 0.5-1.25x own usual gap | -0.497 | | Airplay Top-20: last new song 2+ years ago | -0.069 | | log archive plays per 100 sampled hours (2012-21) | +0.316 | -| log DR lists with the artist before t (2023-) | +0.435 | -| DR list recency 26w | +0.032 | +| log DR lists with the artist before t (2023-) | +0.431 | +| DR list recency 26w | +0.049 | | DR lists known before t at all | -0.016 | -| NMF Denmark in the window | +0.789 | -| NMF Denmark top 10 in the window | +0.775 | -| log NMF Denmark placements last 52w | -0.068 | -| share of releases on NMF Denmark (2 years) | -0.585 | +| NMF Denmark in the window | +0.774 | +| NMF Denmark top 10 in the window | +0.784 | +| log NMF Denmark placements last 52w | -0.067 | +| share of releases on NMF Denmark (2 years) | -0.590 | +| season index of the window (log, IFPI 2002-24) | +0.452 | diff --git a/src/model/adds.rs b/src/model/adds.rs index 714575b..ae47757 100644 --- a/src/model/adds.rs +++ b/src/model/adds.rs @@ -40,12 +40,12 @@ pub const WAVE_NAMES: &[&str] = &["DR: weeks since last add 13-25", "DR: last ad /// 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) -> [f64; WAVE] { +pub fn wave_feats(p: &Panel, r: &Rot, a: usize, t: usize, shift: usize) -> [f64; WAVE] { if off("wave") { return [0.0; WAVE]; } - let since = (0..=t).find(|&i| r.add_any(a, t - i)); + let since = (0..=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; + 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()), @@ -54,11 +54,17 @@ pub fn wave_feats(p: &Panel, r: &Rot, a: usize, t: usize) -> [f64; WAVE] { /// What the forecast assumes about the window: None = what actually happened (training and /// backtests), Some = a scenario (the ranking, the what-if). -#[derive(Clone, Copy, Default)] -pub struct Scenario { pub release: Option, pub nmf: Option } +/// `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 }; - pub fn new(release: bool, nmf: bool) -> Scenario { Scenario { release: Some(release), nmf: Some(nmf) } } + 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 @@ -67,9 +73,18 @@ 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) -> (chrono::NaiveDate, chrono::NaiveDate) { +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(W as i64)) + (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)? @@ -86,11 +101,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)) } -/// Did the artist release something in the CLEAN_WEEKS weeks up to and including week t? -pub fn recent_release(p: &Panel, a: usize, t: usize) -> bool { - ((t + 1).saturating_sub(CLEAN_WEEKS)..=t).any(|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); @@ -102,7 +112,8 @@ pub fn p_nmf(p: &Panel, a: usize, t: usize) -> f64 { 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 (w0, w1) = window(p, t, h); + 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))) } @@ -112,7 +123,7 @@ pub fn nmf_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario) -> [f64; }; 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, _)| (end_t - *d).num_days() <= 364).count() 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; [f(in_win), if sc.nmf == Some(true) { top10_share } else { f(top10) }, last_year.ln_1p(), p_nmf(p, a, t)] } @@ -122,15 +133,26 @@ pub const LONG: usize = 4; 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"]; -pub fn long_feats(p: &Panel, a: usize, t: usize) -> [f64; LONG] { +pub fn long_feats(p: &Panel, a: usize, t: usize, shift: usize) -> [f64; LONG] { if off("long") { return [0.0; LONG]; } 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); + 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] } +/// 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] +} + /// 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", @@ -144,10 +166,10 @@ pub fn rel_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario, in_rot_n 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 since = dates.iter().rev().find(|d| **d <= end_t).map(|d| (end_t - *d).num_days() as f64 / 7.0); - let last_year = dates.iter().filter(|d| **d <= end_t && (end_t - **d).num_days() <= 364).count() as f64; - let w0 = p.weeks[t] + chrono::Duration::weeks(h as i64 + 1); - let w1 = w0 + chrono::Duration::weeks(W as i64); + 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(), @@ -168,7 +190,7 @@ pub const EXTRA: &[&str] = &["in rotation on channel now", "in rotation anywhere "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; +pub const PC: usize = P + 10 + REL + WAVE + LONG + NMF + SEASON; /// Artist-level ("any channel") rows. pub const ANY_NAMES: &[&str] = &[ @@ -178,7 +200,7 @@ pub const ANY_NAMES: &[&str] = &[ "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; +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]; @@ -207,8 +229,10 @@ impl Rot { pub fn add(&self, a: usize, c: usize, w: usize) -> bool { w >= GAP && self.on(a, c, w) && (1..=GAP).all(|k| !self.on(a, c, w - k)) } pub fn add_any(&self, a: usize, w: usize) -> bool { w >= GAP && self.any(a, w) && (1..=GAP).all(|k| !self.any(a, w - k)) } /// 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 { (t + h + 1..=t + h + W).any(|w| self.add(a, c, w)) } - pub fn add_any_in_window(&self, a: usize, t: usize, h: usize) -> bool { (t + h + 1..=t + h + W).any(|w| self.add_any(a, w)) } + 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. @@ -226,11 +250,11 @@ fn run_and_since(flag: impl Fn(usize) -> bool, t: usize) -> (f64, f64) { (run, since) } -fn christmas_in_window(p: &Panel, t: usize, h: usize) -> bool { (t + h + 1..=t + h + W).any(|w| features::christmas(p, w)) } +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) as i32 as f64; + 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; @@ -241,9 +265,10 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, s 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)); - out[P + 10 + REL + WAVE..P + 10 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, a, t)); - out[P + 10 + REL + WAVE + LONG..PC].copy_from_slice(&nmf_feats(p, a, t, h, sc)); + 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, a, t, sc.shift)); + out[P + 10 + REL + WAVE + LONG..PC - SEASON].copy_from_slice(&nmf_feats(p, a, t, h, sc)); + out[PC - SEASON..PC].copy_from_slice(&season_feat(p, t, h, sc.win)); } pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, out: &mut [f64]) { @@ -264,18 +289,21 @@ pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, o 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) 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)); - out[21 + REL + WAVE..21 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, a, t)); - out[21 + REL + WAVE + LONG..PA].copy_from_slice(&nmf_feats(p, a, t, h, sc)); + 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, a, t, sc.shift)); + 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 @@ -290,8 +318,9 @@ 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 { - let hi = last_week.saturating_sub(h + W); +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 { + let hi = last_week.saturating_sub(h + win); LAGS - 1..=hi } @@ -299,32 +328,34 @@ 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) } + 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). - pub fn fit_events(p: &Panel, r: &Rot, last_week: usize) -> AddModel { Self::fit_with(p, r, 0, last_week, true) } + pub fn fit_events(p: &Panel, r: &Rot, last_week: usize) -> AddModel { Self::fit_with(p, r, 0, last_week, true, 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_with(p, r, 0, last_week, true, win) } - fn fit_with(p: &Panel, r: &Rot, h: usize, last_week: usize, events: bool) -> AddModel { - let mut m = Self::fit_raw(p, r, h, last_week, events); - let hi = last_week.saturating_sub(h + W); + 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); 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 + W + 4 { - let inner = Self::fit_raw(p, r, h, c0, events); + 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 events && !clean_release(p, a, t + 1) { continue; } - let (pc, pa) = inner.predict(p, r, a, t, Scenario::ACTUAL, None); + 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_window(a, c, t, h) { continue; } // certain zeros carry no information - dc.push(&[1.0, logit(pc[c])], r.add_in_window(a, c, t, h) as i32 as f64, 1.0); + 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_window(a, t, h) 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)); @@ -336,38 +367,46 @@ impl AddModel { m } - fn fit_raw(p: &Panel, r: &Rot, h: usize, last_week: usize, events: bool) -> AddModel { + 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]); - for t in origins(h, last_week) { + let sc = Scenario::actual(win); + for t in origins_w(h, last_week, win) { for a in 0..p.a() { 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, Scenario::ACTUAL, &mut x); - dc.push(&x, r.add_in_window(a, c, t, h) as i32 as f64, wgt); + 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, Scenario::ACTUAL, &mut xa); - da.push(&xa, r.add_any_in_window(a, t, h) as i32 as f64, 1.0); + 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, channel: glm::logit(&dc, 1.0), any: glm::logit(&da, 1.0), platt_channel: (0.0, 1.0), platt_any: (0.0, 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) } } /// (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) { + 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, self.h, sc, &mut x); + row_channel(p, r, a, c, t, h, sc, &mut x); if let Some(f) = chart { f(&mut x, CHART_IDX_CHANNEL); } pc[c] = platt(self.platt_channel, glm::sigmoid(glm::dot(&self.channel, &x))); } let mut xa = vec![0.0; PA]; - row_any(p, r, a, t, self.h, sc, &mut xa); + 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. @@ -510,9 +549,9 @@ pub fn backtest(p: &Panel, tau: &[u32; C], horizons: &[usize], start: usize, md: 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().zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; } - for (n, b) in REL_NAMES.iter().chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).zip(&m.any[21..]) { writeln!(md, "| {n} | {b:+.3} |")?; } + for (n, b) in REL_NAMES.iter().chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any[21..]) { 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).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; } + for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; } writeln!(md)?; Ok(rows) } @@ -596,6 +635,6 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, md: &mut String) writeln!(md, "\nSkill of the event model, any channel: {:.3} ({:.3} to {:.3}) vs in-rotation-now, {:.3} ({:.3} to {:.3}) vs last-12-weeks level.\n", s1.0, s1.1, s1.2, s2.0, s2.1, s2.2)?; let m = AddModel::fit_events(p, &r, p.w() - 1); 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).zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; } + 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) } diff --git a/src/model/panel.rs b/src/model/panel.rs index e157327..f627c52 100644 --- a/src/model/panel.rs +++ b/src/model/panel.rs @@ -81,6 +81,9 @@ pub struct Panel { pub listed_on: Vec>, /// New Music Friday Denmark placements per artist: (Friday, position if known), sorted. pub nmf_dk: Vec)>>, + /// Season index per ISO week (1..=53): new songs reaching the Airplay Top-20 that week of the + /// year, relative to an average week, 2002–2024 (before the DR panel), smoothed over 5 weeks. + pub season: [f64; 54], } impl Panel { @@ -147,7 +150,8 @@ impl Panel { let releases = load_releases(db, &id_of, artists.len())?; let (archive_rate, listed_on) = load_long_history(db, &id_of, &names, artists.len())?; let nmf_dk = load_nmf_dk(db, &id_of, &artists)?; - Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk }) + let season = load_season(db)?; + Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season }) } } @@ -310,3 +314,25 @@ fn load_nmf_dk(db: &Db, id_of: &HashMap, artists: &[Artist]) -> R for v in out.iter_mut() { v.sort(); v.dedup_by_key(|x| x.0); } Ok(out) } + +fn load_season(db: &Db) -> Result<[f64; 54]> { + let mut first: HashMap = HashMap::new(); + for r in db.conn.prepare("SELECT year, week, artist, title FROM hitlisten_charts WHERE chart = 'airplay' AND year BETWEEN 2002 AND 2024")? + .query_map([], |r| Ok((r.get::<_, i32>(0)?, r.get::<_, u32>(1)?, r.get::<_, String>(2)?, r.get::<_, String>(3)?)))? { + let (y, w, a, t) = r?; + let k = format!("{}|{}", key(&a), key(t.split(" (").next().unwrap_or(&t))); + let e = first.entry(k).or_insert((y, w)); + if (y, w) < *e { *e = (y, w); } + } + let mut n = [0.0f64; 54]; + for (_, w) in first.values() { if (1..=53).contains(w) { n[*w as usize] += 1.0; } } + n[53] = n[52]; // week 53 is rare; treat like 52 + let mut out = [1.0f64; 54]; + let mean = n[1..=52].iter().sum::() / 52.0; + if mean == 0.0 { return Ok(out); } + for w in 1..=53usize { + let s: f64 = (-2i32..=2).map(|d| { let k = ((w as i32 - 1 + d).rem_euclid(52) + 1) as usize; n[k] }).sum::() / 5.0; + out[w] = (s / mean).max(0.2); + } + Ok(out) +} diff --git a/src/model/rank.rs b/src/model/rank.rs index e8a1a02..956f106 100644 --- a/src/model/rank.rs +++ b/src/model/rank.rs @@ -118,12 +118,15 @@ pub fn run(db: &Db, p: &Panel, max_h: usize, show_h: usize) -> Result> // window start: the artist's state now, the new song's NMF placement blended from history. let ev = AddModel::fit_events(p, &rot, p.w() - 1); let t0 = p.w() - 1; - let event: Vec<([f64; C], f64, f64, f64, bool)> = (0..p.a()).map(|a| { + // A new song released in week t0+h+1: time-since features moved forward to then; none if too + // soon after the artist's last release. + let event = |a: usize, h: usize| -> Option<([f64; C], f64, f64, f64)> { + if super::adds::too_soon(p, a, t0, h) { return None; } let q = super::adds::p_nmf(p, a, t0); - let (pcn, pan) = ev.predict(p, &rot, a, t0, Scenario::new(true, true), None); - let (pcr, par) = ev.predict(p, &rot, a, t0, Scenario::new(true, false), None); - (std::array::from_fn(|c| q * pcn[c] + (1.0 - q) * pcr[c]), q * pan + (1.0 - q) * par, pan, q, super::adds::recent_release(p, a, t0)) - }).collect(); + let (pcn, pan) = ev.predict_at(p, &rot, a, t0, h, Scenario::new(true, true).at(h), None); + let (pcr, par) = ev.predict_at(p, &rot, a, t0, h, Scenario::new(true, false).at(h), None); + Some((std::array::from_fn(|c| q * pcn[c] + (1.0 - q) * pcr[c]), q * pan + (1.0 - q) * par, pan, q)) + }; for h in 0..=max_h { let m = AddModel::fit(p, &rot, h, p.w() - 1); let start = (origin + Duration::weeks(h as i64 + 1)).to_string(); @@ -137,19 +140,19 @@ pub fn run(db: &Db, p: &Panel, max_h: usize, show_h: usize) -> Result> let (pc0, pa0) = m.predict(p, &rot, a, t0, Scenario::new(false, false), None); let pr = super::adds::p_release(p, a, t0); let mix = |p1: f64, p0: f64| match pr { Some(r) => r * p1 + (1.0 - r) * p0, None => p0 }; - let (ec, ea, ean, q, recent) = event[a]; + let ev_a = event(a, h); let none = Option::::None; let mut put = |target: &str, ch: &str, prob: f64| ins.execute(params![made, VERSION, target, format!("{id}:{ch}"), h as i64, origin.to_string(), start, prob, none, none, none]); put("add_any", "any", mix(pa1, pa0))?; - put("add_any_rel", "any", ea)?; - put("add_any_nmf", "any", ean)?; - put("p_nmf", "any", q)?; - if recent { put("recent_release", "any", 1.0)?; } + match ev_a { + Some((_, ea, ean, q)) => { put("add_any_rel", "any", ea)?; put("add_any_nmf", "any", ean)?; put("p_nmf", "any", q)?; } + None => { put("too_soon", "any", 1.0)?; } + } if let Some(r) = pr { put("p_release", "any", r)?; } for c in 0..C { put("add", CHANNELS[c], mix(pc1[c], pc0[c]))?; - put("add_rel", CHANNELS[c], ec[c])?; + if let Some((ec, ..)) = ev_a { put("add_rel", CHANNELS[c], ec[c])?; } } } } diff --git a/src/web.rs b/src/web.rs index 9d47f76..1e97408 100644 --- a/src/web.rs +++ b/src/web.rs @@ -25,6 +25,26 @@ use tiny_http::{Header, Method, Response, Server}; const PAGE: &str = include_str!("web/index.html"); const MAX_H: usize = 12; +/// Window lengths offered (weeks after the release within which DR must add the song). +const WINDOWS: [usize; 6] = [2, 4, 6, 8, 10, 12]; +/// Release weeks shown in the best-week calendar. +const CALENDAR: usize = 26; + +/// "If the artist releases a new song in week t0+h+1, will DR add it within `win` weeks?": +/// (per channel, any station, any station if placed on NMF Denmark, P(NMF)), blended over the +/// new song's NMF placement by how often the artist's releases were placed. None = too soon after +/// the artist's last release (the model only learned from releases with a 6-week gap). +fn new_song(m: &Models, a: usize, h: usize, win: usize, nmf: Option) -> Option<([f64; C], f64, f64, f64)> { + use crate::model::adds::{p_nmf, too_soon, Scenario}; + let p = &m.panel; + let t0 = p.w() - 1; + if too_soon(p, a, t0, h) { return None; } + let ev = m.events_w.get(&win).unwrap_or(&m.events); + let q = match nmf { Some(true) => 1.0, Some(false) => 0.0, None => p_nmf(p, a, t0) }; + let (pcn, pan) = ev.predict_at(p, &m.rot, a, t0, h, Scenario::new(true, true).at(h), None); + let (pcr, par) = ev.predict_at(p, &m.rot, a, t0, h, Scenario::new(true, false).at(h), None); + Some((std::array::from_fn(|c| q * pcn[c] + (1.0 - q) * pcr[c]), q * pan + (1.0 - q) * par, pan, q)) +} struct Models { made_at: String, @@ -33,6 +53,8 @@ struct Models { direct: Vec, // index h - 1 adds: Vec, // index h events: AddModel, + /// The release-event model per window length (weeks the add must come within). + events_w: HashMap, rot: Rot, index: HashMap, } @@ -41,7 +63,7 @@ struct State { db_path: String, prefix: String, models: RwLock>>, - rank_cache: RwLock>>, + rank_cache: RwLock>>, } fn ro(path: &str) -> Result { @@ -108,8 +130,9 @@ fn fit_models(db_path: &str, made_at: String) -> Result { let rot = Rot::new(&panel, &tau); let adds = (0..=MAX_H).map(|h| AddModel::fit(&panel, &rot, h, panel.w() - 1)).collect(); let events = AddModel::fit_events(&panel, &rot, panel.w() - 1); + let events_w = WINDOWS.iter().map(|&w| (w, AddModel::fit_events_w(&panel, &rot, panel.w() - 1, w))).collect(); let index = panel.artists.iter().enumerate().map(|(i, a)| (a.id.clone(), i)).collect(); - Ok(Models { made_at, panel, tau, direct, adds, events, rot, index }) + Ok(Models { made_at, panel, tau, direct, adds, events, events_w, rot, index }) } fn query(url: &str) -> HashMap { @@ -150,7 +173,9 @@ fn handle(st: &State, method: &Method, url: &str) -> Result { "" => Ok((301, "text/plain", Arc::new(String::new()))), "/" | "/index.html" => Ok((200, "text/html; charset=utf-8", Arc::new(PAGE.to_string()))), "/api/meta" => meta(st), - "/api/rank" => rank(st, q.get("h").and_then(|h| h.parse().ok()).unwrap_or(0).min(MAX_H)), + "/api/rank" => rank(st, q.get("h").and_then(|h| h.parse().ok()).unwrap_or(0).min(CALENDAR - 1), + q.get("w").and_then(|w| w.parse().ok()).filter(|w| WINDOWS.contains(w)).unwrap_or(8)), + "/api/bestweek" => bestweek(st, &q), "/api/artist" => artist(st, q.get("id").map(String::as_str).unwrap_or("")), "/api/whatif" => whatif(st, &q), _ => Ok((404, "text/plain", Arc::new("not found".into()))), @@ -191,7 +216,7 @@ fn fold(e: &mut Value, target: &str, ch: &str, prob: Option, mean: Option { e["add_any_rel"] = r3(prob); } "add_any_nmf" => { e["add_any_nmf"] = r3(prob); } "p_nmf" => { e["p_nmf"] = r3(prob); } - "recent_release" => { e["recent_release"] = json!(true); } + "too_soon" => { e["too_soon"] = json!(true); } "add_rel" => { if !e["add_rel"].is_object() { e["add_rel"] = json!({}); } e["add_rel"][ch] = r3(prob); } "p_release" => { e["p_release"] = r3(prob); } "now" => { e["now"][ch] = json!({"on": prob.unwrap_or(0.0) > 0.5, "plays": mean}); } @@ -227,10 +252,13 @@ fn bauer_week(c: &Connection) -> Result Result { +fn rank(st: &State, h: usize, win: usize) -> Result { let c = ro(&st.db_path)?; let Some(made) = latest_run(&c)? else { return ok(json!({"rows": []})) }; - if let Some(hit) = st.rank_cache.read().unwrap().get(&(made.clone(), h)) { return Ok((200, "application/json", hit.clone())); } + let live = st.models.read().unwrap().clone(); + let key = (made.clone(), h, win, live.is_some()); + if let Some(hit) = st.rank_cache.read().unwrap().get(&key) { return Ok((200, "application/json", hit.clone())); } + let h_db = h.min(MAX_H); let mut names: HashMap = HashMap::new(); for r in c.prepare("SELECT artist_id, name, json_extract(raw, '$.artist.country'), json_extract(raw, '$.artist.type') FROM dr_artists")? .query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option>(2)?, r.get::<_, Option>(3)?)))? { @@ -239,8 +267,8 @@ fn rank(st: &State, h: usize) -> Result { } let mut rows: HashMap = HashMap::new(); let mut st_ = c.prepare("SELECT key, target, prob, mean, lo, hi, target_week FROM forecasts WHERE made_at = ?1 AND model_version = ?2 - AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release', 'recent_release') AND horizon = ?4) OR target = 'now')")?; - let it = st_.query_map(params![made, VERSION, h.max(1) as i64, h as i64], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option>(2)?, + AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release', 'too_soon') AND horizon = ?4) OR target = 'now')")?; + let it = st_.query_map(params![made, VERSION, h_db.max(1) as i64, h_db as i64], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option>(2)?, r.get::<_, Option>(3)?, r.get::<_, Option>(4)?, r.get::<_, Option>(5)?, r.get::<_, String>(6)?)))?; let mut window = String::new(); for r in it { @@ -258,15 +286,61 @@ fn rank(st: &State, h: usize) -> Result { let k = crate::model::panel::key(e["name"].as_str().unwrap_or("")); if let Some(b) = bauer.get(&k) { e["bauer"] = json!(b.iter().map(|(s, v)| (s.clone(), v.0)).collect::>()); } } + // The new-song numbers: live from the models for any release week and window, else the stored ones (8 weeks). + let mut window = window; + if let Some(m) = live.as_ref() { + window = (m.panel.weeks[m.panel.w() - 1] + chrono::Duration::weeks(h as i64 + 1)).to_string(); + for a in 0..m.panel.a() { + let Some(e) = rows.get_mut(&m.panel.artists[a].id) else { continue }; + match new_song(m, a, h, win, None) { + None => { e["too_soon"] = json!(true); e["add_any_rel"] = Value::Null; e["add_rel"] = json!({}); } + Some((pc, pa, pan, q)) => { + let r3 = |v: f64| (v * 1000.0).round() / 1000.0; + e["too_soon"] = json!(false); + e["add_any_rel"] = json!(r3(pa)); + e["add_any_nmf"] = json!(r3(pan)); + e["p_nmf"] = json!(r3(q)); + e["add_rel"] = json!(CHANNELS.iter().zip(pc).map(|(c, v)| (c.to_string(), json!(r3(v)))).collect::>()); + } + } + } + } let mut out: Vec = rows.into_values().collect(); - out.sort_by(|a, b| b["add_any"].as_f64().unwrap_or(0.0).total_cmp(&a["add_any"].as_f64().unwrap_or(0.0))); - let body = Arc::new(json!({"made_at": made, "h": h, "window_start": window, "rows": out}).to_string()); + out.sort_by(|a, b| b["add_any_rel"].as_f64().unwrap_or(-1.0).total_cmp(&a["add_any_rel"].as_f64().unwrap_or(-1.0))); + let body = Arc::new(json!({"made_at": made, "h": h, "w": win, "live": live.is_some(), "window_start": window, "rows": out}).to_string()); let mut cache = st.rank_cache.write().unwrap(); cache.retain(|k, _| k.0 == made); - cache.insert((made, h), body.clone()); + cache.insert(key, body.clone()); Ok((200, "application/json", body)) } +/// The best week for an artist to release a new song: the next CALENDAR release weeks with the +/// chance DR adds it within `w` weeks (any station and per station group), like a fare calendar. +fn bestweek(st: &State, q: &HashMap) -> Result { + let guard = st.models.read().unwrap(); + let Some(m) = guard.as_ref() else { return Ok((503, "application/json", Arc::new(json!({"loading": true}).to_string()))) }; + let Some(&a) = m.index.get(q.get("id").map(String::as_str).unwrap_or("")) else { + return Ok((404, "application/json", Arc::new(json!({"error": "artist not in the model"}).to_string()))); + }; + let win = q.get("w").and_then(|w| w.parse().ok()).filter(|w| WINDOWS.contains(w)).unwrap_or(8); + let nmf = match q.get("nmf").map(String::as_str) { Some("yes") => Some(true), Some("no") => Some(false), _ => None }; + let p = &m.panel; + let t0 = p.w() - 1; + let groups = [("P3", vec![0usize]), ("P4", (0..C).filter(|&c| crate::model::panel::is_p4(c)).collect()), ("P5", vec![11]), ("P6", vec![12]), ("P8", vec![13])]; + let weeks: Vec = (0..CALENDAR).map(|h| { + let start = p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1); + let xmas = crate::model::features::christmas(p, t0 + h + 1); + match new_song(m, a, h, win, nmf) { + None => json!({"h": h, "week": start.to_string(), "too_soon": true, "christmas": xmas}), + Some((pc, pa, _, _)) => json!({"h": h, "week": start.to_string(), "too_soon": false, "christmas": xmas, "p": pa, + "groups": groups.iter().map(|(n, cs)| (n.to_string(), json!(cs.iter().map(|&c| pc[c]).fold(0.0, f64::max)))).collect::>()}), + } + }).collect(); + let last_release = p.releases[a].as_ref().and_then(|d| d.iter().rev().find(|d| **d <= p.weeks[t0] + chrono::Duration::days(6)).map(|d| d.to_string())); + ok(json!({"id": p.artists[a].id, "name": p.artists[a].name, "w": win, "nmf": q.get("nmf"), "p_nmf": crate::model::adds::p_nmf(p, a, t0), + "last_release": last_release, "weeks": weeks})) +} + fn artist(st: &State, id: &str) -> Result { let c = ro(&st.db_path)?; let Some((name, raw)) = c.query_row("SELECT name, raw FROM dr_artists WHERE artist_id = ?1", [id], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))).optional()? @@ -304,7 +378,7 @@ fn artist(st: &State, id: &str) -> Result { let ch = key.split_once(':').map(|x| x.1).unwrap_or(""); if target == "now" { fold(&mut now, "now", ch, prob, mean, lo, hi); continue; } let e = byh.entry(h).or_insert_with(|| json!({"h": h, "p": {}, "plays": {}, "add": {}})); - if target.starts_with("add") || target.starts_with("p_") || target == "recent_release" { e["window_start"] = json!(week); } else { e["week"] = json!(week); } + if target.starts_with("add") || target.starts_with("p_") || target == "too_soon" { e["window_start"] = json!(week); } else { e["week"] = json!(week); } fold(e, &target, ch, prob, mean, lo, hi); } now_out = now["now"].clone(); @@ -369,7 +443,7 @@ fn whatif(st: &State, q: &HashMap) -> Result { let run = |overrides: bool| -> Value { let f: Option<&dyn Fn(&mut [f64], [usize; 4])> = if overrides { Some(&over) } else { None }; // A new release: the release-event model; no release: the window model (current songs only). - let (add_c, add_any) = if release { m.events.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(true, nmf), f) } + let (add_c, add_any) = if release { m.events.predict_at(p, &m.rot, a, t0, h, crate::model::adds::Scenario::new(true, nmf).at(h), f) } else { am.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(false, false), f) }; let mut x = vec![0.0; P]; let mut mu = [0.0; C]; @@ -392,7 +466,7 @@ fn whatif(st: &State, q: &HashMap) -> Result { }; let now: serde_json::Map = (0..C).filter(|&c| m.rot.on(a, c, t0)).map(|c| (CHANNELS[c].to_string(), json!(p.y(a, c, t0)))).collect(); ok(json!({ - "h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0), "recent_release": crate::model::adds::recent_release(p, a, t0), + "h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0), "too_soon": crate::model::adds::too_soon(p, a, t0, h), "nmf_history": p.nmf_dk[a].iter().rev().take(8).map(|(d, pos)| json!({"friday": d.to_string(), "position": pos})).collect::>(), "window_start": (p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1)).to_string(), "current": {"t40": chart.t40_rank, "air": chart.air_rank, diff --git a/src/web/index.html b/src/web/index.html index fc679ec..9ae8758 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -78,6 +78,21 @@ svg text { fill: var(--muted); font-size: 11px; } .tracks { columns: 2; font-size: 14px; } .tracks div { break-inside: avoid; padding: 2px 0; } .chips { display: flex; flex-wrap: wrap; gap: 6px; } .chip { background: var(--bg); border-radius: 999px; padding: 3px 10px; font-size: 13px; } +/* best week calendar */ +.cal { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; margin-top: 16px; } +.day { border-radius: 10px; padding: 10px; border: 1px solid var(--line); background: var(--panel); position: relative; min-height: 78px; } +.day .d { font-size: 12px; color: var(--muted); } +.day .v { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; } +.day.soon { opacity: .55; background: repeating-linear-gradient(45deg, var(--bg), var(--bg) 6px, var(--panel) 6px, var(--panel) 12px); } +.day.soon .v { font-size: 13px; font-weight: 500; color: var(--muted); } +.day.best { outline: 3px solid var(--good); outline-offset: -1px; } +.day .tag { position: absolute; top: 8px; right: 8px; font-size: 11px; color: var(--muted); } +.bw-head { display: flex; flex-wrap: wrap; gap: 16px; align-items: baseline; margin-top: 18px; } +.bw-head h2 { margin: 0; } +.lvl0 { background: color-mix(in srgb, var(--good) 28%, var(--panel)); } +.lvl1 { background: color-mix(in srgb, var(--good) 14%, var(--panel)); } +.lvl2 { background: var(--panel); } +.lvl3 { background: color-mix(in srgb, var(--bad) 10%, var(--panel)); } /* model tab */ .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; } .card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 18px; box-shadow: var(--shadow); } @@ -101,13 +116,17 @@ svg text { fill: var(--muted); font-size: 11px; }
Loading…
-
8-week window starting —
+
Release week —
+
Added within + +
Station
-
Assume - -
Sort by
Artist
-
-
+
@@ -146,7 +158,23 @@ svg text { fill: var(--muted); font-size: 11px; }
-

"In rotation now" lists DR stations where the artist is on the list, then Bauer stations (grey) with 14+ plays in the last 7 days. "They release a new song" = the chance DR adds a new song within 8 weeks of its release, learned from past releases with no other release in the 6 weeks before (so a current song's success isn't counted as the new one's); the new song's NMF Denmark placement is weighed by how often the artist's releases were placed. "Any add" = any new add in the window, current songs included. New add = the chance that the artist gets a song into rotation within the 8-week window: entering rotation (at or above the station's weekly play threshold) after at least 4 weeks out of rotation. For "any DR station", out of rotation everywhere first. An artist with a song in rotation now usually has to drop out before a new add, which is why current hits rank lower here. "In rotation now" shows where the artist is in rotation in the latest week; plays per week are for the window's first week. Click an artist for history and a what-if.

+

New add = if the artist releases a new song in the chosen week, the chance DR puts it into rotation (at or above the station's weekly play threshold, after at least 4 weeks out) within the chosen number of weeks. Learned from past releases with no other release in the 6 weeks before, so a current song's success isn't counted as a new one's; a release less than 6 weeks after the artist's last one gets no estimate ("too soon"). The new song's NMF Denmark placement is weighed by how often the artist's releases were placed. "In rotation now" lists DR stations where the artist is on the list now, then Bauer stations (grey) with 14+ plays in the last 7 days. Click an artist for history and a what-if, or open Best week for their release calendar.

+
+ +