//! 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 calibrate [--out docs/CALIBRATION.md] rotation thresholds from DR's lists //! airplay model nmf [--out docs/NMF.md] New Music Friday Denmark vs DR adds //! airplay model ifpi the 20-year IFPI test bed -> docs/IFPI.md //! airplay model adds [--h 0,4] [--out docs/BACKTEST-ADDS.md] backtest of the new-add model //! airplay model rank [--h 4] [--max-h 12] [--top 50] pub mod adds; pub mod backtest; pub mod calibrate; pub mod common; pub mod features; pub mod glm; pub mod ifpi; pub mod kalman; pub mod nmf; 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 { 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 = 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("nmf") => { let md = nmf::run(db, &p)?; if let Some(out) = arg(args, "--out") { std::fs::write(out, &md)?; } print!("{md}"); } Some("ifpi") => { let md = ifpi::run(db)?; std::fs::write("docs/IFPI.md", &md)?; print!("{md}"); } Some("calibrate") => { let md = calibrate::run(db, &p)?; // The daily job runs this too; only write the report into the repo when asked. if let Some(out) = arg(args, "--out") { std::fs::write(out, &md)?; } print!("{md}"); } Some("adds") => { if let Some(w) = arg(args, "--without") { let _ = adds::WITHOUT.set(w.split(',').map(str::to_string).collect()); } let horizons: Vec = arg(args, "--h").unwrap_or_else(|| "0,4".into()).split(',').filter_map(|s| s.trim().parse().ok()).collect(); let tau = common::thresholds(&p, 26); let mut md = String::new(); let rows = adds::backtest(&p, &tau, &horizons, 26, &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-ADDS.md".into()); std::fs::write(&out, &md)?; print!("{md}"); } 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::(), 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] | adds [--h 0,4] | rank [--h 4] [--max-h 12] [--top 50]"); std::process::exit(2); } } Ok(()) }