bayes-ab 0.1.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.
package/dist/cli.js ADDED
@@ -0,0 +1,1447 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/core/math.ts
4
+ var LANCZOS_COEFFICIENTS = [
5
+ 676.5203681218851,
6
+ -1259.1392167224028,
7
+ 771.3234287776531,
8
+ -176.6150291621406,
9
+ 12.507343278686905,
10
+ -0.13857109526572012,
11
+ 9984369578019572e-21,
12
+ 15056327351493116e-23
13
+ ];
14
+ function clamp(value, min, max) {
15
+ if (!Number.isFinite(value)) return min;
16
+ if (value < min) return min;
17
+ if (value > max) return max;
18
+ return value;
19
+ }
20
+ function logGamma(z) {
21
+ if (z < 0.5) {
22
+ return Math.log(Math.PI) - Math.log(Math.sin(Math.PI * z)) - logGamma(1 - z);
23
+ }
24
+ const shifted = z - 1;
25
+ let x = 0.9999999999998099;
26
+ for (let index = 0; index < LANCZOS_COEFFICIENTS.length; index += 1) {
27
+ x += LANCZOS_COEFFICIENTS[index] / (shifted + index + 1);
28
+ }
29
+ const t = shifted + LANCZOS_COEFFICIENTS.length - 0.5;
30
+ return 0.9189385332046727 + (shifted + 0.5) * Math.log(t) - t + Math.log(x);
31
+ }
32
+ function logBeta(a, b) {
33
+ return logGamma(a) + logGamma(b) - logGamma(a + b);
34
+ }
35
+ function betaPdf(x, alpha, beta) {
36
+ const boundedX = clamp(x, 1e-9, 1 - 1e-9);
37
+ return Math.exp(
38
+ (alpha - 1) * Math.log(boundedX) + (beta - 1) * Math.log(1 - boundedX) - logBeta(alpha, beta)
39
+ );
40
+ }
41
+ function makeDeterministicRandom(seed) {
42
+ let state = seed >>> 0;
43
+ return () => {
44
+ state = 1664525 * state + 1013904223 >>> 0;
45
+ return state / 4294967296;
46
+ };
47
+ }
48
+ function sampleStandardNormal(random) {
49
+ const u1 = clamp(random(), 1e-12, 1 - 1e-12);
50
+ const u2 = random();
51
+ return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
52
+ }
53
+ function sampleGamma(shape, random) {
54
+ if (shape < 1) {
55
+ const u = clamp(random(), 1e-12, 1 - 1e-12);
56
+ return sampleGamma(shape + 1, random) * u ** (1 / shape);
57
+ }
58
+ const d = shape - 1 / 3;
59
+ const c = 1 / Math.sqrt(9 * d);
60
+ while (true) {
61
+ const x = sampleStandardNormal(random);
62
+ const v = (1 + c * x) ** 3;
63
+ if (v <= 0) continue;
64
+ const u = random();
65
+ if (u < 1 - 0.0331 * x ** 4) return d * v;
66
+ if (Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v))) return d * v;
67
+ }
68
+ }
69
+ function sampleBeta(alpha, beta, random) {
70
+ const x = sampleGamma(alpha, random);
71
+ const y = sampleGamma(beta, random);
72
+ return x / (x + y);
73
+ }
74
+ function sampleBinomialApprox(trials, probability, random) {
75
+ const boundedTrials = Math.max(0, Math.round(trials));
76
+ const boundedProbability = clamp(probability, 0, 1);
77
+ if (boundedTrials === 0 || boundedProbability === 0) return 0;
78
+ if (boundedProbability === 1) return boundedTrials;
79
+ if (boundedTrials <= 50) {
80
+ let successes = 0;
81
+ for (let index = 0; index < boundedTrials; index += 1) {
82
+ if (random() < boundedProbability) successes += 1;
83
+ }
84
+ return successes;
85
+ }
86
+ const mean2 = boundedTrials * boundedProbability;
87
+ const standardDeviation = Math.sqrt(boundedTrials * boundedProbability * (1 - boundedProbability));
88
+ return clamp(Math.round(mean2 + sampleStandardNormal(random) * standardDeviation), 0, boundedTrials);
89
+ }
90
+ function quantile(sortedValues, probability) {
91
+ const boundedProbability = clamp(probability, 0, 1);
92
+ const position = (sortedValues.length - 1) * boundedProbability;
93
+ const lowerIndex = Math.floor(position);
94
+ const upperIndex = Math.ceil(position);
95
+ const lower = sortedValues[lowerIndex];
96
+ if (lowerIndex === upperIndex) return lower;
97
+ const upper = sortedValues[upperIndex];
98
+ const weight = position - lowerIndex;
99
+ return lower * (1 - weight) + upper * weight;
100
+ }
101
+ function mean(values) {
102
+ let sum = 0;
103
+ for (const value of values) sum += value;
104
+ return sum / values.length;
105
+ }
106
+ function probabilityRightBeatsLeftDirect(alphaLeft, betaLeft, alphaRight, betaRight) {
107
+ const base = logBeta(alphaLeft, betaLeft);
108
+ let maxLog = Number.NEGATIVE_INFINITY;
109
+ let scaledSum = 0;
110
+ for (let i = 0; i <= alphaRight - 1; i += 1) {
111
+ const current = logBeta(alphaLeft + i, betaLeft + betaRight) - Math.log(betaRight + i) - logBeta(1 + i, betaRight) - base;
112
+ if (current <= maxLog) {
113
+ scaledSum += Math.exp(current - maxLog);
114
+ continue;
115
+ }
116
+ scaledSum = scaledSum * Math.exp(maxLog - current) + 1;
117
+ maxLog = current;
118
+ }
119
+ return Math.exp(maxLog) * scaledSum;
120
+ }
121
+ function probabilityBBeatsA(alphaA, betaA, alphaB, betaB) {
122
+ if (alphaB <= alphaA) {
123
+ return probabilityRightBeatsLeftDirect(alphaA, betaA, alphaB, betaB);
124
+ }
125
+ return 1 - probabilityRightBeatsLeftDirect(alphaB, betaB, alphaA, betaA);
126
+ }
127
+ function inverseNormalCdf(p) {
128
+ const a = [-39.69683028665376, 220.9460984245205, -275.9285104469687, 138.357751867269, -30.66479806614716, 2.506628277459239];
129
+ const b = [-54.47609879822406, 161.5858368580409, -155.6989798598866, 66.80131188771972, -13.28068155288572];
130
+ const c = [-0.007784894002430293, -0.3223964580411365, -2.400758277161838, -2.549732539343734, 4.374664141464968, 2.938163982698783];
131
+ const d = [0.007784695709041462, 0.3224671290700398, 2.445134137142996, 3.754408661907416];
132
+ const probability = clamp(p, 1e-9, 1 - 1e-9);
133
+ const pLow = 0.02425;
134
+ const pHigh = 1 - pLow;
135
+ const tail = (q2) => (((((c[0] * q2 + c[1]) * q2 + c[2]) * q2 + c[3]) * q2 + c[4]) * q2 + c[5]) / ((((d[0] * q2 + d[1]) * q2 + d[2]) * q2 + d[3]) * q2 + 1);
136
+ if (probability < pLow) return tail(Math.sqrt(-2 * Math.log(probability)));
137
+ if (probability > pHigh) return -tail(Math.sqrt(-2 * Math.log(1 - probability)));
138
+ const q = probability - 0.5;
139
+ const r = q * q;
140
+ 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);
141
+ }
142
+ function erf(x) {
143
+ const sign = x < 0 ? -1 : 1;
144
+ const absoluteX = Math.abs(x);
145
+ const t = 1 / (1 + 0.3275911 * absoluteX);
146
+ const y = 1 - ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * Math.exp(-absoluteX * absoluteX);
147
+ return sign * y;
148
+ }
149
+ function normalCdf(x) {
150
+ return 0.5 * (1 + erf(x / Math.SQRT2));
151
+ }
152
+ function normalPdf(x) {
153
+ return Math.exp(-0.5 * x * x) / Math.sqrt(2 * Math.PI);
154
+ }
155
+ function round(value, decimals = 6) {
156
+ if (!Number.isFinite(value)) return value;
157
+ const factor = 10 ** decimals;
158
+ return Math.round(value * factor) / factor;
159
+ }
160
+
161
+ // src/core/plan.ts
162
+ var SENSITIVITY_EFFECTS = [0.05, 0.1, 0.15, 0.2];
163
+ function requiredSamplePerVariant({
164
+ baseline,
165
+ minEffect,
166
+ alpha = 0.05,
167
+ power = 0.8
168
+ }) {
169
+ const boundedBaseline = clamp(baseline, 1e-6, 1 - 1e-6);
170
+ const absoluteDelta = Math.max(boundedBaseline * minEffect, 1e-6);
171
+ const zAlpha = inverseNormalCdf(1 - alpha / 2);
172
+ const zPower = inverseNormalCdf(power);
173
+ const standardDeviation = Math.sqrt(2 * boundedBaseline * (1 - boundedBaseline));
174
+ return Math.ceil(((zAlpha + zPower) * standardDeviation / absoluteDelta) ** 2);
175
+ }
176
+ function plan({ baseline, minEffect, power = 0.8, alpha = 0.05, dailyVisitors = null }) {
177
+ const daysFor = (perVariant2) => dailyVisitors && dailyVisitors > 0 ? Math.ceil(perVariant2 / dailyVisitors) : null;
178
+ const perVariant = requiredSamplePerVariant({ baseline, minEffect, alpha, power });
179
+ const effects = SENSITIVITY_EFFECTS.includes(minEffect) ? SENSITIVITY_EFFECTS : [...SENSITIVITY_EFFECTS, minEffect].sort((left, right) => left - right);
180
+ return {
181
+ inputs: { baseline, minEffect, power, alpha, dailyVisitors },
182
+ targetRates: { up: baseline * (1 + minEffect), down: baseline * (1 - minEffect) },
183
+ perVariant,
184
+ total: perVariant * 2,
185
+ days: daysFor(perVariant),
186
+ sensitivity: effects.map((effect) => {
187
+ const rowPerVariant = requiredSamplePerVariant({ baseline, minEffect: effect, alpha, power });
188
+ return { minEffect: effect, perVariant: rowPerVariant, days: daysFor(rowPerVariant) };
189
+ })
190
+ };
191
+ }
192
+
193
+ // src/core/share.ts
194
+ var WEB_TOOL_URL = "https://www.joebrundage.com/tools/bayesian-ab-test";
195
+ function formatPercentParam(fraction2) {
196
+ return String(Number((fraction2 * 100).toPrecision(10)));
197
+ }
198
+ function shareUrl(inputs) {
199
+ const params = new URLSearchParams({
200
+ visitorsA: String(inputs.visitorsA),
201
+ conversionsA: String(inputs.conversionsA),
202
+ visitorsB: String(inputs.visitorsB),
203
+ conversionsB: String(inputs.conversionsB),
204
+ thresholdPercent: formatPercentParam(inputs.threshold),
205
+ meaningfulLiftPercent: formatPercentParam(inputs.minEffect)
206
+ });
207
+ return `${WEB_TOOL_URL}?${params.toString()}`;
208
+ }
209
+
210
+ // src/core/analyze.ts
211
+ var DEFAULT_THRESHOLD = 0.95;
212
+ var DEFAULT_MIN_EFFECT = 0.1;
213
+ var DEFAULT_SAMPLES = 12e3;
214
+ var PRIOR_ALPHA = 1;
215
+ var PRIOR_BETA = 1;
216
+ var POSTERIOR_CHART_POINTS = 96;
217
+ var WAITING_SCENARIOS = [1e3, 2e3, 5e3, 1e4];
218
+ var WAITING_VALUE_SIMULATIONS = 1500;
219
+ var DECISION_LABELS = {
220
+ ship_b: "Ship Variant B",
221
+ keep_a: "Keep Variant A",
222
+ continue: "Continue test",
223
+ inconclusive: "Inconclusive"
224
+ };
225
+ function deriveSeed(...parts) {
226
+ let hash = 2166136261;
227
+ for (const part of parts) {
228
+ const text = String(part);
229
+ for (let index = 0; index < text.length; index += 1) {
230
+ hash ^= text.charCodeAt(index);
231
+ hash = Math.imul(hash, 16777619) >>> 0;
232
+ }
233
+ hash ^= 2654435769;
234
+ hash = Math.imul(hash, 16777619) >>> 0;
235
+ }
236
+ return hash >>> 0;
237
+ }
238
+ function twoSidedPValue(visitorsA, conversionsA, visitorsB, conversionsB) {
239
+ const pA = conversionsA / visitorsA;
240
+ const pB = conversionsB / visitorsB;
241
+ const pooled = (conversionsA + conversionsB) / (visitorsA + visitorsB);
242
+ const standardError = Math.sqrt(pooled * (1 - pooled) * (1 / visitorsA + 1 / visitorsB));
243
+ if (standardError === 0) return 1;
244
+ const z = (pB - pA) / standardError;
245
+ return clamp(2 * (1 - normalCdf(Math.abs(z))), 0, 1);
246
+ }
247
+ function evidenceStrength(current, required) {
248
+ const ratio = current / Math.max(required, 1);
249
+ if (ratio < 0.5) return "low";
250
+ if (ratio < 1) return "moderate";
251
+ return "strong";
252
+ }
253
+ function maturity(progress) {
254
+ if (progress < 0.35) return "early";
255
+ if (progress < 0.85) return "building";
256
+ return "mature";
257
+ }
258
+ function decisionStatus({
259
+ probabilityMeaningfulLift,
260
+ probabilityMeaningfulHarm,
261
+ threshold,
262
+ evidence
263
+ }) {
264
+ if (probabilityMeaningfulLift >= threshold) return "ship_b";
265
+ if (probabilityMeaningfulHarm >= threshold) return "keep_a";
266
+ if (evidence === "low") return "continue";
267
+ return "inconclusive";
268
+ }
269
+ function chartData(alphaA, betaA, alphaB, betaB, sortedA, sortedB) {
270
+ const minRate = Math.max(1e-4, Math.min(quantile(sortedA, 1e-3), quantile(sortedB, 1e-3)) * 0.9);
271
+ const maxRate = Math.min(0.9999, Math.max(quantile(sortedA, 0.999), quantile(sortedB, 0.999)) * 1.1);
272
+ const span = maxRate - minRate;
273
+ return Array.from({ length: POSTERIOR_CHART_POINTS }, (_, index) => {
274
+ const rate = minRate + span * index / (POSTERIOR_CHART_POINTS - 1);
275
+ return { rate, densityA: betaPdf(rate, alphaA, betaA), densityB: betaPdf(rate, alphaB, betaB) };
276
+ });
277
+ }
278
+ function waitingScenarios(alphaA, betaA, alphaB, betaB, expectedRegretNow) {
279
+ const random = makeDeterministicRandom(alphaA * 11 + betaA * 13 + alphaB * 17 + betaB * 19);
280
+ return WAITING_SCENARIOS.map((extraPerVariant) => {
281
+ let regretSum = 0;
282
+ for (let index = 0; index < WAITING_VALUE_SIMULATIONS; index += 1) {
283
+ const trueRateA = sampleBeta(alphaA, betaA, random);
284
+ const trueRateB = sampleBeta(alphaB, betaB, random);
285
+ const extraA = sampleBinomialApprox(extraPerVariant, trueRateA, random);
286
+ const extraB = sampleBinomialApprox(extraPerVariant, trueRateB, random);
287
+ const futureMeanA = (alphaA + extraA) / (alphaA + betaA + extraPerVariant);
288
+ const futureMeanB = (alphaB + extraB) / (alphaB + betaB + extraPerVariant);
289
+ const regretIfChooseA = Math.max(trueRateB - trueRateA, 0);
290
+ const regretIfChooseB = Math.max(trueRateA - trueRateB, 0);
291
+ regretSum += futureMeanB >= futureMeanA ? regretIfChooseB : regretIfChooseA;
292
+ }
293
+ const expectedRegret = regretSum / WAITING_VALUE_SIMULATIONS;
294
+ return { extraPerVariant, expectedRegret, valueOfWaiting: Math.max(0, expectedRegretNow - expectedRegret) };
295
+ });
296
+ }
297
+ function analyze({
298
+ visitorsA,
299
+ conversionsA,
300
+ visitorsB,
301
+ conversionsB,
302
+ threshold = DEFAULT_THRESHOLD,
303
+ minEffect = DEFAULT_MIN_EFFECT,
304
+ samples = DEFAULT_SAMPLES,
305
+ seed = null,
306
+ includeChart = false
307
+ }) {
308
+ const alphaA = PRIOR_ALPHA + conversionsA;
309
+ const betaA = PRIOR_BETA + (visitorsA - conversionsA);
310
+ const alphaB = PRIOR_ALPHA + conversionsB;
311
+ const betaB = PRIOR_BETA + (visitorsB - conversionsB);
312
+ const rateA = conversionsA / visitorsA;
313
+ const rateB = conversionsB / visitorsB;
314
+ const posteriorMeanA = alphaA / (alphaA + betaA);
315
+ const posteriorMeanB = alphaB / (alphaB + betaB);
316
+ const pBBeatsA = clamp(probabilityBBeatsA(alphaA, betaA, alphaB, betaB), 0, 1);
317
+ const resolvedSeed = seed ?? alphaA + betaA * 3 + alphaB * 5 + betaB * 7;
318
+ const random = makeDeterministicRandom(resolvedSeed);
319
+ const samplesA = Array.from({ length: samples }, () => sampleBeta(alphaA, betaA, random));
320
+ const samplesB = Array.from({ length: samples }, () => sampleBeta(alphaB, betaB, random));
321
+ const relativeLifts = samplesA.map((sampleA, index) => samplesB[index] / sampleA - 1);
322
+ const absoluteDiffs = samplesA.map((sampleA, index) => samplesB[index] - sampleA);
323
+ const sortedRelativeLifts = [...relativeLifts].sort((left, right) => left - right);
324
+ const sortedAbsoluteDiffs = [...absoluteDiffs].sort((left, right) => left - right);
325
+ const share = (predicate) => mean(relativeLifts.map((lift) => predicate(lift) ? 1 : 0));
326
+ const expectedLossShipA = mean(absoluteDiffs.map((difference) => Math.max(difference, 0)));
327
+ const expectedLossShipB = mean(absoluteDiffs.map((difference) => Math.max(-difference, 0)));
328
+ const bayesActionNow = expectedLossShipB <= expectedLossShipA ? "B" : "A";
329
+ const expectedRegretNow = bayesActionNow === "B" ? expectedLossShipB : expectedLossShipA;
330
+ const baselineRate = (posteriorMeanA + posteriorMeanB) / 2;
331
+ const smallerArm = Math.min(visitorsA, visitorsB);
332
+ const requiredPerVariant = requiredSamplePerVariant({ baseline: baselineRate, minEffect });
333
+ const evidence = evidenceStrength(smallerArm, requiredPerVariant);
334
+ const maturityProgress = clamp(smallerArm / Math.max(requiredPerVariant, 1), 0, 1);
335
+ const probabilityMeaningfulLift = share((lift) => lift > minEffect);
336
+ const probabilityMeaningfulHarm = share((lift) => lift < -minEffect);
337
+ const status = decisionStatus({ probabilityMeaningfulLift, probabilityMeaningfulHarm, threshold, evidence });
338
+ const result = {
339
+ inputs: {
340
+ visitorsA,
341
+ conversionsA,
342
+ visitorsB,
343
+ conversionsB,
344
+ threshold,
345
+ minEffect,
346
+ prior: [PRIOR_ALPHA, PRIOR_BETA],
347
+ samples,
348
+ seed: resolvedSeed
349
+ },
350
+ decision: {
351
+ status,
352
+ label: DECISION_LABELS[status],
353
+ evidence,
354
+ maturity: maturity(maturityProgress),
355
+ maturityProgress,
356
+ bayesActionNow,
357
+ expectedRegretNow
358
+ },
359
+ probability: {
360
+ bBeatsA: pBBeatsA,
361
+ aBeatsB: 1 - pBBeatsA,
362
+ meaningfulLift: probabilityMeaningfulLift,
363
+ meaningfulHarm: probabilityMeaningfulHarm,
364
+ harmful: share((lift) => lift < 0),
365
+ liftAbove1pct: share((lift) => lift > 0.01),
366
+ liftAbove5pct: share((lift) => lift > 0.05),
367
+ harmBeyond5pct: share((lift) => lift < -0.05),
368
+ harmBeyond10pct: share((lift) => lift < -0.1)
369
+ },
370
+ effect: {
371
+ relativeLift: posteriorMeanB / posteriorMeanA - 1,
372
+ relativeLiftCI95: [quantile(sortedRelativeLifts, 0.025), quantile(sortedRelativeLifts, 0.975)],
373
+ absoluteDiff: posteriorMeanB - posteriorMeanA,
374
+ absoluteDiffCI95: [quantile(sortedAbsoluteDiffs, 0.025), quantile(sortedAbsoluteDiffs, 0.975)],
375
+ observedRelativeLift: rateA > 0 ? rateB / rateA - 1 : null,
376
+ observedAbsoluteDiff: rateB - rateA
377
+ },
378
+ expectedLoss: { shipA: expectedLossShipA, shipB: expectedLossShipB },
379
+ variants: {
380
+ A: { visitors: visitorsA, conversions: conversionsA, rate: rateA, posteriorMean: posteriorMeanA, posterior: [alphaA, betaA] },
381
+ B: { visitors: visitorsB, conversions: conversionsB, rate: rateB, posteriorMean: posteriorMeanB, posterior: [alphaB, betaB] }
382
+ },
383
+ sampleSize: {
384
+ requiredPerVariant,
385
+ additionalPerVariant: Math.max(0, requiredPerVariant - smallerArm),
386
+ detectableEffectNow: (inverseNormalCdf(1 - 0.05 / 2) + inverseNormalCdf(0.8)) * Math.sqrt(2 * baselineRate * (1 - baselineRate) / smallerArm) / baselineRate
387
+ },
388
+ waiting: waitingScenarios(alphaA, betaA, alphaB, betaB, expectedRegretNow),
389
+ frequentist: { pValueTwoSided: twoSidedPValue(visitorsA, conversionsA, visitorsB, conversionsB) },
390
+ shareUrl: shareUrl({ visitorsA, conversionsA, visitorsB, conversionsB, threshold, minEffect })
391
+ };
392
+ if (includeChart) {
393
+ const sortedA = [...samplesA].sort((left, right) => left - right);
394
+ const sortedB = [...samplesB].sort((left, right) => left - right);
395
+ result.chart = chartData(alphaA, betaA, alphaB, betaB, sortedA, sortedB);
396
+ }
397
+ return result;
398
+ }
399
+
400
+ // src/core/project.ts
401
+ var DEFAULT_HORIZON_DAYS = 28;
402
+ var DEFAULT_SIMULATIONS = 2e3;
403
+ var DEFAULT_MAX_LOSS = 1e-4;
404
+ function moments({ alpha, beta }) {
405
+ const total = alpha + beta;
406
+ return { mean: alpha / total, variance: alpha * beta / (total * total * (total + 1)) };
407
+ }
408
+ function probabilityBExceedsScaledA(a, b, k) {
409
+ const mA = moments(a);
410
+ const mB = moments(b);
411
+ const mean2 = mB.mean - k * mA.mean;
412
+ const sd = Math.sqrt(mB.variance + k * k * mA.variance);
413
+ if (sd === 0) return mean2 > 0 ? 1 : 0;
414
+ return normalCdf(mean2 / sd);
415
+ }
416
+ function expectedLosses(a, b) {
417
+ const mA = moments(a);
418
+ const mB = moments(b);
419
+ const mean2 = mB.mean - mA.mean;
420
+ const sd = Math.sqrt(mA.variance + mB.variance);
421
+ if (sd === 0) return { shipA: Math.max(mean2, 0), shipB: Math.max(-mean2, 0) };
422
+ const z = mean2 / sd;
423
+ return {
424
+ shipA: mean2 * normalCdf(z) + sd * normalPdf(z),
425
+ shipB: -mean2 * normalCdf(-z) + sd * normalPdf(z)
426
+ };
427
+ }
428
+ function evaluateRule(a, b, rule, threshold, minEffect, maxLoss) {
429
+ if (rule === "loss") {
430
+ const loss = expectedLosses(a, b);
431
+ if (Math.min(loss.shipA, loss.shipB) > maxLoss) return null;
432
+ return loss.shipB <= loss.shipA ? "ship_b" : "keep_a";
433
+ }
434
+ const effect = rule === "lift" ? minEffect : 0;
435
+ if (probabilityBExceedsScaledA(a, b, 1 + effect) >= threshold) return "ship_b";
436
+ if (1 - probabilityBExceedsScaledA(a, b, 1 - effect) >= threshold) return "keep_a";
437
+ return null;
438
+ }
439
+ function decisionNowFrom(current, rule, threshold, maxLoss) {
440
+ if (rule === "lift") {
441
+ if (current.decision.status === "ship_b" || current.decision.status === "keep_a") return current.decision.status;
442
+ return null;
443
+ }
444
+ if (rule === "superiority") {
445
+ if (current.probability.bBeatsA >= threshold) return "ship_b";
446
+ if (current.probability.aBeatsB >= threshold) return "keep_a";
447
+ return null;
448
+ }
449
+ const { shipA, shipB } = current.expectedLoss;
450
+ if (Math.min(shipA, shipB) > maxLoss) return null;
451
+ return shipB <= shipA ? "ship_b" : "keep_a";
452
+ }
453
+ function checkpoints(horizonDays) {
454
+ const days = [];
455
+ for (let day = 7; day < horizonDays; day += 7) days.push(day);
456
+ days.push(horizonDays);
457
+ return days;
458
+ }
459
+ function liftNearThreshold(current, minEffect) {
460
+ const lift = current.effect.relativeLift;
461
+ const [lower, upper] = current.effect.relativeLiftCI95;
462
+ const distance = Math.min(Math.abs(lift - minEffect), Math.abs(lift + minEffect));
463
+ return distance < (upper - lower) / 8;
464
+ }
465
+ function project({
466
+ visitorsA,
467
+ conversionsA,
468
+ visitorsB,
469
+ conversionsB,
470
+ dailyVisitors,
471
+ horizonDays = DEFAULT_HORIZON_DAYS,
472
+ threshold = DEFAULT_THRESHOLD,
473
+ minEffect = DEFAULT_MIN_EFFECT,
474
+ stopRule = "lift",
475
+ maxLoss = DEFAULT_MAX_LOSS,
476
+ simulations = DEFAULT_SIMULATIONS,
477
+ seed = null
478
+ }) {
479
+ const current = analyze({ visitorsA, conversionsA, visitorsB, conversionsB, threshold, minEffect });
480
+ const resolvedSeed = seed ?? deriveSeed(visitorsA, conversionsA, visitorsB, conversionsB, dailyVisitors, horizonDays, ["lift", "superiority", "loss"].indexOf(stopRule), simulations);
481
+ const inputs = {
482
+ visitorsA,
483
+ conversionsA,
484
+ visitorsB,
485
+ conversionsB,
486
+ threshold,
487
+ minEffect,
488
+ prior: [PRIOR_ALPHA, PRIOR_BETA],
489
+ dailyVisitors,
490
+ horizonDays,
491
+ stopRule,
492
+ maxLoss: stopRule === "loss" ? maxLoss : null,
493
+ simulations,
494
+ seed: resolvedSeed
495
+ };
496
+ const link = shareUrl({ visitorsA, conversionsA, visitorsB, conversionsB, threshold, minEffect });
497
+ const diagnostic = stopRule === "lift" && liftNearThreshold(current, minEffect) ? "lift_near_threshold" : null;
498
+ const decisionNow = decisionNowFrom(current, stopRule, threshold, maxLoss);
499
+ if (decisionNow) {
500
+ return {
501
+ inputs,
502
+ alreadyDecided: true,
503
+ decisionNow,
504
+ decisionWithinHorizon: 1,
505
+ outcomes: { shipB: decisionNow === "ship_b" ? 1 : 0, keepA: decisionNow === "keep_a" ? 1 : 0, undecided: 0 },
506
+ daysToDecision: { p25: 0, p50: 0, p75: 0, p90: 0 },
507
+ cumulative: checkpoints(horizonDays).map((day) => ({ day, p: 1 })),
508
+ diagnostic,
509
+ approximation: "normal",
510
+ shareUrl: link
511
+ };
512
+ }
513
+ const alphaA0 = PRIOR_ALPHA + conversionsA;
514
+ const betaA0 = PRIOR_BETA + (visitorsA - conversionsA);
515
+ const alphaB0 = PRIOR_ALPHA + conversionsB;
516
+ const betaB0 = PRIOR_BETA + (visitorsB - conversionsB);
517
+ const random = makeDeterministicRandom(resolvedSeed);
518
+ const decidedByDay = new Array(horizonDays + 1).fill(0);
519
+ const decisionDays = [];
520
+ let shipB = 0;
521
+ let keepA = 0;
522
+ for (let simulation = 0; simulation < simulations; simulation += 1) {
523
+ const trueRateA = sampleBeta(alphaA0, betaA0, random);
524
+ const trueRateB = sampleBeta(alphaB0, betaB0, random);
525
+ const a = { alpha: alphaA0, beta: betaA0 };
526
+ const b = { alpha: alphaB0, beta: betaB0 };
527
+ for (let day = 1; day <= horizonDays; day += 1) {
528
+ const newA = sampleBinomialApprox(dailyVisitors, trueRateA, random);
529
+ const newB = sampleBinomialApprox(dailyVisitors, trueRateB, random);
530
+ a.alpha += newA;
531
+ a.beta += dailyVisitors - newA;
532
+ b.alpha += newB;
533
+ b.beta += dailyVisitors - newB;
534
+ const decision = evaluateRule(a, b, stopRule, threshold, minEffect, maxLoss);
535
+ if (decision) {
536
+ if (decision === "ship_b") shipB += 1;
537
+ else keepA += 1;
538
+ decidedByDay[day] = decidedByDay[day] + 1;
539
+ decisionDays.push(day);
540
+ break;
541
+ }
542
+ }
543
+ }
544
+ let running = 0;
545
+ const cumulativeByDay = decidedByDay.map((count2) => {
546
+ running += count2;
547
+ return running / simulations;
548
+ });
549
+ const sortedDays = [...decisionDays].sort((left, right) => left - right);
550
+ return {
551
+ inputs,
552
+ alreadyDecided: false,
553
+ decisionNow: null,
554
+ decisionWithinHorizon: decisionDays.length / simulations,
555
+ outcomes: {
556
+ shipB: shipB / simulations,
557
+ keepA: keepA / simulations,
558
+ undecided: (simulations - decisionDays.length) / simulations
559
+ },
560
+ daysToDecision: sortedDays.length === 0 ? null : {
561
+ p25: quantile(sortedDays, 0.25),
562
+ p50: quantile(sortedDays, 0.5),
563
+ p75: quantile(sortedDays, 0.75),
564
+ p90: quantile(sortedDays, 0.9)
565
+ },
566
+ cumulative: checkpoints(horizonDays).map((day) => ({ day, p: cumulativeByDay[day] })),
567
+ diagnostic,
568
+ approximation: "normal",
569
+ shareUrl: link
570
+ };
571
+ }
572
+
573
+ // src/cli/args.ts
574
+ import { parseArgs } from "node:util";
575
+ var CliError = class extends Error {
576
+ code;
577
+ field;
578
+ exitCode;
579
+ constructor(message, { code: code2 = "invalid_input", field = null, exitCode = 2 } = {}) {
580
+ super(message);
581
+ this.code = code2;
582
+ this.field = field;
583
+ this.exitCode = exitCode;
584
+ }
585
+ };
586
+ var COMMANDS = ["analyze", "project", "plan", "schema"];
587
+ var OPTIONS = {
588
+ "visitors-a": { type: "string" },
589
+ "conversions-a": { type: "string" },
590
+ "rate-a": { type: "string" },
591
+ "visitors-b": { type: "string" },
592
+ "conversions-b": { type: "string" },
593
+ "rate-b": { type: "string" },
594
+ threshold: { type: "string" },
595
+ "min-effect": { type: "string" },
596
+ "daily-visitors": { type: "string" },
597
+ horizon: { type: "string" },
598
+ "stop-rule": { type: "string" },
599
+ "max-loss": { type: "string" },
600
+ simulations: { type: "string" },
601
+ samples: { type: "string" },
602
+ seed: { type: "string" },
603
+ baseline: { type: "string" },
604
+ power: { type: "string" },
605
+ alpha: { type: "string" },
606
+ json: { type: "boolean" },
607
+ pretty: { type: "boolean" },
608
+ brief: { type: "boolean" },
609
+ "include-chart": { type: "boolean" },
610
+ help: { type: "boolean", short: "h" },
611
+ version: { type: "boolean", short: "v" }
612
+ };
613
+ function cleanNumber(value) {
614
+ return value.trim().replace(/[,_]/g, "");
615
+ }
616
+ function parseCount(flag, value, { min = 0 } = {}) {
617
+ if (value === void 0) throw new CliError(`Missing required flag --${flag}.`, { code: "missing_flag", field: flag });
618
+ const cleaned = cleanNumber(value);
619
+ if (!/^\d+$/.test(cleaned)) throw new CliError(`--${flag} must be a whole number, got "${value}".`, { field: flag });
620
+ const parsed = Number(cleaned);
621
+ if (!Number.isSafeInteger(parsed)) throw new CliError(`--${flag} is too large.`, { field: flag });
622
+ if (parsed < min) throw new CliError(`--${flag} must be at least ${min}.`, { field: flag });
623
+ return parsed;
624
+ }
625
+ function parseNumber(flag, value) {
626
+ const cleaned = cleanNumber(value).replace(/%$/, "");
627
+ const parsed = Number(cleaned);
628
+ if (cleaned === "" || !Number.isFinite(parsed)) throw new CliError(`--${flag} must be a number, got "${value}".`, { field: flag });
629
+ return parsed;
630
+ }
631
+ function parsePercentFlag(flag, value) {
632
+ return parseNumber(flag, value) / 100;
633
+ }
634
+ function parseRateFlag(flag, value) {
635
+ const parsed = parseNumber(flag, value);
636
+ if (value.trim().endsWith("%")) return parsed / 100;
637
+ return parsed > 1 ? parsed / 100 : parsed;
638
+ }
639
+ function parseVariant(values, letter) {
640
+ const visitorsFlag = `visitors-${letter}`;
641
+ const conversionsFlag = `conversions-${letter}`;
642
+ const rateFlag = `rate-${letter}`;
643
+ const visitors = parseCount(visitorsFlag, values[visitorsFlag], { min: 1 });
644
+ const conversionsRaw = values[conversionsFlag];
645
+ const rateRaw = values[rateFlag];
646
+ if (conversionsRaw !== void 0 && rateRaw !== void 0) {
647
+ throw new CliError(`Give either --${conversionsFlag} or --${rateFlag}, not both.`, { field: conversionsFlag });
648
+ }
649
+ if (conversionsRaw === void 0 && rateRaw === void 0) {
650
+ throw new CliError(`Variant ${letter.toUpperCase()} needs --${conversionsFlag} or --${rateFlag}.`, { code: "missing_flag", field: conversionsFlag });
651
+ }
652
+ if (rateRaw !== void 0) {
653
+ const rate = parseRateFlag(rateFlag, rateRaw);
654
+ if (rate < 0 || rate > 1) throw new CliError(`--${rateFlag} must be between 0% and 100%.`, { field: rateFlag });
655
+ return { visitors, conversions: Math.round(visitors * rate), rate };
656
+ }
657
+ const conversions = parseCount(conversionsFlag, conversionsRaw);
658
+ if (conversions > visitors) {
659
+ throw new CliError(
660
+ `Conversions cannot exceed visitors (variant ${letter.toUpperCase()}: ${conversions} > ${visitors}).`,
661
+ { field: conversionsFlag }
662
+ );
663
+ }
664
+ return { visitors, conversions, rate: null };
665
+ }
666
+ function parseVariants(values) {
667
+ const a = parseVariant(values, "a");
668
+ const b = parseVariant(values, "b");
669
+ const derivedFrom2 = a.rate !== null || b.rate !== null ? {} : null;
670
+ if (derivedFrom2 && a.rate !== null) derivedFrom2.rateA = a.rate;
671
+ if (derivedFrom2 && b.rate !== null) derivedFrom2.rateB = b.rate;
672
+ return { visitorsA: a.visitors, conversionsA: a.conversions, visitorsB: b.visitors, conversionsB: b.conversions, derivedFrom: derivedFrom2 };
673
+ }
674
+ function parseThreshold(values) {
675
+ if (values.threshold === void 0) return DEFAULT_THRESHOLD;
676
+ const threshold = parsePercentFlag("threshold", values.threshold);
677
+ if (threshold <= 0.5 || threshold >= 1) throw new CliError("--threshold must be a percent between 50 and 100, exclusive.", { field: "threshold" });
678
+ return threshold;
679
+ }
680
+ function parseMinEffect(values) {
681
+ if (values["min-effect"] === void 0) return DEFAULT_MIN_EFFECT;
682
+ const minEffect = parsePercentFlag("min-effect", values["min-effect"]);
683
+ if (minEffect < 0 || minEffect >= 1) throw new CliError("--min-effect must be a percent between 0 and 100.", { field: "min-effect" });
684
+ return minEffect;
685
+ }
686
+ function parseAnalyzeArgs(values) {
687
+ const samples = values.samples === void 0 ? DEFAULT_SAMPLES : parseCount("samples", values.samples, { min: 100 });
688
+ const seed = values.seed === void 0 ? null : parseCount("seed", values.seed);
689
+ return { ...parseVariants(values), threshold: parseThreshold(values), minEffect: parseMinEffect(values), samples, seed };
690
+ }
691
+ function parseStopRule(value) {
692
+ if (value === void 0) return "lift";
693
+ if (value === "lift" || value === "superiority" || value === "loss") return value;
694
+ throw new CliError(`--stop-rule must be lift, superiority or loss, got "${value}".`, { field: "stop-rule" });
695
+ }
696
+ function parseProjectArgs(values) {
697
+ const base = parseAnalyzeArgs(values);
698
+ const dailyVisitors = parseCount("daily-visitors", values["daily-visitors"], { min: 1 });
699
+ const horizonDays = values.horizon === void 0 ? DEFAULT_HORIZON_DAYS : parseCount("horizon", values.horizon, { min: 1 });
700
+ if (horizonDays > 3650) throw new CliError("--horizon must be 3650 days or fewer.", { field: "horizon" });
701
+ const stopRule = parseStopRule(values["stop-rule"]);
702
+ const maxLoss = values["max-loss"] === void 0 ? DEFAULT_MAX_LOSS : parseNumber("max-loss", values["max-loss"]) / 100;
703
+ if (maxLoss < 0) throw new CliError("--max-loss must be zero or more percentage points.", { field: "max-loss" });
704
+ const simulations = values.simulations === void 0 ? DEFAULT_SIMULATIONS : parseCount("simulations", values.simulations, { min: 100 });
705
+ return { ...base, dailyVisitors, horizonDays, stopRule, maxLoss, simulations };
706
+ }
707
+ function parsePlanArgs(values) {
708
+ if (values.baseline === void 0) throw new CliError("Missing required flag --baseline.", { code: "missing_flag", field: "baseline" });
709
+ const baseline = parseRateFlag("baseline", values.baseline);
710
+ if (baseline <= 0 || baseline >= 1) throw new CliError("--baseline must be between 0% and 100%, exclusive. Use 1% for one percent.", { field: "baseline" });
711
+ if (values["min-effect"] === void 0) throw new CliError("Missing required flag --min-effect.", { code: "missing_flag", field: "min-effect" });
712
+ const minEffect = parsePercentFlag("min-effect", values["min-effect"]);
713
+ if (minEffect <= 0 || minEffect >= 1) throw new CliError("--min-effect must be a percent between 0 and 100, exclusive.", { field: "min-effect" });
714
+ const power = values.power === void 0 ? 0.8 : parsePercentFlag("power", values.power);
715
+ if (power <= 0 || power >= 1) throw new CliError("--power must be a percent between 0 and 100, exclusive.", { field: "power" });
716
+ const alpha = values.alpha === void 0 ? 0.05 : parsePercentFlag("alpha", values.alpha);
717
+ if (alpha <= 0 || alpha >= 1) throw new CliError("--alpha must be a percent between 0 and 100, exclusive.", { field: "alpha" });
718
+ const dailyVisitors = values["daily-visitors"] === void 0 ? null : parseCount("daily-visitors", values["daily-visitors"], { min: 1 });
719
+ return { baseline, minEffect, power, alpha, dailyVisitors };
720
+ }
721
+ function parseOutput(values) {
722
+ return {
723
+ json: values.json ? true : null,
724
+ pretty: Boolean(values.pretty),
725
+ brief: Boolean(values.brief),
726
+ includeChart: Boolean(values["include-chart"])
727
+ };
728
+ }
729
+ function parseCliArgs(argv) {
730
+ let parsed;
731
+ try {
732
+ parsed = parseArgs({ args: argv, options: OPTIONS, allowPositionals: true, strict: true });
733
+ } catch (error) {
734
+ const message = error instanceof Error ? error.message : String(error);
735
+ throw new CliError(message.replace(/^.*?: /, ""), { code: "unknown_flag" });
736
+ }
737
+ const values = parsed.values;
738
+ const [command, ...rest] = parsed.positionals;
739
+ if (values.version) return { kind: "version" };
740
+ if (values.help || command === void 0 || command === "help") return { kind: "help" };
741
+ if (!COMMANDS.includes(command)) {
742
+ throw new CliError(`Unknown command "${command}". Expected one of: ${COMMANDS.join(", ")}.`, { code: "unknown_command" });
743
+ }
744
+ const output = parseOutput(values);
745
+ if (command === "schema") {
746
+ const target = rest[0];
747
+ if (target !== void 0 && !["analyze", "project", "plan"].includes(target)) {
748
+ throw new CliError(`schema takes analyze, project or plan, got "${target}".`, { code: "unknown_command" });
749
+ }
750
+ return { kind: "schema", target: target ?? null, output };
751
+ }
752
+ if (rest.length > 0) {
753
+ throw new CliError(`Unexpected argument "${rest[0]}". Inputs are given as flags, e.g. --visitors-a 12000.`, { code: "unexpected_argument" });
754
+ }
755
+ if (command === "analyze") return { kind: "analyze", args: parseAnalyzeArgs(values), output };
756
+ if (command === "project") return { kind: "project", args: parseProjectArgs(values), output };
757
+ return { kind: "plan", args: parsePlanArgs(values), output };
758
+ }
759
+
760
+ // src/cli/help.ts
761
+ function helpText(version) {
762
+ return `bayes-ab ${version} Bayesian A/B test analysis for humans and agents
763
+
764
+ Compares two variants of a binary outcome (signup, click, purchase) using
765
+ Beta(1,1) posteriors on each conversion rate. Reports the probability that B
766
+ beats A, the probability the lift clears a meaningful threshold, expected
767
+ loss from shipping either variant, and a forward projection of how long the
768
+ test needs to run. Same math as ${WEB_TOOL_URL}
769
+
770
+ USAGE
771
+ bayes-ab analyze --visitors-a N --conversions-a N --visitors-b N --conversions-b N [options]
772
+ bayes-ab project <analyze inputs> --daily-visitors N [--horizon DAYS] [--stop-rule RULE]
773
+ bayes-ab plan --baseline RATE --min-effect PCT [--daily-visitors N]
774
+ bayes-ab schema [analyze|project|plan]
775
+
776
+ COMMANDS
777
+ analyze Given what you observed, what should you do? The web tool's full readout.
778
+ project If traffic continues at this pace, how long until the test decides, and which way?
779
+ plan Before any data: how many visitors per variant to detect a given effect?
780
+ schema Print the JSON Schema of a command's output, for parsing ahead of time.
781
+
782
+ INPUTS (analyze, project)
783
+ --visitors-a N, --visitors-b N Visitors (or users, sessions) in each variant. Required.
784
+ --conversions-a N, --conversions-b N Conversions in each variant. Required unless --rate-X is given.
785
+ --rate-a RATE, --rate-b RATE Use a rate instead of a count for what-if questions.
786
+ Accepts 6.1%, 6.1 (percent when above 1) or 0.061 (fraction).
787
+ Resolves to round(visitors \xD7 rate) conversions.
788
+ --threshold PCT Decision threshold on posterior probability. default 95
789
+ --min-effect PCT Meaningful relative effect, symmetric: with 10, B must be
790
+ 10% better than A to count as a win or 10% worse to count
791
+ as a loss. Relative, not percentage points. default 10
792
+ Whole numbers may include commas or underscores: 12,000 and 12_000 both work.
793
+
794
+ PROJECTION (project)
795
+ --daily-visitors N Visitors per day, per variant. Required.
796
+ --horizon DAYS How far ahead to simulate. default 28
797
+ --stop-rule RULE When a simulated future counts as decided:
798
+ lift P(lift > +min-effect) or P(lift < -min-effect) reaches
799
+ the threshold. Matches the web tool's decision status.
800
+ superiority P(B beats A) or P(A beats B) reaches the threshold.
801
+ Ignores --min-effect. Decides sooner.
802
+ loss Expected loss of the better variant drops below --max-loss.
803
+ default lift
804
+ --max-loss PP Loss rule only, in percentage points of conversion rate. default 0.01
805
+ --simulations N Simulated futures. More is smoother and slower. default 2000
806
+ Future days use a normal approximation to the posterior. Day zero is exact.
807
+
808
+ PLANNING (plan)
809
+ --baseline RATE Current conversion rate, e.g. 5.5% or 0.055. Required.
810
+ --min-effect PCT Relative change to detect. Required.
811
+ --daily-visitors N Converts the sample size to days.
812
+ --power PCT Statistical power. default 80
813
+ --alpha PCT Two-sided significance level. default 5
814
+ This is the frequentist sample-size formula, the same yardstick the web tool
815
+ uses for its maturity bar. A planning number, not the Bayesian decision rule.
816
+
817
+ OUTPUT
818
+ --json One JSON object on stdout. Default when stdout is not a terminal,
819
+ so piping or calling from an agent gets JSON automatically.
820
+ --pretty Indent the JSON.
821
+ --brief One-line human summary (analyze and project).
822
+ --include-chart Add 96 posterior density points to analyze's JSON.
823
+ --samples N Monte Carlo samples for intervals and loss. default 12000
824
+ --seed N RNG seed. Defaults to a value derived from the inputs, so identical
825
+ inputs always give identical output.
826
+ -h, --help This text. -v, --version Print the version.
827
+
828
+ READING THE OUTPUT
829
+ decision.status ship_b | keep_a | continue | inconclusive
830
+ ship_b / keep_a: P(meaningful lift / harm) reached the threshold.
831
+ continue: not there yet and the sample is still small.
832
+ inconclusive: sample is large but the effect straddles the threshold.
833
+ probability.bBeatsA Exact closed-form P(B > A). Often high while status is still "continue",
834
+ because beating A is easier than beating A by --min-effect.
835
+ expectedLoss.shipB Average conversion-rate regret from shipping B if A is truly better.
836
+ A fraction: 0.00005 is 0.005 percentage points, about 0.5 conversions
837
+ per 10,000 visitors.
838
+ decision.bayesActionNow The variant with lower expected loss. Ship this if you must ship today.
839
+ project.decisionWithinHorizon Share of simulated futures that reach a decision by the horizon.
840
+ project.daysToDecision Quantiles of the decision day, among futures that decided. null if none.
841
+ project.diagnostic "lift_near_threshold" when the estimated lift sits on the --min-effect
842
+ line, which makes the lift rule slow to resolve. Try a smaller
843
+ --min-effect or --stop-rule superiority.
844
+ All rates, probabilities and losses in JSON are fractions (0.0603), never percents.
845
+ Every result includes shareUrl, the web tool with these inputs filled in.
846
+
847
+ EXAMPLES
848
+ bayes-ab analyze --visitors-a 12000 --conversions-a 660 --visitors-b 11850 --conversions-b 714
849
+ bayes-ab analyze --visitors-a 12000 --conversions-a 660 --visitors-b 10000 --rate-b 6.1% --brief
850
+ bayes-ab analyze --visitors-a 12000 --conversions-a 660 --visitors-b 11850 --conversions-b 714 --json --pretty
851
+ bayes-ab project --visitors-a 12000 --conversions-a 660 --visitors-b 11850 --conversions-b 714 --daily-visitors 1500
852
+ bayes-ab project --visitors-a 12000 --conversions-a 660 --visitors-b 11850 --conversions-b 714 \\
853
+ --daily-visitors 1500 --horizon 56 --min-effect 5 --json
854
+ bayes-ab plan --baseline 5.5% --min-effect 10 --daily-visitors 1500
855
+ bayes-ab schema project
856
+
857
+ Sweep a question by running several calls in parallel; JSON is one line per call:
858
+ for d in 750 1500 3000; do
859
+ bayes-ab project --visitors-a 12000 --conversions-a 660 --visitors-b 11850 --conversions-b 714 \\
860
+ --daily-visitors $d --json &
861
+ done; wait
862
+
863
+ EXIT CODES
864
+ 0 ok 2 invalid input or unknown flag (error is JSON when output is JSON)
865
+ `;
866
+ }
867
+
868
+ // src/cli/format.ts
869
+ function percent(fraction2, digits = 2) {
870
+ return `${(fraction2 * 100).toFixed(digits)}%`;
871
+ }
872
+ function signedPercent(fraction2, digits = 2) {
873
+ if (fraction2 === null) return "n/a";
874
+ const value = fraction2 * 100;
875
+ return `${value >= 0 ? "+" : ""}${value.toFixed(digits)}%`;
876
+ }
877
+ function points(fraction2, digits = 2) {
878
+ const value = fraction2 * 100;
879
+ return `${value >= 0 ? "+" : ""}${value.toFixed(digits)} pp`;
880
+ }
881
+ function unsignedPoints(fraction2, digits = 4) {
882
+ return `${(fraction2 * 100).toFixed(digits)} pp`;
883
+ }
884
+ function count(value) {
885
+ return Math.round(value).toLocaleString("en-US");
886
+ }
887
+ function missedConversions(regretRate, visitors = 1e4) {
888
+ const missed = regretRate * visitors;
889
+ const digits = missed < 1 ? 2 : missed < 10 ? 1 : 0;
890
+ return `${missed.toFixed(digits)} missed conversions per ${count(visitors)} visitors`;
891
+ }
892
+ function capitalize(text) {
893
+ return text.charAt(0).toUpperCase() + text.slice(1);
894
+ }
895
+ function padRight(text, width) {
896
+ return text.length >= width ? text : text + " ".repeat(width - text.length);
897
+ }
898
+ function padLeft(text, width) {
899
+ return text.length >= width ? text : " ".repeat(width - text.length) + text;
900
+ }
901
+ function keyValueLines(rows, keyWidth, indent = "") {
902
+ return rows.map(([key, value, note]) => {
903
+ const line = `${indent}${padRight(key, keyWidth)}${value}`;
904
+ return note ? `${line} ${note}` : line;
905
+ });
906
+ }
907
+ function table(rows, indent = " ", gap = 4) {
908
+ const widths = [];
909
+ for (const row of rows) {
910
+ row.forEach((cell, index) => {
911
+ widths[index] = Math.max(widths[index] ?? 0, cell.length);
912
+ });
913
+ }
914
+ return rows.map(
915
+ (row) => indent + row.map((cell, index) => {
916
+ const width = widths[index] ?? cell.length;
917
+ return index === 0 ? padRight(cell, width) : padLeft(cell, width);
918
+ }).join(" ".repeat(gap)).trimEnd()
919
+ );
920
+ }
921
+
922
+ // src/cli/render/analyze.ts
923
+ function derivedNote(result, derivedFrom2) {
924
+ if (!derivedFrom2) return null;
925
+ const parts = [];
926
+ if (derivedFrom2.rateA !== void 0) parts.push(`A = ${result.inputs.conversionsA}/${result.inputs.visitorsA} from --rate-a ${percent(derivedFrom2.rateA, 2)}`);
927
+ if (derivedFrom2.rateB !== void 0) parts.push(`B = ${result.inputs.conversionsB}/${result.inputs.visitorsB} from --rate-b ${percent(derivedFrom2.rateB, 2)}`);
928
+ return parts.join(", ");
929
+ }
930
+ function renderAnalyzeBrief(result, derivedFrom2) {
931
+ const minEffect = percent(result.inputs.minEffect, 0);
932
+ const [lower, upper] = result.effect.relativeLiftCI95;
933
+ const parts = [
934
+ result.decision.label,
935
+ `P(B beats A) ${percent(result.probability.bBeatsA)}`,
936
+ `P(lift > +${minEffect}) ${percent(result.probability.meaningfulLift)}`,
937
+ `lift ${signedPercent(result.effect.relativeLift)} (CI ${signedPercent(lower)} to ${signedPercent(upper)})`,
938
+ `loss if ship B ${unsignedPoints(result.expectedLoss.shipB)}`
939
+ ];
940
+ const note = derivedNote(result, derivedFrom2);
941
+ return parts.join(" \xB7 ") + (note ? ` ${note}` : "");
942
+ }
943
+ function renderAnalyze(result, derivedFrom2) {
944
+ const { inputs, decision, probability, effect, expectedLoss, variants, sampleSize, waiting, frequentist } = result;
945
+ const minEffect = percent(inputs.minEffect, 0);
946
+ const [liftLower, liftUpper] = effect.relativeLiftCI95;
947
+ const [absLower, absUpper] = effect.absoluteDiffCI95;
948
+ const lines = [];
949
+ const note = derivedNote(result, derivedFrom2);
950
+ lines.push(
951
+ `Bayesian A/B test Beta(${inputs.prior[0]},${inputs.prior[1]}) prior \xB7 decision threshold ${percent(inputs.threshold, 0)} \xB7 meaningful effect \xB1${minEffect} relative`
952
+ );
953
+ if (note) lines.push(` ${note}`);
954
+ lines.push("");
955
+ lines.push(
956
+ ...keyValueLines(
957
+ [
958
+ ["Decision", decision.label],
959
+ ["Evidence", capitalize(decision.evidence), `(${percent(decision.maturityProgress, 0)} of the way to a stable estimate)`],
960
+ [
961
+ "Bayes action now",
962
+ `Ship ${decision.bayesActionNow}`,
963
+ `(expected regret ${unsignedPoints(decision.expectedRegretNow)}, about ${missedConversions(decision.expectedRegretNow)})`
964
+ ]
965
+ ],
966
+ 20
967
+ )
968
+ );
969
+ lines.push("");
970
+ lines.push(
971
+ ...table([
972
+ ["", "Variant A", "Variant B"],
973
+ ["Visitors", count(variants.A.visitors), count(variants.B.visitors)],
974
+ ["Conversions", count(variants.A.conversions), count(variants.B.conversions)],
975
+ ["Observed rate", percent(variants.A.rate), percent(variants.B.rate)],
976
+ ["Posterior mean", percent(variants.A.posteriorMean, 3), percent(variants.B.posteriorMean, 3)],
977
+ ["Posterior", `Beta(${count(variants.A.posterior[0])}, ${count(variants.A.posterior[1])})`, `Beta(${count(variants.B.posterior[0])}, ${count(variants.B.posterior[1])})`]
978
+ ])
979
+ );
980
+ lines.push("");
981
+ lines.push(
982
+ ...keyValueLines(
983
+ [
984
+ ["P(B beats A)", percent(probability.bBeatsA)],
985
+ [`P(relative lift > +${minEffect})`, percent(probability.meaningfulLift)],
986
+ [`P(relative lift < -${minEffect})`, percent(probability.meaningfulHarm)],
987
+ ["P(B is harmful at all)", percent(probability.harmful)]
988
+ ],
989
+ 36
990
+ )
991
+ );
992
+ lines.push("");
993
+ lines.push(
994
+ ...keyValueLines(
995
+ [
996
+ ["Relative lift", signedPercent(effect.relativeLift), `95% CI ${signedPercent(liftLower)} to ${signedPercent(liftUpper)}`],
997
+ ["Absolute difference", points(effect.absoluteDiff), `95% CI ${points(absLower)} to ${points(absUpper)}`],
998
+ ["Observed difference", points(effect.observedAbsoluteDiff), `relative ${signedPercent(effect.observedRelativeLift)}`]
999
+ ],
1000
+ 23
1001
+ )
1002
+ );
1003
+ lines.push("");
1004
+ lines.push(
1005
+ ...keyValueLines(
1006
+ [
1007
+ ["Expected loss if you ship B", unsignedPoints(expectedLoss.shipB)],
1008
+ ["Expected loss if you ship A", unsignedPoints(expectedLoss.shipA)],
1009
+ ["Two-sided p-value", frequentist.pValueTwoSided.toFixed(3), "frequentist reference only"]
1010
+ ],
1011
+ 36
1012
+ )
1013
+ );
1014
+ lines.push("");
1015
+ lines.push("Sample size");
1016
+ lines.push(
1017
+ ...keyValueLines(
1018
+ [
1019
+ ["Smallest detectable effect now", `~${percent(sampleSize.detectableEffectNow, 0)} relative`],
1020
+ ["Visitors needed per variant", count(sampleSize.requiredPerVariant), `to detect \xB1${minEffect} at 80% power`],
1021
+ ["Additional visitors per variant", count(sampleSize.additionalPerVariant)]
1022
+ ],
1023
+ 36,
1024
+ " "
1025
+ )
1026
+ );
1027
+ lines.push("");
1028
+ lines.push("Value of waiting expected regret after N more visitors per variant");
1029
+ lines.push(
1030
+ ...table([
1031
+ ["ship now", unsignedPoints(decision.expectedRegretNow), ""],
1032
+ ...waiting.map((scenario) => [
1033
+ `+${count(scenario.extraPerVariant)}`,
1034
+ unsignedPoints(scenario.expectedRegret),
1035
+ `value of waiting ${unsignedPoints(scenario.valueOfWaiting)}`
1036
+ ])
1037
+ ])
1038
+ );
1039
+ lines.push("");
1040
+ lines.push(`Share ${result.shareUrl}`);
1041
+ return lines.join("\n");
1042
+ }
1043
+
1044
+ // src/cli/render/plan.ts
1045
+ function renderPlan(result) {
1046
+ const { inputs, targetRates } = result;
1047
+ const e = percent(inputs.minEffect, 0);
1048
+ const lines = [];
1049
+ lines.push(
1050
+ `Sample size plan baseline ${percent(inputs.baseline)} \xB7 detect a \xB1${e} relative change (to ${percent(targetRates.up)} or ${percent(targetRates.down)}) \xB7 ${percent(inputs.power, 0)} power, ${percent(inputs.alpha, 0)} alpha`
1051
+ );
1052
+ lines.push("");
1053
+ const rows = [
1054
+ ["Visitors per variant", count(result.perVariant)],
1055
+ ["Total visitors", count(result.total)]
1056
+ ];
1057
+ if (inputs.dailyVisitors && result.days !== null) {
1058
+ rows.push([`Days at ${count(inputs.dailyVisitors)}/day/variant`, String(result.days)]);
1059
+ }
1060
+ lines.push(...keyValueLines(rows, 29));
1061
+ lines.push("");
1062
+ lines.push("Sensitivity");
1063
+ lines.push(
1064
+ ...table(
1065
+ result.sensitivity.map((row) => {
1066
+ const cells = [`min effect`, percent(row.minEffect, row.minEffect * 100 % 1 === 0 ? 0 : 1), `${count(row.perVariant)} per variant`];
1067
+ if (row.days !== null) cells.push(`${row.days} days`);
1068
+ if (row.minEffect === inputs.minEffect) cells.push("\u2190 you are here");
1069
+ return cells;
1070
+ }),
1071
+ " ",
1072
+ 3
1073
+ ).map((line) => line.replace(/(\d+ days) +(← you are here)$/, "$1 $2"))
1074
+ );
1075
+ if (!inputs.dailyVisitors) {
1076
+ lines.push("");
1077
+ lines.push("Add --daily-visitors N to convert these to days.");
1078
+ }
1079
+ return lines.join("\n");
1080
+ }
1081
+
1082
+ // src/cli/render/project.ts
1083
+ function describeRule(result) {
1084
+ const { stopRule, threshold, minEffect, maxLoss } = result.inputs;
1085
+ const t = percent(threshold, 0);
1086
+ if (stopRule === "superiority") return `P(B beats A) \u2265 ${t} or P(A beats B) \u2265 ${t}`;
1087
+ if (stopRule === "loss") return `expected loss of the better variant \u2264 ${unsignedPoints(maxLoss ?? 0)}`;
1088
+ const e = percent(minEffect, 0);
1089
+ return `P(lift > +${e}) \u2265 ${t} or P(lift < -${e}) \u2265 ${t}`;
1090
+ }
1091
+ function decisionLabel(decision) {
1092
+ return decision === "ship_b" ? "Ship B" : "Keep A";
1093
+ }
1094
+ function diagnosticLines(result) {
1095
+ if (result.diagnostic !== "lift_near_threshold") return [];
1096
+ const e = percent(result.inputs.minEffect, 0);
1097
+ return [
1098
+ "",
1099
+ `Why so slow The estimated lift sits on the \xB1${e} meaningful-effect line.`,
1100
+ " More data sharpens the estimate around that line instead of pushing it across.",
1101
+ " Try a smaller --min-effect, or --stop-rule superiority to ask only whether B beats A."
1102
+ ];
1103
+ }
1104
+ function renderProjectBrief(result) {
1105
+ const { horizonDays, dailyVisitors } = result.inputs;
1106
+ if (result.alreadyDecided && result.decisionNow) {
1107
+ return `Already decided today: ${decisionLabel(result.decisionNow)} under the ${result.inputs.stopRule} rule \xB7 no simulation needed`;
1108
+ }
1109
+ const parts = [
1110
+ `P(decision within ${horizonDays} days) ${percent(result.decisionWithinHorizon, 0)}`,
1111
+ `Ship B ${percent(result.outcomes.shipB, 0)} \xB7 Keep A ${percent(result.outcomes.keepA, 0)}`,
1112
+ result.daysToDecision ? `median day ${result.daysToDecision.p50} if it decides (p90 day ${result.daysToDecision.p90})` : "no simulated future decided",
1113
+ `${count(dailyVisitors)}/day per variant`
1114
+ ];
1115
+ return parts.join(" \xB7 ");
1116
+ }
1117
+ function renderProject(result) {
1118
+ const { inputs } = result;
1119
+ const lines = [];
1120
+ lines.push(
1121
+ `Forward projection ${count(inputs.dailyVisitors)} visitors/day per variant \xB7 ${inputs.horizonDays}-day horizon \xB7 ${count(inputs.simulations)} simulated futures`
1122
+ );
1123
+ lines.push(`Stopping rule ${describeRule(result)}`);
1124
+ lines.push("");
1125
+ if (result.alreadyDecided && result.decisionNow) {
1126
+ lines.push(`Already decided ${decisionLabel(result.decisionNow)}`);
1127
+ lines.push("");
1128
+ lines.push(`The ${inputs.stopRule} rule is already satisfied by today's data, so there is nothing to wait for.`);
1129
+ lines.push("Run analyze for the full readout.");
1130
+ lines.push(...diagnosticLines(result));
1131
+ return lines.join("\n");
1132
+ }
1133
+ lines.push(
1134
+ ...keyValueLines(
1135
+ [[`Chance of a decision within ${inputs.horizonDays} days`, percent(result.decisionWithinHorizon, 0)]],
1136
+ 44
1137
+ )
1138
+ );
1139
+ lines.push(
1140
+ ...table([
1141
+ ["Ship B", percent(result.outcomes.shipB, 0)],
1142
+ ["Keep A", percent(result.outcomes.keepA, 0)],
1143
+ [`Still running at day ${inputs.horizonDays}`, percent(result.outcomes.undecided, 0)]
1144
+ ], " ", 3)
1145
+ );
1146
+ lines.push("");
1147
+ if (result.daysToDecision) {
1148
+ lines.push("If it decides, when?");
1149
+ lines.push(
1150
+ ...table([
1151
+ ["25% of decisions by day", String(Math.round(result.daysToDecision.p25))],
1152
+ ["50%", String(Math.round(result.daysToDecision.p50))],
1153
+ ["75%", String(Math.round(result.daysToDecision.p75))],
1154
+ ["90%", String(Math.round(result.daysToDecision.p90))]
1155
+ ])
1156
+ );
1157
+ } else {
1158
+ lines.push("No simulated future reached a decision inside the horizon.");
1159
+ }
1160
+ lines.push("");
1161
+ lines.push("Cumulative chance of a decision");
1162
+ lines.push(...table(result.cumulative.map((point) => [`by day ${String(point.day).padStart(2)}`, percent(point.p, 0)])));
1163
+ lines.push(...diagnosticLines(result));
1164
+ return lines.join("\n");
1165
+ }
1166
+
1167
+ // src/cli/schema.ts
1168
+ var fraction = { type: "number", minimum: 0, maximum: 1 };
1169
+ var number = { type: "number" };
1170
+ var integer = { type: "integer", minimum: 0 };
1171
+ var pair = (item) => ({ type: "array", items: item, minItems: 2, maxItems: 2 });
1172
+ var object = (properties, required = Object.keys(properties)) => ({
1173
+ type: "object",
1174
+ properties,
1175
+ required,
1176
+ additionalProperties: false
1177
+ });
1178
+ var envelope = {
1179
+ tool: { const: "bayes-ab" },
1180
+ version: { type: "string" }
1181
+ };
1182
+ var variantInputs = {
1183
+ visitorsA: integer,
1184
+ conversionsA: integer,
1185
+ visitorsB: integer,
1186
+ conversionsB: integer,
1187
+ threshold: fraction,
1188
+ minEffect: fraction,
1189
+ prior: pair(number)
1190
+ };
1191
+ var derivedFrom = {
1192
+ ...object({ rateA: fraction, rateB: fraction }, []),
1193
+ description: "Present when --rate-a or --rate-b was used; the rate that produced the conversion count."
1194
+ };
1195
+ var ANALYZE_SCHEMA = {
1196
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1197
+ title: "bayes-ab analyze output",
1198
+ ...object({
1199
+ ...envelope,
1200
+ command: { const: "analyze" },
1201
+ inputs: object({ ...variantInputs, samples: integer, seed: integer, derivedFrom }, [
1202
+ ...Object.keys(variantInputs),
1203
+ "samples",
1204
+ "seed"
1205
+ ]),
1206
+ decision: object({
1207
+ status: { enum: ["ship_b", "keep_a", "continue", "inconclusive"] },
1208
+ label: { type: "string" },
1209
+ evidence: { enum: ["low", "moderate", "strong"] },
1210
+ maturity: { enum: ["early", "building", "mature"] },
1211
+ maturityProgress: fraction,
1212
+ bayesActionNow: { enum: ["A", "B"] },
1213
+ expectedRegretNow: { ...fraction, description: "Expected loss of bayesActionNow, as a conversion-rate fraction." }
1214
+ }),
1215
+ probability: object({
1216
+ bBeatsA: fraction,
1217
+ aBeatsB: fraction,
1218
+ meaningfulLift: { ...fraction, description: "P(relative lift > minEffect)" },
1219
+ meaningfulHarm: { ...fraction, description: "P(relative lift < -minEffect)" },
1220
+ harmful: { ...fraction, description: "P(relative lift < 0)" },
1221
+ liftAbove1pct: fraction,
1222
+ liftAbove5pct: fraction,
1223
+ harmBeyond5pct: fraction,
1224
+ harmBeyond10pct: fraction
1225
+ }),
1226
+ effect: object({
1227
+ relativeLift: number,
1228
+ relativeLiftCI95: pair(number),
1229
+ absoluteDiff: number,
1230
+ absoluteDiffCI95: pair(number),
1231
+ observedRelativeLift: { type: ["number", "null"] },
1232
+ observedAbsoluteDiff: number
1233
+ }),
1234
+ expectedLoss: object({ shipA: fraction, shipB: fraction }),
1235
+ variants: object({
1236
+ A: object({ visitors: integer, conversions: integer, rate: fraction, posteriorMean: fraction, posterior: pair(number) }),
1237
+ B: object({ visitors: integer, conversions: integer, rate: fraction, posteriorMean: fraction, posterior: pair(number) })
1238
+ }),
1239
+ sampleSize: object({
1240
+ requiredPerVariant: integer,
1241
+ additionalPerVariant: integer,
1242
+ detectableEffectNow: number
1243
+ }),
1244
+ waiting: {
1245
+ type: "array",
1246
+ items: object({ extraPerVariant: integer, expectedRegret: fraction, valueOfWaiting: fraction })
1247
+ },
1248
+ frequentist: object({ pValueTwoSided: fraction }),
1249
+ chart: {
1250
+ type: "array",
1251
+ description: "Only with --include-chart.",
1252
+ items: object({ rate: fraction, densityA: number, densityB: number })
1253
+ },
1254
+ shareUrl: { type: "string", format: "uri" }
1255
+ }, [
1256
+ "tool",
1257
+ "version",
1258
+ "command",
1259
+ "inputs",
1260
+ "decision",
1261
+ "probability",
1262
+ "effect",
1263
+ "expectedLoss",
1264
+ "variants",
1265
+ "sampleSize",
1266
+ "waiting",
1267
+ "frequentist",
1268
+ "shareUrl"
1269
+ ])
1270
+ };
1271
+ var PROJECT_SCHEMA = {
1272
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1273
+ title: "bayes-ab project output",
1274
+ ...object({
1275
+ ...envelope,
1276
+ command: { const: "project" },
1277
+ inputs: object(
1278
+ {
1279
+ ...variantInputs,
1280
+ dailyVisitors: integer,
1281
+ horizonDays: integer,
1282
+ stopRule: { enum: ["lift", "superiority", "loss"] },
1283
+ maxLoss: { type: ["number", "null"], description: "Only set for the loss rule, as a fraction." },
1284
+ simulations: integer,
1285
+ seed: integer,
1286
+ derivedFrom
1287
+ },
1288
+ [...Object.keys(variantInputs), "dailyVisitors", "horizonDays", "stopRule", "maxLoss", "simulations", "seed"]
1289
+ ),
1290
+ alreadyDecided: { type: "boolean", description: "True when the stop rule is already met today; no simulation was run." },
1291
+ decisionNow: { enum: ["ship_b", "keep_a", null] },
1292
+ decisionWithinHorizon: fraction,
1293
+ outcomes: object({ shipB: fraction, keepA: fraction, undecided: fraction }),
1294
+ daysToDecision: {
1295
+ oneOf: [object({ p25: number, p50: number, p75: number, p90: number }), { type: "null" }],
1296
+ description: "Quantiles of the decision day among futures that decided. Conditional on deciding."
1297
+ },
1298
+ cumulative: { type: "array", items: object({ day: integer, p: fraction }) },
1299
+ diagnostic: { enum: ["lift_near_threshold", null] },
1300
+ approximation: { const: "normal" },
1301
+ shareUrl: { type: "string", format: "uri" }
1302
+ })
1303
+ };
1304
+ var PLAN_SCHEMA = {
1305
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1306
+ title: "bayes-ab plan output",
1307
+ ...object({
1308
+ ...envelope,
1309
+ command: { const: "plan" },
1310
+ inputs: object({
1311
+ baseline: fraction,
1312
+ minEffect: fraction,
1313
+ power: fraction,
1314
+ alpha: fraction,
1315
+ dailyVisitors: { type: ["integer", "null"] }
1316
+ }),
1317
+ targetRates: object({ up: fraction, down: fraction }),
1318
+ perVariant: integer,
1319
+ total: integer,
1320
+ days: { type: ["integer", "null"] },
1321
+ sensitivity: {
1322
+ type: "array",
1323
+ items: object({ minEffect: fraction, perVariant: integer, days: { type: ["integer", "null"] } })
1324
+ }
1325
+ })
1326
+ };
1327
+ var ERROR_SCHEMA = {
1328
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1329
+ title: "bayes-ab error output",
1330
+ ...object({
1331
+ error: object({
1332
+ code: { type: "string" },
1333
+ message: { type: "string" },
1334
+ field: { type: ["string", "null"], description: "The flag at fault, without leading dashes." }
1335
+ })
1336
+ })
1337
+ };
1338
+ var SCHEMAS = {
1339
+ analyze: ANALYZE_SCHEMA,
1340
+ project: PROJECT_SCHEMA,
1341
+ plan: PLAN_SCHEMA,
1342
+ error: ERROR_SCHEMA
1343
+ };
1344
+
1345
+ // src/cli/main.ts
1346
+ var VERSION = true ? "0.1.0" : "dev";
1347
+ var JSON_DECIMALS = 6;
1348
+ function roundDeep(value) {
1349
+ if (typeof value === "number") return round(value, JSON_DECIMALS);
1350
+ if (Array.isArray(value)) return value.map(roundDeep);
1351
+ if (value && typeof value === "object") {
1352
+ const out = {};
1353
+ for (const [key, inner] of Object.entries(value)) out[key] = roundDeep(inner);
1354
+ return out;
1355
+ }
1356
+ return value;
1357
+ }
1358
+ function emitJson(io, output, payload) {
1359
+ const rounded = roundDeep(payload);
1360
+ io.stdout(output.pretty ? JSON.stringify(rounded, null, 2) + "\n" : JSON.stringify(rounded) + "\n");
1361
+ }
1362
+ function wantsJson(output, io) {
1363
+ return output.json ?? !io.isTty;
1364
+ }
1365
+ function envelope2(command, result, derivedFrom2) {
1366
+ const inputs = derivedFrom2 ? { ...result.inputs, derivedFrom: derivedFrom2 } : result.inputs;
1367
+ return { tool: "bayes-ab", version: VERSION, command, ...result, inputs };
1368
+ }
1369
+ function stripChart(result) {
1370
+ const { chart, ...rest } = result;
1371
+ return rest;
1372
+ }
1373
+ function run(argv, io) {
1374
+ let parsed;
1375
+ try {
1376
+ parsed = parseCliArgs(argv);
1377
+ } catch (error) {
1378
+ return reportError(error, io, argv.includes("--json") || !io.isTty);
1379
+ }
1380
+ if (parsed.kind === "help") {
1381
+ io.stdout(helpText(VERSION));
1382
+ return 0;
1383
+ }
1384
+ if (parsed.kind === "version") {
1385
+ io.stdout(`${VERSION}
1386
+ `);
1387
+ return 0;
1388
+ }
1389
+ const json = wantsJson(parsed.output, io);
1390
+ try {
1391
+ if (parsed.kind === "schema") {
1392
+ const payload = parsed.target ? SCHEMAS[parsed.target] : SCHEMAS;
1393
+ io.stdout(JSON.stringify(payload, null, 2) + "\n");
1394
+ return 0;
1395
+ }
1396
+ if (parsed.kind === "analyze") {
1397
+ const { derivedFrom: derivedFrom2, ...inputs } = parsed.args;
1398
+ const result2 = analyze({ ...inputs, includeChart: parsed.output.includeChart });
1399
+ if (json) {
1400
+ emitJson(io, parsed.output, envelope2("analyze", parsed.output.includeChart ? result2 : stripChart(result2), derivedFrom2));
1401
+ } else {
1402
+ io.stdout((parsed.output.brief ? renderAnalyzeBrief(result2, derivedFrom2) : renderAnalyze(result2, derivedFrom2)) + "\n");
1403
+ }
1404
+ return 0;
1405
+ }
1406
+ if (parsed.kind === "project") {
1407
+ const { derivedFrom: derivedFrom2, samples: _samples, ...inputs } = parsed.args;
1408
+ const result2 = project(inputs);
1409
+ if (json) {
1410
+ emitJson(io, parsed.output, envelope2("project", result2, derivedFrom2));
1411
+ } else {
1412
+ io.stdout((parsed.output.brief ? renderProjectBrief(result2) : renderProject(result2)) + "\n");
1413
+ }
1414
+ return 0;
1415
+ }
1416
+ const result = plan(parsed.args);
1417
+ if (json) {
1418
+ emitJson(io, parsed.output, envelope2("plan", result, null));
1419
+ } else {
1420
+ io.stdout(renderPlan(result) + "\n");
1421
+ }
1422
+ return 0;
1423
+ } catch (error) {
1424
+ return reportError(error, io, json);
1425
+ }
1426
+ }
1427
+ function reportError(error, io, json) {
1428
+ const cliError = error instanceof CliError ? error : new CliError(error instanceof Error ? error.message : String(error), { code: "internal_error", exitCode: 1 });
1429
+ if (json) {
1430
+ io.stdout(JSON.stringify({ error: { code: cliError.code, message: cliError.message, field: cliError.field } }) + "\n");
1431
+ } else {
1432
+ io.stderr(`error ${cliError.message}
1433
+ `);
1434
+ if (cliError.code === "missing_flag" || cliError.code === "unknown_flag" || cliError.code === "unknown_command" || cliError.code === "unexpected_argument") {
1435
+ io.stderr("Run bayes-ab --help for usage.\n");
1436
+ }
1437
+ }
1438
+ return cliError.exitCode;
1439
+ }
1440
+
1441
+ // src/cli.ts
1442
+ var code = run(process.argv.slice(2), {
1443
+ stdout: (text) => process.stdout.write(text),
1444
+ stderr: (text) => process.stderr.write(text),
1445
+ isTty: Boolean(process.stdout.isTTY)
1446
+ });
1447
+ process.exitCode = code;