peeksafe 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +325 -0
  2. package/LICENSE +190 -0
  3. package/NOTICE +78 -0
  4. package/README.md +490 -2
  5. package/RESEARCH-NOTES.md +274 -0
  6. package/dist/baseline.d.ts +45 -0
  7. package/dist/baseline.d.ts.map +1 -0
  8. package/dist/baseline.js +49 -0
  9. package/dist/baseline.js.map +1 -0
  10. package/dist/check.d.ts +64 -0
  11. package/dist/check.d.ts.map +1 -0
  12. package/dist/check.js +172 -0
  13. package/dist/check.js.map +1 -0
  14. package/dist/cluster.d.ts +328 -0
  15. package/dist/cluster.d.ts.map +1 -0
  16. package/dist/cluster.js +606 -0
  17. package/dist/cluster.js.map +1 -0
  18. package/dist/errors.d.ts +108 -0
  19. package/dist/errors.d.ts.map +1 -0
  20. package/dist/errors.js +192 -0
  21. package/dist/errors.js.map +1 -0
  22. package/dist/evidence.d.ts +15 -0
  23. package/dist/evidence.d.ts.map +1 -0
  24. package/dist/evidence.js +30 -0
  25. package/dist/evidence.js.map +1 -0
  26. package/dist/frontier.d.ts +271 -0
  27. package/dist/frontier.d.ts.map +1 -0
  28. package/dist/frontier.js +505 -0
  29. package/dist/frontier.js.map +1 -0
  30. package/dist/gate.d.ts +175 -0
  31. package/dist/gate.d.ts.map +1 -0
  32. package/dist/gate.js +200 -0
  33. package/dist/gate.js.map +1 -0
  34. package/dist/index.d.ts +60 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +79 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/plan.d.ts +280 -0
  39. package/dist/plan.d.ts.map +1 -0
  40. package/dist/plan.js +626 -0
  41. package/dist/plan.js.map +1 -0
  42. package/dist/rand.d.ts +17 -0
  43. package/dist/rand.d.ts.map +1 -0
  44. package/dist/rand.js +104 -0
  45. package/dist/rand.js.map +1 -0
  46. package/dist/stats.d.ts +401 -0
  47. package/dist/stats.d.ts.map +1 -0
  48. package/dist/stats.js +1304 -0
  49. package/dist/stats.js.map +1 -0
  50. package/dist/stop.d.ts +164 -0
  51. package/dist/stop.d.ts.map +1 -0
  52. package/dist/stop.js +270 -0
  53. package/dist/stop.js.map +1 -0
  54. package/package.json +62 -3
  55. package/src/baseline.ts +72 -0
  56. package/src/check.ts +238 -0
  57. package/src/cluster.ts +842 -0
  58. package/src/errors.ts +275 -0
  59. package/src/evidence.ts +62 -0
  60. package/src/frontier.ts +837 -0
  61. package/src/gate.ts +363 -0
  62. package/src/index.ts +206 -0
  63. package/src/plan.ts +968 -0
  64. package/src/rand.ts +137 -0
  65. package/src/stats.ts +1663 -0
  66. package/src/stop.ts +464 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,325 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project uses
5
+ [semantic versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## Unreleased
8
+
9
+ ## 0.2.0 (2026-11-02)
10
+
11
+ ### Added
12
+
13
+ - `PlanConfig` and `FrontierConfig` take an optional `futilityConfidence`, the level you run
14
+ `shouldStopPaired` at. A paired case's typical bill in `makePlan`, `evaluatePoint` (so
15
+ `computeFrontier`) and `affordabilityGrid` was always the pairs it takes to settle at the default
16
+ 0.95, so a stricter rule was billed short: at 75% on a 240-run baseline and a coupling of 0.9 a case
17
+ settles after 52.1 pairs on average at 0.99, where the plan quoted 36. It now prices 52. Unset, it
18
+ keeps the default; outside (0, 1) it is refused with `PEEKSAFE_E_STAT_DOMAIN`.
19
+
20
+ ### Changed
21
+
22
+ - `makePlan` priced the expected bill (`typicalRuns`, `typicalScreenedRuns`, `expectedCostUsd`) and
23
+ the screening passes at one design for the whole suite, while each `PlanCase.design` already chose
24
+ paired or unpaired per case. A mixed suite was billed as if every case ran the suite's design: the
25
+ README's two cases, one paired and one unpaired, priced at 68 runs where they cost 108. Each case is
26
+ now priced at its own design, and a case with none at `recommendedDesign`; `config.design` still
27
+ prices every case at one design. `totals.runsPerObservation` is now the average over the expected
28
+ bill, between 1 and 2 on a mixed suite.
29
+ - `makePlan`'s `recommendedDesign` flipped to paired only when more than a quarter of the suite was
30
+ `PAIRED_ONLY`, so four cases at 85% on a 240-run baseline, each cheaper paired, were billed paired
31
+ while `recommendation` said to screen first or run the sequential test, without naming pairing. It
32
+ now also recommends pairing when more than half of the decidable cases have a paired `design`, and
33
+ `recommendation` says how many.
34
+ - `makePlan` counted a paired case's expected bill in the unpaired SPRT's observations, which ignore
35
+ the coupling, so a paired case was mispriced either way: at 90% and a coupling of 0.6 it priced 28
36
+ pairs where a simulated `shouldStopPaired` loop spends 42.3 on average. A paired case is now counted
37
+ in the pairs `shouldStopPaired` takes to settle it on the mean trajectory at its coupling, on the
38
+ same batch grid: 44 there. The README's two cases cost 100 runs, not 108. Near independence the
39
+ figure is the `maxTrials` cap and over-states the simulated mean.
40
+ - `evaluatePoint` (and so `computeFrontier`) and `affordabilityGrid` still counted a paired typical
41
+ bill in the unpaired SPRT's observations, so they disagreed with `makePlan` about the price of the
42
+ same paired case. Both now count it in the pairs to settle that `makePlan` uses: 100 paired cases at
43
+ 85% and 15 points cost $7220 on the frontier's defaults, not $6420, and at 90% and a coupling of 0.6
44
+ a case takes 44 pairs, not 28. A paired grid cell at 50 cases, 85% over 60 runs and a coupling of
45
+ 0.6 expects 880 runs, not 720.
46
+ - `makePlan` counted an unpaired case's expected bill in Wald's expected sample number for the SPRT,
47
+ a different stopping rule that also ignores the suite size, so the bill was off either way: four
48
+ cases at 85% over 240 baseline runs stop after 58.3 runs each in a simulated `shouldStop` loop where
49
+ the plan quoted 36, and 50 cases at 90% over 60 stop after 17.1 where it quoted 28. An unpaired case
50
+ is now counted in the runs `shouldStop` takes to stop it on the plan's batch grid, computed exactly:
51
+ 58 and 18 there. The README's two cases cost 138 runs, not 100, and `config.futilityConfidence` now
52
+ sets the level for both stopping rules. `expectedSequentialSamples` is still Wald's figure, and
53
+ `evaluatePoint` and `affordabilityGrid` still use it (#106).
54
+
55
+ ### Fixed
56
+
57
+ - `planCase` did not check what `makePlan` checks before calling it. A direct call took a -5/60 or
58
+ 5/-3 baseline as `IMPOSSIBLE`, as if the case could not drop, and on a 51/60 baseline in a suite of
59
+ 10 a `costPerRunUsd` of -1 priced the paired design at -$152 and a `msPerRun` of -5 at -760 ms; a
60
+ `NaN` price came back as a `NaN` bill, and `alpha + beta >= 1` was accepted. It now refuses the
61
+ baseline counts with `PEEKSAFE_E_STAT_DOMAIN` and the error rates and prices with
62
+ `PEEKSAFE_E_CONFIG`. `makePlan` refuses a -5/0 baseline the same way instead of reading it as no
63
+ baseline.
64
+ - `rand.bernoulli` did not check its rate. `next() < p` is false for every draw at `NaN`, so a
65
+ simulation whose rate failed to compute drew a case that never passes, and a rate of 2 or -1 drew
66
+ all passes or all failures, without an error. It now refuses a rate outside [0, 1] with
67
+ `PEEKSAFE_E_STAT_DOMAIN`, as `rand.int` refuses a bad bound. A rate inside it draws the same
68
+ numbers as before.
69
+ - `samplesForEvidence` did not check `logThreshold`. A `NaN` bar failed its ceiling test and it
70
+ answered Infinity, "no number of runs will do", for a case that needs 262 runs at a bar of
71
+ `log(4000)`, and a bar of `-Infinity` answered 1 run, while `mcnemarSamplesForEvidence` refused
72
+ both. It now refuses a bar that is not finite with `PEEKSAFE_E_STAT_DOMAIN`, through the check the
73
+ paired search now shares.
74
+ - `evaluatePoint` (and so `computeFrontier`) withheld a recommendation only for a screen recall of
75
+ `null`, so a recall of `NaN`, which is what 0 caught out of 0 planted regressions computes to,
76
+ counted as measured: on the defaults it moved the frontier headline from 10 cases at 35 points
77
+ ($83.00) to 10 cases at 25 points ($78.00). A recall outside [0, 1] was accepted the same way. Both
78
+ are now refused with `PEEKSAFE_E_STAT_DOMAIN`. An unknown `design` fell through to the paired
79
+ branch and came back feasible on a bill of $Infinity, and an unknown `screen` was priced and
80
+ recommended as the expensive one; both are now refused with `PEEKSAFE_E_CONFIG`.
81
+ - `makePlan` and `expectedSequentialSamples` accepted a case listed twice, which `gate` refuses.
82
+ `makePlan` priced the case twice and counted it twice in the suite size its bar is set from, and
83
+ `expectedSequentialSamples` added it to `samples` twice while `perCase` kept it once, so the two
84
+ disagreed. Both now refuse a repeated id with `PEEKSAFE_E_CASE_DUPLICATE`, the code `gate` and
85
+ `toBaselineMap` use.
86
+ - `evaluatePoint` did not check the budget, the prices, `screenKeepFraction` or `amortisePrs`, which
87
+ only `enumerateFrontier` refused. A direct call at a `costPerRunUsd` of -1 recommended a paired
88
+ point for 100 cases at an mde of 0.15 on a bill of -$6420, a `screenKeepFraction` of 2 kept 200 of
89
+ the 100 cases, NaN kept NaN of them, and an `amortisePrs` of 0 or 0.5 priced the baseline as one pull
90
+ request. It now refuses all four by name with `PEEKSAFE_E_CONFIG`, through the check
91
+ `enumerateFrontier` now shares, which also refuses an infinite cost: against an unlimited budget
92
+ it recommended a point on a bill of $Infinity. A `basis` inherited from `Object.prototype`, such as `"toString"`, passed the
93
+ `undefined` check on the bill lookup and priced a `basisCostUsd` of `undefined`; it is now refused,
94
+ as `costShares` already refused it.
95
+ - `discordant` did not check its pair table. It summed `worse` and `better` as given, so a `worse`
96
+ of -2 against a `better` of 1 answered -1 discordant pairs, 0.5 answered 1.5 and `NaN` answered
97
+ `NaN`, while `pairPhi`, `gate` and `shouldStopPaired` refused the same table. It now refuses a
98
+ count that is not a non-negative integer with `PEEKSAFE_E_STAT_DOMAIN`, through the same check
99
+ `pairPhi` now shares, so `pairPhi`'s refusal carries the offending count in `detail` rather than
100
+ the whole table.
101
+ - The frontier did not check the paired design's baseline. `evaluatePoint` took any
102
+ `pairedBaselineRuns`, so 0 runs priced the typical bill for 100 cases at an mde of 0.15 over the
103
+ defaults at $2400 where the default 20 runs price $6420, and 2.5 runs priced a fractional baseline.
104
+ It now refuses a `pairedBaselineRuns` that is not a positive integer with `PEEKSAFE_E_CONFIG`, and
105
+ so does `computeFrontier`, by that name. `typicalObservationsPerCase` now refuses a `rate` or `mde`
106
+ outside (0, 1) and a `baselineRuns` that is not a positive integer: at 80% over a 60-run baseline an
107
+ `mde` of 0 priced 96 runs and -0.1 priced 52. `evaluatePoint`'s refusal of `baselineRuns` now names
108
+ `evaluatePoint` rather than `frontier`.
109
+ - `gate`, `shouldStop` and `shouldStopPaired` refused an option passed as `undefined`. They merged
110
+ their defaults with a plain spread, so `{ mde: undefined }`, which the option types accept, replaced
111
+ the default 0.15 with `undefined` and was refused as an `mde` outside (0, 1); the same held for
112
+ `fdr`, `altConcentration`, `evidence` and `futilityConfidence`. An option passed as `undefined` now
113
+ takes its default, as it already did in `certifyProbability` and `typeOneError`. `suiteSize` has
114
+ no default and is still refused.
115
+ - The planner did not check the batch grid it prices the gate on. `makePlan`,
116
+ `expectedSequentialSamples` and `affordabilityGrid` took any `minTrials`, `maxTrials` and
117
+ `batchSize`: a `NaN` bound or a `batchSize` of 0 made the typical bill `NaN`, and a `maxTrials` of 1,
118
+ under the default `minTrials` of 8, priced four cases at 85% over a 240-run baseline at 4 typical
119
+ unpaired runs where the defaults price 144. `evaluatePoint` checked `maxTrials` only, so `computeFrontier`
120
+ and `typicalObservationsPerCase` took a `NaN` `minTrials` or a zero batch. All five now refuse,
121
+ with `PEEKSAFE_E_CONFIG`, a `minTrials` or `batchSize` that is not a positive integer and a
122
+ `maxTrials` that is not an integer at least `minTrials`.
123
+ - `makeRand` folded numeric seeds onto one stream. `seed >>> 0` turned `NaN`, `Infinity`, 1.5 and
124
+ 2^32 into 0, and 0 was then remapped to 1, so all of them drew the same numbers as seed 1, and a
125
+ replication loop starting at seed 0 ran its first replicate twice. -1 drew the stream of
126
+ 2^32 - 1. Seed 0 now has its own stream, since the generator has a full period from every seed,
127
+ and a numeric seed that is not an integer in [0, 2^32) is refused with `PEEKSAFE_E_STAT_DOMAIN`.
128
+ String seeds, and every other numeric seed, draw the same numbers as before.
129
+ - `iidMean` did not check its values. A `NaN` or infinite value answered `NaN` bounds and a `NaN`
130
+ point, while `clusterRobustMean`, `randomEffectsMean` and `clusteredEffect` refused the same data
131
+ through `groupByCluster`. It now refuses a value that is not finite with `PEEKSAFE_E_STAT_DOMAIN`
132
+ and names its index.
133
+ - `logGamma`, and so `logBeta`, did not check its argument. At a pole it answered a finite number,
134
+ 37.78 at `x` -1 where log Γ is infinite, and at `x` Infinity it answered `NaN`. It now refuses an
135
+ `x` that is not finite and positive with `PEEKSAFE_E_STAT_DOMAIN`, the domain its doc comment
136
+ already named; `x` in (0, ½) still goes through the reflection formula.
137
+ - `gammaP` did not check its shape. At `a` -0.5 and `x` 0.2 it answered -1.5058, a negative
138
+ probability, and at `x` Infinity it answered `NaN`, so `erf` and `normalCdf` were `NaN` at
139
+ either infinity. It now refuses an `a` that is not finite and positive, and a `NaN` `x`, with
140
+ `PEEKSAFE_E_STAT_DOMAIN`, and answers 1 at `x` Infinity.
141
+ - `sampleSizeTwoProportion` answered `NaN` for a `NaN` `delta`: both of its range checks are false
142
+ for NaN, so it skipped the `Infinity` branch and fed the formula. It now refuses it with
143
+ `PEEKSAFE_E_STAT_DOMAIN`; a `delta` at or below 0, or at or above `p0`, still answers `Infinity`.
144
+ - `normalQuantile` and `betaQuantile` accepted a `NaN` probability. `normalQuantile` answered `NaN`
145
+ and `betaQuantile` bisected it down to about 0. Both now refuse it with `PEEKSAFE_E_STAT_DOMAIN`;
146
+ a probability at or beyond 0 or 1 is still clamped to the end of the support as before.
147
+ - `ebhSoloThreshold` did not check its arguments. A `q` of 2 answered half the bar, 5 for ten cases
148
+ where 0.05 answers 200, and a negative `m` or `q` answered a negative bar. It now refuses an `m`
149
+ that is not a positive integer and a `q` outside [0, 1] with `PEEKSAFE_E_STAT_DOMAIN`, as
150
+ `ebhCorrect` does for `q`.
151
+ - `sprtDecision` and `sprtExpectedN` accepted error rates summing to 1 or more, where Wald's walls
152
+ meet or cross. `sprtDecision` at `alpha` 0.6 and `beta` 0.5 answered `ACCEPT_H1` before a single
153
+ run, and `sprtExpectedN` answered 0 or a small positive number. Both now refuse
154
+ `alpha + beta >= 1` with `PEEKSAFE_E_CONFIG`, as the planners already did.
155
+ - `affordabilityGrid` and `evaluatePoint` (and so `computeFrontier`) refuse an `fdr` outside (0, 1)
156
+ with `PEEKSAFE_E_STAT_DOMAIN`, as `makePlan` does. An `fdr` of 2 priced a 50-case grid at 840 runs
157
+ where 0.05 prices 1310. `affordabilityGrid` also refuses a negative or NaN `costPerRunUsd` and a
158
+ case count that is not a positive integer with `PEEKSAFE_E_CONFIG`.
159
+ - `makePlan` and `evaluatePoint` (and so `computeFrontier`) did not check `runsPerCaseCeiling`. At
160
+ `NaN` or 0 every case came back with no design. They now refuse a ceiling that is not finite and
161
+ at least 1 with `PEEKSAFE_E_CONFIG`. `samplesForEvidence` and `mcnemarSamplesForEvidence` refuse
162
+ the same for `maxN` and `maxPairs` with `PEEKSAFE_E_STAT_DOMAIN`, where a cap of `NaN` or 0
163
+ answered Infinity for a design that needs 262 runs or 172 pairs.
164
+ - `betaCredibleInterval` did not check `level`. A level of `95` or `1.5` returned `[0, 1]`, `-1`
165
+ returned `[1, 0]` and `NaN` a point near 0. It now refuses a level outside (0, 1) with
166
+ `PEEKSAFE_E_STAT_DOMAIN`.
167
+ - `wilsonInterval` and `diffInterval` did not check `z`. A negative `z` returned a lower bound above
168
+ the upper one, and a `NaN` or infinite `z` returned `NaN` bounds. They now refuse a `z` that is
169
+ negative or not finite with `PEEKSAFE_E_STAT_DOMAIN`, also for an empty arm.
170
+ - `shouldStop` accepted `futilityConfidence: 0.9999999999999999`, whose `z` is infinite, and threw
171
+ an unrelated `evidenceCeilingLogE` error about a `NaN` rate. It now refuses it with
172
+ `PEEKSAFE_E_CONFIG`.
173
+
174
+ ## 0.1.0 (2026-10-21)
175
+
176
+ First release. There is no earlier version to compare against, so this section
177
+ describes what the package contains rather than what changed.
178
+
179
+ ### Added
180
+
181
+ - `gate()`, the decision: given per-case candidate and baseline counts, which
182
+ cases regressed, at a false discovery rate you name. Valid at any stopping
183
+ time, so it does not matter when or why you stopped a case.
184
+ - `shouldStop(observed, baseline, options)`: the per-case stopping decision, returning
185
+ `regressed` | `settled` | `futile` | `impossible` | `budget` | `continue` with the evidence
186
+ behind it. This is the loop every caller was writing by hand. `futilityConfidence` is any
187
+ value strictly between 0 and 1.
188
+ - Paired cases in `gate`: a case may carry `paired: { bothPass, worse, better, bothFail }` instead
189
+ of candidate counts and a stored baseline, and is decided by `pairedLogE` in the same e-BH family
190
+ as the unpaired cases. Its null is exact, so it has no ceiling and is valid at every rate by
191
+ construction. The runs need not share a seed: independent runs paired in order are still valid.
192
+ Within 200 pairs, a 10-case suite at 75% losing 15 points is certified 43.5% of the time with
193
+ independent runs and 74.5% with half the pairs seeded, against 50.2% for the default statistic
194
+ with a 240-run stored baseline and 22.3% for `universal`.
195
+ - `shouldStopPaired(counts, options)`: `shouldStop` for a paired case. `settled` comes from an
196
+ e-value against "dropped by at least `mde`", so a case that really dropped that far is settled
197
+ with probability at most `1 - futilityConfidence` however often you ask.
198
+ - `pairedCertifyProbability(options)`: the exact power and type I error of a paired case, at
199
+ separate baseline and candidate rates and a `coupling` for shared seeds.
200
+ - `universalTwoSampleLogE` and `evidence: 'universal'` on `gate`, `shouldStop`, `typeOneError` and
201
+ `certifyProbability`: a two-sample e-value valid at every rate by construction, for anyone who
202
+ wants a proof rather than a computation. It needs roughly four times the baseline runs to match
203
+ the default's power, so the default stays. `universalCeilingLogE` bounds it for `shouldStop`.
204
+ - `certifyProbability(options)`: the exact probability a case is certified at separate baseline
205
+ and candidate rates, which is the power when the candidate is lower.
206
+ - `typeOneError(options)`: the exact probability that a case which did not move is ever
207
+ certified, at a rate, baseline size and bar you name. The README's error-control grid is built
208
+ on it, and it is what to run when your suite sits outside that grid.
209
+ - `GateResult.headline`: one line an operator can read, naming the two things a PASS can hide
210
+ (cases with no baseline, and cases whose baseline is too thin to certify an `mde`-sized drop).
211
+ - Planning: `makePlan`, `planCase`, `affordabilityGrid` answer how many runs a
212
+ suite needs before anything is spent, including the two ways the answer can be
213
+ "no budget does this". An effect larger than the rate is arithmetically
214
+ impossible; an effect under the evidence ceiling is impossible at any
215
+ *candidate* budget and needs more baseline runs instead.
216
+ - The frontier: `computeFrontier` and `enumerateFrontier`, what is certifiable
217
+ inside a budget.
218
+ - The statistical core, exported so callers can check the arithmetic or build a
219
+ different gate: special functions, intervals, the SPRT, two-sample and paired
220
+ e-values, e-BH, power, and the evidence ceiling with its closed form.
221
+ - Cluster-robust suite inference (`clusteredEffect`, CR2 with small-cluster
222
+ degrees of freedom, a random-effects cross-check, and a diagnostic for a
223
+ grouping key that collapses the suite).
224
+ - `RESEARCH-NOTES.md`, a literature check on what in this work is novel and what
225
+ is not. Two of the four claims the prototype made turned out to be documented
226
+ elsewhere, and the prose says so.
227
+ - `PlanCase.design`: the design to run each case with, the one of unpaired and paired that
228
+ decides it within `runsPerCaseCeiling` in fewer runs (unpaired on a tie), or `null` when neither
229
+ does. `PlanCase.pairCoupling` is the coupling its paired figures were priced at.
230
+ - `makePlan(cases, baseline, cfg, pilots)`: an optional map of per-case pilot pair tables. A case
231
+ with one is priced at its measured `pairPhi` (floored at 0) instead of `cfg.pairCoupling`; an
232
+ empty table is ignored. In the README example a case at 75% on a 240-run baseline pairs at the
233
+ assumed 0.9 (118 runs against 164) and stays unpaired at a measured 0.6 (206 runs).
234
+
235
+ ### Notes
236
+
237
+ - Zero runtime dependencies, verified in CI against a packed tarball rather than
238
+ against `package.json`.
239
+ - The package ships `src/` next to `dist/`, so the source and declaration maps resolve and
240
+ go-to-definition lands on the TypeScript rather than on compiled output.
241
+ - Extracted from an internal prototype and reduced to the statistical core. The
242
+ runner, graders, store, CI adapters and CLI were deliberately left behind:
243
+ this library takes counts and returns statistics, and has no opinion about how
244
+ you run evals.
245
+ - The default two-sample e-value has mean exactly 1 averaged over the shared rate, not at every
246
+ fixed rate, so its error control is computed rather than inferred. `test/error-control.test.ts`
247
+ computes the type I error exactly over a grid of baselines, rates and `mde`, and runs
248
+ `shouldStop` and `gate` end to end on simulated suites. At the defaults the worst cell found is
249
+ 0.75 of alpha. `gate` and `shouldStop` refuse an `altConcentration` above 16
250
+ (`MAX_ALT_CONCENTRATION`, exported) with the default statistic, with `PEEKSAFE_E_CONFIG`, because a
251
+ concentrated alternative is the one known way it exceeds alpha inside the checked range: 1.04
252
+ times alpha at 32. At 16 the worst checked cell is 0.91 of alpha. `universal` takes any positive
253
+ value, and `typeOneError` and `certifyProbability` still compute any concentration, so a corner
254
+ can be inspected.
255
+ - The alternative's Beta shapes are floored at 1 rather than anywhere below it, because a shape
256
+ under 1 piles the alternative's mass onto a rate of zero, which is where a thin baseline at a
257
+ very low rate already sits. That corner is the one that costs the most error: a 30-run baseline
258
+ at 0.5% with `mde` 0.05, and an `altConcentration` of 2 with a 5-run baseline, are both under a
259
+ tenth of alpha, pinned in `test/error-control.test.ts`.
260
+ - `settled` is the part an e-value cannot give you on its own: a healthy case never accumulates
261
+ evidence that it is healthy, so it would run to your cap forever. The evidence ceiling stops it
262
+ once the counts have ruled out a certifiable drop.
263
+ - `futile` is kept separate from `settled` because they call for opposite actions: a thin baseline
264
+ is a defect to fix, a settled case is a pass.
265
+ - Futility is judged at the most pessimistic rate the counts still permit, not at the point
266
+ estimate, so it fires late and cannot abandon a case that was about to be certified. A
267
+ catastrophic regression is never stopped early at any sample size.
268
+ - An improvement is never certified as a regression. The Bayes factor behind `twoSampleLogE` has a
269
+ wider alternative than null, so on its own it also grows for a candidate far above its baseline:
270
+ 96/96 against 30/60 scores e^19.5 uncapped. The statistic is therefore capped at 1 whenever the
271
+ candidate's observed rate is at or above the baseline's, and `test/error-control.test.ts` checks
272
+ improved candidates stay under alpha. `pairedLogE` has the same property from a proof rather than
273
+ a cap: its alternative is truncated to "worse more often than not", which makes it an e-value for
274
+ every candidate that did not get worse. `test/paired.test.ts` computes it exactly.
275
+ - A case whose baseline passes at or below `mde` cannot drop by `mde` at all, so `gate` marks it
276
+ `impossible` and does not test it, `makePlan` reports it the same way, and `shouldStop` stops it
277
+ at once with the reason `impossible`. Testing such a case would mean testing it against an
278
+ alternative clamped onto a rate near zero, where the case already sits, and a type I error above
279
+ the nominal level.
280
+ - `PeeksafeErrorCode` lists only the five codes the library throws, and `test/exports.test.ts`
281
+ fails if a code is listed that no source file throws. Refusals are consistent across entry
282
+ points: a duplicate case id is `PEEKSAFE_E_CASE_DUPLICATE` from `toBaselineMap` as well as from
283
+ `gate`, a non-positive `altConcentration` is `PEEKSAFE_E_CONFIG` from `certifyProbability` and
284
+ `typeOneError` as well as from `gate` and `shouldStop`, and `costShares` refuses a `basis` it
285
+ does not recognise rather than quoting one it does. An omitted `basis` is `typical`.
286
+ - A reported `evalue` is clamped to `Number.MAX_VALUE`, because `exp()` of a log e-value past about
287
+ 709 overflows and a strong regression goes well past that: 0/1000 against 990/1000 has a log
288
+ e-value near 1291. `gate` and `shouldStop` both report it through the same clamp, and no decision
289
+ depends on the number.
290
+ - `PlanCase.baselineRunsNeeded` scans for the smallest baseline that clears the bar rather than
291
+ bisecting for one, because the evidence ceiling is not monotone in the baseline size: holding the
292
+ rate fixed still forces an integer success count, so a larger baseline can carry less evidence
293
+ than a smaller one and a bisection returns whichever crossing it happened to bracket.
294
+ `test/budget.test.ts` checks the answer against a scan over a grid of baselines and effects.
295
+ - The expected-cost half of the planner is Wald's approximation, and it errs low. `sprtExpectedN`
296
+ evaluates the operating characteristic exactly, `(1 - B^h) / (A^h - B^h)` at the tilt `h` solving
297
+ `E_p[lambda^h] = 1`, which is `-1` at `p1` and `+1` at `p0` and so agrees with both hypotheses,
298
+ with the limit `-log A * log B / E_p[(log lambda)^2]` taken where the drift vanishes. The
299
+ expected sample number built on it still treats the test as stopping exactly on a wall where a
300
+ real one overshoots it, so it under-states the true expected sample number and every cost built
301
+ on it. No decision depends on this: it is the pricing half of the planner, not the gate.
302
+ - `randomEffectsMean` reports the grand mean with a standard error of 0 and a zero-width interval
303
+ on a suite where every case moved by the same amount, which is what a pull request that moved
304
+ nothing produces, and what `clusterRobustMean` and `iidMean` already answer for the same data.
305
+ `compareEstimators` then reads a ratio of 1 and reports that the two estimators agree.
306
+ - `makePlan`, `affordabilityGrid` and `computeFrontier` refuse error rates with `alpha + beta >= 1`
307
+ with `PEEKSAFE_E_CONFIG`, because the sequential test decides before it sees a run there and
308
+ every case would be priced at `minTrials`. `PlanCase.fixedNPerArm` prices the fixed-N comparator
309
+ at the plan's own `alpha` and `beta`; the defaults are 0.05 and 0.1.
310
+ - `probabilityMoved` refuses an `mde` outside (0, 1) and a `nodes` that is not a positive integer
311
+ with `PEEKSAFE_E_STAT_DOMAIN`.
312
+ - `makePlan`, `affordabilityGrid`, `computeFrontier`, `evaluatePoint` and `typicalObservationsPerCase`
313
+ refuse a `nullQuantile` outside (0, 1) with `PEEKSAFE_E_STAT_DOMAIN`, naming the field.
314
+ `betaQuantile` returns 0 or 1 for such a value, which would price the typical bill against a null
315
+ rate that nobody chose and still return a complete answer.
316
+ - `affordabilityGrid` refuses a `baselineTrials` that is not a positive integer with
317
+ `PEEKSAFE_E_CONFIG`, the same refusal `evaluatePoint` gives, because a suite with no baseline
318
+ would otherwise be priced against a Beta(1, 1) null, a rate of 0.5, and come out cheaper than the
319
+ same suite with a real 60-run baseline. `planCase` still reports it as `impossible` and `gate` as
320
+ missing a baseline.
321
+ - An `alpha` or `beta` outside (0, 1) is refused with `PEEKSAFE_E_STAT_DOMAIN` by `makePlan`, `affordabilityGrid`, `computeFrontier`,
322
+ `evaluatePoint`, `sprtDecision`, `sprtExpectedN` and `sampleSizeTwoProportion`, because
323
+ `sprtExpectedN` returns `NaN` or `Infinity` there and the planners read either as "no finite
324
+ answer" and price every case at the per-case cap, which looks the same as a suite that needs the
325
+ cap.
package/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by the Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding any notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Alexis Rico
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,78 @@
1
+ peeksafe
2
+ Copyright 2026 Alexis Rico
3
+
4
+ Licensed under the Apache License, Version 2.0. See LICENSE.
5
+
6
+ --------------------------------------------------------------------------------
7
+ Third-party code
8
+ --------------------------------------------------------------------------------
9
+
10
+ None. peeksafe has zero runtime dependencies, vendors no source, and ships no
11
+ code it did not write. `.github/workflows/ci.yml` asserts the first of those on
12
+ every pull request by packing the tarball, installing it into an empty project,
13
+ and failing if `pnpm ls` resolves more than two packages.
14
+
15
+ Development dependencies (the devDependencies in package.json) are not
16
+ redistributed and are not part of the published package.
17
+
18
+ --------------------------------------------------------------------------------
19
+ Algorithms and published constants
20
+ --------------------------------------------------------------------------------
21
+
22
+ Everything statistical here is implemented from published closed forms and
23
+ recurrences rather than ported from an existing implementation. Mathematical
24
+ methods and tabulated constants are not anybody's intellectual property, but
25
+ naming their sources is both good manners and the fastest way for a reader to
26
+ check the arithmetic.
27
+
28
+ Regularized incomplete beta Continued fraction: Abramowitz & Stegun,
29
+ src/stats.ts Handbook of Mathematical Functions, 26.5.8.
30
+ Evaluated by the modified Lentz algorithm:
31
+ Lentz (1976), amended by Thompson & Barnett
32
+ (1986).
33
+
34
+ log Γ(x) Lanczos approximation, g = 7, n = 9. The
35
+ src/stats.ts coefficient set is the widely reprinted one
36
+ originating with Lanczos (1964).
37
+
38
+ Φ⁻¹(p) Peter Acklam's algorithm for the inverse
39
+ src/stats.ts normal CDF, refined here by one Halley
40
+ step. Acklam released it for unrestricted
41
+ use.
42
+
43
+ Lower incomplete gamma Standard series and continued-fraction
44
+ src/stats.ts expansions, Abramowitz & Stegun 6.5.
45
+
46
+ Linear congruential generator Multiplier 1664525, increment 1013904223,
47
+ src/rand.ts modulus 2^32. Tabulated parameters from
48
+ Knuth, The Art of Computer Programming,
49
+ volume 2.
50
+
51
+ 32-bit avalanche (mix32) The `lowbias32` multipliers from Chris
52
+ src/rand.ts Wellons' hash function prospector, placed
53
+ by the author in the public domain.
54
+
55
+ hash32 FNV-1a. Public domain by the authors'
56
+ src/rand.ts explicit statement.
57
+
58
+ --------------------------------------------------------------------------------
59
+ Statistical methods
60
+ --------------------------------------------------------------------------------
61
+
62
+ The procedures peeksafe implements are established results. README.md is
63
+ explicit about which of this library's claims are new and which are not, and
64
+ RESEARCH-NOTES.md records an adversarial literature check that withdrew two of
65
+ four originally-claimed novelties.
66
+
67
+ e-values and testing by betting Shafer; Grünwald, de Heide & Koolen (Safe
68
+ Testing); Waudby-Smith & Ramdas.
69
+ e-BH Wang & Ramdas, False discovery rate control
70
+ with e-values (2022).
71
+ Stopped e-BH Wang, Dandapanthula & Ramdas (2025).
72
+ Benjamini-Hochberg Benjamini & Hochberg (1995).
73
+ Sequential probability ratio Wald, Sequential Analysis (1945).
74
+ McNemar's test McNemar (1947).
75
+ Cluster-robust variance (CR2) Liang & Zeger (1986); Bell & McCaffrey
76
+ (2002).
77
+ Bayes factor asymptotics Kass & Raftery (1995).
78
+ Spectrum bias Ransohoff & Feinstein (1978).