Next week page: 1-week add forecast (daily run, isotonic-calibrated), /api/nextweek with live plays and track record, new tab; nextweek backtest

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 12:16:20 +00:00
co-authored by Claude Opus 5.5
parent c61800a51f
commit 2e31b1dc49
6 changed files with 402 additions and 1 deletions
+141 -1
View File
@@ -100,6 +100,52 @@ svg text { fill: var(--muted); font-size: 11px; }
.card p { color: var(--muted); }
.prose { max-width: 760px; }
.prose li { margin: 6px 0; }
/* next week */
.nw-hero { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; align-items: end; margin-top: 6px; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); font-weight: 700; }
.nw-hero h2 { margin: 4px 0 6px; font-size: 26px; letter-spacing: -.02em; }
.nw-kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.nw-kpis .kpi b { font-size: 26px; }
.nw-kpis .kpi small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.nw-h { margin: 26px 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.board { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.board .col { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); border-top: 4px solid var(--c); }
.board .col h4 { margin: 0 0 8px; font-size: 15px; color: var(--c); }
.board .it { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 6px 0; border-bottom: 1px solid var(--line); cursor: pointer; font-size: 14px; }
.board .it:last-child { border-bottom: 0; }
.board .it i { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--c); opacity: .75; }
.board .it b { font-variant-numeric: tabular-nums; }
.nw-filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.pill { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.pill.on { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.cand { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 14px; }
.cc { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); cursor: pointer; display: flex; flex-direction: column; gap: 10px; }
.cc:hover { border-color: var(--accent); }
.cc .top { display: flex; align-items: flex-start; gap: 10px; }
.cc .nm { font-weight: 700; font-size: 17px; line-height: 1.2; }
.cc .nm small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; margin-top: 2px; }
.cc .big { margin-left: auto; text-align: right; }
.cc .big b { font-size: 30px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.cert { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; padding: 2px 8px; border-radius: 999px; }
.cert.c0 { background: color-mix(in srgb, var(--good) 22%, var(--panel)); color: var(--good); }
.cert.c1 { background: color-mix(in srgb, var(--good) 12%, var(--panel)); color: var(--good); }
.cert.c2 { background: var(--accent-soft); color: var(--accent); }
.cert.c3, .cert.c4 { background: var(--bg); color: var(--muted); }
.dots { display: flex; align-items: center; gap: 4px; }
.dots span { width: 11px; height: 11px; border-radius: 50%; border: 1.5px solid var(--accent); }
.dots span.f { background: var(--accent); }
.dots em { font-style: normal; color: var(--muted); font-size: 13px; margin-left: 8px; }
.chs { display: flex; flex-wrap: wrap; gap: 6px; }
.chs span { font-size: 12px; padding: 2px 8px; border-radius: 6px; background: color-mix(in srgb, var(--c) 14%, var(--panel)); color: var(--ink); }
.live { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.live .st { font-weight: 700; }
.meter { position: relative; height: 8px; border-radius: 4px; background: var(--line); margin: 8px 0 4px; overflow: hidden; }
.meter i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; background: var(--c); }
.meter u { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--ink); }
.tag2 { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }
.track { display: grid; grid-template-columns: 340px 1fr; gap: 18px; align-items: start; }
@media (max-width: 900px) { .nw-hero, .track { grid-template-columns: 1fr; } .board { grid-template-columns: repeat(2, 1fr); } .nw-kpis { grid-template-columns: 1fr 1fr 1fr; } }
@media (max-width: 640px) { .board { grid-template-columns: 1fr; } .nw-kpis { grid-template-columns: 1fr; } .cand { grid-template-columns: 1fr; } }
@media (max-width: 640px) {
header, main { padding-left: 16px; padding-right: 16px; }
input[type=range] { width: 100%; }
@@ -116,6 +162,7 @@ svg text { fill: var(--muted); font-size: 11px; }
<div class="sub" id="sub">Loading…</div>
<nav>
<button class="on" data-tab="rank">Ranking</button>
<button data-tab="next">Next week</button>
<button data-tab="best">Best week</button>
<button data-tab="model">How good is it?</button>
</nav>
@@ -161,6 +208,7 @@ svg text { fill: var(--muted); font-size: 11px; }
<p class="note"><b>New add</b> = if the artist releases a new song in the chosen week, the chance DR adds it within the chosen number of weeks: the artist enters rotation (at or above the station's weekly play threshold) after at least 4 weeks out, or, for artists who are always on, their plays jump well above their own baseline and stay up (that finds 77% of the songs DR's real lists show as new). Learned from past releases with no other release in the 6 weeks before, so a current song's success isn't counted as a new one's; a release less than 6 weeks after the artist's last one gets no estimate ("too soon"). The new song's NMF Denmark placement is weighed by how often the artist's releases were placed. "In rotation now" lists DR stations where the artist is on the list now, then Bauer stations (grey) with 14+ plays in the last 7 days. Click an artist for history and a what-if, or open <b>Best week</b> for their release calendar.</p>
</section>
<section id="tab-next" hidden><div id="nw"><p class="empty">Loading…</p></div></section>
<section id="tab-best" hidden>
<div class="controls">
<div class="ctl">Artist <input type="search" id="bw-q" list="bw-list" placeholder="Type an artist…" autocomplete="off"><datalist id="bw-list"></datalist></div>
@@ -447,6 +495,97 @@ function renderModel() {
: '<p class="note">No backtest stored yet (`airplay model events`).</p>';
}
// ---------- next week ----------
let nw = null, nwFilter = 'all', nwGroup = 'any';
const GROUP_C = { P3: 'var(--p3)', P4: 'var(--p4)', P5: 'var(--p5)', P6: 'var(--p6)', P8: 'var(--p8)' };
const certainty = p => p >= 0.7 ? [0, 'Very likely'] : p >= 0.4 ? [1, 'Likely'] : p >= 0.2 ? [2, 'Possible'] : p >= 0.1 ? [3, 'Long shot'] : [4, 'Outside chance'];
const oneIn = p => p >= 0.5 ? `${Math.round(p * 10)} in 10` : `1 in ${Math.max(2, Math.round(1 / p))}`;
const fmtLong = s => new Date(s + 'T00:00:00Z').toLocaleDateString('en-GB', { day: 'numeric', month: 'long', timeZone: 'UTC' });
const plusDays = (s, d) => { const x = new Date(s + 'T00:00:00Z'); x.setUTCDate(x.getUTCDate() + d); return x.toISOString().slice(0, 10); };
async function loadNext() {
if (nw) return renderNext();
const r = await fetch(`${BASE}/api/nextweek`); nw = await r.json();
renderNext();
}
// What this week's plays say, for the artist's most likely station group.
function liveLine(g) {
if (!g || !g.tau) return '';
const tau = g.tau, pace = g.pace, wk = g.this_week, lw = g.last_week;
const [cls, text] = lw >= tau ? ['up', 'Already in rotation last week'] : pace >= tau ? ['up', 'Crossing the line this week'] : pace >= 0.6 * tau ? ['', 'Building'] : wk > 0 ? ['', 'Some plays so far'] : ['down', 'No plays yet this week'];
const w = Math.min(100, 100 * pace / (tau * 1.5)), m = 100 / 1.5;
return `<div class="live" style="--c:${GROUP_C[g.group]}"><span class="st ${cls}">${text}</span> on ${g.group}: ${wk} plays so far this week, on pace for ${pace}; last week ${lw}. Rotation line ${tau}.
<div class="meter"><i style="width:${w}%"></i><u style="left:${m}%" title="rotation line"></u></div></div>`;
}
function renderNext() {
const d = nw;
if (!d || !d.ready) { $('nw').innerHTML = '<p class="empty">The next-week forecast is produced by the daily run; it is not there yet.</p>'; return; }
const end = plusDays(d.week, 6), t = d.track || {};
// Track record: bins of the walk-forward test of exactly this forecast.
const bins = []; let lo = 0;
d.bins.forEach((hi, i) => { if (t[`any_bin${i}_n`]) bins.push({ lo, hi, n: t[`any_bin${i}_n`], pred: t[`any_bin${i}_pred`], obs: t[`any_bin${i}_obs`] }); lo = hi; });
const showcase = bins.filter(b => b.lo >= 0.2).sort((a, b) => b.n - a.n)[0] || bins[bins.length - 1];
const veryLikely = d.list.filter(x => x.p >= 0.7).length, likely = d.list.filter(x => x.p >= 0.4).length;
const groups = ['P3', 'P4', 'P5', 'P6', 'P8'];
const enrich = x => { const gs = Object.entries(x.groups).map(([g, v]) => ({ group: g, ...v })).sort((a, b) => b.p - a.p); return { ...x, gs, best: gs[0] }; };
let list = d.list.map(enrich);
if (nwFilter === 'new') list = list.filter(x => !x.seasonal); else if (nwFilter === 'seasonal') list = list.filter(x => x.seasonal);
if (nwGroup !== 'any') list = list.filter(x => x.groups[nwGroup]).sort((a, b) => b.groups[nwGroup].p - a.groups[nwGroup].p);
const card = x => {
const p = nwGroup === 'any' ? x.p : x.groups[nwGroup].p, [ci, cl] = certainty(p), filled = Math.round(p * 10);
const g = nwGroup === 'any' ? x.best : { group: nwGroup, ...x.groups[nwGroup] };
return `<div class="cc" data-id="${esc(x.id)}">
<div class="top"><div class="nm">${esc(x.name)}<small>${x.danish ? 'Danish' : 'International'}${x.seasonal ? ' · <span class="tag2">Seasonal return</span>' : ''}</small></div>
<div class="big"><b>${pct(p)}</b><br><span class="cert c${ci}">${cl}</span></div></div>
<div class="dots">${Array.from({ length: 10 }, (_, i) => `<span class="${i < filled ? 'f' : ''}"></span>`).join('')}<em>${oneIn(p)}</em></div>
<div class="chs">${x.gs.filter(v => v.p >= 0.01).map(v => `<span style="--c:${GROUP_C[v.group]}">${v.group} ${pct(v.p)}</span>`).join('') || '<span style="--c:var(--line)">no single station stands out</span>'}</div>
${liveLine(g)}
${x.song ? `<div class="note">Playing now: <b>${esc(x.song.title)}</b> (${x.song.plays} plays on DR this week)</div>` : ''}
</div>`;
};
const chart = () => {
if (!bins.length) return '<p class="note">No track record stored yet (`airplay model nextweek`).</p>';
const W = 320, H = 240, P = 36, X = v => P + v * (W - P - 10), Y = v => H - P - v * (H - P - 10);
const maxn = Math.max(...bins.map(b => b.n));
return `<svg viewBox="0 0 ${W} ${H}" width="100%" role="img" aria-label="Predicted against what happened">
<line x1="${X(0)}" y1="${Y(0)}" x2="${X(1)}" y2="${Y(1)}" stroke="var(--line)" stroke-dasharray="4 4"/>
<line x1="${X(0)}" y1="${Y(0)}" x2="${X(1)}" y2="${Y(0)}" stroke="var(--muted)"/><line x1="${X(0)}" y1="${Y(0)}" x2="${X(0)}" y2="${Y(1)}" stroke="var(--muted)"/>
${[0, .25, .5, .75, 1].map(v => `<text x="${X(v)}" y="${H - P + 16}" text-anchor="middle">${v * 100}%</text><text x="${P - 6}" y="${Y(v) + 4}" text-anchor="end">${v * 100}%</text>`).join('')}
<text x="${(W + P) / 2}" y="${H - 2}" text-anchor="middle">we predicted</text>
<text x="10" y="${(H - P) / 2}" text-anchor="middle" transform="rotate(-90 10 ${(H - P) / 2})">it happened</text>
${bins.map(b => `<circle cx="${X(b.pred)}" cy="${Y(b.obs)}" r="${3 + 9 * Math.sqrt(b.n / maxn)}" fill="var(--accent)" fill-opacity=".75"><title>${Math.round(b.lo * 100)}-${Math.round(b.hi * 100)}%: predicted ${pct(b.pred)}, happened ${pct(b.obs)} (${b.n} forecasts)</title></circle>`).join('')}
</svg>`;
};
$('nw').innerHTML = `
<div class="nw-hero">
<div><div class="eyebrow">DR rotation forecast</div>
<h2>Who DR adds the week of ${fmtLong(d.week)} to ${fmtLong(end)}</h2>
<p class="note">The chance each artist enters rotation on a DR music channel that week: a new song, or plays jumping well above their usual level. Forecast made ${fmtLong(d.made_at.slice(0, 10))} from DR's artist pages up to the week of ${fmtLong(d.origin_week)}; the live lines use every DR play logged since ${fmtLong(plusDays(d.this_monday, -7))}.</p></div>
<div class="nw-kpis">
<div class="kpi"><div>Expected adds on DR</div><b>${Math.round(d.expected)}</b><small>90% range ${Math.round(d.range[0])}-${Math.round(d.range[1])}</small></div>
<div class="kpi"><div>Likely or better</div><b>${likely}</b><small>${veryLikely} very likely (70%+)</small></div>
<div class="kpi"><div>Track record</div><b>${showcase ? pct(showcase.obs) : '—'}</b><small>${showcase ? `happened when we said ${Math.round(showcase.lo * 100)}-${Math.round(showcase.hi * 100)}%` : 'not tested yet'}</small></div>
</div>
</div>
<div class="nw-h">Add board: most likely per station</div>
<div class="board">${groups.map(g => `<div class="col" style="--c:${GROUP_C[g]}"><h4>${g}</h4>${(d.boards[g] || []).map(x => `<div class="it" data-id="${esc(x.id)}"><span>${esc(x.name)}</span><b>${pct(x.p)}</b><i style="width:${Math.max(4, x.p * 100)}%"></i></div>`).join('') || '<p class="note">No strong candidates</p>'}</div>`).join('')}</div>
<div class="nw-h">The candidates</div>
<div class="nw-filters">
${[['all', 'All'], ['new', 'New music'], ['seasonal', 'Seasonal returns']].map(([k, l]) => `<button class="pill ${nwFilter === k ? 'on' : ''}" data-f="${k}">${l}</button>`).join('')}
<span style="width:12px"></span>
${['any', ...groups].map(g => `<button class="pill ${nwGroup === g ? 'on' : ''}" data-g="${g}">${g === 'any' ? 'Any station' : g}</button>`).join('')}
</div>
<div class="cand">${list.slice(0, 40).map(card).join('') || '<p class="empty">No candidates above 3%.</p>'}</div>
<div class="nw-h">How sure are we?</div>
<div class="track"><div class="card">${chart()}</div>
<div class="prose note"><p>Each dot is a group of past forecasts made exactly like this one: a 1-week window, ${d.horizon + 1} weeks after the latest complete week of DR data, scored on what DR actually did (walk-forward, ${t.any_origins ? Math.round(t.any_origins) + ' forecast weeks' : 'past year'}, nothing from the future used). On the dashed line the percentages mean what they say. Bigger dots hold more forecasts.</p>
<p>${bins.map(b => `${Math.round(b.lo * 100)}-${Math.round(b.hi * 100)}%: happened ${pct(b.obs)} of ${Math.round(b.n).toLocaleString('en')} times`).join(' · ')}</p>
<p><b>Very likely</b> 70%+ · <b>Likely</b> 40-70% · <b>Possible</b> 20-40% · <b>Long shot</b> 10-20% · <b>Outside chance</b> under 10%. "1 in 4" means: of four artists with this chance, about one gets added. Adds of old songs (Christmas, classics) are marked <span class="tag2">Seasonal return</span> when we can tell; the forecast itself does not yet separate them.</p>
${t.any_roc ? `<p>Ranking quality on the same test: an artist who got added was ranked above one who did not ${Math.round(t.any_roc * 100)}% of the time (ROC-AUC ${t.any_roc.toFixed(3)}).</p>` : ''}</div></div>`;
document.querySelectorAll('#nw [data-f]').forEach(b => b.onclick = () => { nwFilter = b.dataset.f; renderNext(); });
document.querySelectorAll('#nw [data-g]').forEach(b => b.onclick = () => { nwGroup = b.dataset.g; renderNext(); });
document.querySelectorAll('#nw [data-id]').forEach(el => el.onclick = () => showArtist(el.dataset.id));
}
// ---------- wiring ----------
document.querySelectorAll('nav button').forEach(b => b.addEventListener('click', () => {
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x === b));
@@ -454,7 +593,8 @@ document.querySelectorAll('nav button').forEach(b => b.addEventListener('click',
}));
function showTab(t) {
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x.dataset.tab === t));
['rank', 'best', 'model'].forEach(k => $('tab-' + k).hidden = k !== t);
['rank', 'next', 'best', 'model'].forEach(k => $('tab-' + k).hidden = k !== t);
if (t === 'next') loadNext();
}
$('h').addEventListener('input', () => { shown = 100; loadRank(); });
$('w').addEventListener('change', () => { shown = 100; loadRank(); });