// verify.mjs — the offline math notebook for the stratum
// "The Planes That Didn't Come Back" (survivorship bias / Abraham Wald, 1943).
//
// The claim the page rests on: the holes on the bombers that RETURNED are not a map of
// where bombers get hit — they are a map of where a bomber can be hit and still come home.
// Reinforce where the survivors are *unmarked*. We make that precise and re-derive every
// number on the page from a model whose every step is checkable, two ways:
//
//   (1) closed form (Poisson thinning), and
//   (2) a seeded Monte-Carlo fleet (no libraries; mulberry32 PRNG, Knuth Poisson),
//
// and we show the full INVERSION: given the areas a_i (exposure) and the overall loss
// rate, the survivor hole distribution recovers each region's lethality v_i *exactly* —
// and that the one input the folk reading deletes is a_i (and the planes you never see).
//
//   node research/the-planes-that-didnt-come-back/verify.mjs
//
// Exit code 0 iff all checks pass. Rows 0 to 5 use the page's illustrative model;
// rows W1 to W5 recompute the two Part I examples, Part V and Part VIII from Wald's
// memoranda. See the honesty note at the foot of this file and in the stratum's apparatus.

let fails = 0;
const ok = (name, cond, extra = '') => {
  if (!cond) fails++;
  console.log(`${cond ? '  ok  ' : 'FAIL  '}${name}${extra ? '   ' + extra : ''}`);
};
const close = (a, b, eps = 1e-9) => Math.abs(a - b) <= eps;

// ---- seeded PRNG (mulberry32) + Knuth Poisson, so the fleet is reproducible ----
function mulberry32(seed) {
  let a = seed >>> 0;
  return function () {
    a |= 0; a = (a + 0x6D2B79F5) | 0;
    let t = Math.imul(a ^ (a >>> 15), 1 | a);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
function poisson(rng, lambda) {           // Knuth
  const L = Math.exp(-lambda);
  let k = 0, p = 1;
  do { k++; p *= rng(); } while (p > L);
  return k - 1;
}

// ---- the model — five regions of a bomber; ILLUSTRATIVE area + lethality ----
// a_i: share of hits that land in region i (proportional to exposed area; sums to 1).
// v_i: probability that a single hit there downs the plane (its lethality/vulnerability).
const REGION = ['engine', 'cockpit', 'fuel', 'fuselage', 'wings/tail'];
const a = [0.15, 0.08, 0.10, 0.40, 0.27];   // exposure — the deleted baseline
const v = [0.40, 0.35, 0.30, 0.05, 0.10];   // true lethality (what we want to recover)
const lambda = 6;                            // mean hits per mission (illustrative)

const sum = (xs) => xs.reduce((s, x) => s + x, 0);

console.log('\n=== ROW 0 · the model is well-formed ===');
ok('areas a_i sum to 1', close(sum(a), 1));
ok('every a_i, v_i in [0,1]', a.every(x => x >= 0 && x <= 1) && v.every(x => x >= 0 && x <= 1));

console.log('\n=== ROW 1 · survival probability (Poisson thinning) ===');
// Lethal hits ~ Poisson(lambda * sum(a_i v_i)). Survive iff zero lethal hits.
const meanLethalRate = sum(a.map((ai, i) => ai * v[i]));        // sum a_i v_i
const pSurvive = Math.exp(-lambda * meanLethalRate);
ok('sum a_i v_i = 0.165', close(meanLethalRate, 0.165, 1e-12), meanLethalRate.toFixed(4));
ok('P(survive) = exp(-lambda * sum a_i v_i) = exp(-0.99)', close(pSurvive, Math.exp(-0.99)), pSurvive.toFixed(4));
ok('  ...so ~37.2% come home, ~62.8% are lost', close(pSurvive, 0.3716, 5e-4), (pSurvive * 100).toFixed(1) + '%');

console.log('\n=== ROW 2 · the survivor hole distribution q_i ∝ a_i (1 - v_i) ===');
// Among survivors, every visible hole is a NON-lethal hit. A hit is non-lethal-and-in-i
// with weight a_i(1-v_i); given a survivor, its holes' regions are distributed as q_i.
const w = a.map((ai, i) => ai * (1 - v[i]));   // a_i (1 - v_i)
const Z = sum(w);                               // = 1 - sum a_i v_i
const q = w.map(wi => wi / Z);
ok('Z = sum a_i(1-v_i) = 1 - sum a_i v_i = 0.835', close(Z, 1 - meanLethalRate) && close(Z, 0.835, 1e-12), Z.toFixed(4));
ok('q sums to 1', close(sum(q), 1));
// The naive eye reinforces where survivors have the MOST holes:
const naiveIdx = q.indexOf(Math.max(...q));
ok('most survivor holes land on the FUSELAGE (the naive pick)', REGION[naiveIdx] === 'fuselage',
   `${REGION[naiveIdx]} @ ${(q[naiveIdx] * 100).toFixed(1)}%`);
// Wald reinforces where holes are scarcest RELATIVE TO AREA — q_i/a_i ∝ (1 - v_i):
const ratio = q.map((qi, i) => qi / a[i]);
const waldIdx = ratio.indexOf(Math.min(...ratio));
ok('fewest holes-per-area is the ENGINE (Wald\'s pick — the true weak spot)', REGION[waldIdx] === 'engine',
   `${REGION[waldIdx]}, q/a = ${ratio[waldIdx].toFixed(3)}`);
ok('the two recommendations DIFFER — the bias has teeth', naiveIdx !== waldIdx);
// q_i/a_i is proportional to (1 - v_i): verify the proportionality exactly.
let propOK = true;
for (let i = 0; i < REGION.length; i++) propOK = propOK && close(ratio[i], (1 - v[i]) / Z);
ok('q_i / a_i = (1 - v_i) / Z exactly for every region', propOK);

console.log('\n=== ROW 3 · the full inversion — recover each v_i from survivor data + loss rate ===');
// Deleted-baseline inputs you DO have if you keep them: a_i (areas) and the loss rate.
// Z = 1 + ln(P_survive)/lambda  (from ROW 1), then  v_i = 1 - q_i * Z / a_i.
const Zhat = 1 + Math.log(pSurvive) / lambda;
ok('Z recovered from the loss rate alone matches', close(Zhat, Z), Zhat.toFixed(4));
const vHat = q.map((qi, i) => 1 - qi * Zhat / a[i]);
let invOK = true;
for (let i = 0; i < REGION.length; i++) invOK = invOK && close(vHat[i], v[i], 1e-9);
ok('recovered v_i equals the true lethality for every region (exact)', invOK,
   vHat.map((x, i) => `${REGION[i]}=${x.toFixed(3)}`).join(' '));
// And the folk reading, which keeps q_i but DROPS a_i, ranks lethality backwards on top:
const folkRankTop = q.indexOf(Math.max(...q));          // "most holes = most dangerous"
ok('drop a_i and you would armor the SAFEST region (rank inverted at the top)',
   folkRankTop === naiveIdx && v[folkRankTop] === Math.min(...v),
   `${REGION[folkRankTop]} has the LOWEST true lethality v=${v[folkRankTop]}`);

console.log('\n=== ROW 4 · Monte-Carlo fleet confirms the closed forms (seeded) ===');
{
  const rng = mulberry32(19431101);   // Wald's memoranda, Nov 1943
  const N = 400000;
  let survived = 0;
  const holeHist = [0, 0, 0, 0, 0];   // survivor holes per region
  for (let p = 0; p < N; p++) {
    const hits = poisson(rng, lambda);
    let downed = false;
    const planeHoles = [0, 0, 0, 0, 0];
    for (let h = 0; h < hits; h++) {
      // choose region by area weights a
      let r = rng(), i = 0, acc = a[0];
      while (r > acc && i < a.length - 1) { i++; acc += a[i]; }
      if (rng() < v[i]) downed = true;     // lethal hit
      else planeHoles[i]++;                 // a visible hole on a survivor
    }
    if (!downed) {
      survived++;
      for (let i = 0; i < 5; i++) holeHist[i] += planeHoles[i];
    }
  }
  const pSurvMC = survived / N;
  ok('MC survival rate ≈ analytic 0.3716', close(pSurvMC, pSurvive, 3e-3), pSurvMC.toFixed(4));
  const totalHoles = sum(holeHist);
  const qMC = holeHist.map(h => h / totalHoles);
  let histOK = true;
  for (let i = 0; i < 5; i++) histOK = histOK && close(qMC[i], q[i], 5e-3);
  ok('MC survivor hole distribution ≈ analytic q_i (all 5 regions within 0.5pp)', histOK,
     qMC.map((x, i) => `${REGION[i]}=${(x * 100).toFixed(1)}%`).join(' '));
  // Re-run the inversion on the MC-estimated quantities — recover lethality from noisy data:
  const ZhatMC = 1 + Math.log(pSurvMC) / lambda;
  const vHatMC = qMC.map((qi, i) => 1 - qi * ZhatMC / a[i]);
  let invMC = true;
  for (let i = 0; i < 5; i++) invMC = invMC && close(vHatMC[i], v[i], 1.2e-2);
  ok('inversion on noisy MC data still recovers every v_i within ~1pp', invMC,
     vHatMC.map((x, i) => `${REGION[i]}=${x.toFixed(3)}`).join(' '));
  // The Wald pick is robust under sampling noise too:
  const ratioMC = qMC.map((qi, i) => qi / a[i]);
  ok('MC also flags the engine as fewest-holes-per-area', REGION[ratioMC.indexOf(Math.min(...ratioMC))] === 'engine');
}

console.log('\n=== ROW 5 · the everyday face — same selection, no numbers claimed ===');
// These are stated on the page as INSTANCES of the identical mechanism (condition on a
// visible-only sample); none carries a numeric claim, so nothing here to over-assert.
ok('mechanism named, not numbers invented: selection on a survival/visibility filter', true);

// ═══ WALD'S OWN NUMBERS (added 2026-09-26) ═══════════════════════════════════════
// Everything above is the page's illustrative model. Below, the same module the page runs
// recomputes the two Part I examples, Part V and Part VIII (CRC 432 reprint), with W3
// simulating a fleet under his assumptions. Only Part V and Table 8 of Part VIII run live
// in the browser; the examples in Parts III, IV and VI are not recomputed here.
// In the repository this is the file the page serves. Run from /checks/ in an empty folder, there
// is no such file beside it, so fetch the one the deployed page runs and import those bytes.
const W = await import(new URL('../../public/strata/the-planes-that-didnt-come-back/wald.js', import.meta.url))
  .catch(async () => {
    const src = await (await fetch('https://artwaste.land/strata/the-planes-that-didnt-come-back/wald.js')).text();
    console.log('(wald.js fetched from the deployed page, ' + src.length + ' bytes)');
    return import('data:text/javascript;base64,' + Buffer.from(src).toString('base64'));
  });
const { WALD } = W;
const r = (x, d) => Math.round(x * 10 ** d) / 10 ** d;

console.log('\n=== ROW W1 · Part I (pp. 8-9): both worked examples reproduce ===');
{
  const e1 = WALD.partI.example1, a1 = e1.home.map(x => x / e1.sent);
  const q1 = W.rootQ(a1);
  ok('example 1: root of .200q^5 - .080q^4 - .050q^3 - .010q^2 - .005q - .005 is .851 (printed .851)', r(q1, 3) === e1.printedQ, q1.toFixed(5));
  const x1 = W.downedByHit(a1, q1);
  ok('example 1: planes downed by the 1st..5th hit match his .030 .013 .004 .002 .001', x1.every((x, i) => r(x, 3) === e1.printedX[i]), x1.map(x => x.toFixed(4)).join(' '));
  ok('example 1: those five add up to the 5% lost (20 of 400)', close(x1.reduce((s, x) => s + x, 0), 20 / 400, 1e-12));
  const e2 = WALD.partI.example2, q2 = W.rootQ(e2.a);
  ok('example 2: q = .87 (printed .87)', r(q2, 2) === e2.printedQ, q2.toFixed(5));
  const x2 = W.downedByHit(e2.a, q2);
  ok('example 2: downed-by-hit .09 .05 .03 .02 .01 match to his two places', x2.every((x, i) => r(x, 2) === e2.printedX[i]), x2.map(x => x.toFixed(4)).join(' '));
}

console.log('\n=== ROW W2 · Part V (pp. 62-65): 400 planes, 359 home, four parts ===');
const V = WALD.partV;
const pv = W.waldPartV({ sent: V.sent, home: V.home, hitsOnPart: V.hitsOnPart, areaOfPart: V.areaOfPart });
ok('359 returned, 41 lost', pv.returned === 359 && pv.lost === 41);
ok('the returners carry 68 + 2(29) + 3(12) + 4(10) = 202 holes, and the parts table also sums to 202', pv.holes === 202 && pv.holesAgree);
ok('his a_i are the counts over 400 (a_2 printed .072 for 29/400 = .0725)', V.home.every((x, i) => Math.abs(x / 400 - V.printedA[i]) <= 0.0005 + 1e-12));
ok('his gamma(i) are the areas over 130 sq ft, to three places', pv.parts.every((p, i) => r(p.gamma, 3) === V.printedGamma[i]));
ok('his delta(i) are the holes over 202 to three places, except fuel: 31/202 = .1535, printed .154 (which makes q(fuel) = q0 exactly)',
   pv.parts.every((p, i) => i === 2 ? (r(p.delta, 3) === 0.153 && V.printedDelta[2] === 0.154) : r(p.delta, 3) === V.printedDelta[i]),
   pv.parts.map(p => p.delta.toFixed(4)).join(' '));
// The one number that does not reproduce: q0.
const f = (q) => .4 * q ** 4 - .170 * q ** 3 - .072 * q ** 2 - .030 * q - .025;
const qPrinted = W.rootQ(V.printedA), qExact = pv.q0;
ok('FINDING: his printed equation .4q^4 - .170q^3 - .072q^2 - .030q - .025 = 0 has its root at .8459, not the printed .850',
   r(qPrinted, 4) === 0.8459 && f(0.845) < 0 && f(0.846) > 0 && f(0.850) > 0, `root ${qPrinted.toFixed(5)}; f(.850) = ${f(0.85).toExponential(2)} > 0`);
ok('  ...and with the unrounded a_2 = 29/400 the root is .8467; both still round to his .85 at two places',
   r(qExact, 4) === 0.8467 && r(qPrinted, 2) === 0.85 && r(qExact, 2) === 0.85);
ok('with his printed inputs (.850, three-place deltas and gammas) his four q(i) .61 .95 .85 .98 reproduce exactly',
   V.printedDelta.every((d, i) => r(d / V.printedGamma[i] * 0.85, 2) === V.printedQ[i]));
const qFromCounts = pv.parts.map(p => p.q);
ok('from the raw counts, q(i) = .607 .944 .845 .981: at two places the fuselage moves .95 to .94 and fuel .85 to .84, the engine and other parts do not',
   [0.607, 0.944, 0.845, 0.981].every((x, i) => r(qFromCounts[i], 3) === x), qFromCounts.map(x => x.toFixed(4)).join(' '));
ok('the ranking he concluded is untouched: engines most vulnerable, then fuel, fuselage, other parts',
   JSON.stringify(qFromCounts.map((x, i) => [x, i]).sort((m, n) => m[0] - n[0]).map(x => x[1])) === '[0,2,1,3]');
ok('the part estimates are consistent with the whole plane: sum gamma(i) q(i) = q0 exactly',
   close(pv.parts.reduce((s, p) => s + p.gamma * p.q, 0), pv.q0, 1e-12));
ok('the fuselage carries the MOST holes (78 of 202) and is among the LEAST vulnerable: the page\'s one-line bias, in his own table',
   V.hitsOnPart.indexOf(Math.max(...V.hitsOnPart)) === 1 && qFromCounts[1] > qFromCounts[0] && qFromCounts[1] > qFromCounts[2]);
ok('his q(i) = delta(i)/gamma(i) * q0 IS the page\'s q_i/a_i ∝ (1 - v_i): the gloss is his equation 82', close(
   pv.parts[0].q / pv.parts[1].q, (pv.parts[0].delta / pv.parts[0].gamma) / (pv.parts[1].delta / pv.parts[1].gamma), 1e-12));

console.log('\n=== ROW W3 · Part V is exact, not an approximation (a fleet flown under his assumptions) ===');
{
  // Hits per plane from ANY distribution capped at n (Wald never assumes Poisson), each hit's
  // part drawn with gamma, each hit survived with q(part). Then run his estimator on the fleet.
  const rng = mulberry32(1943);
  const gam = [35, 45, 20, 30].map(x => x / 130), qTrue = [0.6, 0.95, 0.85, 0.98];
  const hitDist = [0.35, 0.25, 0.2, 0.12, 0.08];       // P(0..4 hits): deliberately not Poisson
  const N = 400000, home = [0, 0, 0, 0, 0], onPart = [0, 0, 0, 0];
  for (let k = 0; k < N; k++) {
    let u = rng(), n = 0, acc = hitDist[0];
    while (u > acc && n < 4) { n++; acc += hitDist[n]; }
    const holes = [0, 0, 0, 0]; let alive = true;
    for (let h = 0; h < n && alive; h++) {
      let v = rng(), i = 0, g = gam[0];
      while (v > g && i < 3) { i++; g += gam[i]; }
      if (rng() < qTrue[i]) holes[i]++; else alive = false;
    }
    if (alive) { home[n]++; for (let i = 0; i < 4; i++) onPart[i] += holes[i]; }
  }
  const est = W.waldPartV({ sent: N, home, hitsOnPart: onPart, areaOfPart: [35, 45, 20, 30] });
  const qBar = gam.reduce((s, g, i) => s + g * qTrue[i], 0);
  ok('his q0 recovers the fleet\'s true per-hit survival sum gamma q', close(est.q0, qBar, 3e-3), `${est.q0.toFixed(4)} vs ${qBar.toFixed(4)}`);
  ok('his q(i) recover every part\'s true survival within sampling error (0.01)', est.parts.every((p, i) => close(p.q, qTrue[i], 0.01)),
     est.parts.map(p => p.q.toFixed(3)).join(' '));
}

console.log('\n=== ROW W4 · Part VIII (pp. 82-89): three guns, four parts, Table 8 ===');
const E = WALD.partVIII;
ok('634 attacked, 602 returned, 32 lost; 399 holes on the returners, and Table 7 also totals 399',
   E.home.reduce((s, x) => s + x, 0) === 602 && E.sent - 602 === E.lost &&
   E.home.reduce((s, x, i) => s + i * x, 0) === 399 && E.hits.flat().reduce((s, x) => s + x, 0) === 399);
const p8 = W.waldPartVIII({ sent: E.sent, home: E.home, hits: E.hits, gammaGivenGun: E.gammaGivenGun, round: true });
ok('root of 248q^5 - 120q^4 - 47q^3 - 22q^2 - 16q - 11 = 0 is .930 (printed .930)', r(p8.q0, 3) === E.printedQ0, p8.q0.toFixed(5));
// His lambda uses q0 = .930 as printed; recompute with his rounding to compare like with like.
const p8w = (() => { const lam = 0.930 * p8.deltaGun.reduce((s, d, j) => s + d / r(p8.rho[j], 3), 0); return lam; })();
ok('lambda = .930 x (.737/.984 + .125/.850 + .138/.936): the bracket is 1.0435, printed 1.0433, so .9704 where he prints .9703',
   r(p8w / 0.930, 4) === 1.0435 && r(p8w, 4) === 0.9704 && r(0.930 * 1.0433, 4) === E.printedLambda, p8w.toFixed(5));
const qGunW = p8.rho.map(x => r(E.printedLambda * r(x, 3), 3));
ok('q(flak) .955, q(20-mm) .825, q(7.9-mm) .908 (printed)', qGunW.every((x, i) => x === E.printedQGun[i]), qGunW.join(' '));
const vulnW = p8.deltaGivenGun.map((row, j) => row.map((d, i) => r(1 - d / E.gammaGivenGun[j][i] * E.printedQGun[j], 3)));
ok('all twelve cells of Table 8 reproduce from his printed intermediates', vulnW.every((row, j) => row.every((x, i) => x === E.printedVulnerability[j][i])),
   vulnW.map(r => r.join(' ')).join(' | '));
ok('his headline cell: .534, a 20-mm cannon shell on the engine, is the largest; next is 7.9-mm on the forward fuselage (.194)',
   Math.max(...vulnW.flat()) === 0.534 && [...vulnW.flat()].sort((m, n) => n - m)[1] === 0.194);
ok('gun-only column: .045 .175 .092 = 1 - q(gun); any hit at all: .070 = 1 - q0',
   E.printedQGun.every((q, j) => r(1 - q, 3) === E.printedGunOnly[j]) && r(1 - 0.930, 3) === E.printedAnyHit);
const colDelta = [0, 1, 2, 3].map(i => r(E.hits.reduce((s, row) => s + row[i], 0) / 399, 3));
const partOnlyW = colDelta.map((d, i) => r(1 - d / E.assumedGamma[i] * 0.930, 3));
ok('part-only row .114 .179 .074 .038 reproduces by Part V with his ASSUMED gamma .084 .128 .212 .576',
   partOnlyW.every((x, i) => x === E.printedPartOnly[i]), partOnlyW.join(' '));
// Unrounded, the same table differs only in the third place.
const p8x = W.waldPartVIII({ sent: E.sent, home: E.home, hits: E.hits, gammaGivenGun: E.gammaGivenGun });
const vulnX = p8x.q.map(row => row.map(q => 1 - q));
ok('unrounded, every Table 8 cell is within .004 of the printed one', vulnX.every((row, j) => row.every((x, i) => Math.abs(x - E.printedVulnerability[j][i]) <= 0.004)),
   vulnX.map(r => r.map(x => x.toFixed(3)).join(' ')).join(' | '));

console.log('\n=== ROW W5 · FINDING: the baseline Part VIII implies is not the one its footnote assumes ===');
// The hits a gun lands AT the target are its survivor hits divided by its survival chance.
const wMix = p8x.gunTotals.map((t, j) => t / 399 / p8x.qGun[j]);
const wSum = wMix.reduce((s, x) => s + x, 0), wn = wMix.map(x => x / wSum);
const gImp = [0, 1, 2, 3].map(i => wn.reduce((s, w, j) => s + w * E.gammaGivenGun[j][i], 0));
ok('the part-VIII model\'s own hit mix makes gamma = .082 .136 .210 .571 (footnote assumes .084 .128 .212 .576)',
   [0.082, 0.136, 0.210, 0.571].every((x, i) => r(gImp[i], 3) === x), gImp.map(x => x.toFixed(4)).join(' '));
ok('  ...and that mix reproduces q0 exactly: sum over guns of mix x q(gun) = .9299', close(wn.reduce((s, w, j) => s + w * p8x.qGun[j], 0), p8x.q0, 1e-12));
const partOnlyImp = [0, 1, 2, 3].map(i => 1 - wn.reduce((s, w, j) => s + w * E.gammaGivenGun[j][i] * p8x.q[j][i], 0) / gImp[i]);
ok('with the implied baseline the engine\'s part-only vulnerability is .229, not the printed .179; the ranking holds',
   r(partOnlyImp[1], 3) === 0.229 && partOnlyImp.indexOf(Math.max(...partOnlyImp)) === 1, partOnlyImp.map(x => x.toFixed(3)).join(' '));

console.log(`\n${fails === 0 ? 'ALL CHECKS PASS' : fails + ' CHECK(S) FAILED'}\n`);
process.exit(fails === 0 ? 0 : 1);

// ── HONESTY NOTE ───────────────────────────────────────────────────────────────
// • The five regions and their a_i / v_i / lambda are ILLUSTRATIVE — a transparent model
//   to make the mechanism operable, NOT measurements from Wald's memoranda. The page says so.
// • Abraham Wald, "A Method of Estimating Plane Vulnerability Based on Damage of Survivors"
//   (Statistical Research Group, Columbia, 8 memoranda, 1943; reprinted 1980 as CRC 432,
//   Center for Naval Analyses). Analyzed by Mangel & Samaniego, J. Amer. Statist. Assoc.
//   79 (1984), 259–267. Popularized by Jordan Ellenberg, "How Not to Be Wrong" (2014).
// • The iconic red-dotted-bomber cartoon is a MODERN illustration, not Wald's; the pithy
//   "armor where the holes aren't" is a faithful-in-spirit gloss of his method, not a quote.
// • Model idealizations (named on the page): hits independent & Poisson; per-region kill
//   probabilities independent; one mission; areas known. Real combat damage can violate
//   these assumptions; the mechanism is the lesson, the exact figures are the model's, not the war's.
