Surge-based adds (onset, or plays jumping well above the artist's own baseline and staying up): finds 77% of DR's real song adds vs 64% for onset only; addcheck validation against the lists
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
//! How well the artist-level add definitions find real song adds: a song new on a DR list
|
||||
//! (vs the previous list) should come with an add for the artist in between.
|
||||
|
||||
use super::adds::{Rot, ADD_RULE};
|
||||
use super::common::thresholds;
|
||||
use super::panel::{key, Panel, CHANNELS};
|
||||
use crate::db::Db;
|
||||
use anyhow::Result;
|
||||
use chrono::{Duration, NaiveDate};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::fmt::Write as _;
|
||||
|
||||
pub fn run(db: &Db, p: &Panel) -> Result<String> {
|
||||
let mut lists: BTreeMap<NaiveDate, HashMap<String, HashSet<(String, String)>>> = BTreeMap::new();
|
||||
for r in db.conn.prepare("SELECT list_date, channel, artist, title FROM rotation_lists WHERE category IN ('A', 'B', 'C', 'LIST')")?
|
||||
.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, String>(2)?, r.get::<_, String>(3)?)))? {
|
||||
let (d, ch, a, t) = r?;
|
||||
if let Ok(d) = NaiveDate::parse_from_str(&d, "%Y-%m-%d") {
|
||||
let primary = a.split(" [+] ").next().unwrap_or(&a).split(" feat").next().unwrap_or(&a).to_string();
|
||||
lists.entry(d + Duration::days(3)).or_default().entry(ch).or_default().insert((key(&primary), key(t.split(" (").next().unwrap_or(&t))));
|
||||
}
|
||||
}
|
||||
let by_name: HashMap<String, usize> = p.artists.iter().enumerate().map(|(i, a)| (key(&a.name), i)).collect();
|
||||
let week_of: HashMap<NaiveDate, usize> = p.weeks.iter().enumerate().map(|(i, d)| (*d, i)).collect();
|
||||
let tau = thresholds(p, p.w());
|
||||
let mut md = String::new();
|
||||
writeln!(md, "# Add definitions vs DR's own lists\n\nA song new on a list (vs the previous list for the channel) should come with an add for its artist between the two lists (± 1 week).\n")?;
|
||||
writeln!(md, "| Definition | New songs on lists (artist in the pool) | Artist add found | Recall | Artist adds per artist-week |\n|---|---|---|---|---|")?;
|
||||
for rule in ["onset", "surge"] {
|
||||
let _ = ADD_RULE.set(rule.to_string()); // OnceLock: only the first set counts; run each rule in its own process
|
||||
if ADD_RULE.get().map(String::as_str) != Some(rule) { continue; }
|
||||
let r = Rot::new(p, &tau);
|
||||
let (mut n, mut hit) = (0usize, 0usize);
|
||||
let dates: Vec<NaiveDate> = lists.keys().copied().collect();
|
||||
for ch in ["p3", "p4", "p5", "p6beat", "p8jazz"] {
|
||||
let ds: Vec<&NaiveDate> = dates.iter().filter(|d| lists[d].contains_key(ch)).collect();
|
||||
for pair in ds.windows(2) {
|
||||
let (Some(&w0), Some(&w1)) = (week_of.get(pair[0]), week_of.get(pair[1])) else { continue };
|
||||
let (old, new) = (&lists[pair[0]][ch], &lists[pair[1]][ch]);
|
||||
let chans: Vec<usize> = (0..CHANNELS.len()).filter(|&c| if ch == "p4" { CHANNELS[c].starts_with("p4") } else { CHANNELS[c] == ch }).collect();
|
||||
for (artist, song) in new.difference(old) {
|
||||
let _ = song;
|
||||
let Some(&a) = by_name.get(artist) else { continue };
|
||||
n += 1;
|
||||
let lo = w0.saturating_sub(1);
|
||||
let hi = (w1 + 1).min(p.w() - 1);
|
||||
if (lo..=hi).any(|w| chans.iter().any(|&c| r.add(a, c, w))) { hit += 1; }
|
||||
}
|
||||
}
|
||||
}
|
||||
let total: usize = (0..p.a()).map(|a| (0..p.w()).filter(|&w| r.add_any(a, w)).count()).sum();
|
||||
writeln!(md, "| {rule} | {n} | {hit} | {:.0}% | {:.4} |", 100.0 * hit as f64 / n.max(1) as f64, total as f64 / (p.a() * p.w()) as f64)?;
|
||||
}
|
||||
Ok(md)
|
||||
}
|
||||
+36
-3
@@ -230,8 +230,16 @@ pub struct Rot {
|
||||
pub tau: [u32; C],
|
||||
rot: Vec<bool>, // [a][c][w]
|
||||
any: Vec<bool>, // [a][w]
|
||||
adds: Vec<bool>, // [a][c][w]
|
||||
adds_any: Vec<bool>, // [a][w]
|
||||
}
|
||||
|
||||
/// Which add definition: "onset" = entering rotation after GAP+ weeks out (the original); "surge" =
|
||||
/// onset, or plays jumping well above the artist's own 8-week baseline and staying up the next
|
||||
/// week (catches new songs of artists who never leave rotation). Default: surge.
|
||||
pub static ADD_RULE: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
||||
fn surge_rule() -> bool { ADD_RULE.get().is_none_or(|r| r != "onset") }
|
||||
|
||||
impl Rot {
|
||||
pub fn new(p: &Panel, tau: &[u32; C]) -> Rot {
|
||||
let nw = p.w();
|
||||
@@ -242,12 +250,37 @@ impl Rot {
|
||||
rot[(a * C + c) * nw + w] = r;
|
||||
if r { any[a * nw + w] = true; }
|
||||
} } }
|
||||
Rot { nw, tau: *tau, rot, any }
|
||||
let mut adds = vec![false; p.a() * C * nw];
|
||||
let mut adds_any = vec![false; p.a() * nw];
|
||||
let surge = surge_rule();
|
||||
for a in 0..p.a() {
|
||||
for c in 0..C {
|
||||
let on = |w: usize| rot[(a * C + c) * nw + w];
|
||||
let t = tau[c] as f64;
|
||||
for w in GAP..nw {
|
||||
let onset = on(w) && (1..=GAP).all(|k| !on(w - k));
|
||||
let jump = surge && w >= 8 && w + 1 < nw && {
|
||||
let mut b: Vec<f64> = (w - 8..w).map(|k| p.y(a, c, k)).collect();
|
||||
b.sort_by(f64::total_cmp);
|
||||
let base = b[4];
|
||||
let (y0, y1) = (p.y(a, c, w), p.y(a, c, w + 1));
|
||||
y0 >= (0.6 * t).max(base + (0.5 * t).max(0.5 * base)) && y1 >= (0.6 * t).max(base + (0.25 * t).max(0.25 * base))
|
||||
};
|
||||
if (onset || jump) && (1..=GAP).all(|k| !adds[(a * C + c) * nw + w - k]) { adds[(a * C + c) * nw + w] = true; }
|
||||
}
|
||||
}
|
||||
for w in GAP..nw {
|
||||
let classic = any[a * nw + w] && (1..=GAP).all(|k| !any[a * nw + w - k]);
|
||||
let some = if surge { (0..C).any(|c| adds[(a * C + c) * nw + w]) } else { classic };
|
||||
if some && (1..=GAP).all(|k| !adds_any[a * nw + w - k]) { adds_any[a * nw + w] = true; }
|
||||
}
|
||||
}
|
||||
Rot { nw, tau: *tau, rot, any, adds, adds_any }
|
||||
}
|
||||
pub fn on(&self, a: usize, c: usize, w: usize) -> bool { self.rot[(a * C + c) * self.nw + w] }
|
||||
pub fn any(&self, a: usize, w: usize) -> bool { self.any[a * self.nw + w] }
|
||||
pub fn add(&self, a: usize, c: usize, w: usize) -> bool { w >= GAP && self.on(a, c, w) && (1..=GAP).all(|k| !self.on(a, c, w - k)) }
|
||||
pub fn add_any(&self, a: usize, w: usize) -> bool { w >= GAP && self.any(a, w) && (1..=GAP).all(|k| !self.any(a, w - k)) }
|
||||
pub fn add(&self, a: usize, c: usize, w: usize) -> bool { w < self.nw && self.adds[(a * C + c) * self.nw + w] }
|
||||
pub fn add_any(&self, a: usize, w: usize) -> bool { w < self.nw && self.adds_any[a * self.nw + w] }
|
||||
/// An add in weeks t+h+1 ..= t+h+W (all inside the panel).
|
||||
pub fn add_in_window(&self, a: usize, c: usize, t: usize, h: usize) -> bool { self.add_in(a, c, t, h, W) }
|
||||
pub fn add_any_in_window(&self, a: usize, t: usize, h: usize) -> bool { self.add_any_in(a, t, h, W) }
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
//! airplay model adds [--h 0,4] [--out docs/BACKTEST-ADDS.md] backtest of the new-add model
|
||||
//! airplay model rank [--h 4] [--max-h 12] [--top 50]
|
||||
|
||||
pub mod addcheck;
|
||||
pub mod adds;
|
||||
pub mod backtest;
|
||||
pub mod calibrate;
|
||||
@@ -83,7 +84,12 @@ pub fn run(db: &Db, args: &[String]) -> Result<()> {
|
||||
if let Some(out) = arg(args, "--out") { std::fs::write(out, &md)?; }
|
||||
print!("{md}");
|
||||
}
|
||||
Some("addcheck") => {
|
||||
if let Some(r) = arg(args, "--rule") { let _ = adds::ADD_RULE.set(r); }
|
||||
print!("{}", addcheck::run(db, &p)?);
|
||||
}
|
||||
Some("events") => {
|
||||
if let Some(r) = arg(args, "--rule") { let _ = adds::ADD_RULE.set(r); }
|
||||
let tau = common::thresholds(&p, 26);
|
||||
let mut md = String::new();
|
||||
let rows = adds::backtest_events(&p, &tau, 26, &mut md)?;
|
||||
@@ -97,6 +103,7 @@ pub fn run(db: &Db, args: &[String]) -> Result<()> {
|
||||
print!("{md}");
|
||||
}
|
||||
Some("adds") => {
|
||||
if let Some(r) = arg(args, "--rule") { let _ = adds::ADD_RULE.set(r); }
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user