@takk/bayesoutputgate 1.0.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 (65) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/LICENSE +190 -0
  3. package/NOTICE +45 -0
  4. package/README.md +403 -0
  5. package/SECURITY.md +98 -0
  6. package/SPEC.md +467 -0
  7. package/dist/adapter/index.cjs +411 -0
  8. package/dist/adapter/index.d.cts +29 -0
  9. package/dist/adapter/index.d.ts +29 -0
  10. package/dist/adapter/index.js +404 -0
  11. package/dist/audit/index.cjs +82 -0
  12. package/dist/audit/index.d.cts +40 -0
  13. package/dist/audit/index.d.ts +40 -0
  14. package/dist/audit/index.js +77 -0
  15. package/dist/bayesfactor/index.cjs +152 -0
  16. package/dist/bayesfactor/index.d.cts +15 -0
  17. package/dist/bayesfactor/index.d.ts +15 -0
  18. package/dist/bayesfactor/index.js +149 -0
  19. package/dist/beta/index.cjs +180 -0
  20. package/dist/beta/index.d.cts +45 -0
  21. package/dist/beta/index.d.ts +45 -0
  22. package/dist/beta/index.js +178 -0
  23. package/dist/calibration/index.cjs +339 -0
  24. package/dist/calibration/index.d.cts +53 -0
  25. package/dist/calibration/index.d.ts +53 -0
  26. package/dist/calibration/index.js +333 -0
  27. package/dist/cli/index.cjs +968 -0
  28. package/dist/cli/index.d.cts +1 -0
  29. package/dist/cli/index.d.ts +1 -0
  30. package/dist/cli/index.js +966 -0
  31. package/dist/dimensions/index.cjs +106 -0
  32. package/dist/dimensions/index.d.cts +33 -0
  33. package/dist/dimensions/index.d.ts +33 -0
  34. package/dist/dimensions/index.js +104 -0
  35. package/dist/edge/index.cjs +1141 -0
  36. package/dist/edge/index.d.cts +12 -0
  37. package/dist/edge/index.d.ts +12 -0
  38. package/dist/edge/index.js +1109 -0
  39. package/dist/gate/index.cjs +803 -0
  40. package/dist/gate/index.d.cts +77 -0
  41. package/dist/gate/index.d.ts +77 -0
  42. package/dist/gate/index.js +799 -0
  43. package/dist/hypothesis/index.cjs +268 -0
  44. package/dist/hypothesis/index.d.cts +38 -0
  45. package/dist/hypothesis/index.d.ts +38 -0
  46. package/dist/hypothesis/index.js +266 -0
  47. package/dist/index.cjs +1141 -0
  48. package/dist/index.d.cts +29 -0
  49. package/dist/index.d.ts +29 -0
  50. package/dist/index.js +1109 -0
  51. package/dist/likelihood/index.cjs +137 -0
  52. package/dist/likelihood/index.d.cts +23 -0
  53. package/dist/likelihood/index.d.ts +23 -0
  54. package/dist/likelihood/index.js +132 -0
  55. package/dist/node/index.cjs +1282 -0
  56. package/dist/node/index.d.cts +24 -0
  57. package/dist/node/index.d.ts +24 -0
  58. package/dist/node/index.js +1246 -0
  59. package/dist/policy/index.cjs +88 -0
  60. package/dist/policy/index.d.cts +11 -0
  61. package/dist/policy/index.d.ts +11 -0
  62. package/dist/policy/index.js +85 -0
  63. package/dist/types-bMjn1j4e.d.cts +159 -0
  64. package/dist/types-bMjn1j4e.d.ts +159 -0
  65. package/package.json +142 -0
@@ -0,0 +1,137 @@
1
+ 'use strict';
2
+
3
+ // src/errors.ts
4
+ var BayesOutputGateError = class _BayesOutputGateError extends Error {
5
+ code;
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.name = "BayesOutputGateError";
9
+ this.code = code;
10
+ Object.setPrototypeOf(this, _BayesOutputGateError.prototype);
11
+ }
12
+ };
13
+ function invariant(condition, code, message) {
14
+ if (!condition) {
15
+ throw new BayesOutputGateError(code, message);
16
+ }
17
+ }
18
+
19
+ // src/mathspecial.ts
20
+ var LN_SQRT_2PI = 0.9189385332046728;
21
+ var LANCZOS_G = 7;
22
+ var LANCZOS_COEFFICIENTS = [
23
+ 0.9999999999998099,
24
+ 676.5203681218851,
25
+ -1259.1392167224028,
26
+ 771.3234287776531,
27
+ -176.6150291621406,
28
+ 12.507343278686905,
29
+ -0.13857109526572012,
30
+ 9984369578019572e-21,
31
+ 15056327351493116e-23
32
+ ];
33
+ function lgamma(x) {
34
+ if (!Number.isFinite(x) || x <= 0) {
35
+ throw new BayesOutputGateError(
36
+ "NUMERIC",
37
+ `lgamma requires a positive finite argument, got ${x}`
38
+ );
39
+ }
40
+ const z = x - 1;
41
+ let acc = LANCZOS_COEFFICIENTS[0];
42
+ for (let i = 1; i < LANCZOS_COEFFICIENTS.length; i++) {
43
+ acc += LANCZOS_COEFFICIENTS[i] / (z + i);
44
+ }
45
+ const t = z + LANCZOS_G + 0.5;
46
+ return LN_SQRT_2PI + (z + 0.5) * Math.log(t) - t + Math.log(acc);
47
+ }
48
+ function lbeta(a, b) {
49
+ return lgamma(a) + lgamma(b) - lgamma(a + b);
50
+ }
51
+ function betaLogDensity(x, a, b) {
52
+ if (a <= 0 || b <= 0) {
53
+ throw new BayesOutputGateError(
54
+ "NUMERIC",
55
+ `Beta shape parameters must be positive, got a=${a}, b=${b}`
56
+ );
57
+ }
58
+ if (x < 0 || x > 1 || !Number.isFinite(x)) {
59
+ return Number.NEGATIVE_INFINITY;
60
+ }
61
+ if (x === 0) {
62
+ if (a < 1) return Number.POSITIVE_INFINITY;
63
+ if (a > 1) return Number.NEGATIVE_INFINITY;
64
+ return -lbeta(a, b) + (b - 1) * Math.log(1);
65
+ }
66
+ if (x === 1) {
67
+ if (b < 1) return Number.POSITIVE_INFINITY;
68
+ if (b > 1) return Number.NEGATIVE_INFINITY;
69
+ return -lbeta(a, b);
70
+ }
71
+ return (a - 1) * Math.log(x) + (b - 1) * Math.log1p(-x) - lbeta(a, b);
72
+ }
73
+ function logMarginalBernoulli(successes, trials, a, b) {
74
+ if (successes < 0 || trials < 0 || successes > trials) {
75
+ throw new BayesOutputGateError(
76
+ "NUMERIC",
77
+ `invalid Bernoulli counts: ${successes} successes in ${trials} trials`
78
+ );
79
+ }
80
+ if (a <= 0 || b <= 0) {
81
+ throw new BayesOutputGateError(
82
+ "NUMERIC",
83
+ `Beta prior parameters must be positive, got a=${a}, b=${b}`
84
+ );
85
+ }
86
+ return lbeta(successes + a, trials - successes + b) - lbeta(a, b);
87
+ }
88
+
89
+ // src/likelihood/index.ts
90
+ function logScoreLikelihood(score, params) {
91
+ return betaLogDensity(score, params.a, params.b);
92
+ }
93
+ function logBinaryMarginalLikelihood(successes, trials, prior) {
94
+ return logMarginalBernoulli(successes, trials, prior.a, prior.b);
95
+ }
96
+ function toScoreMap(scores) {
97
+ const map = /* @__PURE__ */ new Map();
98
+ for (const s of scores) {
99
+ invariant(
100
+ Number.isFinite(s.value) && s.value >= 0 && s.value <= 1,
101
+ "INVALID_SCORE",
102
+ `score for "${s.dimension}" must be a finite number in [0, 1], got ${s.value}`
103
+ );
104
+ invariant(
105
+ !map.has(s.dimension),
106
+ "INVALID_SCORE",
107
+ `duplicate score for dimension "${s.dimension}"`
108
+ );
109
+ map.set(s.dimension, s.value);
110
+ }
111
+ return map;
112
+ }
113
+ function logVectorLikelihood(scores, models, kind) {
114
+ const map = toScoreMap(scores);
115
+ let logLikelihood = 0;
116
+ let matched = 0;
117
+ for (const model of models) {
118
+ const value = map.get(model.dimension);
119
+ if (value === void 0) {
120
+ continue;
121
+ }
122
+ invariant(
123
+ Number.isFinite(model.weight) && model.weight >= 0,
124
+ "INVALID_CONFIG",
125
+ `weight for "${model.dimension}" must be a non-negative finite number, got ${model.weight}`
126
+ );
127
+ const params = kind === "high" ? model.high : model.low;
128
+ logLikelihood += model.weight * betaLogDensity(value, params.a, params.b);
129
+ matched += 1;
130
+ }
131
+ return { logLikelihood, matched };
132
+ }
133
+
134
+ exports.logBinaryMarginalLikelihood = logBinaryMarginalLikelihood;
135
+ exports.logScoreLikelihood = logScoreLikelihood;
136
+ exports.logVectorLikelihood = logVectorLikelihood;
137
+ exports.toScoreMap = toScoreMap;
@@ -0,0 +1,23 @@
1
+ import { b as BetaParams, S as ScoreVector, e as DimensionModel, H as HypothesisKind } from '../types-bMjn1j4e.cjs';
2
+
3
+ /** Log-likelihood of a single continuous score under a Beta model. */
4
+ declare function logScoreLikelihood(score: number, params: BetaParams): number;
5
+ /**
6
+ * Log marginal likelihood of `successes` binary passes out of `trials` under a hypothesis's Beta
7
+ * prior on the pass rate. This is the Beta-Binomial mode, used when outputs are scored pass/fail
8
+ * rather than on a continuous scale.
9
+ */
10
+ declare function logBinaryMarginalLikelihood(successes: number, trials: number, prior: BetaParams): number;
11
+ /** Build a validated dimension to score map from a score vector. */
12
+ declare function toScoreMap(scores: ScoreVector): Map<string, number>;
13
+ /**
14
+ * Weighted log-likelihood of a score vector under one hypothesis, summed over the dimensions that
15
+ * have both a score and a model. Returns the matched-dimension count so callers can detect an
16
+ * output that scored no modeled dimension.
17
+ */
18
+ declare function logVectorLikelihood(scores: ScoreVector, models: readonly DimensionModel[], kind: HypothesisKind): {
19
+ logLikelihood: number;
20
+ matched: number;
21
+ };
22
+
23
+ export { logBinaryMarginalLikelihood, logScoreLikelihood, logVectorLikelihood, toScoreMap };
@@ -0,0 +1,23 @@
1
+ import { b as BetaParams, S as ScoreVector, e as DimensionModel, H as HypothesisKind } from '../types-bMjn1j4e.js';
2
+
3
+ /** Log-likelihood of a single continuous score under a Beta model. */
4
+ declare function logScoreLikelihood(score: number, params: BetaParams): number;
5
+ /**
6
+ * Log marginal likelihood of `successes` binary passes out of `trials` under a hypothesis's Beta
7
+ * prior on the pass rate. This is the Beta-Binomial mode, used when outputs are scored pass/fail
8
+ * rather than on a continuous scale.
9
+ */
10
+ declare function logBinaryMarginalLikelihood(successes: number, trials: number, prior: BetaParams): number;
11
+ /** Build a validated dimension to score map from a score vector. */
12
+ declare function toScoreMap(scores: ScoreVector): Map<string, number>;
13
+ /**
14
+ * Weighted log-likelihood of a score vector under one hypothesis, summed over the dimensions that
15
+ * have both a score and a model. Returns the matched-dimension count so callers can detect an
16
+ * output that scored no modeled dimension.
17
+ */
18
+ declare function logVectorLikelihood(scores: ScoreVector, models: readonly DimensionModel[], kind: HypothesisKind): {
19
+ logLikelihood: number;
20
+ matched: number;
21
+ };
22
+
23
+ export { logBinaryMarginalLikelihood, logScoreLikelihood, logVectorLikelihood, toScoreMap };
@@ -0,0 +1,132 @@
1
+ // src/errors.ts
2
+ var BayesOutputGateError = class _BayesOutputGateError extends Error {
3
+ code;
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.name = "BayesOutputGateError";
7
+ this.code = code;
8
+ Object.setPrototypeOf(this, _BayesOutputGateError.prototype);
9
+ }
10
+ };
11
+ function invariant(condition, code, message) {
12
+ if (!condition) {
13
+ throw new BayesOutputGateError(code, message);
14
+ }
15
+ }
16
+
17
+ // src/mathspecial.ts
18
+ var LN_SQRT_2PI = 0.9189385332046728;
19
+ var LANCZOS_G = 7;
20
+ var LANCZOS_COEFFICIENTS = [
21
+ 0.9999999999998099,
22
+ 676.5203681218851,
23
+ -1259.1392167224028,
24
+ 771.3234287776531,
25
+ -176.6150291621406,
26
+ 12.507343278686905,
27
+ -0.13857109526572012,
28
+ 9984369578019572e-21,
29
+ 15056327351493116e-23
30
+ ];
31
+ function lgamma(x) {
32
+ if (!Number.isFinite(x) || x <= 0) {
33
+ throw new BayesOutputGateError(
34
+ "NUMERIC",
35
+ `lgamma requires a positive finite argument, got ${x}`
36
+ );
37
+ }
38
+ const z = x - 1;
39
+ let acc = LANCZOS_COEFFICIENTS[0];
40
+ for (let i = 1; i < LANCZOS_COEFFICIENTS.length; i++) {
41
+ acc += LANCZOS_COEFFICIENTS[i] / (z + i);
42
+ }
43
+ const t = z + LANCZOS_G + 0.5;
44
+ return LN_SQRT_2PI + (z + 0.5) * Math.log(t) - t + Math.log(acc);
45
+ }
46
+ function lbeta(a, b) {
47
+ return lgamma(a) + lgamma(b) - lgamma(a + b);
48
+ }
49
+ function betaLogDensity(x, a, b) {
50
+ if (a <= 0 || b <= 0) {
51
+ throw new BayesOutputGateError(
52
+ "NUMERIC",
53
+ `Beta shape parameters must be positive, got a=${a}, b=${b}`
54
+ );
55
+ }
56
+ if (x < 0 || x > 1 || !Number.isFinite(x)) {
57
+ return Number.NEGATIVE_INFINITY;
58
+ }
59
+ if (x === 0) {
60
+ if (a < 1) return Number.POSITIVE_INFINITY;
61
+ if (a > 1) return Number.NEGATIVE_INFINITY;
62
+ return -lbeta(a, b) + (b - 1) * Math.log(1);
63
+ }
64
+ if (x === 1) {
65
+ if (b < 1) return Number.POSITIVE_INFINITY;
66
+ if (b > 1) return Number.NEGATIVE_INFINITY;
67
+ return -lbeta(a, b);
68
+ }
69
+ return (a - 1) * Math.log(x) + (b - 1) * Math.log1p(-x) - lbeta(a, b);
70
+ }
71
+ function logMarginalBernoulli(successes, trials, a, b) {
72
+ if (successes < 0 || trials < 0 || successes > trials) {
73
+ throw new BayesOutputGateError(
74
+ "NUMERIC",
75
+ `invalid Bernoulli counts: ${successes} successes in ${trials} trials`
76
+ );
77
+ }
78
+ if (a <= 0 || b <= 0) {
79
+ throw new BayesOutputGateError(
80
+ "NUMERIC",
81
+ `Beta prior parameters must be positive, got a=${a}, b=${b}`
82
+ );
83
+ }
84
+ return lbeta(successes + a, trials - successes + b) - lbeta(a, b);
85
+ }
86
+
87
+ // src/likelihood/index.ts
88
+ function logScoreLikelihood(score, params) {
89
+ return betaLogDensity(score, params.a, params.b);
90
+ }
91
+ function logBinaryMarginalLikelihood(successes, trials, prior) {
92
+ return logMarginalBernoulli(successes, trials, prior.a, prior.b);
93
+ }
94
+ function toScoreMap(scores) {
95
+ const map = /* @__PURE__ */ new Map();
96
+ for (const s of scores) {
97
+ invariant(
98
+ Number.isFinite(s.value) && s.value >= 0 && s.value <= 1,
99
+ "INVALID_SCORE",
100
+ `score for "${s.dimension}" must be a finite number in [0, 1], got ${s.value}`
101
+ );
102
+ invariant(
103
+ !map.has(s.dimension),
104
+ "INVALID_SCORE",
105
+ `duplicate score for dimension "${s.dimension}"`
106
+ );
107
+ map.set(s.dimension, s.value);
108
+ }
109
+ return map;
110
+ }
111
+ function logVectorLikelihood(scores, models, kind) {
112
+ const map = toScoreMap(scores);
113
+ let logLikelihood = 0;
114
+ let matched = 0;
115
+ for (const model of models) {
116
+ const value = map.get(model.dimension);
117
+ if (value === void 0) {
118
+ continue;
119
+ }
120
+ invariant(
121
+ Number.isFinite(model.weight) && model.weight >= 0,
122
+ "INVALID_CONFIG",
123
+ `weight for "${model.dimension}" must be a non-negative finite number, got ${model.weight}`
124
+ );
125
+ const params = kind === "high" ? model.high : model.low;
126
+ logLikelihood += model.weight * betaLogDensity(value, params.a, params.b);
127
+ matched += 1;
128
+ }
129
+ return { logLikelihood, matched };
130
+ }
131
+
132
+ export { logBinaryMarginalLikelihood, logScoreLikelihood, logVectorLikelihood, toScoreMap };