#!/usr/bin/env node
// verify.mjs — Dunbar's Number, with the confidence intervals nobody shows.
//
// Recomputes, from the published data in data.mjs, every number the page states or
// draws, and pins the numeric core (Student-t, OLS) against standard tables:
//   §1  Dunbar's own equation → the human read-off (147.8) and internal consistency
//   §2  the numeric core: t-quantiles vs table, OLS recovers an exact line, band shapes
//   §3  Lindenfors 2021 Table 1: every CI brackets its point; the spread; 150 "swallowed"
//   §4  the illustrations all fall INSIDE the wide interval (so they can't confirm 150)
//   §5  drift guard: the page embeds byte-identical data
//
// Run: node research/dunbars-number/verify.mjs
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { ols, rma, bandSE, predict, tcrit, tcdf } from './stats.mjs';
import { DUNBAR, PRIMATES, HOMO, LINDENFORS, ILLUSTRATIONS } from './data.mjs';

const HERE = path.dirname(fileURLToPath(import.meta.url));
let n = 0, pass = 0;
function check(name, ok, detail = '') {
  n++;
  if (ok) { pass++; console.log(`  ok ${String(n).padStart(2)} — ${name}${detail ? '  [' + detail + ']' : ''}`); }
  else { console.error(`FAIL ${String(n).padStart(2)} — ${name}${detail ? '  [' + detail + ']' : ''}`); }
}
const close = (a, b, tol) => Math.abs(a - b) <= tol;
const log10 = (x) => Math.log(x) / Math.LN10;

console.log('§1a Dunbar 1992: reproduce the published line from the raw 36 genera');
{
  // Fit the raw data BOTH ways. Dunbar used reduced major axis (Model II) — the
  // allometric standard — so RMA should reproduce his published a & b; plain OLS
  // should reproduce his r² but a shallower slope (a nice tell that the method matters).
  const xs = PRIMATES.map(p => log10(p.cr));
  const ys = PRIMATES.map(p => log10(p.group));
  check('the fit set is 36 genera', PRIMATES.length === 36);
  const o = ols(xs, ys);
  check('OLS reproduces Dunbar\'s r² ≈ 0.764', close(o.r2, 0.764, 0.02), o.r2.toFixed(3));
  const m = rma(xs, ys);
  check('RMA slope reproduces Dunbar\'s 3.389', close(m.b, 3.389, 0.03), m.b.toFixed(3));
  check('RMA intercept reproduces Dunbar\'s 0.093', close(m.a, 0.093, 0.02), m.a.toFixed(3));
  const NfromRaw = Math.pow(10, m.a + m.b * log10(HOMO.cr));
  check('the RMA line read at Homo (CR 4.10) → 147.8 (his prediction)', close(NfromRaw, 147.8, 1.5), NfromRaw.toFixed(1));
  const maxPrimate = Math.max(...PRIMATES.map(p => p.cr));
  check('Homo (4.10) sits ABOVE every fitted primate (Pan, 3.22) — the read-off is an extrapolation',
    HOMO.cr > maxPrimate, `max primate CR = ${maxPrimate}`);
}

console.log('§1b Dunbar 1992: the published equation and the human read-off');
{
  // The page draws Dunbar's OWN published coefficients. Recompute the number.
  const yhat = DUNBAR.a + DUNBAR.b * log10(DUNBAR.humanCR);
  const N = Math.pow(10, yhat);
  check('log10(group)=0.093+3.389·log10(4.1) → 147.8 (Dunbar\'s stated prediction)',
    close(N, DUNBAR.predicted, 0.6), N.toFixed(2));
  check('the read-off rounds to the famous "150"', Math.round(N / 10) * 10 === 150, String(Math.round(N)));
  // internal consistency of the reported fit: r = sqrt(r2); SE(b)=b/t
  const r = Math.sqrt(DUNBAR.r2);
  check('r = √r² ≈ 0.874 (reported correlation)', close(r, 0.874, 0.002), r.toFixed(3));
  const seB = DUNBAR.b / DUNBAR.tSlope;      // = s/√Sxx, from t = b/SE(b)
  check('SE(slope) = b / t₃₄ ≈ 0.327 (self-consistent)', close(seB, 0.3275, 0.002), seB.toFixed(4));
  // Dunbar's own CI is already wide: the half-width is ~40% of the point on each side
  check('Dunbar\'s own 95% CI 100.2–231.1 brackets 147.8',
    DUNBAR.ci95[0] < DUNBAR.predicted && DUNBAR.predicted < DUNBAR.ci95[1]);
  check('…and it is "moderately wide": upper/lower ratio > 2',
    DUNBAR.ci95[1] / DUNBAR.ci95[0] > 2, (DUNBAR.ci95[1] / DUNBAR.ci95[0]).toFixed(2));
}

console.log('§2 the numeric core (Student-t + OLS), pinned to tables');
{
  const tcases = [[1, 12.706], [5, 2.571], [10, 2.228], [30, 2.042], [34, 2.032], [100, 1.984]];
  for (const [df, exp] of tcases)
    check(`t_{${df},0.975} = ${exp} (standard table)`, close(tcrit(0.025, df), exp, 0.01), tcrit(0.025, df).toFixed(3));
  check('tcdf is a CDF: tcdf(0,df)=0.5', close(tcdf(0, 34), 0.5, 1e-9));
  // OLS recovers an exact line y = 2 + 3x
  const f = ols([1, 2, 3, 4, 5], [5, 8, 11, 14, 17]);
  check('OLS recovers exact line: a=2, b=3, r²=1', close(f.a, 2, 1e-9) && close(f.b, 3, 1e-9) && close(f.r2, 1, 1e-12));
  // a noisy fit: prediction band is wider than confidence band, and both widen with |x0-x̄|
  const g = ols([1, 2, 3, 4, 5], [1.1, 1.9, 3.2, 3.9, 5.3]);
  const sePredNear = bandSE(g, g.xbar, 'pred'), seMeanNear = bandSE(g, g.xbar, 'mean');
  check('prediction band ⊃ confidence band (at x̄)', sePredNear > seMeanNear);
  check('band widens away from x̄ (leverage grows with extrapolation)',
    bandSE(g, g.xbar + 4, 'mean') > bandSE(g, g.xbar, 'mean'));
  // demo of the whole point: extrapolating a noisy line balloons the interval
  const tc = tcrit(0.025, g.df);
  const half0 = tc * bandSE(g, g.xbar, 'pred');
  const halfFar = tc * bandSE(g, g.xbar + 10, 'pred');
  check('extrapolating far past the data balloons the ± band > 2×', halfFar / half0 > 2, (halfFar / half0).toFixed(2) + '×');
  void predict;
}

console.log('§3 Lindenfors 2021 Table 1: the intervals that dwarf 150');
{
  let allBracket = true, minLo = Infinity, maxHi = -Infinity, minPt = Infinity, maxPt = -Infinity, contain150 = 0;
  for (const r of LINDENFORS) {
    if (!(r.lo < r.point && r.point < r.hi)) allBracket = false;
    minLo = Math.min(minLo, r.lo); maxHi = Math.max(maxHi, r.hi);
    minPt = Math.min(minPt, r.point); maxPt = Math.max(maxPt, r.point);
    if (r.lo <= 150 && 150 <= r.hi) contain150++;
  }
  check('8 methods reported', LINDENFORS.length === 8);
  check('every 95% CI brackets its own point estimate', allBracket);
  check('point estimates span ~16 to ~109', close(minPt, 16.4, 0.1) && close(maxPt, 108.6, 0.1), `${minPt}–${maxPt}`);
  check('95% CIs span from ~2 to ~520', close(minLo, 2.1, 0.1) && close(maxHi, 520.0, 0.1), `${minLo}–${maxHi}`);
  // the swallowing: most method CIs are so wide they include 150 AND half the point estimates are well below it
  check('≥5 of 8 method CIs are wide enough to include 150', contain150 >= 5, `${contain150}/8`);
  check('a majority of point estimates sit BELOW 150', LINDENFORS.filter(r => r.point < 150).length >= 5,
    `${LINDENFORS.filter(r => r.point < 150).length}/8`);
}

console.log('§4 the illustrations all land INSIDE the wide interval');
{
  // The honest claim: a band this wide is "confirmed" by almost any human grouping.
  // Take the interval to be Dunbar's own conservative one, [100.2, 231.1]... but the
  // stronger point uses the phylogenetic span. Show every illustration sits inside 2–520.
  const LO = 2, HI = 520;
  let inside = 0, total = 0;
  for (const it of ILLUSTRATIONS) {
    const vals = [];
    if (typeof it.value === 'number') vals.push(it.value);
    if (it.range) vals.push(it.range[0], it.range[1]);
    for (const v of vals) { total++; if (v >= LO && v <= HI) inside++; }
  }
  check('every illustrative figure falls within the phylogenetic 95% span (2–520)', inside === total, `${inside}/${total}`);
  // and the "confirming" numbers scatter widely themselves (106.9 … 290) — not a tight cluster on 150
  const pts = ILLUSTRATIONS.filter(i => typeof i.value === 'number').map(i => i.value);
  check('the "confirming" figures themselves scatter (min<120, max>200)',
    Math.min(...pts) < 120 && Math.max(...pts) > 200, `${Math.min(...pts)}…${Math.max(...pts)}`);
}

console.log('§5 drift guard: the page embeds byte-identical data');
{
  const html = fs.readFileSync(path.join(HERE, '../../public/strata/dunbars-number/index.html'), 'utf8');
  const m = html.match(/<script[^>]*id="dunbar-data"[^>]*>([\s\S]*?)<\/script>/);
  check('page carries an embedded #dunbar-data JSON block', !!m);
  if (m) {
    let embedded = null;
    try { embedded = JSON.parse(m[1].trim()); } catch (e) { /* leave null */ }
    check('the embedded block is valid JSON', !!embedded);
    if (embedded) {
      const canon = JSON.parse(JSON.stringify({ DUNBAR, PRIMATES, HOMO, LINDENFORS, ILLUSTRATIONS }));
      check('page DUNBAR === data.mjs DUNBAR', JSON.stringify(embedded.DUNBAR) === JSON.stringify(canon.DUNBAR));
      check('page PRIMATES === data.mjs PRIMATES (all 36 rows)', JSON.stringify(embedded.PRIMATES) === JSON.stringify(canon.PRIMATES));
      check('page HOMO === data.mjs HOMO', JSON.stringify(embedded.HOMO) === JSON.stringify(canon.HOMO));
      check('page LINDENFORS === data.mjs LINDENFORS', JSON.stringify(embedded.LINDENFORS) === JSON.stringify(canon.LINDENFORS));
      check('page ILLUSTRATIONS === data.mjs ILLUSTRATIONS', JSON.stringify(embedded.ILLUSTRATIONS) === JSON.stringify(canon.ILLUSTRATIONS));
    }
  }
}

console.log(`\n${pass}/${n} checks passed`);
process.exit(pass === n ? 0 : 1);
