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)
|
||||
}
|
||||
|
||||
+24
-1
@@ -51,6 +51,8 @@ pub struct Panel {
|
||||
chart: Vec<Chart>,
|
||||
/// Rotation thresholds fitted to DR's rotation lists, if calibrated.
|
||||
pub list_tau: Option<[u32; C]>,
|
||||
/// Release dates per artist (Deezer discography: singles, EPs, albums), sorted; None = not looked up / not found.
|
||||
pub releases: Vec<Option<Vec<NaiveDate>>>,
|
||||
}
|
||||
|
||||
impl Panel {
|
||||
@@ -114,7 +116,8 @@ impl Panel {
|
||||
|
||||
let chart = chart_states(db, &weeks, &names, artists.len())?;
|
||||
let list_tau = super::calibrate::stored(db)?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau })
|
||||
let releases = load_releases(db, &id_of, artists.len())?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,3 +189,23 @@ fn chart_states(db: &Db, weeks: &[NaiveDate], names: &HashMap<String, Vec<usize>
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn load_releases(db: &Db, id_of: &HashMap<String, usize>, n: usize) -> Result<Vec<Option<Vec<NaiveDate>>>> {
|
||||
let mut out: Vec<Option<Vec<NaiveDate>>> = vec![None; n];
|
||||
let exists: i64 = db.conn.query_row("SELECT count(*) FROM sqlite_master WHERE name = 'deezer_artists'", [], |r| r.get(0))?;
|
||||
if exists == 0 { return Ok(out); }
|
||||
for r in db.conn.prepare("SELECT dr_artist_id FROM deezer_artists WHERE deezer_artist_id IS NOT NULL")?.query_map([], |r| r.get::<_, String>(0))? {
|
||||
if let Some(&a) = id_of.get(&r?) { out[a] = Some(Vec::new()); }
|
||||
}
|
||||
for r in db.conn.prepare(
|
||||
"SELECT d.dr_artist_id, r.release_date FROM deezer_artists d JOIN deezer_releases r ON r.deezer_artist_id = d.deezer_artist_id
|
||||
WHERE coalesce(r.record_type, '') != 'compile' AND r.release_date > '1900'")?
|
||||
.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? {
|
||||
let (id, d) = r?;
|
||||
if let (Some(&a), Ok(d)) = (id_of.get(&id), NaiveDate::parse_from_str(&d, "%Y-%m-%d")) {
|
||||
if let Some(v) = out[a].as_mut() { v.push(d); }
|
||||
}
|
||||
}
|
||||
for v in out.iter_mut().flatten() { v.sort(); v.dedup(); }
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
+17
-6
@@ -33,7 +33,9 @@ pub fn create_tables(db: &Db) -> Result<()> {
|
||||
"CREATE TABLE IF NOT EXISTS forecasts (
|
||||
made_at TEXT NOT NULL, model_version TEXT NOT NULL,
|
||||
target TEXT NOT NULL, -- 'rotation' (prob, mean plays) per channel, 'any' across channels;
|
||||
-- 'add' / 'add_any': P(new add in the 8 weeks from target_week);
|
||||
-- 'add' / 'add_any': P(new add in the 8 weeks from target_week), blended
|
||||
-- by the artist's chance of releasing ('p_release'); 'add_rel' /
|
||||
-- 'add_any_rel': the same if the artist releases in the window;
|
||||
-- 'now': in rotation in the origin week (prob 1/0, mean = plays)
|
||||
key TEXT NOT NULL, -- '{dr artist id}:{channel}' or '{dr artist id}:any'
|
||||
horizon INTEGER NOT NULL, -- weeks after origin_week
|
||||
@@ -119,12 +121,21 @@ pub fn run(db: &Db, p: &Panel, max_h: usize, show_h: usize) -> Result<Vec<Line>>
|
||||
let mut ins = tx.prepare("INSERT OR REPLACE INTO forecasts VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)")?;
|
||||
for a in 0..p.a() {
|
||||
let id = &p.artists[a].id;
|
||||
let (pc, pa) = m.predict(p, &rot, a, p.w() - 1, None);
|
||||
ins.execute(params![made, VERSION, "add_any", format!("{id}:any"), h as i64, origin.to_string(), start, pa,
|
||||
Option::<f64>::None, Option::<f64>::None, Option::<f64>::None])?;
|
||||
// If they release in the window, if they don't, and blended by their release rate.
|
||||
let t0 = p.w() - 1;
|
||||
let (pc1, pa1) = m.predict(p, &rot, a, t0, Some(true), None);
|
||||
let (pc0, pa0) = m.predict(p, &rot, a, t0, Some(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 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", pa1)?;
|
||||
if let Some(r) = pr { put("p_release", "any", r)?; }
|
||||
for c in 0..C {
|
||||
ins.execute(params![made, VERSION, "add", format!("{id}:{}", CHANNELS[c]), h as i64, origin.to_string(), start, pc[c],
|
||||
Option::<f64>::None, Option::<f64>::None, Option::<f64>::None])?;
|
||||
put("add", CHANNELS[c], mix(pc1[c], pc0[c]))?;
|
||||
put("add_rel", CHANNELS[c], pc1[c])?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user