use it elsewhere

Biased coin entropy

Put it on your own page with two lines, or fork it and make it yours. It is CC-BY-4.0.

Embed

The script finds its own origin, so nothing else is needed; the widget runs in a sandboxed frame and cannot touch your page. Set the controls and the two lines update; the preview beneath shows what your readers get.

<script src="https://learn.mimmsy.com/learn-widget.js"></script> <learn-widget name="coin-entropy"></learn-widget>

It is CC-BY-4.0: keep the credit line the frame shows.

Preview

Fork

A fork is a copy whose manifest names its parent and the parent's version; lineage is kept forever, and the copy is yours to change. The library is its own repository: clone it, copy coin-entropy/ to a new name (one DNS label), set forkedFrom to { "name": "coin-entropy", "version": 1 }, change what you want, run npm run check, and open a pull request. Passing the check is the whole gate. Without a checkout, submit the same files to POST /api/widgets and it is served instantly as an unreviewed draft.

Source

The whole widget is these files; the repository has their history.

widget.json

{
  "name": "coin-entropy",
  "title": "Biased coin entropy",
  "version": 1,
  "claim": "A coin's entropy H(p) = −p log₂ p − (1−p) log₂(1−p) peaks at exactly 1 bit per flip when p = ½ and falls to 0 as the coin becomes certain.",
  "summary": "Drag P(heads) and watch a stream of flips: a fair coin is maximally unpredictable, a 95% coin produces a repetitive stream whose flips carry almost no information, and the readout follows the binary entropy curve.",
  "topics": [
    "information/entropy",
    "math/probability"
  ],
  "aliases": [
    "binary entropy",
    "Shannon entropy",
    "H(p)",
    "biased coin"
  ],
  "params": {
    "p": {
      "type": "number",
      "default": 0.5,
      "min": 0,
      "max": 1,
      "label": "P(heads)"
    }
  },
  "check": [
    {
      "q": "Which coin gives you the most information per flip, on average?",
      "options": [
        "P(heads) = 0.5",
        "P(heads) = 0.99",
        "P(heads) = 0.01",
        "A two-headed coin"
      ],
      "answer": 0,
      "why": "The binary entropy curve peaks at p = ½, where a flip is least predictable and carries exactly 1 bit, while a two-headed coin's flips carry 0 bits."
    },
    {
      "q": "A coin lands heads 95% of the time. Compared to a fair coin, its entropy per flip is…",
      "options": [
        "higher, because the rare outcome carries more information",
        "the same, because there are still two outcomes",
        "much lower, about 0.29 bits",
        "exactly 0"
      ],
      "answer": 2,
      "why": "H(0.95) ≈ 0.29 bits: the rare tails carries 4.3 bits when it occurs, but it occurs too rarely to raise the average."
    }
  ],
  "capabilities": [],
  "height": 490,
  "requires": [
    "surprise-bits"
  ],
  "forkedFrom": null,
  "authors": []
}

index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Biased coin entropy</title>
<style>
  :root {
    color-scheme: light dark;
    --bg: #f7f3ea; --bg-card: #efe9da; --border: #d8cfba; --text: #211d14; --text-dim: #6e6553;
    --accent: #31597f; --accent2: #b04e1b; --hot: #a82433; --ok: #3d6b4f;
    --ink-rgb: 33, 29, 20; --paper-rgb: 247, 243, 234; --accent-rgb: 49, 89, 127;
    --accent2-rgb: 176, 78, 27; --hot-rgb: 168, 36, 51; --ok-rgb: 61, 107, 79;
    --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #161410; --bg-card: #1e1b15; --border: #383225; --text: #e9e3d3; --text-dim: #9c917c;
      --accent: #8fb8e0; --accent2: #dd9355; --hot: #df7a88; --ok: #82bd97;
      --ink-rgb: 233, 227, 211; --paper-rgb: 22, 20, 16; --accent-rgb: 143, 184, 224;
      --accent2-rgb: 221, 147, 85; --hot-rgb: 223, 122, 136; --ok-rgb: 130, 189, 151;
    }
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; }
  body { background: transparent; color: var(--text); font-family: var(--serif); font-size: 17px; line-height: 1.5; }
  .widget { background: var(--bg-card); border: 1px solid var(--border); border-radius: 4px; padding: 22px; }
  .control-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
  .control-row label { font-size: 14px; color: var(--text-dim); white-space: nowrap; }
  input[type="range"] { flex: 1; min-width: 160px; accent-color: var(--accent); margin: 0; }
  .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
  .val { min-width: 4ch; font-size: 14px; }
  .btn { font-family: var(--mono); font-size: 12.5px; color: var(--text); background: transparent; border: 1px solid var(--border); border-radius: 3px; padding: 6px 13px; cursor: pointer; transition: border-color .15s, color .15s; }
  .btn:hover { border-color: var(--accent); color: var(--accent); }
  .stream { font-family: var(--mono); font-size: 15px; letter-spacing: 3px; word-break: break-all; line-height: 1.5; height: 72px; overflow: hidden; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; user-select: none; }
  .stream .h { color: var(--accent); }
  .stream .t { color: var(--accent2); }
  .readout-row { display: flex; gap: 28px; flex-wrap: wrap; align-items: flex-end; margin: 14px 0; }
  .readout .big { display: block; font-size: 34px; font-weight: 600; color: var(--accent); line-height: 1.1; min-width: 5ch; }
  .readout.accent .big { color: var(--accent2); }
  .readout small { color: var(--text-dim); font-size: 12.5px; }
  .plot { display: block; width: 100%; height: 220px; margin-top: 8px; }
  @media (max-width: 560px) { .readout .big { font-size: 27px; } }
</style>
</head>
<body>
  <div class="widget">
    <div class="control-row">
      <label for="p">P(heads)</label>
      <input type="range" id="p" min="0" max="100" value="50">
      <span class="mono val" id="p-label">0.50</span>
      <button id="reflip" class="btn">flip again</button>
    </div>
    <div id="stream" class="stream"></div>
    <div class="readout-row">
      <div class="readout">
        <span class="big mono" id="h">1.00</span>
        <small>H(p) &middot; bits per flip</small>
      </div>
      <div class="readout accent">
        <span class="big mono" id="i-heads">1.00</span>
        <small>bits when heads &middot; &minus;log&#8322; p</small>
      </div>
      <div class="readout accent">
        <span class="big mono" id="i-tails">1.00</span>
        <small>bits when tails &middot; &minus;log&#8322;(1&minus;p)</small>
      </div>
    </div>
    <canvas id="plot" class="plot"></canvas>
  </div>
  <script src="/w/_sdk/host.js?v=1"></script>
  <script src="/w/_lib/info.js?v=1"></script>
  <script src="widget.js?v=1"></script>
</body>
</html>

widget.js

/* Biased coin entropy: a stream of flips at P(heads) = p and the binary entropy curve H(p). */
(() => {
  'use strict';
  const $ = (id) => document.getElementById(id);
  const { clamp, binaryH, readTheme, plotCurve, onResize } = window.Info;
  let p = 0.5;
  let host = null;

  const surprise = (x) => (x > 0 ? (-Math.log2(x)).toFixed(2) : '∞');

  function renderStream() {
    const N = clamp(Math.floor(($('stream').clientWidth - 28) / 12.2) * 2, 24, 128);
    let html = '';
    for (let i = 0; i < N; i++) {
      html += Math.random() < p ? '<span class="h">H</span>' : '<span class="t">T</span>';
    }
    $('stream').innerHTML = html;
  }

  function render() {
    $('p-label').textContent = p.toFixed(2);
    $('h').textContent = binaryH(p).toFixed(2);
    $('i-heads').textContent = surprise(p);
    $('i-tails').textContent = surprise(1 - p);
    plotCurve($('plot'), {
      fn: binaryH, xmin: 0, xmax: 1, ymax: 1.05, dotX: p, xlabel: 'P(heads)', ylabel: 'H bits',
    });
  }

  function setP(v, fromSlider) {
    p = clamp(v, 0, 1);
    if (!fromSlider) $('p').value = Math.round(p * 100);
    render();
    renderStream();
  }

  const ping = (data) => { if (host && host.signal) host.signal('interaction', data); };

  $('p').addEventListener('input', () => { setP($('p').value / 100, true); ping({ p }); });
  $('reflip').addEventListener('click', () => { renderStream(); ping({ p }); });
  onResize(render);

  readTheme();
  render();
  renderStream();

  LearnWidget.connect().then((h) => {
    host = h;
    readTheme();
    if (typeof h.params.p === 'number') setP(h.params.p);
    else render();
    h.on('theme.changed', () => { readTheme(); render(); });
    h.on('params.changed', (np) => { if (typeof np.p === 'number') setP(np.p); });
  });
})();