Next week page: 1-week add forecast (daily run, isotonic-calibrated), /api/nextweek with live plays and track record, new tab; nextweek backtest

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 12:16:20 +00:00
co-authored by Claude Opus 5.5
parent c61800a51f
commit 2e31b1dc49
6 changed files with 402 additions and 1 deletions
+24
View File
@@ -0,0 +1,24 @@
# Backtest: who is added next week
A 1-week window starting 3 weeks after the latest complete DR week (the live page's lag), 21 forecast weeks from 2026-05-18. Release chance blended in as on the site; channels capped at the any-channel chance.
- any: 183391 rows, 0.29% added, log loss 0.0153, ROC-AUC 0.912, PR-AUC 0.042
| Predicted | Rows | Mean predicted | Happened |
|---|---|---|---|
| 0-5% | 181885 | 0.2% | 0.2% |
| 5-10% | 1121 | 6.9% | 6.9% |
| 10-20% | 337 | 13.0% | 5.6% |
| 20-30% | 39 | 23.4% | 7.7% |
| 30-50% | 9 | 35.7% | 11.1% |
- ch: 2567474 rows, 0.05% added, log loss 0.0028, ROC-AUC 0.979, PR-AUC 0.045
| Predicted | Rows | Mean predicted | Happened |
|---|---|---|---|
| 0-5% | 2563814 | 0.0% | 0.0% |
| 5-10% | 2810 | 6.8% | 6.9% |
| 10-20% | 765 | 12.9% | 8.6% |
| 20-30% | 82 | 22.3% | 15.9% |
| 30-50% | 3 | 39.5% | 33.3% |
+77
View File
@@ -487,6 +487,8 @@ impl AddModel {
/// on windows closed before the last CAL_ORIGINS origins predicts those origins, and a Platt fit /// on windows closed before the last CAL_ORIGINS origins predicts those origins, and a Platt fit
/// maps its probabilities to what happened. (The raw fits were overconfident above ~0.4.) /// maps its probabilities to what happened. (The raw fits were overconfident above ~0.4.)
pub fn fit(p: &Panel, r: &Rot, h: usize, last_week: usize) -> AddModel { Self::fit_with(p, r, h, last_week, false, W) } pub fn fit(p: &Panel, r: &Rot, h: usize, last_week: usize) -> AddModel { Self::fit_with(p, r, h, last_week, false, W) }
/// The general add model for a window of `win` weeks (the "next week" page uses 1).
pub fn fit_win(p: &Panel, r: &Rot, h: usize, last_week: usize, win: usize) -> AddModel { Self::fit_with(p, r, h, last_week, false, win) }
/// The release-event model: "if the artist releases a new song in week t+1, will DR add it /// The release-event model: "if the artist releases a new song in week t+1, will DR add it
/// within 8 weeks?" Trained only on artist-weeks where the artist did release in week t+1, with /// within 8 weeks?" Trained only on artist-weeks where the artist did release in week t+1, with
@@ -1056,3 +1058,78 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, win: usize, md:
for (n, b) in ANY_NAMES.iter().chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; } for (n, b) in ANY_NAMES.iter().chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; }
Ok(rows) Ok(rows)
} }
/// Bins for the "next week" track record (upper edges).
pub const NEXT_BINS: [f64; 7] = [0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0];
/// Walk-forward test of the "next week" forecast: a 1-week window starting h + 1 weeks after the
/// origin (the page's lag between the latest complete DR week and next week), the release chance
/// blended in as on the site, each channel capped at the any-channel chance. Writes calibration bins
/// and scores as (model, h, metric, value) rows for the site's track record.
pub fn backtest_next(db: &crate::db::Db, p: &Panel, tau: &[u32; C], start: usize, h: usize, md: &mut String) -> Result<Vec<(String, usize, String, f64)>> {
let r = Rot::new(p, tau);
// (origin, raw, outcome); calibrated pairs scored as the site would have shown them.
let (mut pool_any, mut pool_ch): (Vec<(usize, f64, bool)>, Vec<(usize, f64, bool)>) = (Vec::new(), Vec::new());
let (mut raw_any, mut cal_any, mut cal_ch): (Vec<(f64, bool)>, Vec<(f64, bool)>, Vec<(f64, bool)>) = (Vec::new(), Vec::new(), Vec::new());
let last = p.w().saturating_sub(h + 3).min(TEST_END.get().map_or(usize::MAX, |e| *e));
let mut m: Option<AddModel> = None;
let mut origins = 0;
let fit = |pool: &[(usize, f64, bool)], t0: usize| -> Calib {
let pairs: Vec<(f64, bool)> = pool.iter().filter(|(o, _, _)| o + h + 2 <= t0).map(|(_, q, y)| (*q, *y)).collect();
Calib::isotonic(&pairs)
};
for t0 in start..=last {
if (t0 - start) % 2 == 0 || m.is_none() { m = Some(AddModel::fit_win(p, &r, h, t0, 1)); }
let mm = m.as_ref().unwrap();
let (ca, cc) = (fit(&pool_any, t0), fit(&pool_ch, t0));
for a in 0..p.a() {
if !p.known(a, t0) { continue; }
let (pc1, pa1) = mm.predict(p, &r, a, t0, Scenario::new(true, false), None);
let (pc0, pa0) = mm.predict(p, &r, a, t0, Scenario::new(false, false), None);
let pr = p_release(p, a, t0);
let mix = |x1: f64, x0: f64| match pr { Some(q) => q * x1 + (1.0 - q) * x0, None => x0 };
let (pa, ya) = (mix(pa1, pa0), r.add_any_in(a, t0, h, 1));
let pa_c = ca.apply(pa);
raw_any.push((pa, ya)); cal_any.push((pa_c, ya)); pool_any.push((t0, pa, ya));
for c in 0..C {
let (pc, yc) = (mix(pc1[c], pc0[c]), r.add_in(a, c, t0, h, 1));
cal_ch.push((cc.apply(pc).min(pa_c), yc)); pool_ch.push((t0, pc, yc));
}
}
origins += 1;
eprintln!("next-week backtest: origin {} done", p.weeks[t0]);
}
// The maps the daily run applies: isotonic on every forecast in the test.
let all = |pool: &[(usize, f64, bool)]| Calib::isotonic(&pool.iter().map(|(_, q, y)| (*q, *y)).collect::<Vec<_>>());
db.conn.execute_batch("CREATE TABLE IF NOT EXISTS calibration_maps (name TEXT PRIMARY KEY, knots TEXT NOT NULL, made_at TEXT NOT NULL);")?;
for (name, cal) in [("next_any", all(&pool_any)), ("next_ch", all(&pool_ch))] {
if let Calib::Iso(k) = cal {
db.conn.execute("INSERT OR REPLACE INTO calibration_maps VALUES (?1, ?2, ?3)", rusqlite::params![name, serde_json::to_string(&k)?, chrono::Utc::now().to_rfc3339()])?;
}
}
let mut rows = Vec::new();
writeln!(md, "# Backtest: who is added next week\n")?;
writeln!(md, "A 1-week window starting {} weeks after the latest complete DR week (the live page's lag), {origins} forecast weeks from {}. Release chance blended in as on the site; channels capped at the any-channel chance. \"Calibrated\" = an isotonic map refitted each week on earlier weeks' forecasts only (what the site would have shown); the final map is stored for the daily run.\n", h + 1, p.weeks[start])?;
for (label, v) in [("raw_any", &raw_any), ("any", &cal_any), ("ch", &cal_ch)] {
let n = v.len() as f64;
let rate = v.iter().filter(|x| x.1).count() as f64 / n.max(1.0);
let ll = v.iter().map(|(q, y)| ll(q.clamp(1e-6, 1.0 - 1e-6), *y)).sum::<f64>() / n.max(1.0);
let (roc, pr) = (roc_auc(v.clone()), average_precision(v.clone()));
writeln!(md, "- {label}: {} rows, {:.2}% added, log loss {ll:.4}, ROC-AUC {roc:.3}, PR-AUC {pr:.3}", v.len(), 100.0 * rate)?;
for (m, x) in [("rate", rate), ("logloss", ll), ("roc", roc), ("prauc", pr), ("n", n), ("origins", origins as f64)] { rows.push(("next_week".to_string(), h, format!("{label}_{m}"), x)); }
writeln!(md, "\n| Predicted | Rows | Mean predicted | Happened |\n|---|---|---|---|")?;
let mut lo = 0.0;
for (i, hi) in NEXT_BINS.iter().enumerate() {
let b: Vec<&(f64, bool)> = v.iter().filter(|x| x.0 >= lo && x.0 < *hi).collect();
let k = b.len() as f64;
if k > 0.0 {
let (mp, ob) = (b.iter().map(|x| x.0).sum::<f64>() / k, b.iter().filter(|x| x.1).count() as f64 / k);
writeln!(md, "| {:.0}-{:.0}% | {} | {:.1}% | {:.1}% |", 100.0 * lo, 100.0 * hi, b.len(), 100.0 * mp, 100.0 * ob)?;
for (m, x) in [("n", k), ("pred", mp), ("obs", ob)] { rows.push(("next_week".to_string(), h, format!("{label}_bin{i}_{m}"), x)); }
}
lo = *hi;
}
writeln!(md)?;
}
Ok(rows)
}
+17
View File
@@ -11,6 +11,7 @@
//! airplay model adds [--h 0,4] [--out docs/BACKTEST-ADDS.md] backtest of the new-add model //! airplay model adds [--h 0,4] [--out docs/BACKTEST-ADDS.md] backtest of the new-add model
//! airplay model long [--tau 20] [--w 2] [--stay 3] [--out docs/LONG.md] 2019– any-DR model, all years vs 1 year //! airplay model long [--tau 20] [--w 2] [--stay 3] [--out docs/LONG.md] 2019– any-DR model, all years vs 1 year
//! airplay model comebacks [--tau 20] [--out docs/COMEBACKS.md] //! airplay model comebacks [--tau 20] [--out docs/COMEBACKS.md]
//! airplay model nextweek [--h 2] the "next week" page's 1-week forecast, walk-forward
//! airplay model rank [--h 4] [--max-h 12] [--top 50] //! airplay model rank [--h 4] [--max-h 12] [--top 50]
pub mod addcheck; pub mod addcheck;
@@ -116,6 +117,22 @@ pub fn run(db: &Db, args: &[String]) -> Result<()> {
if let Some(out) = arg(args, "--out") { std::fs::write(out, &md)?; } if let Some(out) = arg(args, "--out") { std::fs::write(out, &md)?; }
print!("{md}"); print!("{md}");
} }
Some("nextweek") => {
// The "next week" page's forecast, tested walk-forward; scores stored for the site.
let h: usize = arg(args, "--h").and_then(|s| s.parse().ok()).unwrap_or(2);
let tau = common::thresholds(&p, 26);
let start = test_start(&p, args);
let mut md = String::new();
let rows = adds::backtest_next(db, &p, &tau, start, h, &mut md)?;
rank::create_tables(db)?;
let at = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
for (m, h, metric, v) in &rows {
db.conn.execute("INSERT OR REPLACE INTO backtest_scores VALUES (?1, ?2, ?3, ?4, ?5)", rusqlite::params![at, m, *h as i64, metric, v])?;
}
let out = arg(args, "--out").unwrap_or_else(|| "docs/BACKTEST-NEXTWEEK.md".into());
std::fs::write(&out, &md)?;
print!("{md}");
}
Some("leakcheck") => { print!("{}", leakcheck::run(&p)?); } Some("leakcheck") => { print!("{}", leakcheck::run(&p)?); }
Some("addcheck") => { Some("addcheck") => {
if let Some(r) = arg(args, "--rule") { let _ = adds::ADD_RULE.set(r); } if let Some(r) = arg(args, "--rule") { let _ = adds::ADD_RULE.set(r); }
+37
View File
@@ -160,6 +160,43 @@ pub fn run(db: &Db, p: &Panel, max_h: usize, show_h: usize) -> Result<Vec<Line>>
tx.commit()?; tx.commit()?;
eprintln!("rank: adds h={h} stored"); eprintln!("rank: adds h={h} stored");
} }
// Next week (Monday to Sunday after today): any add in that one week ('add_next' per channel,
// 'add_next_any'), the general model with a 1-week window at the horizon that reaches it.
{
let today = Utc::now().date_naive();
let next_monday = today + Duration::days(7 - chrono::Datelike::weekday(&today).num_days_from_monday() as i64);
let h_next = ((next_monday - origin).num_days() / 7 - 1).max(0) as usize;
let m = AddModel::fit_win(p, &rot, h_next, t0, 1);
// The isotonic maps from `airplay model nextweek` (the raw 1-week chances were overconfident above ~10%).
let map = |name: &str| -> Option<super::calib::Calib> {
let k: String = db.conn.query_row("SELECT knots FROM calibration_maps WHERE name = ?1", [name], |r| r.get(0)).ok()?;
Some(super::calib::Calib::Iso(serde_json::from_str(&k).ok()?))
};
let (cal_any, cal_ch) = (map("next_any"), map("next_ch"));
let tx = db.conn.unchecked_transaction()?;
{
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 (pc1, pa1) = m.predict(p, &rot, a, t0, Scenario::new(true, false), None);
let (pc0, pa0) = m.predict(p, &rot, a, t0, Scenario::new(false, 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 pa = mix(pa1, pa0);
let pa = cal_any.as_ref().map_or(pa, |k| k.apply(pa));
ins.execute(params![made, VERSION, "add_next_any", format!("{id}:any"), h_next as i64, origin.to_string(), next_monday.to_string(), pa, none, none, none])?;
for c in 0..C {
let pc = mix(pc1[c], pc0[c]);
let pc = cal_ch.as_ref().map_or(pc, |k| k.apply(pc)).min(pa);
if pc < 0.002 { continue; }
ins.execute(params![made, VERSION, "add_next", format!("{id}:{}", CHANNELS[c]), h_next as i64, origin.to_string(), next_monday.to_string(), pc, none, none, none])?;
}
}
}
tx.commit()?;
eprintln!("rank: next week ({next_monday}, h={h_next}) stored");
}
// Where each artist stands in the origin week: prob = 1 if in rotation, mean = plays. // Where each artist stands in the origin week: prob = 1 if in rotation, mean = plays.
let tx = db.conn.unchecked_transaction()?; let tx = db.conn.unchecked_transaction()?;
{ {
+106
View File
@@ -4,6 +4,7 @@
//! {prefix}/api/meta latest forecast run, horizons, channels, backtest scores //! {prefix}/api/meta latest forecast run, horizons, channels, backtest scores
//! {prefix}/api/rank?h=4 every artist's forecast for one horizon //! {prefix}/api/rank?h=4 every artist's forecast for one horizon
//! {prefix}/api/artist?id= one artist: profile, 51 weeks of plays per channel, forecasts, chart runs //! {prefix}/api/artist?id= one artist: profile, 51 weeks of plays per channel, forecasts, chart runs
//! {prefix}/api/nextweek who DR adds next week: 1-week forecast, live plays, track record
//! {prefix}/api/whatif?id=&h=&entry=&t40=&air= //! {prefix}/api/whatif?id=&h=&entry=&t40=&air=
//! re-run the model for one artist with a changed chart situation //! re-run the model for one artist with a changed chart situation
//! //!
@@ -181,10 +182,115 @@ fn handle(st: &State, method: &Method, url: &str) -> Result<Reply> {
"/api/bestweek" => bestweek(st, &q), "/api/bestweek" => bestweek(st, &q),
"/api/artist" => artist(st, q.get("id").map(String::as_str).unwrap_or("")), "/api/artist" => artist(st, q.get("id").map(String::as_str).unwrap_or("")),
"/api/whatif" => whatif(st, &q), "/api/whatif" => whatif(st, &q),
"/api/nextweek" => nextweek(st),
_ => Ok((404, "text/plain", Arc::new("not found".into()))), _ => Ok((404, "text/plain", Arc::new("not found".into()))),
} }
} }
/// The station group a channel belongs to, as shown on the page.
fn group_label(ch: &str) -> &'static str {
match ch { "p3" => "P3", "p5" => "P5", "p6beat" => "P6", "p8jazz" => "P8", c if c.starts_with("p4") => "P4", _ => "" }
}
/// "Who DR adds next week": the stored 1-week forecast for next Monday-Sunday ('add_next_any',
/// 'add_next'; channels capped at the any-channel chance), this week's and last week's plays from
/// the play-by-play collector (with the song), rotation thresholds, Christmas flags and the
/// walk-forward track record of exactly this forecast.
fn nextweek(st: &State) -> Result<Reply> {
use chrono::Datelike;
let c = ro(&st.db_path)?;
let made: Option<String> = c.query_row("SELECT max(made_at) FROM forecasts WHERE model_version = ?1 AND target = 'add_next_any'", [VERSION], |r| r.get(0)).optional()?.flatten();
let Some(made) = made else { return ok(json!({"ready": false})) };
let (origin, week, h): (String, String, i64) = c.query_row(
"SELECT origin_week, target_week, horizon FROM forecasts WHERE made_at = ?1 AND target = 'add_next_any' LIMIT 1", [&made], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))?;
let mut names: HashMap<String, (String, bool)> = HashMap::new();
for r in c.prepare("SELECT artist_id, name, json_extract(raw, '$.artist.country') FROM dr_artists")?
.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<String>>(2)?)))? {
let (id, n, country) = r?;
names.insert(id, (n, country.as_deref() == Some("Danmark")));
}
let mut p_any: HashMap<String, f64> = HashMap::new();
let mut p_ch: HashMap<String, HashMap<String, f64>> = HashMap::new();
let mut now: HashMap<String, HashMap<String, f64>> = HashMap::new();
for r in c.prepare("SELECT key, target, prob, mean FROM forecasts WHERE made_at = ?1 AND model_version = ?2 AND target IN ('add_next_any', 'add_next', 'now')")?
.query_map(params![made, VERSION], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<f64>>(2)?, r.get::<_, Option<f64>>(3)?)))? {
let (key, target, prob, mean) = r?;
let Some((id, ch)) = key.split_once(':') else { continue };
match target.as_str() {
"add_next_any" => { p_any.insert(id.to_string(), prob.unwrap_or(0.0)); }
"add_next" => { p_ch.entry(id.to_string()).or_default().insert(ch.to_string(), prob.unwrap_or(0.0)); }
_ => { now.entry(id.to_string()).or_default().insert(ch.to_string(), mean.unwrap_or(0.0)); }
}
}
let tau: HashMap<String, f64> = c.prepare("SELECT channel, tau FROM rotation_thresholds")?.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, f64>(1)?)))?.collect::<Result<_, _>>()?;
// Live plays: last week and this week so far, per artist and station, and the most played song this week.
let now_t = chrono::Utc::now();
let today = now_t.date_naive();
let this_monday = today - chrono::Duration::days(today.weekday().num_days_from_monday() as i64);
let last_monday = this_monday - chrono::Duration::weeks(1);
let elapsed = ((now_t - this_monday.and_hms_opt(0, 0, 0).unwrap().and_utc()).num_minutes() as f64 / (7.0 * 1440.0)).clamp(0.05, 1.0);
let mut live: HashMap<String, HashMap<String, (i64, i64)>> = HashMap::new();
let mut songs: HashMap<String, HashMap<String, i64>> = HashMap::new();
for r in c.prepare("SELECT artist_ids, station, played_at, title FROM plays WHERE source = 'dr' AND played_at >= ?1")?
.query_map([last_monday.to_string()], |r| Ok((r.get::<_, Option<String>>(0)?, r.get::<_, String>(1)?, r.get::<_, String>(2)?, r.get::<_, String>(3)?)))? {
let (ids, station, at, title) = r?;
let this = at.as_str() >= this_monday.to_string().as_str();
for id in ids.unwrap_or_default().split(',').filter(|x| !x.is_empty()) {
let e = live.entry(id.to_string()).or_default().entry(station.clone()).or_insert((0, 0));
if this { e.1 += 1; *songs.entry(id.to_string()).or_default().entry(title.clone()).or_insert(0) += 1; } else { e.0 += 1; }
}
}
let xmas: std::collections::HashSet<String> = st.models.read().unwrap().as_ref()
.map(|m| (0..m.panel.a()).filter(|&a| m.panel.xmas[a]).map(|a| m.panel.artists[a].id.clone()).collect()).unwrap_or_default();
let christmas_title = |t: &str| { let l = t.to_lowercase(); ["jul", "christmas", "xmas", "navidad", "santa", "sleigh", "mistletoe", "nisse", "snow", "winter wonderland"].iter().any(|k| l.contains(k)) };
// Expected number of adds next week and a 90% range (sum of independent chances).
let total: f64 = p_any.values().sum();
let sd = p_any.values().map(|p| p * (1.0 - p)).sum::<f64>().sqrt();
let groups = ["P3", "P4", "P5", "P6", "P8"];
let artist = |id: &str, pa: f64| -> Value {
let (n, dk) = names.get(id).cloned().unwrap_or_default();
let chs = p_ch.get(id).cloned().unwrap_or_default();
let mut by_group = serde_json::Map::new();
for g in groups {
let best = chs.iter().filter(|(ch, _)| group_label(ch) == g).max_by(|a, b| a.1.total_cmp(b.1));
if let Some((ch, pc)) = best {
let lv = live.get(id).and_then(|l| l.get(ch)).copied().unwrap_or((0, 0));
by_group.insert(g.into(), json!({"ch": ch, "p": (pc.min(pa) * 1000.0).round() / 1000.0, "tau": tau.get(ch), "origin_plays": now.get(id).and_then(|n| n.get(ch)),
"last_week": lv.0, "this_week": lv.1, "pace": (lv.1 as f64 / elapsed).round()}));
}
}
let song = songs.get(id).and_then(|s| s.iter().max_by_key(|x| x.1)).map(|(t, k)| json!({"title": t, "plays": k}));
let seasonal = xmas.contains(id) || song.as_ref().and_then(|s| s["title"].as_str()).is_some_and(christmas_title);
json!({"id": id, "name": n, "danish": dk, "p": (pa * 1000.0).round() / 1000.0, "groups": by_group, "song": song, "seasonal": seasonal})
};
let mut ranked: Vec<(&String, &f64)> = p_any.iter().collect();
ranked.sort_by(|a, b| b.1.total_cmp(a.1));
let list: Vec<Value> = ranked.iter().take_while(|(_, p)| **p >= 0.03).take(80).map(|(id, p)| artist(id, **p)).collect();
// The add board: per station group, the most likely adds (channel chance capped at "any").
let mut boards = serde_json::Map::new();
for g in groups {
let mut v: Vec<(f64, &String)> = p_ch.iter().filter_map(|(id, chs)| {
let pa = *p_any.get(id)?;
chs.iter().filter(|(ch, _)| group_label(ch) == g).map(|(_, p)| p.min(pa)).fold(None, |m: Option<f64>, x| Some(m.map_or(x, |m| m.max(x)))).map(|p| (p, id))
}).collect();
v.sort_by(|a, b| b.0.total_cmp(&a.0));
boards.insert(g.into(), json!(v.iter().take(6).map(|(p, id)| json!({"id": id, "name": names.get(*id).map(|n| n.0.clone()), "p": (p * 1000.0).round() / 1000.0})).collect::<Vec<_>>()));
}
// Track record: the latest walk-forward run of this forecast.
let mut track = serde_json::Map::new();
let run: Option<String> = c.query_row("SELECT max(run_at) FROM backtest_scores WHERE model = 'next_week'", [], |r| r.get(0)).optional()?.flatten();
if let Some(run) = &run {
for r in c.prepare("SELECT metric, value FROM backtest_scores WHERE model = 'next_week' AND run_at = ?1")?.query_map([run], |r| Ok((r.get::<_, String>(0)?, r.get::<_, Option<f64>>(1)?)))? {
let (m, v) = r?;
track.insert(m, json!(v));
}
}
let bins: Vec<f64> = crate::model::adds::NEXT_BINS.to_vec();
ok(json!({"ready": true, "made_at": made, "origin_week": origin, "week": week, "horizon": h, "this_monday": this_monday.to_string(),
"elapsed": elapsed, "expected": total, "range": [(total - 1.645 * sd).max(0.0), total + 1.645 * sd],
"list": list, "boards": boards, "track": track, "track_run": run, "bins": bins}))
}
fn meta(st: &State) -> Result<Reply> { fn meta(st: &State) -> Result<Reply> {
let c = ro(&st.db_path)?; let c = ro(&st.db_path)?;
let Some(made) = latest_run(&c)? else { return ok(json!({"ready": false})) }; let Some(made) = latest_run(&c)? else { return ok(json!({"ready": false})) };
+141 -1
View File
@@ -100,6 +100,52 @@ svg text { fill: var(--muted); font-size: 11px; }
.card p { color: var(--muted); } .card p { color: var(--muted); }
.prose { max-width: 760px; } .prose { max-width: 760px; }
.prose li { margin: 6px 0; } .prose li { margin: 6px 0; }
/* next week */
.nw-hero { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; align-items: end; margin-top: 6px; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); font-weight: 700; }
.nw-hero h2 { margin: 4px 0 6px; font-size: 26px; letter-spacing: -.02em; }
.nw-kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.nw-kpis .kpi b { font-size: 26px; }
.nw-kpis .kpi small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.nw-h { margin: 26px 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.board { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.board .col { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); border-top: 4px solid var(--c); }
.board .col h4 { margin: 0 0 8px; font-size: 15px; color: var(--c); }
.board .it { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 6px 0; border-bottom: 1px solid var(--line); cursor: pointer; font-size: 14px; }
.board .it:last-child { border-bottom: 0; }
.board .it i { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--c); opacity: .75; }
.board .it b { font-variant-numeric: tabular-nums; }
.nw-filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.pill { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.pill.on { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.cand { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 14px; }
.cc { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); cursor: pointer; display: flex; flex-direction: column; gap: 10px; }
.cc:hover { border-color: var(--accent); }
.cc .top { display: flex; align-items: flex-start; gap: 10px; }
.cc .nm { font-weight: 700; font-size: 17px; line-height: 1.2; }
.cc .nm small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; margin-top: 2px; }
.cc .big { margin-left: auto; text-align: right; }
.cc .big b { font-size: 30px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.cert { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; padding: 2px 8px; border-radius: 999px; }
.cert.c0 { background: color-mix(in srgb, var(--good) 22%, var(--panel)); color: var(--good); }
.cert.c1 { background: color-mix(in srgb, var(--good) 12%, var(--panel)); color: var(--good); }
.cert.c2 { background: var(--accent-soft); color: var(--accent); }
.cert.c3, .cert.c4 { background: var(--bg); color: var(--muted); }
.dots { display: flex; align-items: center; gap: 4px; }
.dots span { width: 11px; height: 11px; border-radius: 50%; border: 1.5px solid var(--accent); }
.dots span.f { background: var(--accent); }
.dots em { font-style: normal; color: var(--muted); font-size: 13px; margin-left: 8px; }
.chs { display: flex; flex-wrap: wrap; gap: 6px; }
.chs span { font-size: 12px; padding: 2px 8px; border-radius: 6px; background: color-mix(in srgb, var(--c) 14%, var(--panel)); color: var(--ink); }
.live { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.live .st { font-weight: 700; }
.meter { position: relative; height: 8px; border-radius: 4px; background: var(--line); margin: 8px 0 4px; overflow: hidden; }
.meter i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; background: var(--c); }
.meter u { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--ink); }
.tag2 { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }
.track { display: grid; grid-template-columns: 340px 1fr; gap: 18px; align-items: start; }
@media (max-width: 900px) { .nw-hero, .track { grid-template-columns: 1fr; } .board { grid-template-columns: repeat(2, 1fr); } .nw-kpis { grid-template-columns: 1fr 1fr 1fr; } }
@media (max-width: 640px) { .board { grid-template-columns: 1fr; } .nw-kpis { grid-template-columns: 1fr; } .cand { grid-template-columns: 1fr; } }
@media (max-width: 640px) { @media (max-width: 640px) {
header, main { padding-left: 16px; padding-right: 16px; } header, main { padding-left: 16px; padding-right: 16px; }
input[type=range] { width: 100%; } input[type=range] { width: 100%; }
@@ -116,6 +162,7 @@ svg text { fill: var(--muted); font-size: 11px; }
<div class="sub" id="sub">Loading…</div> <div class="sub" id="sub">Loading…</div>
<nav> <nav>
<button class="on" data-tab="rank">Ranking</button> <button class="on" data-tab="rank">Ranking</button>
<button data-tab="next">Next week</button>
<button data-tab="best">Best week</button> <button data-tab="best">Best week</button>
<button data-tab="model">How good is it?</button> <button data-tab="model">How good is it?</button>
</nav> </nav>
@@ -161,6 +208,7 @@ svg text { fill: var(--muted); font-size: 11px; }
<p class="note"><b>New add</b> = if the artist releases a new song in the chosen week, the chance DR adds it within the chosen number of weeks: the artist enters rotation (at or above the station's weekly play threshold) after at least 4 weeks out, or, for artists who are always on, their plays jump well above their own baseline and stay up (that finds 77% of the songs DR's real lists show as new). Learned from past releases with no other release in the 6 weeks before, so a current song's success isn't counted as a new one's; a release less than 6 weeks after the artist's last one gets no estimate ("too soon"). The new song's NMF Denmark placement is weighed by how often the artist's releases were placed. "In rotation now" lists DR stations where the artist is on the list now, then Bauer stations (grey) with 14+ plays in the last 7 days. Click an artist for history and a what-if, or open <b>Best week</b> for their release calendar.</p> <p class="note"><b>New add</b> = if the artist releases a new song in the chosen week, the chance DR adds it within the chosen number of weeks: the artist enters rotation (at or above the station's weekly play threshold) after at least 4 weeks out, or, for artists who are always on, their plays jump well above their own baseline and stay up (that finds 77% of the songs DR's real lists show as new). Learned from past releases with no other release in the 6 weeks before, so a current song's success isn't counted as a new one's; a release less than 6 weeks after the artist's last one gets no estimate ("too soon"). The new song's NMF Denmark placement is weighed by how often the artist's releases were placed. "In rotation now" lists DR stations where the artist is on the list now, then Bauer stations (grey) with 14+ plays in the last 7 days. Click an artist for history and a what-if, or open <b>Best week</b> for their release calendar.</p>
</section> </section>
<section id="tab-next" hidden><div id="nw"><p class="empty">Loading…</p></div></section>
<section id="tab-best" hidden> <section id="tab-best" hidden>
<div class="controls"> <div class="controls">
<div class="ctl">Artist <input type="search" id="bw-q" list="bw-list" placeholder="Type an artist…" autocomplete="off"><datalist id="bw-list"></datalist></div> <div class="ctl">Artist <input type="search" id="bw-q" list="bw-list" placeholder="Type an artist…" autocomplete="off"><datalist id="bw-list"></datalist></div>
@@ -447,6 +495,97 @@ function renderModel() {
: '<p class="note">No backtest stored yet (`airplay model events`).</p>'; : '<p class="note">No backtest stored yet (`airplay model events`).</p>';
} }
// ---------- next week ----------
let nw = null, nwFilter = 'all', nwGroup = 'any';
const GROUP_C = { P3: 'var(--p3)', P4: 'var(--p4)', P5: 'var(--p5)', P6: 'var(--p6)', P8: 'var(--p8)' };
const certainty = p => p >= 0.7 ? [0, 'Very likely'] : p >= 0.4 ? [1, 'Likely'] : p >= 0.2 ? [2, 'Possible'] : p >= 0.1 ? [3, 'Long shot'] : [4, 'Outside chance'];
const oneIn = p => p >= 0.5 ? `${Math.round(p * 10)} in 10` : `1 in ${Math.max(2, Math.round(1 / p))}`;
const fmtLong = s => new Date(s + 'T00:00:00Z').toLocaleDateString('en-GB', { day: 'numeric', month: 'long', timeZone: 'UTC' });
const plusDays = (s, d) => { const x = new Date(s + 'T00:00:00Z'); x.setUTCDate(x.getUTCDate() + d); return x.toISOString().slice(0, 10); };
async function loadNext() {
if (nw) return renderNext();
const r = await fetch(`${BASE}/api/nextweek`); nw = await r.json();
renderNext();
}
// What this week's plays say, for the artist's most likely station group.
function liveLine(g) {
if (!g || !g.tau) return '';
const tau = g.tau, pace = g.pace, wk = g.this_week, lw = g.last_week;
const [cls, text] = lw >= tau ? ['up', 'Already in rotation last week'] : pace >= tau ? ['up', 'Crossing the line this week'] : pace >= 0.6 * tau ? ['', 'Building'] : wk > 0 ? ['', 'Some plays so far'] : ['down', 'No plays yet this week'];
const w = Math.min(100, 100 * pace / (tau * 1.5)), m = 100 / 1.5;
return `<div class="live" style="--c:${GROUP_C[g.group]}"><span class="st ${cls}">${text}</span> on ${g.group}: ${wk} plays so far this week, on pace for ${pace}; last week ${lw}. Rotation line ${tau}.
<div class="meter"><i style="width:${w}%"></i><u style="left:${m}%" title="rotation line"></u></div></div>`;
}
function renderNext() {
const d = nw;
if (!d || !d.ready) { $('nw').innerHTML = '<p class="empty">The next-week forecast is produced by the daily run; it is not there yet.</p>'; return; }
const end = plusDays(d.week, 6), t = d.track || {};
// Track record: bins of the walk-forward test of exactly this forecast.
const bins = []; let lo = 0;
d.bins.forEach((hi, i) => { if (t[`any_bin${i}_n`]) bins.push({ lo, hi, n: t[`any_bin${i}_n`], pred: t[`any_bin${i}_pred`], obs: t[`any_bin${i}_obs`] }); lo = hi; });
const showcase = bins.filter(b => b.lo >= 0.2).sort((a, b) => b.n - a.n)[0] || bins[bins.length - 1];
const veryLikely = d.list.filter(x => x.p >= 0.7).length, likely = d.list.filter(x => x.p >= 0.4).length;
const groups = ['P3', 'P4', 'P5', 'P6', 'P8'];
const enrich = x => { const gs = Object.entries(x.groups).map(([g, v]) => ({ group: g, ...v })).sort((a, b) => b.p - a.p); return { ...x, gs, best: gs[0] }; };
let list = d.list.map(enrich);
if (nwFilter === 'new') list = list.filter(x => !x.seasonal); else if (nwFilter === 'seasonal') list = list.filter(x => x.seasonal);
if (nwGroup !== 'any') list = list.filter(x => x.groups[nwGroup]).sort((a, b) => b.groups[nwGroup].p - a.groups[nwGroup].p);
const card = x => {
const p = nwGroup === 'any' ? x.p : x.groups[nwGroup].p, [ci, cl] = certainty(p), filled = Math.round(p * 10);
const g = nwGroup === 'any' ? x.best : { group: nwGroup, ...x.groups[nwGroup] };
return `<div class="cc" data-id="${esc(x.id)}">
<div class="top"><div class="nm">${esc(x.name)}<small>${x.danish ? 'Danish' : 'International'}${x.seasonal ? ' · <span class="tag2">Seasonal return</span>' : ''}</small></div>
<div class="big"><b>${pct(p)}</b><br><span class="cert c${ci}">${cl}</span></div></div>
<div class="dots">${Array.from({ length: 10 }, (_, i) => `<span class="${i < filled ? 'f' : ''}"></span>`).join('')}<em>${oneIn(p)}</em></div>
<div class="chs">${x.gs.filter(v => v.p >= 0.01).map(v => `<span style="--c:${GROUP_C[v.group]}">${v.group} ${pct(v.p)}</span>`).join('') || '<span style="--c:var(--line)">no single station stands out</span>'}</div>
${liveLine(g)}
${x.song ? `<div class="note">Playing now: <b>${esc(x.song.title)}</b> (${x.song.plays} plays on DR this week)</div>` : ''}
</div>`;
};
const chart = () => {
if (!bins.length) return '<p class="note">No track record stored yet (`airplay model nextweek`).</p>';
const W = 320, H = 240, P = 36, X = v => P + v * (W - P - 10), Y = v => H - P - v * (H - P - 10);
const maxn = Math.max(...bins.map(b => b.n));
return `<svg viewBox="0 0 ${W} ${H}" width="100%" role="img" aria-label="Predicted against what happened">
<line x1="${X(0)}" y1="${Y(0)}" x2="${X(1)}" y2="${Y(1)}" stroke="var(--line)" stroke-dasharray="4 4"/>
<line x1="${X(0)}" y1="${Y(0)}" x2="${X(1)}" y2="${Y(0)}" stroke="var(--muted)"/><line x1="${X(0)}" y1="${Y(0)}" x2="${X(0)}" y2="${Y(1)}" stroke="var(--muted)"/>
${[0, .25, .5, .75, 1].map(v => `<text x="${X(v)}" y="${H - P + 16}" text-anchor="middle">${v * 100}%</text><text x="${P - 6}" y="${Y(v) + 4}" text-anchor="end">${v * 100}%</text>`).join('')}
<text x="${(W + P) / 2}" y="${H - 2}" text-anchor="middle">we predicted</text>
<text x="10" y="${(H - P) / 2}" text-anchor="middle" transform="rotate(-90 10 ${(H - P) / 2})">it happened</text>
${bins.map(b => `<circle cx="${X(b.pred)}" cy="${Y(b.obs)}" r="${3 + 9 * Math.sqrt(b.n / maxn)}" fill="var(--accent)" fill-opacity=".75"><title>${Math.round(b.lo * 100)}-${Math.round(b.hi * 100)}%: predicted ${pct(b.pred)}, happened ${pct(b.obs)} (${b.n} forecasts)</title></circle>`).join('')}
</svg>`;
};
$('nw').innerHTML = `
<div class="nw-hero">
<div><div class="eyebrow">DR rotation forecast</div>
<h2>Who DR adds the week of ${fmtLong(d.week)} to ${fmtLong(end)}</h2>
<p class="note">The chance each artist enters rotation on a DR music channel that week: a new song, or plays jumping well above their usual level. Forecast made ${fmtLong(d.made_at.slice(0, 10))} from DR's artist pages up to the week of ${fmtLong(d.origin_week)}; the live lines use every DR play logged since ${fmtLong(plusDays(d.this_monday, -7))}.</p></div>
<div class="nw-kpis">
<div class="kpi"><div>Expected adds on DR</div><b>${Math.round(d.expected)}</b><small>90% range ${Math.round(d.range[0])}-${Math.round(d.range[1])}</small></div>
<div class="kpi"><div>Likely or better</div><b>${likely}</b><small>${veryLikely} very likely (70%+)</small></div>
<div class="kpi"><div>Track record</div><b>${showcase ? pct(showcase.obs) : '—'}</b><small>${showcase ? `happened when we said ${Math.round(showcase.lo * 100)}-${Math.round(showcase.hi * 100)}%` : 'not tested yet'}</small></div>
</div>
</div>
<div class="nw-h">Add board: most likely per station</div>
<div class="board">${groups.map(g => `<div class="col" style="--c:${GROUP_C[g]}"><h4>${g}</h4>${(d.boards[g] || []).map(x => `<div class="it" data-id="${esc(x.id)}"><span>${esc(x.name)}</span><b>${pct(x.p)}</b><i style="width:${Math.max(4, x.p * 100)}%"></i></div>`).join('') || '<p class="note">No strong candidates</p>'}</div>`).join('')}</div>
<div class="nw-h">The candidates</div>
<div class="nw-filters">
${[['all', 'All'], ['new', 'New music'], ['seasonal', 'Seasonal returns']].map(([k, l]) => `<button class="pill ${nwFilter === k ? 'on' : ''}" data-f="${k}">${l}</button>`).join('')}
<span style="width:12px"></span>
${['any', ...groups].map(g => `<button class="pill ${nwGroup === g ? 'on' : ''}" data-g="${g}">${g === 'any' ? 'Any station' : g}</button>`).join('')}
</div>
<div class="cand">${list.slice(0, 40).map(card).join('') || '<p class="empty">No candidates above 3%.</p>'}</div>
<div class="nw-h">How sure are we?</div>
<div class="track"><div class="card">${chart()}</div>
<div class="prose note"><p>Each dot is a group of past forecasts made exactly like this one: a 1-week window, ${d.horizon + 1} weeks after the latest complete week of DR data, scored on what DR actually did (walk-forward, ${t.any_origins ? Math.round(t.any_origins) + ' forecast weeks' : 'past year'}, nothing from the future used). On the dashed line the percentages mean what they say. Bigger dots hold more forecasts.</p>
<p>${bins.map(b => `${Math.round(b.lo * 100)}-${Math.round(b.hi * 100)}%: happened ${pct(b.obs)} of ${Math.round(b.n).toLocaleString('en')} times`).join(' · ')}</p>
<p><b>Very likely</b> 70%+ · <b>Likely</b> 40-70% · <b>Possible</b> 20-40% · <b>Long shot</b> 10-20% · <b>Outside chance</b> under 10%. "1 in 4" means: of four artists with this chance, about one gets added. Adds of old songs (Christmas, classics) are marked <span class="tag2">Seasonal return</span> when we can tell; the forecast itself does not yet separate them.</p>
${t.any_roc ? `<p>Ranking quality on the same test: an artist who got added was ranked above one who did not ${Math.round(t.any_roc * 100)}% of the time (ROC-AUC ${t.any_roc.toFixed(3)}).</p>` : ''}</div></div>`;
document.querySelectorAll('#nw [data-f]').forEach(b => b.onclick = () => { nwFilter = b.dataset.f; renderNext(); });
document.querySelectorAll('#nw [data-g]').forEach(b => b.onclick = () => { nwGroup = b.dataset.g; renderNext(); });
document.querySelectorAll('#nw [data-id]').forEach(el => el.onclick = () => showArtist(el.dataset.id));
}
// ---------- wiring ---------- // ---------- wiring ----------
document.querySelectorAll('nav button').forEach(b => b.addEventListener('click', () => { document.querySelectorAll('nav button').forEach(b => b.addEventListener('click', () => {
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x === b)); document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x === b));
@@ -454,7 +593,8 @@ document.querySelectorAll('nav button').forEach(b => b.addEventListener('click',
})); }));
function showTab(t) { function showTab(t) {
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x.dataset.tab === t)); document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x.dataset.tab === t));
['rank', 'best', 'model'].forEach(k => $('tab-' + k).hidden = k !== t); ['rank', 'next', 'best', 'model'].forEach(k => $('tab-' + k).hidden = k !== t);
if (t === 'next') loadNext();
} }
$('h').addEventListener('input', () => { shown = 100; loadRank(); }); $('h').addEventListener('input', () => { shown = 100; loadRank(); });
$('w').addEventListener('change', () => { shown = 100; loadRank(); }); $('w').addEventListener('change', () => { shown = 100; loadRank(); });