papertrail/tests/trialSequential.test.ts
caelum0x 827794eba3 fix(stats): correct scientific-integrity bugs found in a biostatistics audit
A rigorous correctness audit of the deterministic statistical core (meta-analysis pooling
CONFIRMED correct) surfaced real defects; each fixed and pinned with worked-example tests:

- effectSize.reconcile(): no longer flags a claim that sits INSIDE the source's own 95% CI.
  The bare materiality-factor heuristic now only fires when NO usable CI was parsed; a present
  significant CI is the authoritative boundary. Fixes false 'overstated' verdicts on
  marginally-significant small-effect trials (statin/aspirin RR ~0.90-0.95) on the core path.
- trialSequential required information size: was 4x n_arm (risPerGroup=2x, risTotal=4x),
  now the correct 2x n_arm total — every TSA verdict was biased toward 'insufficient'.
- GRADE imprecision: a numerically wide ratio CI that decisively excludes the null (e.g.
  RR 0.30, 95% CI 0.15-0.60) is no longer downgraded for 'imprecision'; the wide-CI rule now
  requires a bound approaching the null.
- GRADE publication bias: automated Egger downgrade now gated behind k>=10 studies
  (Sterne 2011/Cochrane); below that a funnel test is uninterpretable.
- riskOfBias: a trial judged overall:high now contributes >=1 GRADE step (was 0 when the
  high judgement came via the pragmatic-escalation path) — removes an internal contradiction.
- Hedges' g variance: second-term denominator corrected to 2N (Borenstein Eq 4.24) from 2(N-2).
- Honesty: docstrings no longer overclaim the OBF boundary as 'Lan-DeMets' or Egger as
  'exact metafor regtest' — both are described as the approximations/classic forms they are.

Full suite green (1062 passed). Meta-analysis pooling, biostats RR/OR CIs, absolute effects/NNT,
and Egger math were all audited and CONFIRMED correct — unchanged.
2026-07-12 01:18:01 +03:00

99 lines
3.6 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.

import { describe, it, expect } from "vitest";
import {
requiredInformationSize,
obrienFlemingBoundary,
trialSequentialVerdict,
} from "../lib/trialSequential";
import { normalQuantile } from "../lib/stats/distributions";
// Oracle test: locks the trial-sequential engine to hand-computable closed forms.
// Every reference value is derived independently of the implementation.
describe("requiredInformationSize — hand-checkable RIS oracle", () => {
it("controlRisk 0.10, RRR 0.25, alpha 0.05, power 0.80", () => {
// p2 = 0.10, p1 = 0.10 * (1 - 0.25) = 0.075, delta = -0.025.
// z_{0.025} = 1.959964, z_{0.80} = 0.841621, (za+zb)^2 = 7.848878.
// variancePart = 0.075*0.925 + 0.10*0.90 = 0.069375 + 0.09 = 0.159375.
// base = 7.848878 * 0.159375 / 0.025^2 = 2001.4643 = the per-arm sample size (n_arm).
// Required information size = a single adequately-powered trial: perGroup = n_arm,
// total = 2*n_arm. (A prior version doubled both, giving 4003 / 8006 — a 2x inflation.)
const r = requiredInformationSize({
controlRisk: 0.1,
relativeRiskReduction: 0.25,
alpha: 0.05,
power: 0.8,
});
expect(r.p2).toBeCloseTo(0.1, 10);
expect(r.p1).toBeCloseTo(0.075, 10);
expect(r.risPerGroup).toBe(2002); // ceil(2001.4643)
expect(r.risTotal).toBe(4003); // ceil(4002.93)
expect(r.diversityAdjusted).toBe(false);
});
it("inflates by 1/(1 - I²) when heterogeneity is supplied", () => {
const base = requiredInformationSize({
controlRisk: 0.1,
relativeRiskReduction: 0.25,
});
const adjusted = requiredInformationSize({
controlRisk: 0.1,
relativeRiskReduction: 0.25,
iSquared: 0.5, // factor 1/(1-0.5) = 2x
});
expect(adjusted.diversityAdjusted).toBe(true);
// ~2x the per-group RIS (ceil rounding aside).
expect(adjusted.risPerGroup).toBeGreaterThan(base.risPerGroup * 1.99);
expect(adjusted.risPerGroup).toBeLessThan(base.risPerGroup * 2.01 + 2);
});
});
describe("obrienFlemingBoundary — Lan–DeMets OBF oracle", () => {
it("Z(0.5) = z_{0.0125} / sqrt(0.5)", () => {
const expected = normalQuantile(1 - 0.05 / 4) / Math.sqrt(0.5);
const b = obrienFlemingBoundary({ informationFraction: 0.5, alpha: 0.05 });
expect(b.z).toBeCloseTo(expected, 10);
// z_{0.0125} ≈ 2.2414, / sqrt(0.5) ≈ 3.1698.
expect(b.z).toBeCloseTo(3.1698, 3);
expect(b.informationFraction).toBe(0.5);
});
});
describe("trialSequentialVerdict — discrete verdicts", () => {
it("conclusive_benefit when cumulative Z crosses the OBF boundary", () => {
// At t = 0.5 the boundary is ≈ 3.17; a cumulative Z of 4.0 crosses it.
const v = trialSequentialVerdict({
accruedN: 4000,
ris: 8000,
cumulativeZ: 4.0,
alpha: 0.05,
});
expect(v.verdict).toBe("conclusive_benefit");
expect(v.crossedBenefit).toBe(true);
expect(v.informationFraction).toBeCloseTo(0.5, 10);
});
it("insufficient_information when neither boundary nor RIS is reached", () => {
// Same 50% information but a modest Z of 1.5 does not clear the ≈3.17 boundary.
const v = trialSequentialVerdict({
accruedN: 4000,
ris: 8000,
cumulativeZ: 1.5,
alpha: 0.05,
});
expect(v.verdict).toBe("insufficient_information");
expect(v.crossedBenefit).toBe(false);
expect(v.crossedNoEffect).toBe(false);
});
it("conclusive_no_effect once the full RIS accrues without crossing", () => {
const v = trialSequentialVerdict({
accruedN: 8000,
ris: 8000,
cumulativeZ: 0.5,
alpha: 0.05,
});
expect(v.verdict).toBe("conclusive_no_effect");
expect(v.crossedNoEffect).toBe(true);
});
});