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:
+35
-7
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user