Event model: optional NMF Denmark position score (AIRPLAY_NMF_POS), for backtest comparison

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 22:04:19 +00:00
co-authored by Claude Opus 5.5
parent 7e340a074f
commit d7eeeafda9
+12
View File
@@ -128,6 +128,12 @@ pub fn p_nmf(p: &Panel, a: usize, t: usize) -> f64 {
((placed + 0.1) / (rels + 1.0)).clamp(0.0, 0.95)
}
/// NMF Denmark position as a score: 1 at #1, falling with the log of the position to 0 at #120
/// (a placement without a known position counts as #40). Used instead of "top 10" when the
/// AIRPLAY_NMF_POS environment variable is set (for the backtest comparison).
fn pos_score(pos: Option<i64>) -> f64 { (1.0 - (pos.unwrap_or(40).max(1) as f64).ln() / 120f64.ln()).clamp(0.0, 1.0) }
fn nmf_pos() -> bool { static V: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); *V.get_or_init(|| std::env::var("AIRPLAY_NMF_POS").is_ok()) }
pub fn nmf_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario) -> [f64; NMF] {
if off("nmf") { return [0.0; NMF]; }
let end_t = p.weeks[t] + chrono::Duration::days(6);
@@ -144,6 +150,12 @@ pub fn nmf_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario) -> [f64;
let top10_share = if past.is_empty() { 0.0 } else { past.iter().filter(|(_, pos)| pos.is_some_and(|x| x <= 10)).count() as f64 / past.len() as f64 };
let last_year = past.iter().filter(|(d, _)| (asof - *d).num_days() <= 364).count() as f64;
let f = |b: bool| b as i32 as f64;
if nmf_pos() {
let best = p.nmf_dk[a].iter().filter(|(d, _)| *d >= w0 && *d < w1).map(|(_, pos)| pos_score(*pos)).fold(0.0, f64::max);
let past_mean = if past.is_empty() { 0.0 } else { past.iter().map(|(_, pos)| pos_score(*pos)).sum::<f64>() / past.len() as f64 };
let score = match sc.nmf { None => best, Some(false) => 0.0, Some(true) => past_mean };
return [f(in_win), score, last_year.ln_1p(), p_nmf(p, a, t)];
}
[f(in_win), if sc.nmf == Some(true) { top10_share } else { f(top10) }, last_year.ln_1p(), p_nmf(p, a, t)]
}