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 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 12:24:01 +00:00
co-authored by Claude Opus 5.5
parent 347680bc9e
commit 3699944692
6 changed files with 369 additions and 154 deletions
+37 -36
View File
@@ -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 |
+99 -60
View File
@@ -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<bool>, pub nmf: Option<bool> }
/// `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<bool>, pub nmf: Option<bool>, 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<i64>)> = 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::<f64>() / 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<f64>,
pub any: Vec<f64>,
/// 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<usize> {
let hi = last_week.saturating_sub(h + W);
fn origins(h: usize, last_week: usize) -> std::ops::RangeInclusive<usize> { origins_w(h, last_week, W) }
fn origins_w(h: usize, last_week: usize, win: usize) -> std::ops::RangeInclusive<usize> {
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)
}
+27 -1
View File
@@ -81,6 +81,9 @@ pub struct Panel {
pub listed_on: Vec<Vec<NaiveDate>>,
/// New Music Friday Denmark placements per artist: (Friday, position if known), sorted.
pub nmf_dk: Vec<Vec<(NaiveDate, Option<i64>)>>,
/// 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<String, usize>, 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<String, (i32, u32)> = 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::<f64>() / 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::<f64>() / 5.0;
out[w] = (s / mean).max(0.2);
}
Ok(out)
}
+14 -11
View File
@@ -118,12 +118,15 @@ pub fn run(db: &Db, p: &Panel, max_h: usize, show_h: usize) -> Result<Vec<Line>>
// 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<Vec<Line>>
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::<f64>::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])?; }
}
}
}
+88 -14
View File
@@ -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<bool>) -> 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<Direct>, // index h - 1
adds: Vec<AddModel>, // index h
events: AddModel,
/// The release-event model per window length (weeks the add must come within).
events_w: HashMap<usize, AddModel>,
rot: Rot,
index: HashMap<String, usize>,
}
@@ -41,7 +63,7 @@ struct State {
db_path: String,
prefix: String,
models: RwLock<Option<Arc<Models>>>,
rank_cache: RwLock<HashMap<(String, usize), Arc<String>>>,
rank_cache: RwLock<HashMap<(String, usize, usize, bool), Arc<String>>>,
}
fn ro(path: &str) -> Result<Connection> {
@@ -108,8 +130,9 @@ fn fit_models(db_path: &str, made_at: String) -> Result<Models> {
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<String, String> {
@@ -150,7 +173,9 @@ fn handle(st: &State, method: &Method, url: &str) -> Result<Reply> {
"" => 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<f64>, mean: Option<f
"add_any_rel" => { 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<HashMap<String, HashMap<String, (i64, Ve
Ok(out)
}
fn rank(st: &State, h: usize) -> Result<Reply> {
fn rank(st: &State, h: usize, win: usize) -> Result<Reply> {
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<String, (String, bool, bool)> = 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<String>>(2)?, r.get::<_, Option<String>>(3)?)))? {
@@ -239,8 +267,8 @@ fn rank(st: &State, h: usize) -> Result<Reply> {
}
let mut rows: HashMap<String, Value> = 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<f64>>(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<f64>>(2)?,
r.get::<_, Option<f64>>(3)?, r.get::<_, Option<f64>>(4)?, r.get::<_, Option<f64>>(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<Reply> {
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::<HashMap<_, _>>()); }
}
// 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::<serde_json::Map<_, _>>());
}
}
}
}
let mut out: Vec<Value> = 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<String, String>) -> Result<Reply> {
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<Value> = (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::<serde_json::Map<_, _>>()}),
}
}).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<Reply> {
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<Reply> {
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<String, String>) -> Result<Reply> {
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<String, String>) -> Result<Reply> {
};
let now: serde_json::Map<String, Value> = (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::<Vec<_>>(),
"window_start": (p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1)).to_string(),
"current": {"t40": chart.t40_rank, "air": chart.air_rank,
+104 -32
View File
@@ -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; }
<div class="sub" id="sub">Loading…</div>
<nav>
<button class="on" data-tab="rank">Ranking</button>
<button data-tab="best">Best week</button>
<button data-tab="model">How good is it?</button>
</nav>
</header>
<main>
<section id="tab-rank">
<div class="controls">
<div class="ctl">8-week window starting <b id="hlabel">—</b><input type="range" id="h" min="0" max="12" value="0"></div>
<div class="ctl">Release week <b id="hlabel">—</b><input type="range" id="h" min="0" max="25" value="0"></div>
<div class="ctl">Added within
<select id="w"><option value="2">2 weeks</option><option value="4">4 weeks</option><option value="6">6 weeks</option><option value="8" selected>8 weeks</option><option value="10">10 weeks</option><option value="12">12 weeks</option></select>
</div>
<div class="ctl">Station
<select id="ch">
<option value="any">Any DR station</option>
@@ -118,12 +137,6 @@ svg text { fill: var(--muted); font-size: 11px; }
<option value="p8jazz">P8 Jazz</option>
</select>
</div>
<div class="ctl">Assume
<select id="mode">
<option value="rel">They release a new song</option>
<option value="mix">Any add (current songs included)</option>
</select>
</div>
<div class="ctl">Sort by
<select id="sort">
<option value="p">Chance of a new add</option>
@@ -132,8 +145,7 @@ svg text { fill: var(--muted); font-size: 11px; }
</select>
</div>
<div class="ctl">Artist <input type="search" id="q" placeholder="Search…"></div>
<div class="ctl"><label class="chk"><input type="checkbox" id="dk"> Danish acts only</label>
<label class="chk" title="Artists with a release in the last 6 weeks: their current song is still running, so a new-song estimate doesn't apply yet"><input type="checkbox" id="recent"> Include artists who just released</label></div>
<div class="ctl"><label class="chk"><input type="checkbox" id="dk"> Danish acts only</label></div>
</div>
<div class="tablewrap">
<table>
@@ -146,7 +158,23 @@ svg text { fill: var(--muted); font-size: 11px; }
</table>
</div>
<button class="more" id="more" hidden>Show more</button>
<p class="note">"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. <b>New add</b> = 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.</p>
<p class="note"><b>New add</b> = 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 <b>Best week</b> for their release calendar.</p>
</section>
<section id="tab-best" hidden>
<div class="controls">
<div class="ctl">Artist <input type="search" id="bw-q" list="bw-list" placeholder="Type an artist…" autocomplete="off"><datalist id="bw-list"></datalist></div>
<div class="ctl">Station
<select id="bw-ch"><option value="any">Any DR station</option><option value="P3">P3</option><option value="P4">P4 (best region)</option><option value="P5">P5</option><option value="P6">P6 Beat</option><option value="P8">P8 Jazz</option></select>
</div>
<div class="ctl">Added within
<select id="bw-w"><option value="2">2 weeks</option><option value="4">4 weeks</option><option value="6">6 weeks</option><option value="8" selected>8 weeks</option><option value="10">10 weeks</option><option value="12">12 weeks</option></select>
</div>
<div class="ctl">New Music Friday Denmark
<select id="bw-nmf"><option value="auto">As often as their releases were placed</option><option value="yes">Placed</option><option value="no">Not placed</option></select>
</div>
</div>
<div id="bw-out"><p class="empty">Pick an artist to see the best week to release a new song.</p></div>
</section>
<section id="tab-model" hidden>
@@ -192,10 +220,10 @@ const fmtDate = s => new Date(s + 'T00:00:00Z').toLocaleDateString('en-GB', { da
// Chance of a new add in the window (station group g, or any DR station).
function chanP(r, g) {
// "They release in the window" uses the conditional forecast where a discography is known.
const rel = document.getElementById('mode')?.value === 'rel' && r.add_rel;
const any = rel ? r.add_any_rel : r.add_any, per = rel ? r.add_rel : r.add;
// The new-song chance (null when the release week is too soon after the artist's last release).
const any = r.add_any_rel, per = r.add_rel;
if (g === 'any') return any;
if (any == null) return null;
if (g === 'p4') return Math.max(...P4S.map(c => per?.[c] ?? 0));
return per?.[g];
}
@@ -240,16 +268,20 @@ function windowStart(h) {
return d.toISOString().slice(0, 10);
}
async function loadRank() {
const h = +$('h').value;
const h = +$('h').value, w = $('w').value, k = h + '|' + w;
$('hlabel').textContent = `${fmtDate(windowStart(h))}${h ? ` (+${h} wk)` : ' (next week)'}`;
if (!data[h]) data[h] = (await API('rank?h=' + h)).rows;
if (!data[k]) {
const res = await API(`rank?h=${h}&w=${w}`);
data[k] = res.rows;
if (!res.live) setTimeout(() => { delete data[k]; loadRank(); }, 60000); // models still loading on the server
fillArtistList(res.rows);
}
render();
}
function render() {
const h = +$('h').value, g = $('ch').value, sort = $('sort').value, q = $('q').value.trim().toLowerCase(), dk = $('dk').checked;
const newSong = $('mode').value === 'rel';
let rows = data[h].filter(r => (!q || r.name.toLowerCase().includes(q)) && (!dk || r.danish) && (!newSong || !r.recent_release || $('recent').checked));
let rows = data[h + '|' + $('w').value].filter(r => (!q || r.name.toLowerCase().includes(q)) && (!dk || r.danish));
const key = sort === 'p' ? r => [chanP(r, g) ?? 0, chanPlays(r, g) ?? 0]
: sort === 'plays' ? r => [chanPlays(r, g) ?? 0, 0]
: r => [(chanPlays(r, g) ?? 0) - nowPlays(r, g), 0];
@@ -260,7 +292,7 @@ function render() {
const range = g === 'any' ? ` <small class="note">(${Math.round(r.lo)}–${Math.round(r.hi)})</small>` : '';
return `<tr data-id="${esc(r.id)}">
<td class="l">${i + 1}</td>
<td class="l name">${esc(r.name)}${r.danish ? '<small>DK</small>' : ''}${r.recent_release && $('mode').value === 'rel' ? '<small title="Released in the last 6 weeks: a new song now would compete with it">just released</small>' : ''}</td>
<td class="l name">${esc(r.name)}${r.danish ? '<small>DK</small>' : ''}${r.too_soon ? '<small title="Less than 6 weeks after their last release: no new-song estimate for this week">too soon</small>' : ''}</td>
<td><span class="bar"><i style="width:${Math.round((p ?? 0) * 70)}px"></i>${pct(p)}</span></td>
${GROUPS.map(([c]) => `<td class="hide-sm"><span class="cell" style="${heat(chanP(r, c))}">${pct(chanP(r, c))}</span></td>`).join('')}
<td class="l hide-sm">${nowChips(r.now, r.bauer)}</td>
@@ -318,9 +350,9 @@ async function showArtist(id) {
openDrawer('<div class="sec">Loading…</div>');
const a = await API('artist?id=' + encodeURIComponent(id));
if (a.error) { openDrawer(`<div class="sec">${esc(a.error)}</div>`); return; }
const h = +$('h').value;
const fc = a.forecasts.find(x => x.h === h) || a.forecasts[0] || {};
const fp = a.forecasts.find(x => x.h === Math.max(h, 1)) || {};
const h = +$('h').value, w = $('w').value;
const fc = (data[h + '|' + w] || []).find(r => r.id === id) || {};
const fp = a.forecasts.find(x => x.h === Math.max(Math.min(h, 12), 1)) || {};
const facts = [a.country, a.description, a.debut ? `active since ${a.debut}` : null].filter(Boolean).map(esc).join(' · ');
const runs = {};
a.charts.forEach(c => { const k = c.chart + '|' + c.title; (runs[k] ??= { ...c, weeks: 0, best: 99, first: c }); runs[k].weeks++; runs[k].best = Math.min(runs[k].best, c.position); runs[k].first = c; });
@@ -332,13 +364,13 @@ async function showArtist(id) {
<button class="x" aria-label="Close" onclick="closeDrawer()">×</button>
</div>
<div class="sec">
<h3>${$('mode').value === 'rel' && fc.add_rel ? 'If they release a new song: chance DR adds it within 8 weeks' : `Chance of any new add in the 8 weeks from ${fc.window_start ? fmtDate(fc.window_start) : '—'} (current songs included)`}</h3>
<div class="kpis">
<div class="kpi"><div>Any DR station</div><b>${pct(fc.add_any)}</b></div>
<h3>If they release a new song in the week of ${fmtDate(windowStart(h))}: chance DR adds it within ${w} weeks</h3>
${fc.too_soon ? '<p class="note"><b>Too soon:</b> less than 6 weeks after their last release, so there\'s no new-song estimate for this week. Pick a later week, or see their calendar.</p>' : `<div class="kpis">
<div class="kpi"><div>Any DR station</div><b>${pct(fc.add_any_rel)}</b></div>
${GROUPS.map(([g, l]) => `<div class="kpi"><div>${l}</div><b>${pct(chanP(fc, g))}</b></div>`).join('')}
</div>
${fc.recent_release ? '<p class="note"><b>Just released</b> (in the last 6 weeks). DR counts plays per artist, so the model can\'t tell a new song\'s chances from the current song\'s; the new-song estimate below doesn\'t really apply until the current song has run its course, and these artists are left out of the new-song ranking by default.</p>' : ''}
${fc.p_release != null ? `<p class="note">If they release a new song: <b>${pct(fc.add_any_rel)}</b>; if it's placed on New Music Friday Denmark: <b>${pct(fc.add_any_nmf)}</b> (their releases were placed about ${pct(fc.p_nmf)} of the time). Any add in the window, current songs included: ${pct(fc.add_any)} (chance they release in the window: ${pct(fc.p_release)}).</p>` : '<p class="note">No discography found on Deezer for this artist, so release timing isn\'t in the forecast.</p>'}
${fc.add_any_nmf != null ? `<p class="note">If it's placed on New Music Friday Denmark: <b>${pct(fc.add_any_nmf)}</b> (their releases were placed about ${pct(fc.p_nmf)} of the time).</p>` : ''}`}
<p><button class="more" style="margin:8px 0" onclick="closeDrawer(); openBest('${esc(id)}', '${esc(a.name).replace(/'/g, "\\'")}')">See the best week to release →</button></p>
<p class="note">In rotation now: ${nowChips(a.now, Object.fromEntries(Object.entries(a.bauer || {}).map(([s, v]) => [s, v.plays])))} · plays in the week of ${fp.week ? fmtDate(fp.week) : '—'}: <b>${Math.round(fp.mean ?? 0)}</b> (${Math.round(fp.lo ?? 0)}–${Math.round(fp.hi ?? 0)})</p>
</div>
<div class="sec"><h3>Weekly plays, last 51 weeks and next 12</h3>${historyChart(a)}</div>
@@ -353,14 +385,14 @@ async function showArtist(id) {
function whatIfUI(a, h) {
const box = $('wi');
box.innerHTML = `<div class="wi">
<div class="row"><label for="wi-h">Window starts in (weeks)</label><input type="range" id="wi-h" min="0" max="${meta.horizons.length}" value="${h}"><b id="wi-hv"></b></div>
<div class="row"><label for="wi-r">Releases a song in the window</label><input type="checkbox" id="wi-r" checked><b></b></div>
<div class="row"><label for="wi-h">Release week (weeks from now)</label><input type="range" id="wi-h" min="0" max="12" value="${Math.min(h, 12)}"><b id="wi-hv"></b></div>
<div class="row"><label for="wi-r">Releases a new song that week</label><input type="checkbox" id="wi-r" checked><b></b></div>
<div class="row"><label for="wi-n">…placed on New Music Friday Denmark</label><input type="checkbox" id="wi-n"><b></b></div>
<div class="row"><label for="wi-e">New chart entry, weeks ago</label><input type="range" id="wi-e" min="-1" max="26" value="-1"><b id="wi-ev"></b></div>
<div class="row"><label for="wi-t">Track Top-40 position now</label><input type="range" id="wi-t" min="0" max="40" value="0"><b id="wi-tv"></b></div>
<div class="row"><label for="wi-a">Airplay Top-20 position now</label><input type="range" id="wi-a" min="0" max="20" value="0"><b id="wi-av"></b></div>
<div id="wi-out"></div>
<p class="note">Switch the release on or off and change the artist's chart situation (as if it were true this week) to see how the forecast moves.</p>
<p class="note">Move the release week, switch the release or NMF placement, and change the artist's chart situation (as if it were true this week) to see how the forecast moves.</p>
</div>`;
let init = false, timer = null;
const label = () => {
@@ -387,7 +419,7 @@ function whatIfUI(a, h) {
const d = (x, y, f) => { const v = y - x; if (Math.abs(v) < 0.005 * (f === 'p' ? 1 : 100)) return ''; return `<span class="d ${v > 0 ? 'up' : 'down'}">${v > 0 ? '+' : ''}${f === 'p' ? Math.round(v * 100) + ' pts' : Math.round(v)}</span>`; };
const rows = [['Any DR station', w.base.add_any, w.scenario.add_any]].concat(GROUPS.map(([g, l]) => [l, chanP(w.base, g), chanP(w.scenario, g)]));
const onNow = Object.keys(w.current.in_rotation || {});
$('wi-out').innerHTML = `<p class="note">Chance of a new add in the 8 weeks from ${fmtDate(w.window_start)}. In rotation now on ${onNow.length ? onNow.length + ' station(s)' : 'no station'}. Now: ${w.current.t40 ? 'Track Top-40 #' + w.current.t40 : 'not on the Track Top-40'}, ${w.current.air ? 'Airplay Top-20 #' + w.current.air : 'not on the Airplay Top-20'}, ${w.current.entry == null ? 'no chart entry on record' : 'latest new chart entry ' + w.current.entry + ' weeks ago'}.</p>
$('wi-out').innerHTML = `<p class="note">${w.release ? (w.too_soon ? 'Too soon after their last release for a new-song estimate this week; the numbers below are the model\'s extrapolation.' : `A new song released the week of ${fmtDate(w.window_start)}: chance DR adds it within 8 weeks.`) : `No new release: chance of a new add from current songs in the 8 weeks from ${fmtDate(w.window_start)}.`} In rotation now on ${onNow.length ? onNow.length + ' station(s)' : 'no station'}. Now: ${w.current.t40 ? 'Track Top-40 #' + w.current.t40 : 'not on the Track Top-40'}, ${w.current.air ? 'Airplay Top-20 #' + w.current.air : 'not on the Airplay Top-20'}, ${w.current.entry == null ? 'no chart entry on record' : 'latest new chart entry ' + w.current.entry + ' weeks ago'}.</p>
<div class="kpis">${rows.map(([l, b, s]) => `<div class="kpi"><div>${l}</div><b>${pct(s)}</b>${d(b, s, 'p')}<div>was ${pct(b)}</div></div>`).join('')}
<div class="kpi"><div>Plays, first week</div><b>${Math.round(w.scenario.mean)}</b>${d(w.base.mean, w.scenario.mean, 'n')}<div>was ${Math.round(w.base.mean)}</div></div></div>`;
};
@@ -415,10 +447,50 @@ function renderModel() {
// ---------- wiring ----------
document.querySelectorAll('nav button').forEach(b => b.addEventListener('click', () => {
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x === b));
$('tab-rank').hidden = b.dataset.tab !== 'rank'; $('tab-model').hidden = b.dataset.tab !== 'model';
showTab(b.dataset.tab);
}));
function showTab(t) {
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x.dataset.tab === t));
['rank', 'best', 'model'].forEach(k => $('tab-' + k).hidden = k !== t);
}
$('h').addEventListener('input', () => { shown = 100; loadRank(); });
['ch', 'sort', 'dk', 'mode', 'recent'].forEach(id => $(id).addEventListener('change', () => { shown = 100; render(); }));
$('w').addEventListener('change', () => { shown = 100; loadRank(); });
['ch', 'sort', 'dk'].forEach(id => $(id).addEventListener('change', () => { shown = 100; render(); }));
// ---------- best week ----------
const artistByName = new Map();
let bestId = null;
function fillArtistList(rows) {
if (artistByName.size) return;
const opts = [];
rows.slice().sort((a, b) => a.name.localeCompare(b.name)).forEach(r => { if (!artistByName.has(r.name)) { artistByName.set(r.name, r.id); opts.push(`<option value="${esc(r.name)}">`); } });
$('bw-list').innerHTML = opts.join('');
}
function openBest(id, name) { bestId = id; $('bw-q').value = name; showTab('best'); loadBest(); }
async function loadBest() {
if (!bestId) return;
const w = $('bw-w').value, nmf = $('bw-nmf').value, ch = $('bw-ch').value;
$('bw-out').innerHTML = '<p class="empty">Loading…</p>';
const r = await fetch(`${BASE}/api/bestweek?id=${encodeURIComponent(bestId)}&w=${w}&nmf=${nmf}`);
if (r.status === 503) { $('bw-out').innerHTML = '<p class="empty">The model is still loading on the server; try again in a minute.</p>'; return; }
const b = await r.json();
if (b.error) { $('bw-out').innerHTML = `<p class="empty">${esc(b.error)}</p>`; return; }
const val = x => x.too_soon ? null : (ch === 'any' ? x.p : x.groups[ch]);
const vals = b.weeks.map(val).filter(v => v != null);
const max = Math.max(...vals), min = Math.min(...vals), avg = vals.reduce((s, v) => s + v, 0) / (vals.length || 1);
const best = b.weeks.find(x => val(x) === max);
// like a fare calendar: green = among the best weeks for this artist, red = among the worst
const level = v => { if (v == null || max === min) return 2; const z = (v - min) / (max - min); return z >= 0.85 ? 0 : z >= 0.5 ? 1 : z >= 0.2 ? 2 : 3; };
$('bw-out').innerHTML = vals.length ? `
<div class="bw-head"><h2>${esc(b.name)}</h2>
<span>Best week: <b>${fmtDate(best.week)}</b> · <b>${pct(max)}</b> chance DR adds a new song within ${w} weeks${ch === 'any' ? '' : ' on ' + ch} (average over the calendar ${pct(avg)}).</span></div>
<div class="cal">${b.weeks.map(x => { const v = val(x); return `<div class="day ${x.too_soon ? 'soon' : 'lvl' + level(v)} ${x === best ? 'best' : ''}" title="Release the week of ${fmtDate(x.week)}">
<div class="d">Week of ${fmtDate(x.week)}</div><div class="v">${x.too_soon ? 'too soon' : pct(v)}</div>${x.christmas ? '<span class="tag" title="Christmas weeks">🎄</span>' : ''}</div>`; }).join('')}</div>
<p class="note">Each tile: if ${esc(b.name)} releases a new song that week, the chance DR puts it into rotation within ${w} weeks. ${b.last_release ? `Their last release: ${fmtDate(b.last_release)}; weeks less than 6 weeks after it are "too soon" (the model only learned from releases with a clear gap).` : ''} The new song's NMF Denmark placement: ${nmf === 'auto' ? `as often as their releases were placed (${pct(b.p_nmf)})` : nmf === 'yes' ? 'placed' : 'not placed'}. What moves the numbers from week to week: time since their last release and last DR add, Christmas, and their NMF history; their current airplay is taken as it is now.</p>`
: `<h2>${esc(b.name)}</h2><p class="empty">No week in the next 26 has an estimate.</p>`;
}
$('bw-q').addEventListener('change', () => { const id = artistByName.get($('bw-q').value); if (id) { bestId = id; loadBest(); } });
['bw-w', 'bw-nmf', 'bw-ch'].forEach(id => $(id).addEventListener('change', loadBest));
$('q').addEventListener('input', () => { shown = 100; render(); });
$('more').addEventListener('click', () => { shown += 100; render(); });
$('rows').addEventListener('click', e => { const tr = e.target.closest('tr[data-id]'); if (tr) showArtist(tr.dataset.id); });