@rangefoundry/statistics 0.0.0-stage → 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/LICENSE +661 -0
- package/README.md +20 -2
- package/dist/bisect.d.ts +2 -0
- package/dist/bisect.js +15 -0
- package/dist/chi-square.d.ts +6 -0
- package/dist/chi-square.js +75 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +23 -0
- package/dist/f-distribution.d.ts +2 -0
- package/dist/f-distribution.js +10 -0
- package/dist/huber.d.ts +12 -0
- package/dist/huber.js +10 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +10 -0
- package/dist/interval.d.ts +7 -0
- package/dist/interval.js +2 -0
- package/dist/issue.d.ts +5 -0
- package/dist/issue.js +4 -0
- package/dist/least-squares.d.ts +87 -0
- package/dist/least-squares.js +287 -0
- package/dist/percentile.d.ts +2 -0
- package/dist/percentile.js +9 -0
- package/dist/random.d.ts +4 -0
- package/dist/random.js +16 -0
- package/dist/student-t.d.ts +2 -0
- package/dist/student-t.js +82 -0
- package/dist/velocity-statistics.d.ts +22 -0
- package/dist/velocity-statistics.js +72 -0
- package/package.json +48 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { nth } from './errors.js';
|
|
2
|
+
/** Percentile `q` (0 to 1) of ascending `sorted` by linear interpolation between the closest ranks. */
|
|
3
|
+
export function percentile(sorted, q) {
|
|
4
|
+
const rank = q * (sorted.length - 1);
|
|
5
|
+
const i = Math.floor(rank);
|
|
6
|
+
const low = nth(sorted, i);
|
|
7
|
+
return low + (rank - i) * ((sorted[i + 1] ?? low) - low);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=percentile.js.map
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Deterministic uniform numbers in [0, 1) from an unsigned 32-bit `seed`: the mulberry32 generator. */
|
|
2
|
+
export declare function seededUniforms(seed: number): () => number;
|
|
3
|
+
/** Box-Muller from `uniform`, two uniforms each: the radius from the first, the angle from the second. */
|
|
4
|
+
export declare function normalsFrom(uniform: () => number): () => number;
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Deterministic uniform numbers in [0, 1) from an unsigned 32-bit `seed`: the mulberry32 generator. */
|
|
2
|
+
export function seededUniforms(seed) {
|
|
3
|
+
let a = seed >>> 0;
|
|
4
|
+
return () => {
|
|
5
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
6
|
+
let t = a;
|
|
7
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
8
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
9
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** Box-Muller from `uniform`, two uniforms each: the radius from the first, the angle from the second. */
|
|
13
|
+
export function normalsFrom(uniform) {
|
|
14
|
+
return () => Math.sqrt(-2 * Math.log(1 - uniform())) * Math.cos(2 * Math.PI * uniform());
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=random.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** The `t`, not negative, at which the probability of |T| below `t` is `level`; level in (0, 1), `df` a positive whole number. Every level a double holds in (0, 1) is accepted: for `df` up to 1,000 and levels from 1e-40 to 1 - 2^-53 the result keeps a relative accuracy of 1e-12; below 1e-40 the 200 bisection steps from 2 stop short of `t` (level 1e-100 at `df` 1 returns about 6e-61, not 1.6e-100). */
|
|
2
|
+
export declare function studentTwoSidedQuantile(level: number, df: number): number;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** For whole degrees of freedom: bisection on the closed forms of P(|T| < t) for odd and even degrees (Abramowitz and Stegun 26.7.3 and 26.7.4) below level 0.5, on the tail P(|T| > t) = I_x(df/2, 1/2) from 0.5 up (DLMF 8.17.22), so that levels near 1 keep their digits (`apps/docs/docs/models/groups.md`, "Student's t quantile"). */
|
|
2
|
+
import { bisect } from './bisect.js';
|
|
3
|
+
import { need } from './issue.js';
|
|
4
|
+
/** P(|T| < t) for even `df` at theta = atan(t / sqrt(df)): sin(theta) times a series in cos^2 (A&S 26.7.3). */
|
|
5
|
+
function evenMass(theta, df) {
|
|
6
|
+
const c2 = Math.cos(theta) ** 2;
|
|
7
|
+
// term k: (2k-1)!! / (2k)!! cos^(2k)
|
|
8
|
+
let term = 1;
|
|
9
|
+
let sum = term;
|
|
10
|
+
for (let k = 1; 2 * k < df; k++) {
|
|
11
|
+
term *= ((2 * k - 1) / (2 * k)) * c2;
|
|
12
|
+
sum += term;
|
|
13
|
+
}
|
|
14
|
+
return Math.sin(theta) * sum;
|
|
15
|
+
}
|
|
16
|
+
/** P(|T| < t) for odd `df` at theta = atan(t / sqrt(df)): 2 theta / pi plus a series in cos^2 (A&S 26.7.4). */
|
|
17
|
+
function oddMass(theta, df) {
|
|
18
|
+
if (df === 1)
|
|
19
|
+
return (2 * theta) / Math.PI;
|
|
20
|
+
const c2 = Math.cos(theta) ** 2;
|
|
21
|
+
// term k: (2k)!! / (2k+1)!! cos^(2k+1)
|
|
22
|
+
let term = Math.cos(theta);
|
|
23
|
+
let sum = term;
|
|
24
|
+
for (let k = 1; 2 * k < df - 1; k++) {
|
|
25
|
+
term *= ((2 * k) / (2 * k + 1)) * c2;
|
|
26
|
+
sum += term;
|
|
27
|
+
}
|
|
28
|
+
return (2 / Math.PI) * (theta + Math.sin(theta) * sum);
|
|
29
|
+
}
|
|
30
|
+
/** P(|T| < t) for `df` degrees of freedom, t not negative. */
|
|
31
|
+
function centralMass(t, df) {
|
|
32
|
+
const theta = Math.atan(t / Math.sqrt(df));
|
|
33
|
+
return df % 2 === 0 ? evenMass(theta, df) : oddMass(theta, df);
|
|
34
|
+
}
|
|
35
|
+
/** P(|T| > t) for `df` degrees of freedom, t not negative: the continued fraction of I_x(df/2, 1/2) at x = df / (df + t^2) (DLMF 8.17.22, https://dlmf.nist.gov/8.17#E22) by the modified Lentz method where it converges fast, else 1 - `centralMass`, which is at least 0.08 there. */
|
|
36
|
+
function tailMass(t, df) {
|
|
37
|
+
const a = df / 2;
|
|
38
|
+
const r = (t * t) / df;
|
|
39
|
+
const x = 1 / (1 + r);
|
|
40
|
+
if (x >= (a + 1) / (a + 2.5))
|
|
41
|
+
return 1 - centralMass(t, df);
|
|
42
|
+
const tiny = 1e-300;
|
|
43
|
+
const guard = (v) => (Math.abs(v) < tiny ? tiny : v);
|
|
44
|
+
let c = 1;
|
|
45
|
+
let d = 1 / guard(1 - ((a + 0.5) * x) / (a + 1));
|
|
46
|
+
let h = d;
|
|
47
|
+
for (let m = 1; m < 10000; m++) {
|
|
48
|
+
const even = (m * (0.5 - m) * x) / ((a + 2 * m - 1) * (a + 2 * m));
|
|
49
|
+
d = 1 / guard(1 + even * d);
|
|
50
|
+
c = guard(1 + even / c);
|
|
51
|
+
h *= d * c;
|
|
52
|
+
const odd = (-(a + m) * (a + 0.5 + m) * x) / ((a + 2 * m) * (a + 2 * m + 1));
|
|
53
|
+
d = 1 / guard(1 + odd * d);
|
|
54
|
+
c = guard(1 + odd / c);
|
|
55
|
+
const delta = d * c;
|
|
56
|
+
h *= delta;
|
|
57
|
+
if (Math.abs(delta - 1) < 1e-16)
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
return (Math.exp(-a * Math.log1p(r)) * Math.sqrt(r / (1 + r)) * h) / betaTimesA(a);
|
|
61
|
+
}
|
|
62
|
+
/** a B(a, 1/2) = Gamma(a + 1) sqrt(pi) / Gamma(a + 1/2) for a whole or half-whole a > 0, as a product of ratios near 1, which keeps the digits that a difference of ln Gamma loses for large a. */
|
|
63
|
+
function betaTimesA(a) {
|
|
64
|
+
const whole = Number.isInteger(a);
|
|
65
|
+
let product = whole ? 1 : Math.PI / 2;
|
|
66
|
+
for (let j = whole ? 1 : 1.5; j <= a; j++)
|
|
67
|
+
product *= j / (j - 0.5);
|
|
68
|
+
return product;
|
|
69
|
+
}
|
|
70
|
+
/** The `t`, not negative, at which the probability of |T| below `t` is `level`; level in (0, 1), `df` a positive whole number. Every level a double holds in (0, 1) is accepted: for `df` up to 1,000 and levels from 1e-40 to 1 - 2^-53 the result keeps a relative accuracy of 1e-12; below 1e-40 the 200 bisection steps from 2 stop short of `t` (level 1e-100 at `df` 1 returns about 6e-61, not 1.6e-100). */
|
|
71
|
+
export function studentTwoSidedQuantile(level, df) {
|
|
72
|
+
need(level > 0 && level < 1, { code: 'levelOutsideUnit', params: {} });
|
|
73
|
+
need(Number.isInteger(df) && df > 0, { code: 'degreesOfFreedomNotPositiveWhole', params: {} });
|
|
74
|
+
// from 0.5 up 1 - level is exact, while 1 - centralMass loses the tail's digits near level 1
|
|
75
|
+
const alpha = 1 - level;
|
|
76
|
+
const below = level < 0.5 ? (t) => centralMass(t, df) < level : (t) => tailMass(t, df) > alpha;
|
|
77
|
+
let hi = 2;
|
|
78
|
+
while (below(hi))
|
|
79
|
+
hi *= 2;
|
|
80
|
+
return bisect(below, [0, hi], 200, true);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=student-t.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Mean, sample standard deviation, extreme spread and the chi-square interval of the standard deviation (the one-axis form of "Confidence intervals by shot count"), and the shot count for a stated interval width (`apps/docs/docs/models/groups.md`, "Velocity strings"); velocities in m/s. */
|
|
2
|
+
import type { Interval } from './interval.ts';
|
|
3
|
+
/** Values of one string of velocities, m/s. */
|
|
4
|
+
export interface VelocityStatistics {
|
|
5
|
+
/** Shots in the statistics. */
|
|
6
|
+
count: number;
|
|
7
|
+
mean: number;
|
|
8
|
+
/** Sample standard deviation, divisor n - 1; present from 2 shots. */
|
|
9
|
+
standardDeviation?: number;
|
|
10
|
+
/** Largest minus smallest velocity. */
|
|
11
|
+
extremeSpread: number;
|
|
12
|
+
min: number;
|
|
13
|
+
max: number;
|
|
14
|
+
/** Two-sided level of `standardDeviationInterval`, in (0, 1). */
|
|
15
|
+
confidenceLevel: number;
|
|
16
|
+
/** Chi-square interval of the population standard deviation; present from 3 shots, valid for normally distributed velocities. */
|
|
17
|
+
standardDeviationInterval?: Interval;
|
|
18
|
+
}
|
|
19
|
+
/** Of the velocities of one string, m/s, finite values, `undefined` for no shot; the standard deviation's interval is sqrt((n - 1) s^2 / chi-square) at the two chi-square quantiles of `level` (two-sided, in (0, 1), default 0.95). */
|
|
20
|
+
export declare function velocityStatistics(velocities: readonly number[], level?: number): VelocityStatistics | undefined;
|
|
21
|
+
/** The half-width is (upper - lower) / 2 over the sample value, at `level` (default 0.95), and falls with the shot count; at least 3 shots, `relativeHalfWidth` from 0.01 up. */
|
|
22
|
+
export declare function shotsNeeded(relativeHalfWidth: number, level?: number): number;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Mean, sample standard deviation, extreme spread and the chi-square interval of the standard deviation (the one-axis form of "Confidence intervals by shot count"), and the shot count for a stated interval width (`apps/docs/docs/models/groups.md`, "Velocity strings"); velocities in m/s. */
|
|
2
|
+
import { chiSquareQuantile } from './chi-square.js';
|
|
3
|
+
import { need } from './issue.js';
|
|
4
|
+
/** Level of the intervals when none is given. */
|
|
5
|
+
const defaultLevel = 0.95;
|
|
6
|
+
/** Fewest shots with an interval of the standard deviation. */
|
|
7
|
+
const minimumIntervalShots = 3;
|
|
8
|
+
/** Most shots `shotsNeeded` searches: a narrower interval is refused. */
|
|
9
|
+
const shotLimit = 100_000;
|
|
10
|
+
/** Limits of the interval of the standard deviation as multiples of the sample value, n >= 2 shots. */
|
|
11
|
+
function intervalFactors(n, level) {
|
|
12
|
+
const tail = (1 - level) / 2;
|
|
13
|
+
const nu = n - 1;
|
|
14
|
+
return {
|
|
15
|
+
lower: Math.sqrt(nu / chiSquareQuantile(1 - tail, nu)),
|
|
16
|
+
upper: Math.sqrt(nu / chiSquareQuantile(tail, nu)),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Of the velocities of one string, m/s, finite values, `undefined` for no shot; the standard deviation's interval is sqrt((n - 1) s^2 / chi-square) at the two chi-square quantiles of `level` (two-sided, in (0, 1), default 0.95). */
|
|
20
|
+
export function velocityStatistics(velocities, level = defaultLevel) {
|
|
21
|
+
need(level > 0 && level < 1, { code: 'confidenceLevelOutsideUnit', params: {} });
|
|
22
|
+
need(velocities.every(Number.isFinite), { code: 'velocitiesNotFinite', params: {} });
|
|
23
|
+
const n = velocities.length;
|
|
24
|
+
if (n === 0)
|
|
25
|
+
return undefined;
|
|
26
|
+
const mean = velocities.reduce((a, x) => a + x, 0) / n;
|
|
27
|
+
const min = velocities.reduce((a, x) => Math.min(a, x), Number.POSITIVE_INFINITY);
|
|
28
|
+
const max = velocities.reduce((a, x) => Math.max(a, x), Number.NEGATIVE_INFINITY);
|
|
29
|
+
const out = {
|
|
30
|
+
count: n,
|
|
31
|
+
mean,
|
|
32
|
+
extremeSpread: max - min,
|
|
33
|
+
min,
|
|
34
|
+
max,
|
|
35
|
+
confidenceLevel: level,
|
|
36
|
+
};
|
|
37
|
+
if (n < 2)
|
|
38
|
+
return out;
|
|
39
|
+
const sd = Math.sqrt(velocities.reduce((a, x) => a + (x - mean) ** 2, 0) / (n - 1));
|
|
40
|
+
out.standardDeviation = sd;
|
|
41
|
+
if (n >= minimumIntervalShots) {
|
|
42
|
+
const f = intervalFactors(n, level);
|
|
43
|
+
out.standardDeviationInterval = { lower: f.lower * sd, upper: f.upper * sd };
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
/** The half-width is (upper - lower) / 2 over the sample value, at `level` (default 0.95), and falls with the shot count; at least 3 shots, `relativeHalfWidth` from 0.01 up. */
|
|
48
|
+
export function shotsNeeded(relativeHalfWidth, level = defaultLevel) {
|
|
49
|
+
need(level > 0 && level < 1, { code: 'confidenceLevelOutsideUnit', params: {} });
|
|
50
|
+
need(relativeHalfWidth >= 0.01, { code: 'relativeHalfWidthBelowMinimum', params: {} });
|
|
51
|
+
const narrow = (n) => {
|
|
52
|
+
const f = intervalFactors(n, level);
|
|
53
|
+
return (f.upper - f.lower) / 2 <= relativeHalfWidth;
|
|
54
|
+
};
|
|
55
|
+
let hi = minimumIntervalShots;
|
|
56
|
+
while (!narrow(hi) && hi < shotLimit)
|
|
57
|
+
hi = Math.min(hi * 2, shotLimit);
|
|
58
|
+
need(narrow(hi), { code: 'relativeHalfWidthOutOfReach', params: {} });
|
|
59
|
+
let lo = Math.max(minimumIntervalShots, hi / 2);
|
|
60
|
+
// narrow(hi) holds; find the first n in (lo, hi] that holds
|
|
61
|
+
if (narrow(lo))
|
|
62
|
+
return lo;
|
|
63
|
+
while (hi - lo > 1) {
|
|
64
|
+
const mid = Math.floor((lo + hi) / 2);
|
|
65
|
+
if (narrow(mid))
|
|
66
|
+
hi = mid;
|
|
67
|
+
else
|
|
68
|
+
lo = mid;
|
|
69
|
+
}
|
|
70
|
+
return hi;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=velocity-statistics.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rangefoundry/statistics",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Distributions, velocity statistics, robust least squares on log parameters and a seeded random generator.",
|
|
5
|
+
"license": "AGPL-3.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/RangeFoundry/workbench.git",
|
|
9
|
+
"directory": "packages/statistics"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=26"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"!dist/**/*.map",
|
|
28
|
+
"!dist/*.tsbuildinfo",
|
|
29
|
+
"!dist/api-extractor",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
34
|
+
"@microsoft/api-extractor": "7.60.1",
|
|
35
|
+
"@rangefoundry/test-kit": "0.0.0",
|
|
36
|
+
"@types/node": "26.6.6",
|
|
37
|
+
"@vitest/coverage-v8": "5.1.1",
|
|
38
|
+
"fast-check": "4.11.0",
|
|
39
|
+
"oxlint": "1.94.0",
|
|
40
|
+
"oxlint-tsgolint": "7.0.2008",
|
|
41
|
+
"publint": "0.3.25",
|
|
42
|
+
"typescript": "7.0.2",
|
|
43
|
+
"vitest": "5.1.1"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc -b tsconfig.build.json && api-extractor run",
|
|
47
|
+
"typecheck": "tsc --noEmit && tsc -p test",
|
|
48
|
+
"lint": "oxlint --type-aware --deny-warnings --report-unused-disable-directives",
|
|
49
|
+
"test": "vitest run --coverage && publint --strict && attw --pack . --profile esm-only"
|
|
50
|
+
}
|
|
6
51
|
}
|