Wave features (time since the last DR add / Airplay Top-20 wave, peak band 13-25 weeks, 0.5-1.25x the artist's usual gap); ablation switch (model adds --without rel,wave); fix the per-channel row slice

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 03:56:10 +00:00
co-authored by Claude Opus 5.5
parent 7d656834fb
commit 992cc2b157
6 changed files with 209 additions and 15 deletions
+35 -7
View File
@@ -27,6 +27,31 @@ pub const GAP: usize = 4;
pub const W: usize = 8;
/// Per-channel rows: the panel features plus these.
/// Wave covariates: the chance of a next wave peaks 13–25 weeks after the last one and at
/// 0.5–1.25x the artist's usual gap (25 years of Airplay Top-20: weekly hazard 1.7% at 13–25
/// weeks vs 0.5% before and 0.3–0.4% after a year). Appended after the release block.
pub const WAVE: usize = 6;
/// Feature blocks switched off for an ablation run ("rel", "wave").
pub static WITHOUT: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
fn off(block: &str) -> bool { WITHOUT.get().is_some_and(|v| v.iter().any(|b| b == block)) }
pub const WAVE_NAMES: &[&str] = &["DR: weeks since last add 13-25", "DR: last add 26+ weeks ago", "DR: no add in the panel yet",
"Airplay Top-20: last new song 13-25 weeks ago", "Airplay Top-20: 0.5-1.25x own usual gap", "Airplay Top-20: last new song 2+ years ago"];
/// Weeks since the artist's last add on any DR channel (as of week t; None = none in the panel),
/// and the IFPI wave position.
pub fn wave_feats(p: &Panel, r: &Rot, a: usize, t: usize) -> [f64; WAVE] {
if off("wave") { return [0.0; WAVE]; }
let since = (0..=t).find(|&i| r.add_any(a, t - i));
let ch = p.chart(a, t);
let f = |b: bool| b as i32 as f64;
let air = ch.air_since as f64;
let known = ch.air_since != u16::MAX;
let ratio_ok = known && ch.air_gap > 0 && { let x = air / ch.air_gap as f64; (0.5..=1.25).contains(&x) };
[f(since.is_some_and(|s| (13..=25).contains(&s))), f(since.is_some_and(|s| s >= 26)), f(since.is_none()),
f(known && (13.0..=25.0).contains(&air)), f(ratio_ok), f(known && air >= 104.0)]
}
/// 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",
@@ -36,6 +61,7 @@ pub const REL_NAMES: &[&str] = &["discography known", "release recency 4w", "rel
/// 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] {
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);
@@ -62,7 +88,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;
pub const PC: usize = P + 10 + REL + WAVE;
/// Artist-level ("any channel") rows.
pub const ANY_NAMES: &[&str] = &[
@@ -72,7 +98,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;
pub const PA: usize = 21 + REL + WAVE;
/// 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];
@@ -124,10 +150,11 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, r
let on = r.on(a, c, t);
let ratio = p.y(a, c, t) / tau_of(p, r, a, c, t);
let f = |b: bool| b as i32 as f64;
out[P..PC].copy_from_slice(&[f(on), f(r.any(a, t)), n_on / C as f64,
out[P..P + 10].copy_from_slice(&[f(on), f(r.any(a, t)), n_on / C 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..PC].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, rel, on));
out[P + 10 + REL..PC].copy_from_slice(&wave_feats(p, r, a, t));
}
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, rel: Option<bool>, out: &mut [f64]) {
@@ -151,7 +178,8 @@ 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..PA].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, rel, r.any(a, t)));
out[21 + REL..PA].copy_from_slice(&wave_feats(p, r, a, t));
}
/// Logit models for one h, trained on origins whose whole window is observed by `last_week`.
@@ -376,9 +404,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().zip(&m.any[21..]) { writeln!(md, "| {n} | {b:+.3} |")?; }
for (n, b) in REL_NAMES.iter().chain(WAVE_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).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).chain(WAVE_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
writeln!(md)?;
Ok(rows)
}
+1
View File
@@ -76,6 +76,7 @@ pub fn run(db: &Db, args: &[String]) -> Result<()> {
print!("{md}");
}
Some("adds") => {
if let Some(w) = arg(args, "--without") { let _ = adds::WITHOUT.set(w.split(',').map(str::to_string).collect()); }
let horizons: Vec<usize> = arg(args, "--h").unwrap_or_else(|| "0,4".into()).split(',').filter_map(|s| s.trim().parse().ok()).collect();
let tau = common::thresholds(&p, 26);
let mut md = String::new();
+16 -1
View File
@@ -41,6 +41,10 @@ pub struct Chart {
/// Distinct songs on the Track Top-40 / Airplay Top-20 so far.
pub t40_hits: u16,
pub air_hits: u16,
/// Airplay Top-20 "waves": weeks since the artist's latest new song on the Airplay Top-20
/// (u16::MAX = never), and the median gap between their earlier new songs (0 = fewer than 2 gaps).
pub air_since: u16,
pub air_gap: u16,
}
pub struct Panel {
@@ -170,10 +174,12 @@ fn chart_states(db: &Db, weeks: &[NaiveDate], names: &HashMap<String, Vec<usize>
for entries in &by_artist {
for w in 0..nw {
let cutoff = cutoffs[w];
let mut s = Chart { t40_rank: 0, air_rank: 0, weeks_since_entry: u16::MAX, t40_hits: 0, air_hits: 0 };
let mut s = Chart { t40_rank: 0, air_rank: 0, weeks_since_entry: u16::MAX, t40_hits: 0, air_hits: 0, air_since: u16::MAX, air_gap: 0 };
let mut air_firsts: BTreeSet<NaiveDate> = BTreeSet::new();
let (mut t40_songs, mut air_songs) = (BTreeSet::new(), BTreeSet::new());
for e in entries.iter().filter(|e| e.monday <= cutoff) {
if e.t40 { t40_songs.insert(&e.song); } else { air_songs.insert(&e.song); }
if !e.t40 && e.new { air_firsts.insert(e.monday); }
let best = |cur: u8| if cur == 0 { e.rank } else { cur.min(e.rank) };
if e.t40 && Some(e.monday) == latest_t40[w] { s.t40_rank = best(s.t40_rank); }
if !e.t40 && Some(e.monday) == latest_air[w] { s.air_rank = best(s.air_rank); }
@@ -184,6 +190,15 @@ fn chart_states(db: &Db, weeks: &[NaiveDate], names: &HashMap<String, Vec<usize>
}
s.t40_hits = t40_songs.len() as u16;
s.air_hits = air_songs.len() as u16;
// Waves: new songs at least 2 weeks apart.
let mut waves: Vec<NaiveDate> = Vec::new();
for d in &air_firsts { if waves.last().is_none_or(|l| (*d - *l).num_days() >= 14) { waves.push(*d); } }
if let Some(last) = waves.last() { s.air_since = ((cutoff - *last).num_days() / 7).clamp(0, u16::MAX as i64 - 1) as u16; }
if waves.len() >= 3 {
let mut gaps: Vec<i64> = waves.windows(2).map(|w| (w[1] - w[0]).num_days() / 7).collect();
gaps.sort();
s.air_gap = gaps[gaps.len() / 2].clamp(1, 2000) as u16;
}
out.push(s);
}
}