#!/usr/bin/env node

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const here = path.dirname(fileURLToPath(import.meta.url));
const pagePath = path.resolve(here, "../../public/strata/the-exponent-that-bends/index.html");
const html = fs.readFileSync(pagePath, "utf8");
const match = html.match(/<script id="mammal-data" type="application\/json">([\s\S]*?)<\/script>/);

if (!match) {
  throw new Error("Could not find the embedded mammal dataset in the public page");
}

const sourceRows = JSON.parse(match[1]);
const data = sourceRows.map(([species, order, mass, bmr]) => ({
  species,
  order,
  mass,
  bmr,
  x: Math.log10(mass),
  y: Math.log10(bmr),
}));

function solve(matrix, vector) {
  const n = vector.length;
  const augmented = matrix.map((row, i) => [...row, vector[i]]);
  for (let column = 0; column < n; column += 1) {
    let pivot = column;
    for (let row = column + 1; row < n; row += 1) {
      if (Math.abs(augmented[row][column]) > Math.abs(augmented[pivot][column])) {
        pivot = row;
      }
    }
    if (Math.abs(augmented[pivot][column]) < 1e-14) {
      throw new Error("Singular regression matrix");
    }
    [augmented[column], augmented[pivot]] = [augmented[pivot], augmented[column]];
    const divisor = augmented[column][column];
    for (let j = column; j <= n; j += 1) {
      augmented[column][j] /= divisor;
    }
    for (let row = 0; row < n; row += 1) {
      if (row === column) continue;
      const factor = augmented[row][column];
      for (let j = column; j <= n; j += 1) {
        augmented[row][j] -= factor * augmented[column][j];
      }
    }
  }
  return augmented.map((row) => row[n]);
}

function fit(rows, degree) {
  const coefficientCount = degree + 1;
  const xtx = Array.from({ length: coefficientCount }, (_, i) =>
    Array.from({ length: coefficientCount }, (__, j) =>
      rows.reduce((sum, point) => sum + point.x ** (i + j), 0)
    )
  );
  const xty = Array.from({ length: coefficientCount }, (_, i) =>
    rows.reduce((sum, point) => sum + point.x ** i * point.y, 0)
  );
  const beta = solve(xtx, xty);
  const inverseColumns = Array.from({ length: coefficientCount }, (_, column) =>
    solve(
      xtx,
      Array.from({ length: coefficientCount }, (__, row) => row === column ? 1 : 0)
    )
  );
  const rss = rows.reduce((sum, point) => {
    const predicted = beta.reduce(
      (value, coefficient, power) => value + coefficient * point.x ** power,
      0
    );
    return sum + (point.y - predicted) ** 2;
  }, 0);
  const meanY = rows.reduce((sum, point) => sum + point.y, 0) / rows.length;
  const tss = rows.reduce((sum, point) => sum + (point.y - meanY) ** 2, 0);
  const mse = rss / (rows.length - coefficientCount);
  const standardErrors = beta.map((_, i) => Math.sqrt(mse * inverseColumns[i][i]));
  const likelihoodParameterCount = coefficientCount + 1;
  const aic = rows.length *
    (Math.log(2 * Math.PI) + 1 + Math.log(rss / rows.length)) +
    2 * likelihoodParameterCount;
  return {
    n: rows.length,
    beta,
    standardErrors,
    rss,
    r2: 1 - rss / tss,
    aic,
  };
}

function logGamma(z) {
  const coefficients = [
    676.5203681218851,
    -1259.1392167224028,
    771.3234287776531,
    -176.6150291621406,
    12.507343278686905,
    -0.13857109526572012,
    9.984369578019572e-6,
    1.5056327351493116e-7,
  ];
  if (z < 0.5) {
    return Math.log(Math.PI) - Math.log(Math.sin(Math.PI * z)) - logGamma(1 - z);
  }
  z -= 1;
  let x = 0.9999999999998099;
  coefficients.forEach((coefficient, i) => {
    x += coefficient / (z + i + 1);
  });
  const t = z + coefficients.length - 0.5;
  return 0.5 * Math.log(2 * Math.PI) +
    (z + 0.5) * Math.log(t) -
    t +
    Math.log(x);
}

function betaFraction(a, b, x) {
  const qab = a + b;
  const qap = a + 1;
  const qam = a - 1;
  let c = 1;
  let d = 1 - qab * x / qap;
  if (Math.abs(d) < 3e-14) d = 3e-14;
  d = 1 / d;
  let h = d;
  for (let m = 1; m <= 200; m += 1) {
    const m2 = 2 * m;
    let aa = m * (b - m) * x / ((qam + m2) * (a + m2));
    d = 1 + aa * d;
    if (Math.abs(d) < 3e-14) d = 3e-14;
    c = 1 + aa / c;
    if (Math.abs(c) < 3e-14) c = 3e-14;
    d = 1 / d;
    h *= d * c;

    aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
    d = 1 + aa * d;
    if (Math.abs(d) < 3e-14) d = 3e-14;
    c = 1 + aa / c;
    if (Math.abs(c) < 3e-14) c = 3e-14;
    d = 1 / d;
    const change = d * c;
    h *= change;
    if (Math.abs(change - 1) < 3e-14) break;
  }
  return h;
}

function regularizedBeta(x, a, b) {
  if (x <= 0) return 0;
  if (x >= 1) return 1;
  const front = Math.exp(
    logGamma(a + b) -
    logGamma(a) -
    logGamma(b) +
    a * Math.log(x) +
    b * Math.log(1 - x)
  );
  if (x < (a + 1) / (a + b + 2)) {
    return front * betaFraction(a, b, x) / a;
  }
  return 1 - front * betaFraction(b, a, 1 - x) / b;
}

function tTwoSidedP(t, degreesOfFreedom) {
  const x = degreesOfFreedom / (degreesOfFreedom + t * t);
  return regularizedBeta(x, degreesOfFreedom / 2, 0.5);
}

function tCritical95(degreesOfFreedom) {
  let low = 0;
  let high = 10;
  for (let i = 0; i < 80; i += 1) {
    const middle = (low + high) / 2;
    if (tTwoSidedP(middle, degreesOfFreedom) > 0.05) {
      low = middle;
    } else {
      high = middle;
    }
  }
  return (low + high) / 2;
}

let assertions = 0;

function check(condition, message) {
  assertions += 1;
  if (!condition) throw new Error(`Assertion ${assertions} failed: ${message}`);
}

function near(actual, expected, tolerance, label) {
  check(
    Math.abs(actual - expected) <= tolerance,
    `${label}: got ${actual}, expected ${expected} ± ${tolerance}`
  );
}

check(Array.isArray(sourceRows), "embedded data is an array");
check(data.length === 626, "Savage table contains 626 species-average rows");
check(new Set(data.map((d) => d.species)).size === 625, "626 rows contain 625 distinct species-name strings");
check(data.filter((d) => d.species === "Perameles gunnii").length === 2, "published duplicate species label is retained");
check(data.every((d) => d.mass > 0 && d.bmr > 0), "all masses and BMR values are positive");
check(new Set(data.map((d) => d.order)).size === 21, "dataset contains 21 mammalian orders");
near(Math.min(...data.map((d) => d.mass)), 2.4, 1e-12, "minimum mass in grams");
near(Math.max(...data.map((d) => d.mass)), 3672000, 1e-9, "maximum mass in grams");

const smallRows = data.filter((d) => d.x >= 0 && d.x <= 3);
const largerRows = data.filter((d) => d.x >= 2 && d.x <= 5);
const small = fit(smallRows, 1);
const larger = fit(largerRows, 1);
const linear = fit(data, 1);
const quadratic = fit(data, 2);

check(small.n === 477, "small three-decade window contains 477 species");
check(larger.n === 306, "larger three-decade window contains 306 species");
near(small.beta[1], 0.662508, 5e-7, "small-window OLS slope");
near(larger.beta[1], 0.7484459, 5e-7, "larger-window OLS slope");
check(Math.abs(small.beta[1] - 2/3) < 0.01, "small-window slope is close to two-thirds");
check(Math.abs(larger.beta[1] - 3/4) < 0.01, "larger-window slope is close to three-quarters");
check(larger.beta[1] - small.beta[1] > 0.08, "mass-window slope gap exceeds 0.08");

near(linear.beta[0], -1.6816, 0.00005, "published Savage linear intercept");
near(linear.beta[1], 0.7115, 0.00005, "published Savage linear slope");
near(quadratic.beta[0], -1.4812, 0.00005, "published Savage quadratic intercept");
near(quadratic.beta[1], 0.5347, 0.00005, "published Savage quadratic linear term");
near(quadratic.beta[2], 0.0320, 0.00005, "published Savage curvature coefficient");

const degreesOfFreedom = data.length - 3;
const tStatistic = quadratic.beta[2] / quadratic.standardErrors[2];
const pValue = tTwoSidedP(tStatistic, degreesOfFreedom);
const critical = tCritical95(degreesOfFreedom);
const confidenceLow = quadratic.beta[2] - critical * quadratic.standardErrors[2];
const confidenceHigh = quadratic.beta[2] + critical * quadratic.standardErrors[2];

check(quadratic.beta[2] > 0, "curvature coefficient is positive");
check(confidenceLow > 0, "classical 95% confidence interval excludes zero");
check(pValue < 1e-9, "classical OLS curvature p-value is below 1e-9");
near(pValue, 9.81841085e-12, 1e-19, "two-sided Student t p-value");
near(confidenceLow, 0.02291806, 1e-8, "curvature confidence lower endpoint");
near(confidenceHigh, 0.04100400, 1e-8, "curvature confidence upper endpoint");

check(quadratic.rss < linear.rss, "quadratic residual sum of squares is lower");
check(quadratic.aic < linear.aic, "quadratic Gaussian AIC is lower");
near(linear.rss, 18.2369034, 1e-7, "linear residual sum of squares");
near(quadratic.rss, 16.9279595, 1e-7, "quadratic residual sum of squares");
near(linear.aic, -430.96434, 1e-5, "linear full-likelihood Gaussian AIC");
near(quadratic.aic, -475.589159, 1e-5, "quadratic full-likelihood Gaussian AIC");
check((linear.rss - quadratic.rss) / linear.rss > 0.07, "quadratic removes over 7% of linear residual sum of squares");

console.log("The Exponent That Bends verification");
console.log(`dataset: ${data.length} species-average rows, 625 distinct labels, ${new Set(data.map((d) => d.order)).size} orders`);
console.log(`small window [1 g, 1 kg]: n=${small.n}, slope=${small.beta[1].toFixed(6)}`);
console.log(`larger window [100 g, 100 kg]: n=${larger.n}, slope=${larger.beta[1].toFixed(6)}`);
console.log(`full linear: slope=${linear.beta[1].toFixed(6)}, RSS=${linear.rss.toFixed(6)}, AIC=${linear.aic.toFixed(6)}`);
console.log(`full quadratic: b2=${quadratic.beta[2].toFixed(6)}, SE=${quadratic.standardErrors[2].toFixed(6)}`);
console.log(`classical 95% CI=[${confidenceLow.toFixed(6)}, ${confidenceHigh.toFixed(6)}], p=${pValue.toExponential(6)}`);
console.log(`quadratic RSS=${quadratic.rss.toFixed(6)}, AIC=${quadratic.aic.toFixed(6)}, ΔAIC=${(linear.aic - quadratic.aic).toFixed(6)}`);
console.log(`PASS ${assertions}/${assertions}`);
