Count the ten regional P4s as one station in the features; cap P(add on any station) at what the stations allow together (1 - prod(1 - p) over P3, P4, P5, P6, P8)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 04:42:26 +00:00
co-authored by Claude Opus 5.5
parent c114da9a63
commit a357a9b7e4
4 changed files with 51 additions and 10 deletions
+21 -9
View File
@@ -19,7 +19,7 @@
use super::common::{mix, Rng};
use super::features::{self, LAGS, P};
use super::glm::{self, Design};
use super::panel::{Panel, C};
use super::panel::{group, Panel, C, GROUPS};
use anyhow::Result;
use std::fmt::Write as _;
@@ -84,7 +84,7 @@ pub fn p_release(p: &Panel, a: usize, t: usize) -> Option<f64> {
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)",
pub const EXTRA: &[&str] = &["in rotation on channel now", "in rotation anywhere now", "station groups in rotation now (/5)",
"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)"];
@@ -93,7 +93,7 @@ pub const PC: usize = P + 10 + REL + WAVE;
/// Artist-level ("any channel") rows.
pub const ANY_NAMES: &[&str] = &[
"intercept", "log DR plays t", "log DR plays 4w", "log DR plays 12w", "log max DR plays 12w",
"in rotation anywhere now", "channels in rotation now (/14)", "current run anywhere (/12)", "weeks since in rotation anywhere (/12)",
"in rotation anywhere now", "station groups in rotation now (/5)", "current run anywhere (/12)", "weeks since in rotation anywhere (/12)",
"Track Top-40 score", "Airplay Top-20 score", "chart entry recency 6w", "chart entry recency 26w",
"log Track Top-40 hits", "log Airplay Top-20 hits", "Danish", "group", "female", "career years (/50)", "debut unknown",
"Christmas in window",
@@ -131,6 +131,13 @@ impl Rot {
pub fn add_any_in_window(&self, a: usize, t: usize, h: usize) -> bool { (t + h + 1..=t + h + W).any(|w| self.add_any(a, w)) }
}
/// Station groups (P4 once) where the artist is in rotation in week t.
fn groups_on(r: &Rot, a: usize, t: usize) -> f64 {
let mut on = [false; GROUPS];
for c in 0..C { if r.on(a, c, t) { on[group(c)] = true; } }
on.iter().filter(|x| **x).count() as f64
}
fn tau_of(_p: &Panel, r: &Rot, _a: usize, c: usize, _t: usize) -> f64 { r.tau[c] as f64 }
fn run_and_since(flag: impl Fn(usize) -> bool, t: usize) -> (f64, f64) {
@@ -144,13 +151,13 @@ fn christmas_in_window(p: &Panel, t: usize, h: usize) -> bool { (t + h + 1..=t +
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;
let n_on = groups_on(r, a, t);
let (run, since) = run_and_since(|w| r.on(a, c, w), t);
let weeks = (run * LAGS as f64).round() as usize;
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..P + 10].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 / GROUPS 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..P + 10 + REL].copy_from_slice(&rel_feats(p, a, t, h, rel, on));
@@ -158,10 +165,10 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, r
}
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 tot = |w: usize| p.group_total(a, w, None);
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);
let n_on = (0..C).filter(|&k| r.on(a, k, t)).count() as f64;
let n_on = groups_on(r, a, t);
let (run, since) = run_and_since(|w| r.any(a, w), t);
let ch = p.chart(a, t);
let art = &p.artists[a];
@@ -169,7 +176,7 @@ pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, rel: Option<boo
let year = chrono::Datelike::year(&p.weeks[t]);
let v = [
1.0, tot(t).ln_1p(), mean(4).ln_1p(), mean(12).ln_1p(), max12.ln_1p(),
r.any(a, t) as i32 as f64, n_on / C as f64, run, since,
r.any(a, t) as i32 as f64, n_on / GROUPS as f64, run, since,
if ch.t40_rank > 0 { (41.0 - ch.t40_rank as f64) / 40.0 } else { 0.0 },
if ch.air_rank > 0 { (21.0 - ch.air_rank as f64) / 20.0 } else { 0.0 },
recency(6.0), recency(26.0), (ch.t40_hits as f64).ln_1p(), (ch.air_hits as f64).ln_1p(),
@@ -268,7 +275,12 @@ impl AddModel {
let mut xa = vec![0.0; PA];
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))))
// Coherence: "any station" can't exceed what the stations allow together (P4 counted once,
// as its best region): at most 1 - prod(1 - p) over the station groups.
let mut best = [0.0f64; GROUPS];
for c in 0..C { best[group(c)] = best[group(c)].max(pc[c]); }
let cap = 1.0 - best.iter().map(|p| 1.0 - p).product::<f64>();
(pc, platt(self.platt_any, glm::sigmoid(glm::dot(&self.any, &xa))).min(cap.max(1e-6)))
}
}
+2 -1
View File
@@ -35,7 +35,8 @@ pub fn christmas(p: &Panel, target: usize) -> bool {
pub fn row(p: &Panel, a: usize, c: usize, t: usize, target: usize, out: &mut [f64]) {
debug_assert!(t + 1 >= LAGS && out.len() == P);
let y = |w: usize| p.y(a, c, w);
let others = |w: usize| (0..C).filter(|&k| k != c).map(|k| p.y(a, k, w)).sum::<f64>();
// other station groups (the regional P4s count once)
let others = |w: usize| p.group_total(a, w, Some(super::panel::group(c)));
let mean = |f: &dyn Fn(usize) -> f64, n: usize| (0..n).map(|i| f(t - i)).sum::<f64>() / n as f64;
let max12 = (0..LAGS).map(|i| y(t - i)).fold(0.0, f64::max);
let since = (0..LAGS).find(|&i| y(t - i) > 0.0).unwrap_or(LAGS) as f64 / LAGS as f64;
+18
View File
@@ -20,6 +20,24 @@ pub const C: usize = CHANNELS.len();
pub fn is_p4(c: usize) -> bool { CHANNELS[c].starts_with("p4") }
/// Station groups: P3, P4 (the ten regional P4s broadcast mostly the same national programme,
/// so they count as one), P5, P6 Beat, P8 Jazz.
pub const GROUPS: usize = 5;
pub fn group(c: usize) -> usize { match CHANNELS[c] { "p3" => 0, "p5" => 2, "p6beat" => 3, "p8jazz" => 4, _ => 1 } }
impl Panel {
/// The artist's plays in week w across station groups (P4 = the mean of the regions), optionally
/// leaving out one group.
pub fn group_total(&self, a: usize, w: usize, skip: Option<usize>) -> f64 {
let (mut p4, mut n4, mut tot) = (0.0, 0.0, 0.0);
for c in 0..C {
if Some(group(c)) == skip { continue; }
if is_p4(c) { p4 += self.y(a, c, w); n4 += 1.0; } else { tot += self.y(a, c, w); }
}
tot + if n4 > 0.0 { p4 / n4 } else { 0.0 }
}
}
pub struct Artist {
pub id: String,
pub name: String,