Deezer discographies for every DR artist; release features in the add model (recency, releases in the window); rankings 'if they release in the window' and blended by release rate; site switch and what-if release toggle
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+48
-13
@@ -27,11 +27,42 @@ pub const GAP: usize = 4;
|
||||
pub const W: usize = 8;
|
||||
|
||||
/// Per-channel rows: the panel features plus these.
|
||||
/// 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",
|
||||
"releases in the window", "releases in window x not in rotation now"];
|
||||
|
||||
/// 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] {
|
||||
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 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(),
|
||||
f(in_window), f(in_window && !in_rot_now)]
|
||||
}
|
||||
|
||||
/// P(the artist releases something in an 8-week window), from their release rate over two years
|
||||
/// (shrunk towards one release a year for artists with little history).
|
||||
pub fn p_release(p: &Panel, a: usize, t: usize) -> Option<f64> {
|
||||
let dates = p.releases[a].as_ref()?;
|
||||
let end_t = p.weeks[t] + chrono::Duration::days(6);
|
||||
let n = dates.iter().filter(|d| **d <= end_t && (end_t - **d).num_days() <= 730).count() as f64;
|
||||
let per_year = (n + 1.0) / 3.0;
|
||||
Some(1.0 - (-per_year * W as f64 / 52.0).exp())
|
||||
}
|
||||
|
||||
pub const EXTRA: &[&str] = &["in rotation on channel now", "in rotation anywhere now", "channels in rotation now (/14)",
|
||||
"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;
|
||||
pub const PC: usize = P + 10 + REL;
|
||||
|
||||
/// Artist-level ("any channel") rows.
|
||||
pub const ANY_NAMES: &[&str] = &[
|
||||
@@ -41,7 +72,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;
|
||||
pub const PA: usize = 21 + REL;
|
||||
/// 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];
|
||||
@@ -84,7 +115,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, out: &mut [f64]) {
|
||||
pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, rel: Option<bool>, 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 = (0..C).filter(|&k| r.on(a, k, t)).count() as f64;
|
||||
@@ -96,9 +127,10 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, o
|
||||
out[P..PC].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));
|
||||
}
|
||||
|
||||
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, out: &mut [f64]) {
|
||||
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, rel: Option<bool>, out: &mut [f64]) {
|
||||
let tot = |w: usize| (0..C).map(|c| p.y(a, c, w)).sum::<f64>();
|
||||
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);
|
||||
@@ -118,7 +150,8 @@ pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, out: &mut [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,
|
||||
];
|
||||
out.copy_from_slice(&v);
|
||||
out[..21].copy_from_slice(&v);
|
||||
out[21..PA].copy_from_slice(&rel_feats(p, a, t, h, rel, r.any(a, t)));
|
||||
}
|
||||
|
||||
/// Logit models for one h, trained on origins whose whole window is observed by `last_week`.
|
||||
@@ -158,7 +191,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);
|
||||
let (pc, pa) = inner.predict(p, r, a, t, None, 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);
|
||||
@@ -184,10 +217,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, &mut x);
|
||||
row_channel(p, r, a, c, t, h, None, &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, &mut xa);
|
||||
row_any(p, r, a, t, h, None, &mut xa);
|
||||
da.push(&xa, r.add_any_in_window(a, t, h) as i32 as f64, 1.0);
|
||||
}
|
||||
}
|
||||
@@ -195,16 +228,17 @@ impl AddModel {
|
||||
}
|
||||
|
||||
/// (P(add) per channel, P(add on any channel)) from origin t.
|
||||
pub fn predict(&self, p: &Panel, r: &Rot, a: usize, t: usize, chart: Option<&dyn Fn(&mut [f64], [usize; 4])>) -> ([f64; C], f64) {
|
||||
/// `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) {
|
||||
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, &mut x);
|
||||
row_channel(p, r, a, c, t, self.h, rel, &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, &mut xa);
|
||||
row_any(p, r, a, t, self.h, rel, &mut xa);
|
||||
if let Some(f) = chart { f(&mut xa, CHART_IDX_ANY); }
|
||||
(pc, platt(self.platt_any, glm::sigmoid(glm::dot(&self.any, &xa))))
|
||||
}
|
||||
@@ -289,7 +323,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);
|
||||
let (pc, pa) = m.predict(p, &r, a, t0, None, None);
|
||||
let mut tot = 0.0;
|
||||
for c in 0..C {
|
||||
let y = r.add_in_window(a, c, t0, h);
|
||||
@@ -342,8 +376,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} |")?; }
|
||||
writeln!(md, "\n| Per-channel feature | Coefficient |\n|---|---|")?;
|
||||
for (n, b) in features::NAMES.iter().chain(EXTRA).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
writeln!(md)?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user