Channel affinity (share of the artist's DR plays per station before t, never played there) and genre fit (DR genre x station) in the per-station rows; Best week: any date range up to 12 months
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+25
-4
@@ -153,6 +153,26 @@ pub fn season_feat(p: &Panel, t: usize, h: usize, win: usize) -> [f64; SEASON] {
|
||||
[v]
|
||||
}
|
||||
|
||||
/// Channel affinity and genre fit (per-channel rows only): the share of the artist's DR plays that
|
||||
/// were on this station group (all-time top tracks, minus panel plays after week t), the log of
|
||||
/// those plays, "never played here", and how much DR plays the artist's genre on this group.
|
||||
pub const AFF: usize = 4;
|
||||
pub const AFF_NAMES: &[&str] = &["share of the artist's DR plays on this station (before t)", "log DR plays on this station (before t)",
|
||||
"never played on this station (before t)", "genre fit for this station"];
|
||||
pub fn aff_feats(p: &Panel, a: usize, c: usize, t: usize) -> [f64; AFF] {
|
||||
if off("aff") { return [0.0; AFF]; }
|
||||
// Plays after week t (the future, as seen from t) taken out of the all-time counts.
|
||||
let after = |g: usize| -> f64 {
|
||||
let cs: Vec<usize> = (0..C).filter(|&k| group(k) == g).collect();
|
||||
let n = cs.len() as f64;
|
||||
(t + 1..p.w()).map(|w| cs.iter().map(|&k| p.y(a, k, w)).sum::<f64>() / n).sum()
|
||||
};
|
||||
let before: [f64; GROUPS] = std::array::from_fn(|g| (p.alltime[a][g] - after(g)).max(0.0));
|
||||
let tot: f64 = before.iter().sum();
|
||||
let g = group(c);
|
||||
[if tot > 0.0 { before[g] / tot } else { 0.0 }, before[g].ln_1p(), (before[g] == 0.0) as i32 as f64, p.genre_fit[a][g]]
|
||||
}
|
||||
|
||||
/// 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",
|
||||
@@ -190,7 +210,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 + WAVE + LONG + NMF + SEASON;
|
||||
pub const PC: usize = P + 10 + REL + WAVE + LONG + NMF + SEASON + AFF;
|
||||
|
||||
/// Artist-level ("any channel") rows.
|
||||
pub const ANY_NAMES: &[&str] = &[
|
||||
@@ -267,8 +287,9 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, s
|
||||
out[P + 10..P + 10 + REL].copy_from_slice(&rel_feats(p, a, t, h, sc, on));
|
||||
out[P + 10 + REL..P + 10 + REL + WAVE].copy_from_slice(&wave_feats(p, r, a, t, sc.shift));
|
||||
out[P + 10 + REL + WAVE..P + 10 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, a, t, sc.shift));
|
||||
out[P + 10 + REL + WAVE + LONG..PC - SEASON].copy_from_slice(&nmf_feats(p, a, t, h, sc));
|
||||
out[PC - SEASON..PC].copy_from_slice(&season_feat(p, t, h, sc.win));
|
||||
out[P + 10 + REL + WAVE + LONG..PC - SEASON - AFF].copy_from_slice(&nmf_feats(p, a, t, h, sc));
|
||||
out[PC - SEASON - AFF..PC - AFF].copy_from_slice(&season_feat(p, t, h, sc.win));
|
||||
out[PC - AFF..PC].copy_from_slice(&aff_feats(p, a, c, t));
|
||||
}
|
||||
|
||||
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, out: &mut [f64]) {
|
||||
@@ -551,7 +572,7 @@ pub fn backtest(p: &Panel, tau: &[u32; C], horizons: &[usize], start: usize, md:
|
||||
for (n, b) in ANY_NAMES.iter().zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
for (n, b) in REL_NAMES.iter().chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_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).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).chain(AFF_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
writeln!(md)?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
+70
-1
@@ -81,6 +81,12 @@ pub struct Panel {
|
||||
pub listed_on: Vec<Vec<NaiveDate>>,
|
||||
/// New Music Friday Denmark placements per artist: (Friday, position if known), sorted.
|
||||
pub nmf_dk: Vec<Vec<(NaiveDate, Option<i64>)>>,
|
||||
/// All-time DR plays per station group (P3, P4, P5, P6, P8) from the artist page's most played
|
||||
/// tracks (P4: the national count, or the regional average when only regions are given).
|
||||
pub alltime: Vec<[f64; GROUPS]>,
|
||||
/// How much DR plays the artist's genre on each station group, relative to all music: ln of
|
||||
/// (genre's share of plays on the group / the group's share of all plays); 0 = genre unknown.
|
||||
pub genre_fit: Vec<[f64; GROUPS]>,
|
||||
/// Season index per ISO week (1..=53): new songs reaching the Airplay Top-20 that week of the
|
||||
/// year, relative to an average week, 2002–2024 (before the DR panel), smoothed over 5 weeks.
|
||||
pub season: [f64; 54],
|
||||
@@ -151,7 +157,8 @@ impl Panel {
|
||||
let (archive_rate, listed_on) = load_long_history(db, &id_of, &names, artists.len())?;
|
||||
let nmf_dk = load_nmf_dk(db, &id_of, &artists)?;
|
||||
let season = load_season(db)?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season })
|
||||
let (alltime, genre_fit) = load_affinity(db, &id_of, artists.len())?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season, alltime, genre_fit })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,3 +343,65 @@ fn load_season(db: &Db) -> Result<[f64; 54]> {
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Broad genre from DR's description ("Sanger (Afrobeats/Pop/R&B)" -> the first genre that maps).
|
||||
pub fn genre_bucket(description: &str) -> Option<usize> {
|
||||
let inner = description.split('(').nth(1)?.split(')').next()?;
|
||||
for tok in inner.split('/') {
|
||||
let t = tok.trim().to_lowercase();
|
||||
let b = if ["hip hop", "hiphop", "rap", "grime", "trap", "drill"].iter().any(|k| t.contains(k)) { 1 }
|
||||
else if ["jazz", "fusion", "bebop", "swing"].iter().any(|k| t.contains(k)) { 4 }
|
||||
else if ["klassisk", "filmmusik", "musical", "opera", "kor"].iter().any(|k| t.contains(k)) { 7 }
|
||||
else if ["r&b", "soul", "funk", "gospel", "afrobeat", "dancehall", "reggae"].iter().any(|k| t.contains(k)) { 5 }
|
||||
else if ["house", "techno", "elektronisk", "electronica", "electro", "dubstep", "dance", "disco"].iter().any(|k| t.contains(k)) { 3 }
|
||||
else if ["rock", "punk", "metal", "indie", "new wave", "blues", "grunge"].iter().any(|k| t.contains(k)) { 2 }
|
||||
else if ["folk", "country", "vise", "singer"].iter().any(|k| t.contains(k)) { 6 }
|
||||
else if ["latin", "world", "verdens"].iter().any(|k| t.contains(k)) { 8 }
|
||||
else if ["pop", "dansktop", "schlager", "underholdning"].iter().any(|k| t.contains(k)) { 0 }
|
||||
else { continue };
|
||||
return Some(b);
|
||||
}
|
||||
None
|
||||
}
|
||||
const GENRES: usize = 9;
|
||||
|
||||
fn load_affinity(db: &Db, id_of: &HashMap<String, usize>, n: usize) -> Result<(Vec<[f64; GROUPS]>, Vec<[f64; GROUPS]>)> {
|
||||
let mut alltime = vec![[0.0f64; GROUPS]; n];
|
||||
let mut genre: Vec<Option<usize>> = vec![None; n];
|
||||
for r in db.conn.prepare("SELECT artist_id, raw FROM dr_artists")?.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? {
|
||||
let (id, raw) = r?;
|
||||
let Some(&a) = id_of.get(&id) else { continue };
|
||||
let v: Value = serde_json::from_str(&raw).unwrap_or(Value::Null);
|
||||
genre[a] = v["artist"]["description"].as_str().and_then(genre_bucket);
|
||||
let (mut p4_nat, mut p4_reg, mut n_reg) = (0.0, 0.0, 0.0);
|
||||
for t in v["mostPlayed"].as_array().into_iter().flatten() {
|
||||
for s in t["statisticsByChannels"].as_array().into_iter().flatten() {
|
||||
let cnt = s["count"].as_f64().unwrap_or(0.0);
|
||||
match s["channel"]["slug"].as_str().unwrap_or("") {
|
||||
"p3" => alltime[a][0] += cnt, "p5" => alltime[a][2] += cnt, "p6beat" => alltime[a][3] += cnt, "p8jazz" => alltime[a][4] += cnt,
|
||||
"p4" => p4_nat += cnt,
|
||||
s if s.starts_with("p4") => { p4_reg += cnt; n_reg += 1.0; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
alltime[a][1] = p4_nat.max(if n_reg > 0.0 { p4_reg / 10.0 } else { 0.0 });
|
||||
}
|
||||
// Genre x station group: share of the genre's plays on each group vs the group's share overall.
|
||||
let mut by = [[0.0f64; GROUPS]; GENRES];
|
||||
let mut all = [0.0f64; GROUPS];
|
||||
for a in 0..n {
|
||||
for g in 0..GROUPS { all[g] += alltime[a][g]; if let Some(b) = genre[a] { by[b][g] += alltime[a][g]; } }
|
||||
}
|
||||
let tot: f64 = all.iter().sum();
|
||||
let mut fit = vec![[0.0f64; GROUPS]; n];
|
||||
for a in 0..n {
|
||||
let Some(b) = genre[a] else { continue };
|
||||
let tb: f64 = by[b].iter().sum();
|
||||
if tb <= 0.0 || tot <= 0.0 { continue; }
|
||||
for g in 0..GROUPS {
|
||||
fit[a][g] = (((by[b][g] + 1.0) / (tb + 5.0)) / ((all[g] + 1.0) / (tot + 5.0))).ln().clamp(-4.0, 2.0);
|
||||
}
|
||||
}
|
||||
Ok((alltime, fit))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user