@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.
- package/CHANGELOG.md +92 -0
- package/LICENSE +190 -0
- package/NOTICE +45 -0
- package/README.md +403 -0
- package/SECURITY.md +98 -0
- package/SPEC.md +467 -0
- package/dist/adapter/index.cjs +411 -0
- package/dist/adapter/index.d.cts +29 -0
- package/dist/adapter/index.d.ts +29 -0
- package/dist/adapter/index.js +404 -0
- package/dist/audit/index.cjs +82 -0
- package/dist/audit/index.d.cts +40 -0
- package/dist/audit/index.d.ts +40 -0
- package/dist/audit/index.js +77 -0
- package/dist/bayesfactor/index.cjs +152 -0
- package/dist/bayesfactor/index.d.cts +15 -0
- package/dist/bayesfactor/index.d.ts +15 -0
- package/dist/bayesfactor/index.js +149 -0
- package/dist/beta/index.cjs +180 -0
- package/dist/beta/index.d.cts +45 -0
- package/dist/beta/index.d.ts +45 -0
- package/dist/beta/index.js +178 -0
- package/dist/calibration/index.cjs +339 -0
- package/dist/calibration/index.d.cts +53 -0
- package/dist/calibration/index.d.ts +53 -0
- package/dist/calibration/index.js +333 -0
- package/dist/cli/index.cjs +968 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +966 -0
- package/dist/dimensions/index.cjs +106 -0
- package/dist/dimensions/index.d.cts +33 -0
- package/dist/dimensions/index.d.ts +33 -0
- package/dist/dimensions/index.js +104 -0
- package/dist/edge/index.cjs +1141 -0
- package/dist/edge/index.d.cts +12 -0
- package/dist/edge/index.d.ts +12 -0
- package/dist/edge/index.js +1109 -0
- package/dist/gate/index.cjs +803 -0
- package/dist/gate/index.d.cts +77 -0
- package/dist/gate/index.d.ts +77 -0
- package/dist/gate/index.js +799 -0
- package/dist/hypothesis/index.cjs +268 -0
- package/dist/hypothesis/index.d.cts +38 -0
- package/dist/hypothesis/index.d.ts +38 -0
- package/dist/hypothesis/index.js +266 -0
- package/dist/index.cjs +1141 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +1109 -0
- package/dist/likelihood/index.cjs +137 -0
- package/dist/likelihood/index.d.cts +23 -0
- package/dist/likelihood/index.d.ts +23 -0
- package/dist/likelihood/index.js +132 -0
- package/dist/node/index.cjs +1282 -0
- package/dist/node/index.d.cts +24 -0
- package/dist/node/index.d.ts +24 -0
- package/dist/node/index.js +1246 -0
- package/dist/policy/index.cjs +88 -0
- package/dist/policy/index.d.cts +11 -0
- package/dist/policy/index.d.ts +11 -0
- package/dist/policy/index.js +85 -0
- package/dist/types-bMjn1j4e.d.cts +159 -0
- package/dist/types-bMjn1j4e.d.ts +159 -0
- package/package.json +142 -0
|
@@ -0,0 +1,106 @@
|
|
|
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/dimensions/index.ts
|
|
20
|
+
function dependenceDiagnostic(history, options = {}) {
|
|
21
|
+
const threshold = options.threshold ?? 0.5;
|
|
22
|
+
invariant(
|
|
23
|
+
Number.isFinite(threshold) && threshold >= 0 && threshold <= 1,
|
|
24
|
+
"INVALID_CONFIG",
|
|
25
|
+
`threshold must be in [0, 1], got ${threshold}`
|
|
26
|
+
);
|
|
27
|
+
invariant(
|
|
28
|
+
history.length >= 2,
|
|
29
|
+
"INVALID_OBSERVATION",
|
|
30
|
+
"dependence diagnostic needs at least two score vectors"
|
|
31
|
+
);
|
|
32
|
+
const dimensionSet = /* @__PURE__ */ new Set();
|
|
33
|
+
for (const vector of history) {
|
|
34
|
+
for (const score of vector) {
|
|
35
|
+
dimensionSet.add(score.dimension);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const dimensions = [...dimensionSet].sort();
|
|
39
|
+
const rows = history.map((vector) => {
|
|
40
|
+
const map = /* @__PURE__ */ new Map();
|
|
41
|
+
for (const score of vector) {
|
|
42
|
+
map.set(score.dimension, score.value);
|
|
43
|
+
}
|
|
44
|
+
return map;
|
|
45
|
+
});
|
|
46
|
+
const pairs = [];
|
|
47
|
+
for (let i = 0; i < dimensions.length; i++) {
|
|
48
|
+
for (let j = i + 1; j < dimensions.length; j++) {
|
|
49
|
+
const a = dimensions[i];
|
|
50
|
+
const b = dimensions[j];
|
|
51
|
+
const xs = [];
|
|
52
|
+
const ys = [];
|
|
53
|
+
for (const row of rows) {
|
|
54
|
+
const x = row.get(a);
|
|
55
|
+
const y = row.get(b);
|
|
56
|
+
if (x !== void 0 && y !== void 0) {
|
|
57
|
+
xs.push(x);
|
|
58
|
+
ys.push(y);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (xs.length >= 2) {
|
|
62
|
+
const correlation = pearson(xs, ys);
|
|
63
|
+
if (Number.isFinite(correlation)) {
|
|
64
|
+
pairs.push({ a, b, correlation, samples: xs.length });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
pairs.sort((left, right) => Math.abs(right.correlation) - Math.abs(left.correlation));
|
|
70
|
+
const maxAbsCorrelation = pairs.length > 0 ? Math.abs(pairs[0].correlation) : 0;
|
|
71
|
+
const flagged = pairs.filter((pair) => Math.abs(pair.correlation) >= threshold);
|
|
72
|
+
return {
|
|
73
|
+
dimensions,
|
|
74
|
+
pairs,
|
|
75
|
+
flagged,
|
|
76
|
+
maxAbsCorrelation,
|
|
77
|
+
independenceAssumptionSafe: flagged.length === 0
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function pearson(xs, ys) {
|
|
81
|
+
const n = xs.length;
|
|
82
|
+
let sumX = 0;
|
|
83
|
+
let sumY = 0;
|
|
84
|
+
for (let i = 0; i < n; i++) {
|
|
85
|
+
sumX += xs[i];
|
|
86
|
+
sumY += ys[i];
|
|
87
|
+
}
|
|
88
|
+
const meanX = sumX / n;
|
|
89
|
+
const meanY = sumY / n;
|
|
90
|
+
let covariance = 0;
|
|
91
|
+
let varianceX = 0;
|
|
92
|
+
let varianceY = 0;
|
|
93
|
+
for (let i = 0; i < n; i++) {
|
|
94
|
+
const dx = xs[i] - meanX;
|
|
95
|
+
const dy = ys[i] - meanY;
|
|
96
|
+
covariance += dx * dy;
|
|
97
|
+
varianceX += dx * dx;
|
|
98
|
+
varianceY += dy * dy;
|
|
99
|
+
}
|
|
100
|
+
if (varianceX === 0 || varianceY === 0) {
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
return covariance / Math.sqrt(varianceX * varianceY);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
exports.dependenceDiagnostic = dependenceDiagnostic;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { S as ScoreVector } from '../types-bMjn1j4e.cjs';
|
|
2
|
+
|
|
3
|
+
/** Pearson correlation between the scores of two dimensions across the history. */
|
|
4
|
+
interface DimensionCorrelation {
|
|
5
|
+
readonly a: string;
|
|
6
|
+
readonly b: string;
|
|
7
|
+
readonly correlation: number;
|
|
8
|
+
readonly samples: number;
|
|
9
|
+
}
|
|
10
|
+
/** The result of measuring dependence between dimensions over a score history. */
|
|
11
|
+
interface DependenceDiagnostic {
|
|
12
|
+
readonly dimensions: readonly string[];
|
|
13
|
+
/** All dimension pairs with a defined correlation, sorted by absolute correlation descending. */
|
|
14
|
+
readonly pairs: readonly DimensionCorrelation[];
|
|
15
|
+
/** Pairs whose absolute correlation is at or above the threshold. */
|
|
16
|
+
readonly flagged: readonly DimensionCorrelation[];
|
|
17
|
+
readonly maxAbsCorrelation: number;
|
|
18
|
+
/** True when no pair crosses the threshold, so the independence assumption is reasonable. */
|
|
19
|
+
readonly independenceAssumptionSafe: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** Options for {@link dependenceDiagnostic}. */
|
|
22
|
+
interface DependenceOptions {
|
|
23
|
+
/** Absolute correlation at or above which a pair is flagged. Defaults to 0.5. */
|
|
24
|
+
readonly threshold?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Measure pairwise dependence between dimensions across a history of score vectors. Correlation for
|
|
28
|
+
* a pair is computed over the rows where both dimensions are present. A flagged pair warns that the
|
|
29
|
+
* combined Bayes Factor will double-count evidence from those dimensions.
|
|
30
|
+
*/
|
|
31
|
+
declare function dependenceDiagnostic(history: readonly ScoreVector[], options?: DependenceOptions): DependenceDiagnostic;
|
|
32
|
+
|
|
33
|
+
export { type DependenceDiagnostic, type DependenceOptions, type DimensionCorrelation, dependenceDiagnostic };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { S as ScoreVector } from '../types-bMjn1j4e.js';
|
|
2
|
+
|
|
3
|
+
/** Pearson correlation between the scores of two dimensions across the history. */
|
|
4
|
+
interface DimensionCorrelation {
|
|
5
|
+
readonly a: string;
|
|
6
|
+
readonly b: string;
|
|
7
|
+
readonly correlation: number;
|
|
8
|
+
readonly samples: number;
|
|
9
|
+
}
|
|
10
|
+
/** The result of measuring dependence between dimensions over a score history. */
|
|
11
|
+
interface DependenceDiagnostic {
|
|
12
|
+
readonly dimensions: readonly string[];
|
|
13
|
+
/** All dimension pairs with a defined correlation, sorted by absolute correlation descending. */
|
|
14
|
+
readonly pairs: readonly DimensionCorrelation[];
|
|
15
|
+
/** Pairs whose absolute correlation is at or above the threshold. */
|
|
16
|
+
readonly flagged: readonly DimensionCorrelation[];
|
|
17
|
+
readonly maxAbsCorrelation: number;
|
|
18
|
+
/** True when no pair crosses the threshold, so the independence assumption is reasonable. */
|
|
19
|
+
readonly independenceAssumptionSafe: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** Options for {@link dependenceDiagnostic}. */
|
|
22
|
+
interface DependenceOptions {
|
|
23
|
+
/** Absolute correlation at or above which a pair is flagged. Defaults to 0.5. */
|
|
24
|
+
readonly threshold?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Measure pairwise dependence between dimensions across a history of score vectors. Correlation for
|
|
28
|
+
* a pair is computed over the rows where both dimensions are present. A flagged pair warns that the
|
|
29
|
+
* combined Bayes Factor will double-count evidence from those dimensions.
|
|
30
|
+
*/
|
|
31
|
+
declare function dependenceDiagnostic(history: readonly ScoreVector[], options?: DependenceOptions): DependenceDiagnostic;
|
|
32
|
+
|
|
33
|
+
export { type DependenceDiagnostic, type DependenceOptions, type DimensionCorrelation, dependenceDiagnostic };
|
|
@@ -0,0 +1,104 @@
|
|
|
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/dimensions/index.ts
|
|
18
|
+
function dependenceDiagnostic(history, options = {}) {
|
|
19
|
+
const threshold = options.threshold ?? 0.5;
|
|
20
|
+
invariant(
|
|
21
|
+
Number.isFinite(threshold) && threshold >= 0 && threshold <= 1,
|
|
22
|
+
"INVALID_CONFIG",
|
|
23
|
+
`threshold must be in [0, 1], got ${threshold}`
|
|
24
|
+
);
|
|
25
|
+
invariant(
|
|
26
|
+
history.length >= 2,
|
|
27
|
+
"INVALID_OBSERVATION",
|
|
28
|
+
"dependence diagnostic needs at least two score vectors"
|
|
29
|
+
);
|
|
30
|
+
const dimensionSet = /* @__PURE__ */ new Set();
|
|
31
|
+
for (const vector of history) {
|
|
32
|
+
for (const score of vector) {
|
|
33
|
+
dimensionSet.add(score.dimension);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const dimensions = [...dimensionSet].sort();
|
|
37
|
+
const rows = history.map((vector) => {
|
|
38
|
+
const map = /* @__PURE__ */ new Map();
|
|
39
|
+
for (const score of vector) {
|
|
40
|
+
map.set(score.dimension, score.value);
|
|
41
|
+
}
|
|
42
|
+
return map;
|
|
43
|
+
});
|
|
44
|
+
const pairs = [];
|
|
45
|
+
for (let i = 0; i < dimensions.length; i++) {
|
|
46
|
+
for (let j = i + 1; j < dimensions.length; j++) {
|
|
47
|
+
const a = dimensions[i];
|
|
48
|
+
const b = dimensions[j];
|
|
49
|
+
const xs = [];
|
|
50
|
+
const ys = [];
|
|
51
|
+
for (const row of rows) {
|
|
52
|
+
const x = row.get(a);
|
|
53
|
+
const y = row.get(b);
|
|
54
|
+
if (x !== void 0 && y !== void 0) {
|
|
55
|
+
xs.push(x);
|
|
56
|
+
ys.push(y);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (xs.length >= 2) {
|
|
60
|
+
const correlation = pearson(xs, ys);
|
|
61
|
+
if (Number.isFinite(correlation)) {
|
|
62
|
+
pairs.push({ a, b, correlation, samples: xs.length });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
pairs.sort((left, right) => Math.abs(right.correlation) - Math.abs(left.correlation));
|
|
68
|
+
const maxAbsCorrelation = pairs.length > 0 ? Math.abs(pairs[0].correlation) : 0;
|
|
69
|
+
const flagged = pairs.filter((pair) => Math.abs(pair.correlation) >= threshold);
|
|
70
|
+
return {
|
|
71
|
+
dimensions,
|
|
72
|
+
pairs,
|
|
73
|
+
flagged,
|
|
74
|
+
maxAbsCorrelation,
|
|
75
|
+
independenceAssumptionSafe: flagged.length === 0
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function pearson(xs, ys) {
|
|
79
|
+
const n = xs.length;
|
|
80
|
+
let sumX = 0;
|
|
81
|
+
let sumY = 0;
|
|
82
|
+
for (let i = 0; i < n; i++) {
|
|
83
|
+
sumX += xs[i];
|
|
84
|
+
sumY += ys[i];
|
|
85
|
+
}
|
|
86
|
+
const meanX = sumX / n;
|
|
87
|
+
const meanY = sumY / n;
|
|
88
|
+
let covariance = 0;
|
|
89
|
+
let varianceX = 0;
|
|
90
|
+
let varianceY = 0;
|
|
91
|
+
for (let i = 0; i < n; i++) {
|
|
92
|
+
const dx = xs[i] - meanX;
|
|
93
|
+
const dy = ys[i] - meanY;
|
|
94
|
+
covariance += dx * dy;
|
|
95
|
+
varianceX += dx * dx;
|
|
96
|
+
varianceY += dy * dy;
|
|
97
|
+
}
|
|
98
|
+
if (varianceX === 0 || varianceY === 0) {
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
return covariance / Math.sqrt(varianceX * varianceY);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { dependenceDiagnostic };
|