papertrail/lib/stats/distributions.ts
caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

216 lines
6.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Self-contained statistical distribution functions used by the deterministic
// meta-analysis engine. These implement the standard closed forms (regularized
// incomplete gamma/beta) so the pooling engine never has to reach for a heavy
// stats dependency, and every value is oracle-tested against reference tables.
//
// No LLM, no randomness, no mutation — pure numeric functions only.
// Normal quantile Phi^-1(p) via Peter Acklam's rational approximation
// (relative error < 1.15e-9). `simple-statistics`' probit is only ~3-decimal
// accurate, which is not enough for oracle-grade confidence intervals, so we
// implement the higher-precision form here.
export function normalQuantile(p: number): number {
if (p <= 0 || p >= 1) return NaN;
const a = [
-3.969683028665376e1, 2.209460984245205e2, -2.759285104469687e2,
1.38357751867269e2, -3.066479806614716e1, 2.506628277459239,
];
const b = [
-5.447609879822406e1, 1.615858368580409e2, -1.556989798598866e2,
6.680131188771972e1, -1.328068155288572e1,
];
const c = [
-7.784894002430293e-3, -3.223964580411365e-1, -2.400758277161838,
-2.549732539343734, 4.374664141464968, 2.938163982698783,
];
const d = [
7.784695709041462e-3, 3.224671290700398e-1, 2.445134137142996,
3.754408661907416,
];
const pLow = 0.02425;
const pHigh = 1 - pLow;
let q: number;
let r: number;
if (p < pLow) {
q = Math.sqrt(-2 * Math.log(p));
return (
(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) /
((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1)
);
}
if (p <= pHigh) {
q = p - 0.5;
r = q * q;
return (
((((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r + a[4]) * r + a[5]) * q) /
(((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r + b[4]) * r + 1)
);
}
q = Math.sqrt(-2 * Math.log(1 - p));
return (
-(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) /
((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1)
);
}
// z for a two-sided (1 - alpha) CI, e.g. ciZ(95) = Phi^-1(0.975) ≈ 1.95996.
export function ciZ(ciPct: number): number {
const alpha = 1 - ciPct / 100;
return normalQuantile(1 - alpha / 2);
}
const MAX_ITER = 300;
const EPS = 1e-12;
const FPMIN = 1e-300;
// ln(Gamma(x)) via the Lanczos approximation (Numerical Recipes coefficients).
export function logGamma(x: number): number {
const c = [
76.18009172947146, -86.50532032941677, 24.01409824083091,
-1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5,
];
let y = x;
let tmp = x + 5.5;
tmp -= (x + 0.5) * Math.log(tmp);
let ser = 1.000000000190015;
for (let j = 0; j < 6; j++) {
y += 1;
ser += c[j] / y;
}
return -tmp + Math.log((2.5066282746310005 * ser) / x);
}
// Regularized lower incomplete gamma P(a, x) via series expansion (x < a+1).
function gammaSeries(a: number, x: number): number {
if (x <= 0) return 0;
let ap = a;
let sum = 1 / a;
let del = sum;
for (let n = 0; n < MAX_ITER; n++) {
ap += 1;
del *= x / ap;
sum += del;
if (Math.abs(del) < Math.abs(sum) * EPS) break;
}
return sum * Math.exp(-x + a * Math.log(x) - logGamma(a));
}
// Regularized upper incomplete gamma Q(a, x) via continued fraction (x >= a+1).
function gammaContinuedFraction(a: number, x: number): number {
let b = x + 1 - a;
let c = 1 / FPMIN;
let d = 1 / b;
let h = d;
for (let i = 1; i < MAX_ITER; i++) {
const an = -i * (i - a);
b += 2;
d = an * d + b;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = b + an / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
const del = d * c;
h *= del;
if (Math.abs(del - 1) < EPS) break;
}
return Math.exp(-x + a * Math.log(x) - logGamma(a)) * h;
}
// Regularized upper incomplete gamma Q(a, x) = 1 - P(a, x).
function gammaQ(a: number, x: number): number {
if (x < 0 || a <= 0) return NaN;
if (x === 0) return 1;
if (x < a + 1) return 1 - gammaSeries(a, x);
return gammaContinuedFraction(a, x);
}
/**
* Upper-tail p-value of a chi-square statistic: P(X > x) for `df` degrees of
* freedom. Used for Cochran's Q heterogeneity test. Returns 1 for x<=0.
*/
export function chiSquareSurvival(x: number, df: number): number {
if (df <= 0) return NaN;
if (x <= 0) return 1;
return gammaQ(df / 2, x / 2);
}
// Continued fraction for the incomplete beta function (Numerical Recipes betacf).
function betaContinuedFraction(a: number, b: number, x: number): number {
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) < FPMIN) d = FPMIN;
d = 1 / d;
let h = d;
for (let m = 1; m < MAX_ITER; m++) {
const m2 = 2 * m;
let aa = (m * (b - m) * x) / ((qam + m2) * (a + m2));
d = 1 + aa * d;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = 1 + aa / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
h *= d * c;
aa = (-(a + m) * (qab + m) * x) / ((a + m2) * (qap + m2));
d = 1 + aa * d;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = 1 + aa / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
const del = d * c;
h *= del;
if (Math.abs(del - 1) < EPS) break;
}
return h;
}
/**
* Regularized incomplete beta function I_x(a, b). Building block for the
* Student-t CDF.
*/
export function incompleteBeta(x: number, a: number, b: number): number {
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 * betaContinuedFraction(a, b, x)) / a;
}
return 1 - (front * betaContinuedFraction(b, a, 1 - x)) / b;
}
/**
* Two-sided Student-t CDF: P(T <= t) for `df` degrees of freedom.
*/
export function studentTCdf(t: number, df: number): number {
const x = df / (df + t * t);
const tail = 0.5 * incompleteBeta(x, df / 2, 0.5);
return t > 0 ? 1 - tail : tail;
}
/**
* Inverse Student-t CDF: the quantile t such that P(T <= t) = p, for `df`
* degrees of freedom. Solved by bisection on the monotone CDF — robust across
* all df >= 1 (unlike the Cornish–Fisher expansion, which degrades at df=1).
*/
export function studentTInverse(p: number, df: number): number {
if (p <= 0 || p >= 1 || df <= 0) return NaN;
if (p === 0.5) return 0;
let lo = -1000;
let hi = 1000;
for (let i = 0; i < 200; i++) {
const mid = (lo + hi) / 2;
const cdf = studentTCdf(mid, df);
if (cdf < p) {
lo = mid;
} else {
hi = mid;
}
if (hi - lo < 1e-10) break;
}
return (lo + hi) / 2;
}