Default window 2 weeks (1-week option added) on the site, best week and what-if; new-song backtest takes the window (--w, default 2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+19
-18
@@ -419,7 +419,8 @@ impl AddModel {
|
||||
/// within 8 weeks?" Trained only on artist-weeks where the artist did release in week t+1, with
|
||||
/// the artist's state the week before (earlier releases and NMF placements count as history;
|
||||
/// the new song's own NMF placement is the scenario variable).
|
||||
pub fn fit_events(p: &Panel, r: &Rot, last_week: usize) -> AddModel { Self::fit_with(p, r, 0, last_week, true, W) }
|
||||
/// As `fit_events`, for a window of `win` weeks (6-origin recalibration; used in backtests).
|
||||
pub fn fit_events_win(p: &Panel, r: &Rot, last_week: usize, win: usize) -> AddModel { Self::fit_with(p, r, 0, last_week, true, win) }
|
||||
/// The site's release-event model (8-week window, pooled recalibration).
|
||||
pub fn fit_events_site(p: &Panel, r: &Rot, last_week: usize) -> AddModel { Self::fit_events_pooled(p, r, last_week, W) }
|
||||
/// The release-event model for a window of `win` weeks.
|
||||
@@ -771,29 +772,29 @@ pub fn backtest(p: &Panel, tau: &[u32; C], horizons: &[usize], start: usize, md:
|
||||
/// Walk-forward test of the release-event model: at each origin t0 the model sees only events
|
||||
/// whose window had closed (a week before t0), and scores the releases of week t0+1. Pre-release
|
||||
/// rows blend the new song's NMF placement from the artist's history (as on the site).
|
||||
pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, md: &mut String) -> Result<Vec<(String, usize, String, f64)>> {
|
||||
pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, win: usize, md: &mut String) -> Result<Vec<(String, usize, String, f64)>> {
|
||||
let r = Rot::new(p, tau);
|
||||
let mut rows = Vec::new();
|
||||
let (mut s_c, mut s_a) = (Scores::new(SCORED.to_vec()), Scores::new(SCORED.to_vec()));
|
||||
let (mut pool_c, mut pool_a) = (Pool(Vec::new()), Pool(Vec::new()));
|
||||
let lvl = |m: f64| ((m.ln_1p() / 0.5) as usize).min(7);
|
||||
let level = |a: usize, t: usize| (0..LAGS).map(|i| p.group_total(a, t - i, None)).sum::<f64>() / LAGS as f64;
|
||||
for t0 in start..p.w().saturating_sub(W + 1) {
|
||||
let m = AddModel::fit_events(p, &r, t0);
|
||||
for t0 in start..p.w().saturating_sub(win + 1) {
|
||||
let m = AddModel::fit_events_win(p, &r, t0, win);
|
||||
// Baselines from the training events. Release timing only: by weeks since the previous release.
|
||||
let (mut by_rot, mut by_lvl, mut cr, mut cl) = ([[1.0f64, 2.0]; 2], [[1.0f64, 2.0]; 8], [[1.0f64, 2.0]; 2], [[1.0f64, 2.0]; 8]);
|
||||
let mut tm = Timing::new();
|
||||
for t in origins(0, t0) {
|
||||
for t in origins_w(0, t0, win) {
|
||||
for a in 0..p.a() {
|
||||
if !p.known(a, t) || !clean_release(p, a, t + 1) { continue; }
|
||||
let key = since_bin(p, a, t);
|
||||
let y = r.add_any_in_window(a, t, 0) as i32 as f64;
|
||||
let y = r.add_any_in(a, t, 0, win) as i32 as f64;
|
||||
let on = r.any(a, t) as usize;
|
||||
by_rot[on][0] += y; by_rot[on][1] += 1.0;
|
||||
let b = lvl(level(a, t)); by_lvl[b][0] += y; by_lvl[b][1] += 1.0;
|
||||
tm.any[key][0] += y; tm.any[key][1] += 1.0;
|
||||
for c in 0..C {
|
||||
let yc = r.add_in_window(a, c, t, 0) as i32 as f64;
|
||||
let yc = r.add_in(a, c, t, 0, win) as i32 as f64;
|
||||
let onc = r.on(a, c, t) as usize;
|
||||
cr[onc][0] += yc; cr[onc][1] += 1.0;
|
||||
let bc = lvl((0..LAGS).map(|i| p.y(a, c, t - i)).sum::<f64>() / LAGS as f64);
|
||||
@@ -802,20 +803,20 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, md: &mut String)
|
||||
}
|
||||
}
|
||||
}
|
||||
let gap = W + 1;
|
||||
let gap = win + 1;
|
||||
let ((pl_c, iso_c), (pl_a, iso_a)) = (pool_c.fit(t0, gap), pool_a.fit(t0, gap));
|
||||
let mut started = false;
|
||||
for a in 0..p.a() {
|
||||
if !p.known(a, t0) || !clean_release(p, a, t0 + 1) { continue; }
|
||||
if !started { s_c.start_origin(); s_a.start_origin(); started = true; }
|
||||
let q = p_nmf(p, a, t0);
|
||||
let (pcn, pan) = m.predict(p, &r, a, t0, Scenario::new(true, true), None);
|
||||
let (pcr, par) = m.predict(p, &r, a, t0, Scenario::new(true, false), None);
|
||||
let (rcn, ran) = m.predict_raw(p, &r, a, t0, 0, Scenario::new(true, true));
|
||||
let (rcr, rar) = m.predict_raw(p, &r, a, t0, 0, Scenario::new(true, false));
|
||||
let (pcx, pax) = m.predict(p, &r, a, t0, Scenario::ACTUAL, None);
|
||||
let (pcn, pan) = m.predict(p, &r, a, t0, Scenario::new(true, true).window(win), None);
|
||||
let (pcr, par) = m.predict(p, &r, a, t0, Scenario::new(true, false).window(win), None);
|
||||
let (rcn, ran) = m.predict_raw(p, &r, a, t0, 0, Scenario::new(true, true).window(win));
|
||||
let (rcr, rar) = m.predict_raw(p, &r, a, t0, 0, Scenario::new(true, false).window(win));
|
||||
let (pcx, pax) = m.predict(p, &r, a, t0, Scenario::actual(win), None);
|
||||
let key = since_bin(p, a, t0);
|
||||
let y = r.add_any_in_window(a, t0, 0);
|
||||
let y = r.add_any_in(a, t0, 0, win);
|
||||
let on = r.any(a, t0) as usize;
|
||||
let raw = q * ran + (1.0 - q) * rar;
|
||||
let tma = tm.any[key];
|
||||
@@ -823,7 +824,7 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, md: &mut String)
|
||||
q * pan + (1.0 - q) * par, raw, pl_a.apply(raw), iso_a.apply(raw), pax], y);
|
||||
pool_a.0.push((t0, raw, y));
|
||||
for c in 0..C {
|
||||
let yc = r.add_in_window(a, c, t0, 0);
|
||||
let yc = r.add_in(a, c, t0, 0, win);
|
||||
let onc = r.on(a, c, t0) as usize;
|
||||
let bc = lvl((0..LAGS).map(|i| p.y(a, c, t0 - i)).sum::<f64>() / LAGS as f64);
|
||||
let rawc = q * rcn[c] + (1.0 - q) * rcr[c];
|
||||
@@ -836,10 +837,10 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, md: &mut String)
|
||||
eprintln!("event backtest: origin {} done", p.weeks[t0]);
|
||||
}
|
||||
writeln!(md, "# Backtest: if the artist releases a new song, will DR add it?\n")?;
|
||||
writeln!(md, "Events: an artist knowable at the time releases something (Deezer) in week t0+1, with no other release in the {CLEAN_WEEKS} weeks before (so an add can't be the previous song's); target: a DR add (entering rotation after {GAP}+ weeks out, or plays jumping well above the artist's own 8-week baseline and staying up) within the {W} weeks from the release week. The model knows the artist's state the week before; pre-release rows blend the new song's NMF Denmark placement from the artist's history. \"Release timing only\" = the add rate by weeks since the artist's previous release. {} release events scored, {} ({:.1}%) got an add on some DR channel.\n",
|
||||
writeln!(md, "Events: an artist knowable at the time releases something (Deezer) in week t0+1, with no other release in the {CLEAN_WEEKS} weeks before (so an add can't be the previous song's); target: a DR add (entering rotation after {GAP}+ weeks out, or plays jumping well above the artist's own 8-week baseline and staying up) within {win} week(s) from the release week. The model knows the artist's state the week before; pre-release rows blend the new song's NMF Denmark placement from the artist's history. \"Release timing only\" = the add rate by weeks since the artist's previous release. {} release events scored, {} ({:.1}%) got an add on some DR channel.\n",
|
||||
s_a.n, s_a.hits, 100.0 * s_a.hits / s_a.n.max(1.0))?;
|
||||
table(md, &s_c, &s_a, &mut rows, 0, "event_")?;
|
||||
let m = AddModel::fit_events(p, &r, p.w() - 1);
|
||||
table(md, &s_c, &s_a, &mut rows, win, "event_")?;
|
||||
let m = AddModel::fit_events_win(p, &r, p.w() - 1, win);
|
||||
writeln!(md, "| Any-channel feature (all events) | Coefficient |\n|---|---|")?;
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user