figures / unit4-which-groups.html
sergiopaniego's picture
sergiopaniego HF Staff
Brand teal accent, readable text colors, U4 nudge redesign, U7 figures
39824c1 verified
Raw History Blame Contribute Delete
10.8 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Which groups teach</title>
<!-- Generated by build.py from fragments/. Runtime and theme tokens come from the research-article template. -->
<link rel="stylesheet" href="runtime/variables.css">
<script>
// The iframe can't read the course page's theme, so follow ?theme= if given, else the OS setting.
(function () {
const forced = new URLSearchParams(location.search).get('theme');
const mq = matchMedia('(prefers-color-scheme: dark)');
const apply = () => document.documentElement.setAttribute('data-theme', forced || (mq.matches ? 'dark' : 'light'));
apply();
if (!forced) mq.addEventListener('change', apply);
})();
</script>
<script src="runtime/color-palettes.js"></script>
<script src="runtime/rough.js"></script>
<script src="runtime/hand.js"></script>
<script src="runtime/embed-runtime.js"></script>
<style>
html, body { margin: 0; padding: 0; }
/* An opaque card with its own background, so it stays readable whatever theme the course page uses */
body { background: transparent; }
.figure-card { font-family: 'Source Sans 3', var(--default-font-family); color: var(--text-color); background: var(--page-bg);
border: 1px solid var(--border-color); border-radius: 10px; padding: 12px 14px 10px; }
</style>
<script>
// Clean look by default, in Source Sans. ?style=hand brings back the old hand-drawn look for comparison:
// it loads Excalifont and uses it for the whole card.
if (new URLSearchParams(location.search).get('style') === 'hand') {
const st = document.createElement('style');
st.textContent = "@font-face { font-family: 'Excalifont'; src: url('runtime/fonts/Excalifont-Regular.woff2') format('woff2'); font-display: swap; }" +
".figure-card { font-family: 'Excalifont', 'Source Sans 3', var(--default-font-family); }";
document.head.appendChild(st);
} else {
document.documentElement.classList.add('style-clean');
const l = document.createElement('link'); l.rel = 'stylesheet';
l.href = 'https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@400;600;700&display=swap';
document.head.appendChild(l);
}
</script>
</head>
<body>
<div class="figure-card">
<!-- title: Which groups teach -->
<div class="fig-which-groups"></div>
<style>
.fig-which-groups { position: relative; color: var(--text-color); font-size: 14px; }
.fig-which-groups svg { display: block; width: 100%; overflow: visible; }
.fig-which-groups .fig-controls { margin-top: 10px; }
.fig-which-groups .fig-controls label { display: block; font-size: 14px; margin-bottom: 4px; }
.fig-which-groups .fig-slider { display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "lo range hi"; align-items: center; gap: 4px 10px; }
.fig-which-groups .fig-end { font-size: 13px; color: var(--muted-color); }
.fig-which-groups .fig-end.lo { grid-area: lo; }
.fig-which-groups .fig-end.hi { grid-area: hi; text-align: right; }
.fig-which-groups input[type="range"] { grid-area: range; width: 100%; margin: 0; accent-color: var(--primary-color); }
@media (max-width: 520px) {
.fig-which-groups .fig-slider { grid-template-columns: 1fr 1fr; grid-template-areas: "range range" "lo hi"; }
}
.fig-which-groups input[type="range"]:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
.fig-which-groups .fig-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.fig-which-groups button {
font: inherit; font-size: 14px; height: 30px; padding: 0 12px; border-radius: 6px; cursor: pointer;
color: var(--text-color); background: var(--surface-bg); border: 1px solid var(--border-color);
}
.fig-which-groups button[aria-pressed="true"] { border-color: var(--primary-color); }
.fig-which-groups button:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
.fig-which-groups .fig-note { margin: 8px 0 0; min-height: 2.8em; color: var(--muted-color); }
</style>
<script>
Embed.mount('fig-which-groups', (ctx) => {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
ctx.root.appendChild(svg);
// A simple model, not measured data. Every attempt passes or fails on its own, with the same chance p.
// Then a group of G attempts gets one reward everywhere with probability p^G + (1 - p)^G.
const GROUPS = 12;
let seed = 7;
const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
// Fixed random draws, so moving the slider changes the sample smoothly instead of reshuffling it.
const U = Array.from({ length: GROUPS }, () => Array.from({ length: 8 }, rand));
let p = 0.5, G = 8, width = 640;
const same = (pp, g) => pp ** g + (1 - pp) ** g;
const pct = (x) => (x < 0.001 && x > 0 ? '<0.1' : x < 0.1 ? (x * 100).toFixed(1) : Math.round(x * 100)) + '%';
const controls = document.createElement('div');
controls.className = 'fig-controls';
const id = 'fig-which-groups-range-' + Math.random().toString(36).slice(2, 8);
const label = document.createElement('label');
label.htmlFor = id;
label.textContent = 'How often the model solves this problem';
const lo = document.createElement('span');
lo.className = 'fig-end lo';
lo.textContent = 'never';
const range = document.createElement('input');
Object.assign(range, { type: 'range', id, min: 0, max: 100, step: 1, value: 50 });
const hi = document.createElement('span');
hi.className = 'fig-end hi';
hi.textContent = 'always';
range.addEventListener('input', () => { p = Number(range.value) / 100; draw(); }, { signal: ctx.signal });
const slider = document.createElement('div');
slider.className = 'fig-slider';
slider.append(lo, range, hi);
const buttons = document.createElement('div');
buttons.className = 'fig-buttons';
const sizeButtons = {};
[4, 8].forEach((g) => {
const b = document.createElement('button');
b.type = 'button';
b.textContent = `${g} attempts per group`;
b.addEventListener('click', () => { G = g; draw(); }, { signal: ctx.signal });
sizeButtons[g] = b;
buttons.appendChild(b);
});
controls.append(label, slider, buttons);
const note = document.createElement('p');
note.className = 'fig-note';
ctx.root.append(controls, note);
ctx.describe('Twelve sampled groups of attempts at one problem, each attempt a pass (green circle with a check mark) or a fail (red circle with a cross). A slider sets how often the model solves the problem, and buttons switch between groups of 4 and 8 attempts. Groups where every attempt got the same result, all passes or all fails, are greyed out because they teach nothing. The chance that a group has no signal is p to the power G plus (1 minus p) to the power G: about 0.8% for 8 attempts at a 50% pass rate, 43% at 90%. Smaller groups lose their signal sooner. A simple model where each attempt passes or fails independently.');
// One attempt: a green circle with a check mark if it passed, a red one with a cross if it failed,
// so pass and fail don't rely on color alone.
function mark(h, C, cx, cy, d, pass) {
h.circle(cx, cy, d, pass ? { fill: C.greenSoft, stroke: C.green, width: 1 } : { fill: C.redSoft, stroke: C.red, width: 1 });
const k = d * 0.24, o = { stroke: pass ? C.greenText : C.redText, width: 1.6 };
if (pass) h.path(`M${cx - k * 1.15},${cy + k * 0.05} L${cx - k * 0.25},${cy + k * 0.95} L${cx + k * 1.25},${cy - k * 0.9}`, o);
else { h.line(cx - k, cy - k, cx + k, cy + k, o); h.line(cx - k, cy + k, cx + k, cy - k, o); }
}
function draw() {
Object.entries(sizeButtons).forEach(([g, b]) => b.setAttribute('aria-pressed', String(Number(g) === G)));
const C = Hand.colors(ctx.root);
svg.replaceChildren();
const h = Hand.create(svg, { seed: 5 });
const narrow = width < 560;
const cols = narrow ? 2 : 3, gap = narrow ? 10 : 16;
const cellW = (width - gap * (cols - 1)) / cols;
const dot = Math.min(16, (cellW - 16) / G - 4), step = dot + 4;
const rowH = dot + 22, rows = Math.ceil(GROUPS / cols);
const exact = same(p, G), other = G === 8 ? 4 : 8;
h.text(0, 18, `${GROUPS} groups of ${G} attempts, at a problem solved${narrow ? '\n' : ' '}${Math.round(p * 100)}% of the time`, { size: narrow ? 14 : 15, color: C.muted });
const top = narrow ? 52 : 34;
let dead = 0;
for (let k = 0; k < GROUPS; k++) {
const c = k % cols, r = Math.floor(k / cols);
const x = c * (cellW + gap), y = top + r * rowH;
const res = U[k].slice(0, G).map((u) => u < p);
const flat = res.every((v) => v === res[0]);
if (flat) dead++;
h.rect(x, y, cellW, rowH - 6, flat
? { fill: C.greySoft, stroke: C.grey, width: 1, dashed: true }
: { stroke: C.border, width: 1 });
const x0 = x + (cellW - G * step + 4) / 2;
res.forEach((pass, j) => {
const cx = x0 + j * step + dot / 2, cy = y + (rowH - 6) / 2;
mark(h, C, cx, cy, dot, pass);
});
}
let y = top + rows * rowH + 14;
// Legend: one dot per attempt.
const r = Math.min(dot, 14);
mark(h, C, r / 2 + 1, y - 5, r, true);
h.text(24, y, 'attempt passed', { size: 14, color: C.ink });
const fx = narrow ? 150 : 170;
mark(h, C, fx + r / 2 + 1, y - 5, r, false);
h.text(fx + 24, y, 'attempt failed', { size: 14, color: C.ink });
y += 26;
h.rect(0, y - 12, 16, 14, { fill: C.greySoft, stroke: C.grey, width: 1, dashed: true });
h.text(24, y, narrow ? `all the same, no signal: ${dead} of ${GROUPS}` : `all the same result, no signal: ${dead} of ${GROUPS} here`, { size: 14, color: C.ink });
y += 30;
h.text(0, y, narrow ? `Chance a group has no signal,\n${G} attempts: ${pct(exact)}` : `Chance a group has no signal, ${G} attempts: ${pct(exact)}`, { size: 16, color: C.ink });
if (narrow) y += 20;
y += 22;
h.text(0, y, `with ${other} attempts: ${pct(same(p, other))}`, { size: 14, color: C.muted });
const H = y + 8;
svg.setAttribute('viewBox', `0 0 ${width} ${H}`);
svg.setAttribute('height', H);
range.setAttribute('aria-valuetext', `${Math.round(p * 100)}%, chance of a group with no signal ${pct(exact)}`);
note.textContent = `With the same chance p for every attempt, a group has no signal with chance p^${G} + (1 − p)^${G}. Partial credit makes equal groups a little rarer in practice.`;
}
return ({ width: w }) => { width = w; draw(); };
}, { deps: [] });
</script>
</div>
</body>
</html>