@seanhogg/builderforce-memory 2026.10.74 → 2026.10.76
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/stats/index.d.ts +2 -0
- package/dist/stats/index.d.ts.map +1 -1
- package/dist/stats/index.js +1 -0
- package/dist/stats/index.js.map +1 -1
- package/dist/stats/moments.d.ts +35 -0
- package/dist/stats/moments.d.ts.map +1 -0
- package/dist/stats/moments.js +40 -0
- package/dist/stats/moments.js.map +1 -0
- package/dist/stats/prng.d.ts +5 -1
- package/dist/stats/prng.d.ts.map +1 -1
- package/dist/stats/prng.js +11 -3
- package/dist/stats/prng.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/exports.snapshot.json +1 -0
- package/src/contract/regression.prng.test.ts +126 -0
- package/src/contract/types.contract.test.ts +4 -0
- package/src/stats/index.ts +3 -0
- package/src/stats/moments.test.ts +58 -0
- package/src/stats/moments.ts +54 -0
- package/src/stats/prng.ts +11 -3
package/dist/stats/index.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export type { ChangePointResult, CusumResult } from "./timeSeries.js";
|
|
|
25
25
|
export { ledoitWolfCovariance, psi } from "./multivariate.js";
|
|
26
26
|
export type { CovarianceResult } from "./multivariate.js";
|
|
27
27
|
export { entropyOfCounts, shannonEntropy } from "./information.js";
|
|
28
|
+
export { meanAndSd } from "./moments.js";
|
|
29
|
+
export type { MeanAndSd } from "./moments.js";
|
|
28
30
|
export { chapmanEstimate, gammaPoissonPosterior, goelOkumotoMLE, kaplanMeier, lincolnPetersenEstimate, } from "./estimators.js";
|
|
29
31
|
export type { InsufficientData, KaplanMeierResult } from "./estimators.js";
|
|
30
32
|
export { brierScore, effortAwareRecall, expectedCalibrationError, ndcgAtK, popt, prAuc, } from "./scores.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stats/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;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CAAC;AAE1D,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEnE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EACH,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,WAAW,EACX,uBAAuB,GAC1B,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE3E,OAAO,EACH,UAAU,EACV,iBAAiB,EACjB,wBAAwB,EACxB,OAAO,EACP,IAAI,EACJ,KAAK,GACR,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC7D,YAAY,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/stats/index.js
CHANGED
|
@@ -19,6 +19,7 @@ export { pairedBootstrapCi, signTestSprt, wilcoxonSignedRank, } from "./tests.js
|
|
|
19
19
|
export { bayesianOnlineChangePoint, cusum } from "./timeSeries.js";
|
|
20
20
|
export { ledoitWolfCovariance, psi } from "./multivariate.js";
|
|
21
21
|
export { entropyOfCounts, shannonEntropy } from "./information.js";
|
|
22
|
+
export { meanAndSd } from "./moments.js";
|
|
22
23
|
export { chapmanEstimate, gammaPoissonPosterior, goelOkumotoMLE, kaplanMeier, lincolnPetersenEstimate, } from "./estimators.js";
|
|
23
24
|
export { brierScore, effortAwareRecall, expectedCalibrationError, ndcgAtK, popt, prAuc, } from "./scores.js";
|
|
24
25
|
export { thompsonRank, thompsonSample } from "./policies.js";
|
package/dist/stats/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stats/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,WA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B,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAEjF,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAGjF,OAAO,EACH,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,GACrB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,yBAAyB,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGnE,OAAO,EAAE,oBAAoB,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAG9D,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEnE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,OAAO,EACH,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,WAAW,EACX,uBAAuB,GAC1B,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACH,UAAU,EACV,iBAAiB,EACjB,wBAAwB,EACxB,OAAO,EACP,IAAI,EACJ,KAAK,GACR,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sample moments for the stats module (PRD 41 Part D / D13): the ONE mean and
|
|
3
|
+
* standard deviation for predicted measures, so callers stop hand-rolling them.
|
|
4
|
+
*/
|
|
5
|
+
/** Result of {@link meanAndSd}. */
|
|
6
|
+
export interface MeanAndSd {
|
|
7
|
+
/** Number of finite values actually used. */
|
|
8
|
+
n: number;
|
|
9
|
+
/** Arithmetic mean of the finite values; NaN when `n` is 0. */
|
|
10
|
+
mean: number;
|
|
11
|
+
/** Sample standard deviation (n − 1 denominator); 0 when `n` < 2. */
|
|
12
|
+
sd: number;
|
|
13
|
+
/**
|
|
14
|
+
* Values that were not finite (NaN or ±Infinity) and were skipped, the same
|
|
15
|
+
* convention as `WilcoxonResult.nonFinite`.
|
|
16
|
+
*/
|
|
17
|
+
nonFinite: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Mean and SAMPLE standard deviation by Welford's online algorithm, which stays
|
|
21
|
+
* accurate where the naive Σx² − (Σx)²/n form cancels catastrophically (values
|
|
22
|
+
* with a large common offset).
|
|
23
|
+
*
|
|
24
|
+
* - Non-finite values (NaN, ±Infinity) are skipped and counted in `nonFinite`;
|
|
25
|
+
* `n` is the count of values used.
|
|
26
|
+
* - `n` = 0: `mean` is NaN and `sd` is 0. One result shape is kept rather than the
|
|
27
|
+
* estimators' `InsufficientData` union, so callers branch on `n`.
|
|
28
|
+
* - `n` = 1: `mean` is the value and `sd` is 0 (a sample sd needs two values).
|
|
29
|
+
* - A z-score consumer must guard `n >= 2 && sd > 0`: sd is 0 (not NaN) for one
|
|
30
|
+
* value or all-equal data, so an unguarded division yields ±Infinity.
|
|
31
|
+
*
|
|
32
|
+
* Pure and deterministic: one pass in input order, no allocation beyond the result.
|
|
33
|
+
*/
|
|
34
|
+
export declare function meanAndSd(values: readonly number[]): MeanAndSd;
|
|
35
|
+
//# sourceMappingURL=moments.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moments.d.ts","sourceRoot":"","sources":["../../src/stats/moments.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,mCAAmC;AACnC,MAAM,WAAW,SAAS;IACtB,6CAA6C;IAC7C,CAAC,EAAE,MAAM,CAAC;IACV,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAC;IACb,qEAAqE;IACrE,EAAE,EAAE,MAAM,CAAC;IACX;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAkB9D"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sample moments for the stats module (PRD 41 Part D / D13): the ONE mean and
|
|
3
|
+
* standard deviation for predicted measures, so callers stop hand-rolling them.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Mean and SAMPLE standard deviation by Welford's online algorithm, which stays
|
|
7
|
+
* accurate where the naive Σx² − (Σx)²/n form cancels catastrophically (values
|
|
8
|
+
* with a large common offset).
|
|
9
|
+
*
|
|
10
|
+
* - Non-finite values (NaN, ±Infinity) are skipped and counted in `nonFinite`;
|
|
11
|
+
* `n` is the count of values used.
|
|
12
|
+
* - `n` = 0: `mean` is NaN and `sd` is 0. One result shape is kept rather than the
|
|
13
|
+
* estimators' `InsufficientData` union, so callers branch on `n`.
|
|
14
|
+
* - `n` = 1: `mean` is the value and `sd` is 0 (a sample sd needs two values).
|
|
15
|
+
* - A z-score consumer must guard `n >= 2 && sd > 0`: sd is 0 (not NaN) for one
|
|
16
|
+
* value or all-equal data, so an unguarded division yields ±Infinity.
|
|
17
|
+
*
|
|
18
|
+
* Pure and deterministic: one pass in input order, no allocation beyond the result.
|
|
19
|
+
*/
|
|
20
|
+
export function meanAndSd(values) {
|
|
21
|
+
let n = 0;
|
|
22
|
+
let nonFinite = 0;
|
|
23
|
+
let mean = 0;
|
|
24
|
+
let m2 = 0;
|
|
25
|
+
for (const x of values) {
|
|
26
|
+
if (!Number.isFinite(x)) {
|
|
27
|
+
nonFinite++;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
n++;
|
|
31
|
+
const delta = x - mean;
|
|
32
|
+
mean += delta / n;
|
|
33
|
+
m2 += delta * (x - mean);
|
|
34
|
+
}
|
|
35
|
+
if (n === 0)
|
|
36
|
+
return { n: 0, mean: Number.NaN, sd: 0, nonFinite };
|
|
37
|
+
const sd = n < 2 ? 0 : Math.sqrt(Math.max(0, m2) / (n - 1));
|
|
38
|
+
return { n, mean, sd, nonFinite };
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=moments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moments.js","sourceRoot":"","sources":["../../src/stats/moments.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAiBH;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,SAAS,CAAC,MAAyB;IAC/C,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;YACtB,SAAS,EAAE,CAAC;YACZ,SAAS;QACb,CAAC;QACD,CAAC,EAAE,CAAC;QACJ,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC;QACvB,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC;QAClB,EAAE,IAAI,KAAK,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IAC7B,CAAC;IACD,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC;IACjE,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC5D,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC;AACtC,CAAC"}
|
package/dist/stats/prng.d.ts
CHANGED
|
@@ -4,7 +4,11 @@
|
|
|
4
4
|
* same inputs give byte-identical outputs on any machine. Algorithm: xoshiro128**.
|
|
5
5
|
*/
|
|
6
6
|
export type Rng = () => number;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* A deterministic RNG in [0, 1), seeded once. `seed = 0` is a valid seed. State (a, b, c, d) = (s0, s1, s2, s3)
|
|
9
|
+
* of the reference xoshiro128** (Blackman & Vigna): result = rotl(s1·5, 7)·9; t = s1 << 9 (a SHIFT, not a
|
|
10
|
+
* rotate — rotating collapsed every stream to 0 within a few hundred draws).
|
|
11
|
+
*/
|
|
8
12
|
export declare function createRng(seed: number): Rng;
|
|
9
13
|
/** Box–Muller standard-normal draw from a uniform RNG (deterministic under seed). */
|
|
10
14
|
export declare function normalDraw(rng: Rng): number;
|
package/dist/stats/prng.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prng.d.ts","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"prng.d.ts","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC;AAoB/B;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAc3C;AAED,qFAAqF;AACrF,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAI3C"}
|
package/dist/stats/prng.js
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
* stochastic routine here draws from THIS generator, seeded by the caller, so the
|
|
4
4
|
* same inputs give byte-identical outputs on any machine. Algorithm: xoshiro128**.
|
|
5
5
|
*/
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* SplitMix32-style seed expansion to four uint32 words. Never the all-zero state (the one fixed point of
|
|
8
|
+
* xoshiro): the finaliser is a bijection on uint32 (xor-shifts and odd multipliers), and its four inputs
|
|
9
|
+
* s + k·0x9e3779b9 (k = 1..4) are distinct mod 2^32, so at most one of the four words can be 0.
|
|
10
|
+
*/
|
|
7
11
|
function seedWords(seed) {
|
|
8
12
|
let s = seed >>> 0;
|
|
9
13
|
const next = () => {
|
|
@@ -16,13 +20,17 @@ function seedWords(seed) {
|
|
|
16
20
|
};
|
|
17
21
|
return [next(), next(), next(), next()];
|
|
18
22
|
}
|
|
19
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* A deterministic RNG in [0, 1), seeded once. `seed = 0` is a valid seed. State (a, b, c, d) = (s0, s1, s2, s3)
|
|
25
|
+
* of the reference xoshiro128** (Blackman & Vigna): result = rotl(s1·5, 7)·9; t = s1 << 9 (a SHIFT, not a
|
|
26
|
+
* rotate — rotating collapsed every stream to 0 within a few hundred draws).
|
|
27
|
+
*/
|
|
20
28
|
export function createRng(seed) {
|
|
21
29
|
let [a, b, c, d] = seedWords(seed);
|
|
22
30
|
return () => {
|
|
23
31
|
const result = (Math.imul(b, 5) << 7 | Math.imul(b, 5) >>> 25) >>> 0;
|
|
24
32
|
const out = ((result * 9) >>> 0) / 4294967296;
|
|
25
|
-
const t = (b << 9
|
|
33
|
+
const t = (b << 9) >>> 0;
|
|
26
34
|
c = (c ^ a) >>> 0;
|
|
27
35
|
d = (d ^ b) >>> 0;
|
|
28
36
|
b = (b ^ c) >>> 0;
|
package/dist/stats/prng.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prng.js","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH
|
|
1
|
+
{"version":3,"file":"prng.js","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;GAIG;AACH,SAAS,SAAS,CAAC,IAAY;IAC3B,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;IACnB,MAAM,IAAI,GAAG,GAAG,EAAE;QACd,CAAC,GAAG,CAAC,CAAC,GAAG,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C,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QACnC,OAAO,GAAG,CAAC;IACf,CAAC,CAAC;AACN,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,UAAU,CAAC,GAAQ;IAC/B,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;IAC3C,MAAM,EAAE,GAAG,GAAG,EAAE,CAAC;IACjB,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;AACrE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seanhogg/builderforce-memory",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.76",
|
|
4
4
|
"description": "BuilderForce Agent Memory — runtime layer. SSM execution, Transformer orchestration, online distillation, enterprise RAG (ingestion pipelines, vector-store adapters, tenant/ACL-scoped hybrid retrieval), multi-agent graph orchestration, LLM telemetry and cost accounting, persistent agent memory, and token-saving agent-run memory (code maps, a prompt-cache-safe working memory, chunked transcript windowing) for BuilderForce.ai agents.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/contract/regression.prng.test.ts — the stats module's xoshiro128** (stats/prng.ts `createRng`, shipped wrong in
|
|
3
|
+
* 2026.10.72) computed `t = rotl(s1, 9)` where the algorithm shifts (`t = s1 << 9`). The state degenerated: every
|
|
4
|
+
* stream collapsed to 0 within ~76–200 draws, so the paired bootstrap (2,000 × n draws from one stream) returned
|
|
5
|
+
* collapsed CIs and Thompson draws were biased over long streams.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from '@jest/globals';
|
|
9
|
+
import { createRng, normalDraw } from '../stats/prng.js';
|
|
10
|
+
import { pairedBootstrapCi } from '../stats/index.js';
|
|
11
|
+
|
|
12
|
+
const MASK = 0xffffffffn;
|
|
13
|
+
|
|
14
|
+
/** The seed expansion createRng documents (SplitMix32-style), in BigInt so it shares no code path with prng.ts. */
|
|
15
|
+
function referenceSeedWords(seed: number): bigint[] {
|
|
16
|
+
let s = BigInt(seed >>> 0);
|
|
17
|
+
const words: bigint[] = [];
|
|
18
|
+
for (let k = 0; k < 4; k++) {
|
|
19
|
+
s = (s + 0x9e3779b9n) & MASK;
|
|
20
|
+
let z = s;
|
|
21
|
+
z = ((z ^ (z >> 16n)) * 0x21f0aaadn) & MASK;
|
|
22
|
+
z = ((z ^ (z >> 15n)) * 0x735a2d97n) & MASK;
|
|
23
|
+
z = (z ^ (z >> 15n)) & MASK;
|
|
24
|
+
words.push(z);
|
|
25
|
+
}
|
|
26
|
+
return words;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** xoshiro128** transcribed from Blackman & Vigna's reference C (xoshiro128starstar.c), uint32 arithmetic via BigInt. */
|
|
30
|
+
function referenceXoshiro128ss(state: readonly bigint[]): () => bigint {
|
|
31
|
+
const s = [...state];
|
|
32
|
+
const rotl = (x: bigint, k: bigint) => ((x << k) | (x >> (32n - k))) & MASK;
|
|
33
|
+
return () => {
|
|
34
|
+
const result = (rotl((s[1]! * 5n) & MASK, 7n) * 9n) & MASK;
|
|
35
|
+
const t = (s[1]! << 9n) & MASK;
|
|
36
|
+
s[2] = s[2]! ^ s[0]!;
|
|
37
|
+
s[3] = s[3]! ^ s[1]!;
|
|
38
|
+
s[1] = s[1]! ^ s[2]!;
|
|
39
|
+
s[0] = s[0]! ^ s[3]!;
|
|
40
|
+
s[2] = s[2]! ^ t;
|
|
41
|
+
s[3] = rotl(s[3]!, 11n);
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('regression: createRng is the reference xoshiro128**', () => {
|
|
47
|
+
it('regression: matches an inline reference implementation over 10,000 draws for 50 seeds', () => {
|
|
48
|
+
for (let seed = 0; seed < 50; seed++) {
|
|
49
|
+
const rng = createRng(seed);
|
|
50
|
+
const ref = referenceXoshiro128ss(referenceSeedWords(seed));
|
|
51
|
+
for (let i = 0; i < 10_000; i++) {
|
|
52
|
+
const expected = Number(ref()) / 4294967296;
|
|
53
|
+
const got = rng();
|
|
54
|
+
if (got !== expected) expect({ seed, i, got }).toEqual({ seed, i, got: expected });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('regression: known answer for the fixed state (1, 2, 3, 4)', () => {
|
|
60
|
+
// Hand-derived from the reference C. Step 1: s1 = 2 → rotl(10, 7)·9 = 1280·9 = 11520; t = 1024;
|
|
61
|
+
// s = (1^6, 2^2, (3^1)^1024, rotl(4^2, 11)) = (7, 0, 1026, 12288).
|
|
62
|
+
const ref = referenceXoshiro128ss([1n, 2n, 3n, 4n]);
|
|
63
|
+
expect(ref()).toBe(11520n);
|
|
64
|
+
// Step 2: s1 = 0 → result 0; t = 0; s = (7^12288, 0^1029, 1029, rotl(12288, 11)) = (12295, 1029, 1029, 25165824).
|
|
65
|
+
expect(ref()).toBe(0n);
|
|
66
|
+
// Step 3: s1 = 1029 → rotl(5145, 7)·9 = 658560·9 = 5927040.
|
|
67
|
+
expect(ref()).toBe(5927040n);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('regression: seedWords never yields the all-zero state', () => {
|
|
71
|
+
for (const seed of [0, 1, -1, 0x61c88647, 0xffffffff, 2 ** 31, -(2 ** 31)]) {
|
|
72
|
+
expect({ seed, zero: referenceSeedWords(seed).every((w) => w === 0n) }).toEqual({ seed, zero: false });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('regression: no stream collapses to 0', () => {
|
|
78
|
+
it('regression: 200 seeds × 2,000 draws (the rotate collapsed within ~200) — no run of ≥ 8 exact zeros, zero fraction < 1e-6', () => {
|
|
79
|
+
let zeros = 0;
|
|
80
|
+
let total = 0;
|
|
81
|
+
let longestRun = 0;
|
|
82
|
+
for (let seed = 0; seed < 200; seed++) {
|
|
83
|
+
const rng = createRng(seed);
|
|
84
|
+
let run = 0;
|
|
85
|
+
for (let i = 0; i < 2_000; i++) {
|
|
86
|
+
total++;
|
|
87
|
+
if (rng() === 0) {
|
|
88
|
+
zeros++;
|
|
89
|
+
run++;
|
|
90
|
+
if (run > longestRun) longestRun = run;
|
|
91
|
+
} else run = 0;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
expect(longestRun).toBeLessThan(8);
|
|
95
|
+
expect(zeros / total).toBeLessThan(1e-6);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('regression: 1M draws from one seed have mean ≈ 1/2 and variance ≈ 1/12', () => {
|
|
99
|
+
const rng = createRng(12345);
|
|
100
|
+
const N = 1_000_000;
|
|
101
|
+
let sum = 0;
|
|
102
|
+
let sumSq = 0;
|
|
103
|
+
for (let i = 0; i < N; i++) {
|
|
104
|
+
const u = rng();
|
|
105
|
+
sum += u;
|
|
106
|
+
sumSq += u * u;
|
|
107
|
+
}
|
|
108
|
+
const mean = sum / N;
|
|
109
|
+
const variance = sumSq / N - mean * mean;
|
|
110
|
+
expect(Math.abs(mean - 0.5)).toBeLessThan(0.001);
|
|
111
|
+
expect(Math.abs(variance - 1 / 12)).toBeLessThan(0.001);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('regression: the paired bootstrap CI does not collapse', () => {
|
|
116
|
+
it('regression: a noisy no-shift series (seeded normal noise, n = 30) gives a CI that contains 0', () => {
|
|
117
|
+
const rng = createRng(2026);
|
|
118
|
+
const before = Array.from({ length: 30 }, () => 10 + normalDraw(rng));
|
|
119
|
+
const after = Array.from({ length: 30 }, () => 10 + normalDraw(rng));
|
|
120
|
+
const ci = pairedBootstrapCi(before, after, { seed: 7 });
|
|
121
|
+
expect(ci.low).toBeLessThan(0);
|
|
122
|
+
expect(ci.high).toBeGreaterThan(0);
|
|
123
|
+
// diff sd ≈ √2, se ≈ 0.26: a 95% interval is ≈ 1 wide, never a collapsed point.
|
|
124
|
+
expect(ci.high - ci.low).toBeGreaterThan(0.5);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -36,6 +36,7 @@ import type {
|
|
|
36
36
|
NodeRef, StationTransition, TransitionRow, TransitionSource, TransitionTable,
|
|
37
37
|
} from '../knowledge/index.js';
|
|
38
38
|
import type { betaMoments, logisticProbability, onlineLogisticStep, rateInterval, sigmoid, wilsonInterval } from '../stats/index.js';
|
|
39
|
+
import type { meanAndSd, MeanAndSd } from '../stats/index.js';
|
|
39
40
|
import type {
|
|
40
41
|
CallFact, CapabilityImportFact, DerivedRows, EntryPointFact, EntryPointKind, ExportFact, ExtractorStamp, FactChange, FactGraph, FactGraphOptions,
|
|
41
42
|
FactGraphUpdate, FactKind, FactRule, FileFacts, RuleEvaluation, RuleScope, RuleWorld, TestCaseFact, TestFacts,
|
|
@@ -323,6 +324,9 @@ export type wilsonInterval_signature = Expect<Equal<
|
|
|
323
324
|
typeof wilsonInterval, (successes: number, n: number, z?: number) => { low: number; high: number }
|
|
324
325
|
>>;
|
|
325
326
|
export type betaMoments_signature = Expect<Equal<typeof betaMoments, (alpha: number, beta: number) => { mean: number; variance: number }>>;
|
|
327
|
+
// PRD 41 D13: the one mean / sample sd (Welford) for predicted measures; run-delta's naturalness model migrates onto it.
|
|
328
|
+
export type meanAndSd_signature = Expect<Equal<typeof meanAndSd, (values: readonly number[]) => MeanAndSd>>;
|
|
329
|
+
export type MeanAndSd_fields = Expect<Equal<MeanAndSd, { n: number; mean: number; sd: number; nonFinite: number }>>;
|
|
326
330
|
// PRD 38 P7: station transitions (the view groups stations; memory owns the formula). Reference rows carry no interval.
|
|
327
331
|
export type TransitionSource_values = Expect<Equal<TransitionSource, 'traces' | 'references'>>;
|
|
328
332
|
export type StationTransition_fields = Expect<Equal<StationTransition, { to: string; p: number; k: number; n: number; low?: number; high?: number }>>;
|
package/src/stats/index.ts
CHANGED
|
@@ -50,6 +50,9 @@ export type { CovarianceResult } from "./multivariate.js";
|
|
|
50
50
|
|
|
51
51
|
export { entropyOfCounts, shannonEntropy } from "./information.js";
|
|
52
52
|
|
|
53
|
+
export { meanAndSd } from "./moments.js";
|
|
54
|
+
export type { MeanAndSd } from "./moments.js";
|
|
55
|
+
|
|
53
56
|
export {
|
|
54
57
|
chapmanEstimate,
|
|
55
58
|
gammaPoissonPosterior,
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The PRD 41 D13 mean / sample sd primitive (`meanAndSd`): the one mean and standard
|
|
3
|
+
* deviation for predicted measures (Builderforce run-delta's naturalness model).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "@jest/globals";
|
|
7
|
+
import { meanAndSd } from "./index.js";
|
|
8
|
+
|
|
9
|
+
describe("meanAndSd", () => {
|
|
10
|
+
it("gives the textbook mean and sample sd", () => {
|
|
11
|
+
// 2,4,4,4,5,5,7,9: mean 5, Σ(x−5)² = 32, sample variance 32/7.
|
|
12
|
+
const r = meanAndSd([2, 4, 4, 4, 5, 5, 7, 9]);
|
|
13
|
+
expect(r.n).toBe(8);
|
|
14
|
+
expect(r.nonFinite).toBe(0);
|
|
15
|
+
expect(r.mean).toBe(5);
|
|
16
|
+
expect(r.sd).toBeCloseTo(Math.sqrt(32 / 7), 14);
|
|
17
|
+
const s = meanAndSd([1, 2, 3, 4]);
|
|
18
|
+
expect(s.mean).toBe(2.5);
|
|
19
|
+
expect(s.sd).toBeCloseTo(Math.sqrt(5 / 3), 14);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("survives catastrophic cancellation (large common offset)", () => {
|
|
23
|
+
const r = meanAndSd([1e9 + 4, 1e9 + 7, 1e9 + 13, 1e9 + 16]);
|
|
24
|
+
expect(r.n).toBe(4);
|
|
25
|
+
expect(r.mean).toBe(1e9 + 10);
|
|
26
|
+
expect(r.sd * r.sd).toBeCloseTo(30, 9);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("n = 0: mean NaN, sd 0", () => {
|
|
30
|
+
expect(meanAndSd([])).toEqual({ n: 0, mean: Number.NaN, sd: 0, nonFinite: 0 });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("n = 1: mean is the value, sd 0", () => {
|
|
34
|
+
expect(meanAndSd([3.25])).toEqual({ n: 1, mean: 3.25, sd: 0, nonFinite: 0 });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("all-equal input has sd exactly 0", () => {
|
|
38
|
+
const r = meanAndSd([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]);
|
|
39
|
+
expect(r.n).toBe(7);
|
|
40
|
+
expect(r.mean).toBeCloseTo(0.1, 15);
|
|
41
|
+
expect(r.sd).toBe(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("skips NaN and ±Infinity, counting them in nonFinite", () => {
|
|
45
|
+
const r = meanAndSd([1, Number.NaN, 2, Number.POSITIVE_INFINITY, 3, Number.NEGATIVE_INFINITY, 4]);
|
|
46
|
+
expect(r.n).toBe(4);
|
|
47
|
+
expect(r.nonFinite).toBe(3);
|
|
48
|
+
expect(r.mean).toBe(2.5);
|
|
49
|
+
expect(r.sd).toBeCloseTo(Math.sqrt(5 / 3), 14);
|
|
50
|
+
expect(meanAndSd([Number.NaN, Number.NaN])).toEqual({ n: 0, mean: Number.NaN, sd: 0, nonFinite: 2 });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("is deterministic and does not mutate its input", () => {
|
|
54
|
+
const input = Object.freeze([5, 1, 9, 3, 7]);
|
|
55
|
+
expect(meanAndSd(input)).toEqual(meanAndSd(input));
|
|
56
|
+
expect(input).toEqual([5, 1, 9, 3, 7]);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sample moments for the stats module (PRD 41 Part D / D13): the ONE mean and
|
|
3
|
+
* standard deviation for predicted measures, so callers stop hand-rolling them.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Result of {@link meanAndSd}. */
|
|
7
|
+
export interface MeanAndSd {
|
|
8
|
+
/** Number of finite values actually used. */
|
|
9
|
+
n: number;
|
|
10
|
+
/** Arithmetic mean of the finite values; NaN when `n` is 0. */
|
|
11
|
+
mean: number;
|
|
12
|
+
/** Sample standard deviation (n − 1 denominator); 0 when `n` < 2. */
|
|
13
|
+
sd: number;
|
|
14
|
+
/**
|
|
15
|
+
* Values that were not finite (NaN or ±Infinity) and were skipped, the same
|
|
16
|
+
* convention as `WilcoxonResult.nonFinite`.
|
|
17
|
+
*/
|
|
18
|
+
nonFinite: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Mean and SAMPLE standard deviation by Welford's online algorithm, which stays
|
|
23
|
+
* accurate where the naive Σx² − (Σx)²/n form cancels catastrophically (values
|
|
24
|
+
* with a large common offset).
|
|
25
|
+
*
|
|
26
|
+
* - Non-finite values (NaN, ±Infinity) are skipped and counted in `nonFinite`;
|
|
27
|
+
* `n` is the count of values used.
|
|
28
|
+
* - `n` = 0: `mean` is NaN and `sd` is 0. One result shape is kept rather than the
|
|
29
|
+
* estimators' `InsufficientData` union, so callers branch on `n`.
|
|
30
|
+
* - `n` = 1: `mean` is the value and `sd` is 0 (a sample sd needs two values).
|
|
31
|
+
* - A z-score consumer must guard `n >= 2 && sd > 0`: sd is 0 (not NaN) for one
|
|
32
|
+
* value or all-equal data, so an unguarded division yields ±Infinity.
|
|
33
|
+
*
|
|
34
|
+
* Pure and deterministic: one pass in input order, no allocation beyond the result.
|
|
35
|
+
*/
|
|
36
|
+
export function meanAndSd(values: readonly number[]): MeanAndSd {
|
|
37
|
+
let n = 0;
|
|
38
|
+
let nonFinite = 0;
|
|
39
|
+
let mean = 0;
|
|
40
|
+
let m2 = 0;
|
|
41
|
+
for (const x of values) {
|
|
42
|
+
if (!Number.isFinite(x)) {
|
|
43
|
+
nonFinite++;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
n++;
|
|
47
|
+
const delta = x - mean;
|
|
48
|
+
mean += delta / n;
|
|
49
|
+
m2 += delta * (x - mean);
|
|
50
|
+
}
|
|
51
|
+
if (n === 0) return { n: 0, mean: Number.NaN, sd: 0, nonFinite };
|
|
52
|
+
const sd = n < 2 ? 0 : Math.sqrt(Math.max(0, m2) / (n - 1));
|
|
53
|
+
return { n, mean, sd, nonFinite };
|
|
54
|
+
}
|
package/src/stats/prng.ts
CHANGED
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
export type Rng = () => number;
|
|
8
8
|
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* SplitMix32-style seed expansion to four uint32 words. Never the all-zero state (the one fixed point of
|
|
11
|
+
* xoshiro): the finaliser is a bijection on uint32 (xor-shifts and odd multipliers), and its four inputs
|
|
12
|
+
* s + k·0x9e3779b9 (k = 1..4) are distinct mod 2^32, so at most one of the four words can be 0.
|
|
13
|
+
*/
|
|
10
14
|
function seedWords(seed: number): [number, number, number, number] {
|
|
11
15
|
let s = seed >>> 0;
|
|
12
16
|
const next = () => {
|
|
@@ -20,13 +24,17 @@ function seedWords(seed: number): [number, number, number, number] {
|
|
|
20
24
|
return [next(), next(), next(), next()];
|
|
21
25
|
}
|
|
22
26
|
|
|
23
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* A deterministic RNG in [0, 1), seeded once. `seed = 0` is a valid seed. State (a, b, c, d) = (s0, s1, s2, s3)
|
|
29
|
+
* of the reference xoshiro128** (Blackman & Vigna): result = rotl(s1·5, 7)·9; t = s1 << 9 (a SHIFT, not a
|
|
30
|
+
* rotate — rotating collapsed every stream to 0 within a few hundred draws).
|
|
31
|
+
*/
|
|
24
32
|
export function createRng(seed: number): Rng {
|
|
25
33
|
let [a, b, c, d] = seedWords(seed);
|
|
26
34
|
return () => {
|
|
27
35
|
const result = (Math.imul(b, 5) << 7 | Math.imul(b, 5) >>> 25) >>> 0;
|
|
28
36
|
const out = ((result * 9) >>> 0) / 4294967296;
|
|
29
|
-
const t = (b << 9
|
|
37
|
+
const t = (b << 9) >>> 0;
|
|
30
38
|
c = (c ^ a) >>> 0;
|
|
31
39
|
d = (d ^ b) >>> 0;
|
|
32
40
|
b = (b ^ c) >>> 0;
|