NMF Denmark in the add model (placed in the window, top 10, history), scenario type (release / NMF) for the ranking and the what-if; NMF Denmark export

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 11:43:52 +00:00
co-authored by Claude Opus 5.5
parent e6b080246b
commit e94601b997
6 changed files with 268 additions and 151 deletions
+68 -19
View File
@@ -52,6 +52,52 @@ pub fn wave_feats(p: &Panel, r: &Rot, a: usize, t: usize) -> [f64; WAVE] {
f(known && (13.0..=25.0).contains(&air)), f(ratio_ok), f(known && air >= 104.0)]
}
/// What the forecast assumes about the window: None = what actually happened (training and
/// backtests), Some = a scenario (the ranking, the what-if).
#[derive(Clone, Copy, Default)]
pub struct Scenario { pub release: Option<bool>, pub nmf: Option<bool> }
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) } }
}
/// New Music Friday Denmark: placed in the window (and in its top 10), placements in the past
/// year, and the share of the artist's releases over two years that were placed.
pub const NMF: usize = 4;
pub const NMF_NAMES: &[&str] = &["NMF Denmark in the window", "NMF Denmark top 10 in the window", "log NMF Denmark placements last 52w",
"share of releases on NMF Denmark (2 years)"];
fn window(p: &Panel, t: usize, h: usize) -> (chrono::NaiveDate, chrono::NaiveDate) {
let w0 = p.weeks[t] + chrono::Duration::weeks(h as i64 + 1);
(w0, w0 + chrono::Duration::weeks(W as i64))
}
/// P(an artist's release is placed on NMF Denmark), from the past two years (shrunk).
pub fn p_nmf(p: &Panel, a: usize, t: usize) -> f64 {
let end_t = p.weeks[t] + chrono::Duration::days(6);
let placed = p.nmf_dk[a].iter().filter(|(d, _)| *d <= end_t && (end_t - *d).num_days() <= 730).count() as f64;
let rels = p.releases[a].as_ref().map(|v| v.iter().filter(|d| **d <= end_t && (end_t - **d).num_days() <= 730).count()).unwrap_or(0) as f64;
((placed + 0.1) / (rels + 1.0)).clamp(0.0, 0.95)
}
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 placed = p.nmf_dk[a].iter().filter(|(d, _)| *d >= w0 && *d < w1);
let (in_win, top10) = match sc.nmf {
None => { let v: Vec<_> = placed.collect(); (!v.is_empty(), v.iter().any(|(_, pos)| pos.is_some_and(|x| x <= 10))) }
Some(false) => (false, false),
// scenario "placed": top 10 as often as the artist's past placements were
Some(true) => (true, false),
};
let past: Vec<&(chrono::NaiveDate, Option<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 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)]
}
/// Long-history covariates: DR's 2012–2021 archive and DR's real rotation lists before week t.
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-)",
@@ -74,7 +120,8 @@ pub const REL_NAMES: &[&str] = &["discography known", "release recency 4w", "rel
/// Weeks since the latest release, releases in the past year, and whether the artist releases in
/// the window t+h+1 ..= t+h+W (the actual one when `rel` is None: training and backtests; else the
/// scenario "releases / doesn't release in the window").
pub fn rel_feats(p: &Panel, a: usize, t: usize, h: usize, rel: Option<bool>, in_rot_now: bool) -> [f64; REL] {
pub fn rel_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario, in_rot_now: bool) -> [f64; REL] {
let rel = sc.release;
if off("rel") { return [0.0; REL]; }
let Some(dates) = p.releases[a].as_ref() else { return [0.0; REL] };
let end_t = p.weeks[t] + chrono::Duration::days(6);
@@ -102,7 +149,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;
pub const PC: usize = P + 10 + REL + WAVE + LONG + NMF;
/// Artist-level ("any channel") rows.
pub const ANY_NAMES: &[&str] = &[
@@ -112,7 +159,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;
pub const PA: usize = 21 + REL + WAVE + LONG + NMF;
/// 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];
@@ -162,7 +209,7 @@ fn run_and_since(flag: impl Fn(usize) -> bool, t: usize) -> (f64, f64) {
fn christmas_in_window(p: &Panel, t: usize, h: usize) -> bool { (t + h + 1..=t + h + W).any(|w| features::christmas(p, w)) }
pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, rel: Option<bool>, out: &mut [f64]) {
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;
let n_on = groups_on(r, a, t);
@@ -174,12 +221,13 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, r
out[P..P + 10].copy_from_slice(&[f(on), f(r.any(a, t)), n_on / GROUPS as f64,
f(weeks == 1), f(weeks == 2), f((3..=5).contains(&weeks)), f(weeks >= 6),
f(!on && ratio >= 0.5), ratio.min(3.0) / 3.0, since]);
out[P + 10..P + 10 + REL].copy_from_slice(&rel_feats(p, a, t, h, rel, on));
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..PC].copy_from_slice(&long_feats(p, 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));
}
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, rel: Option<bool>, out: &mut [f64]) {
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, out: &mut [f64]) {
let tot = |w: usize| p.group_total(a, w, None);
let mean = |n: usize| (0..n).map(|i| tot(t - i)).sum::<f64>() / n as f64;
let max12 = (0..LAGS).map(|i| tot(t - i)).fold(0.0, f64::max);
@@ -200,9 +248,10 @@ pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, rel: Option<boo
christmas_in_window(p, t, h) as i32 as f64,
];
out[..21].copy_from_slice(&v);
out[21..21 + REL].copy_from_slice(&rel_feats(p, a, t, h, rel, r.any(a, t)));
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..PA].copy_from_slice(&long_feats(p, 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));
}
/// Logit models for one h, trained on origins whose whole window is observed by `last_week`.
@@ -242,7 +291,7 @@ impl AddModel {
let (mut dc, mut da) = (Design::new(2), Design::new(2));
for t in c0..=hi {
for a in 0..p.a() {
let (pc, pa) = inner.predict(p, r, a, t, None, None);
let (pc, pa) = inner.predict(p, r, a, t, Scenario::ACTUAL, 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);
@@ -268,10 +317,10 @@ impl AddModel {
// 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 { 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, None, &mut x);
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_any(p, r, a, t, h, None, &mut xa);
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);
}
}
@@ -279,17 +328,17 @@ impl AddModel {
}
/// (P(add) per channel, P(add on any channel)) from origin t.
/// `rel`: None = whether the artist actually released in the window (backtests), Some = scenario.
pub fn predict(&self, p: &Panel, r: &Rot, a: usize, t: usize, rel: Option<bool>, chart: Option<&dyn Fn(&mut [f64], [usize; 4])>) -> ([f64; C], f64) {
/// `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) {
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, rel, &mut x);
row_channel(p, r, a, c, t, self.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, rel, &mut xa);
row_any(p, r, a, t, self.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.
@@ -379,7 +428,7 @@ pub fn backtest(p: &Panel, tau: &[u32; C], horizons: &[usize], start: usize, md:
}
let (mut lc, mut la) = ([0.0; 3], [0.0; 3]);
for a in 0..p.a() {
let (pc, pa) = m.predict(p, &r, a, t0, None, None);
let (pc, pa) = m.predict(p, &r, a, t0, Scenario::ACTUAL, None);
let mut tot = 0.0;
for c in 0..C {
let y = r.add_in_window(a, c, t0, h);
@@ -432,9 +481,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).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).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).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).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
writeln!(md)?;
Ok(rows)
}