Models, first layer: artist × DR channel × week panel with IFPI chart history; NB and logit direct h-step models, Kalman artist form, persistence/popularity baselines; walk-forward backtest (docs/BACKTEST.md) and the ranking (forecasts table)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
//! Models (docs/MODELS.md). This first layer is the doc's "model we can build now": artist ×
|
||||
//! DR channel × week, trained on the DR artist pages' 52 weeks, with IFPI chart history.
|
||||
//!
|
||||
//! airplay model panel what the panel holds
|
||||
//! airplay model backtest [--h 1,4,8] [--refit 1] [--out docs/BACKTEST.md]
|
||||
//! airplay model rank [--h 4] [--max-h 12] [--top 50]
|
||||
|
||||
pub mod backtest;
|
||||
pub mod common;
|
||||
pub mod features;
|
||||
pub mod glm;
|
||||
pub mod kalman;
|
||||
pub mod panel;
|
||||
pub mod rank;
|
||||
|
||||
use crate::db::Db;
|
||||
use anyhow::Result;
|
||||
use panel::{is_p4, Panel, C, CHANNELS};
|
||||
|
||||
fn arg(args: &[String], name: &str) -> Option<String> {
|
||||
args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).cloned()
|
||||
}
|
||||
|
||||
pub fn run(db: &Db, args: &[String]) -> Result<()> {
|
||||
let p = Panel::load(db)?;
|
||||
match args.get(2).map(String::as_str) {
|
||||
Some("panel") => {
|
||||
println!("{} artists × {} channels × {} complete weeks ({} to {})", p.a(), C, p.w(), p.weeks[0], p.weeks[p.w() - 1]);
|
||||
let tau = common::thresholds(&p, p.w());
|
||||
for c in 0..C {
|
||||
let plays: f64 = (0..p.a()).flat_map(|a| (0..p.w()).map(move |w| (a, w))).map(|(a, w)| p.y(a, c, w)).sum();
|
||||
let rot = (0..p.a()).flat_map(|a| (0..p.w()).map(move |w| (a, w))).filter(|&(a, w)| p.y(a, c, w) >= tau[c] as f64).count();
|
||||
println!(" {:12} plays {:>8} τ {:>2} artist-weeks in rotation {:>6} ({:.1} artists per week)", CHANNELS[c], plays, tau[c], rot, rot as f64 / p.w() as f64);
|
||||
}
|
||||
let last = p.w() - 1;
|
||||
let charted = (0..p.a()).filter(|&a| p.chart(a, last).t40_hits > 0 || p.chart(a, last).air_hits > 0).count();
|
||||
let danish = p.artists.iter().filter(|a| a.danish).count();
|
||||
println!("artists matched to IFPI chart history: {charted}; Danish: {danish}");
|
||||
}
|
||||
Some("backtest") => {
|
||||
let horizons: Vec<usize> = arg(args, "--h").unwrap_or_else(|| "1,4,8".into()).split(',').filter_map(|s| s.trim().parse().ok()).collect();
|
||||
let refit: usize = arg(args, "--refit").and_then(|s| s.parse().ok()).unwrap_or(1).max(1);
|
||||
let report = backtest::run(&p, &horizons, refit)?;
|
||||
rank::create_tables(db)?;
|
||||
let at = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
|
||||
for (m, h, metric, v) in &report.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.md".into());
|
||||
std::fs::write(&out, &report.markdown)?;
|
||||
print!("{}", report.markdown);
|
||||
eprintln!("written to {out}");
|
||||
}
|
||||
Some("rank") => {
|
||||
let h: usize = arg(args, "--h").and_then(|s| s.parse().ok()).unwrap_or(4);
|
||||
let max_h: usize = arg(args, "--max-h").and_then(|s| s.parse().ok()).unwrap_or(12).max(h);
|
||||
let top: usize = arg(args, "--top").and_then(|s| s.parse().ok()).unwrap_or(50);
|
||||
let mut lines = rank::run(db, &p, max_h, h)?;
|
||||
lines.sort_by(|a, b| b.p_any.total_cmp(&a.p_any).then(b.mean.total_cmp(&a.mean)));
|
||||
println!("Week of {} (h = {h}), model {}", p.weeks[p.w() - 1] + chrono::Duration::weeks(h as i64), rank::VERSION);
|
||||
println!("{:>3} {:28} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>16}", "#", "artist", "any", "P3", "P4", "P5", "P6", "P8", "plays (80%)");
|
||||
for (i, l) in lines.iter().take(top).enumerate() {
|
||||
let p4 = (0..C).filter(|&c| is_p4(c)).map(|c| l.p_channel[c]).fold(0.0, f64::max);
|
||||
println!("{:>3} {:28} {:>6.2} {:>6.2} {:>6.2} {:>6.2} {:>6.2} {:>6.2} {:>5.0} ({:.0}–{:.0})", i + 1,
|
||||
p.artists[l.artist].name.chars().take(28).collect::<String>(), l.p_any, l.p_channel[0], p4,
|
||||
l.p_channel[11], l.p_channel[12], l.p_channel[13], l.mean, l.lo, l.hi);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
eprintln!("usage: airplay model panel | backtest [--h 1,4,8] [--refit 1] [--out FILE] | rank [--h 4] [--max-h 12] [--top 50]");
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user