Release-event model: 'if the artist releases a new song, will DR add it' trained on clean releases (no other release in the 6 weeks before), with its own walk-forward test; ranking defaults to it and leaves out artists who just released; pool note updated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+10
-5
@@ -32,6 +32,7 @@ struct Models {
|
||||
tau: [u32; C],
|
||||
direct: Vec<Direct>, // index h - 1
|
||||
adds: Vec<AddModel>, // index h
|
||||
events: AddModel,
|
||||
rot: Rot,
|
||||
index: HashMap<String, usize>,
|
||||
}
|
||||
@@ -106,8 +107,9 @@ fn fit_models(db_path: &str, made_at: String) -> Result<Models> {
|
||||
let direct = (1..=MAX_H).map(|h| Direct::fit(&panel, h, panel.w() - 1, &tau, false)).collect();
|
||||
let rot = Rot::new(&panel, &tau);
|
||||
let adds = (0..=MAX_H).map(|h| AddModel::fit(&panel, &rot, h, panel.w() - 1)).collect();
|
||||
let events = AddModel::fit_events(&panel, &rot, panel.w() - 1);
|
||||
let index = panel.artists.iter().enumerate().map(|(i, a)| (a.id.clone(), i)).collect();
|
||||
Ok(Models { made_at, panel, tau, direct, adds, rot, index })
|
||||
Ok(Models { made_at, panel, tau, direct, adds, events, rot, index })
|
||||
}
|
||||
|
||||
fn query(url: &str) -> HashMap<String, String> {
|
||||
@@ -189,6 +191,7 @@ fn fold(e: &mut Value, target: &str, ch: &str, prob: Option<f64>, mean: Option<f
|
||||
"add_any_rel" => { e["add_any_rel"] = r3(prob); }
|
||||
"add_any_nmf" => { e["add_any_nmf"] = r3(prob); }
|
||||
"p_nmf" => { e["p_nmf"] = r3(prob); }
|
||||
"recent_release" => { e["recent_release"] = json!(true); }
|
||||
"add_rel" => { if !e["add_rel"].is_object() { e["add_rel"] = json!({}); } e["add_rel"][ch] = r3(prob); }
|
||||
"p_release" => { e["p_release"] = r3(prob); }
|
||||
"now" => { e["now"][ch] = json!({"on": prob.unwrap_or(0.0) > 0.5, "plays": mean}); }
|
||||
@@ -236,7 +239,7 @@ fn rank(st: &State, h: usize) -> Result<Reply> {
|
||||
}
|
||||
let mut rows: HashMap<String, Value> = HashMap::new();
|
||||
let mut st_ = c.prepare("SELECT key, target, prob, mean, lo, hi, target_week FROM forecasts WHERE made_at = ?1 AND model_version = ?2
|
||||
AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release') AND horizon = ?4) OR target = 'now')")?;
|
||||
AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release', 'recent_release') AND horizon = ?4) OR target = 'now')")?;
|
||||
let it = st_.query_map(params![made, VERSION, h.max(1) as i64, h as i64], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<f64>>(2)?,
|
||||
r.get::<_, Option<f64>>(3)?, r.get::<_, Option<f64>>(4)?, r.get::<_, Option<f64>>(5)?, r.get::<_, String>(6)?)))?;
|
||||
let mut window = String::new();
|
||||
@@ -301,7 +304,7 @@ fn artist(st: &State, id: &str) -> Result<Reply> {
|
||||
let ch = key.split_once(':').map(|x| x.1).unwrap_or("");
|
||||
if target == "now" { fold(&mut now, "now", ch, prob, mean, lo, hi); continue; }
|
||||
let e = byh.entry(h).or_insert_with(|| json!({"h": h, "p": {}, "plays": {}, "add": {}}));
|
||||
if target.starts_with("add") || target.starts_with("p_") { e["window_start"] = json!(week); } else { e["week"] = json!(week); }
|
||||
if target.starts_with("add") || target.starts_with("p_") || target == "recent_release" { e["window_start"] = json!(week); } else { e["week"] = json!(week); }
|
||||
fold(e, &target, ch, prob, mean, lo, hi);
|
||||
}
|
||||
now_out = now["now"].clone();
|
||||
@@ -365,7 +368,9 @@ fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
|
||||
|
||||
let run = |overrides: bool| -> Value {
|
||||
let f: Option<&dyn Fn(&mut [f64], [usize; 4])> = if overrides { Some(&over) } else { None };
|
||||
let (add_c, add_any) = am.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(release, release && nmf), f);
|
||||
// A new release: the release-event model; no release: the window model (current songs only).
|
||||
let (add_c, add_any) = if release { m.events.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(true, nmf), f) }
|
||||
else { am.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(false, false), f) };
|
||||
let mut x = vec![0.0; P];
|
||||
let mut mu = [0.0; C];
|
||||
for c in 0..C {
|
||||
@@ -387,7 +392,7 @@ fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
|
||||
};
|
||||
let now: serde_json::Map<String, Value> = (0..C).filter(|&c| m.rot.on(a, c, t0)).map(|c| (CHANNELS[c].to_string(), json!(p.y(a, c, t0)))).collect();
|
||||
ok(json!({
|
||||
"h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0),
|
||||
"h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0), "recent_release": crate::model::adds::recent_release(p, a, t0),
|
||||
"nmf_history": p.nmf_dk[a].iter().rev().take(8).map(|(d, pos)| json!({"friday": d.to_string(), "position": pos})).collect::<Vec<_>>(),
|
||||
"window_start": (p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1)).to_string(),
|
||||
"current": {"t40": chart.t40_rank, "air": chart.air_rank,
|
||||
|
||||
Reference in New Issue
Block a user