@mark1russell7/rigor-generators 0.2.5 → 0.2.6
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/README.md +1 -0
- package/build/normal.d.ts.map +1 -1
- package/build/normal.js +12 -3
- package/build/normal.js.map +1 -1
- package/build/quantiles.d.ts +7 -4
- package/build/quantiles.d.ts.map +1 -1
- package/build/quantiles.js +192 -35
- package/build/quantiles.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,6 +112,7 @@ The npm package holds the notice of the Ptolemy II license, because the field `n
|
|
|
112
112
|
|
|
113
113
|
- `logBeta` keeps the digits of its value when a parameter is near 1e13. There, the sum of `logGamma` loses all of them.
|
|
114
114
|
- `betaShares` takes the point `x` and its distance `y = 1 - x` from 1. Thus a point near 1 keeps its digits. The function gives the shares of the law below and above the point.
|
|
115
|
+
- `betaShares` uses the methods of TOMS 708 (as `pbeta` does in R). With two large parameters near the mean, it uses the asymptotic expansion. Elsewhere, it uses the continued fraction. The relative error of each share is below 1e-12 for parameters up to 1e13.
|
|
115
116
|
|
|
116
117
|
The Student t law with `df` degrees of freedom has its shares between `-t` and `t` and above `|t|` in `betaShares(t^2 / (df + t^2), df / (df + t^2), 0.5, df / 2)`.
|
|
117
118
|
|
package/build/normal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normal.d.ts","names":[],"sources":["../src/normal.ts"],"mappings":";;;;;;;;wBAyBgB,UAAU;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"normal.d.ts","names":[],"sources":["../src/normal.ts"],"mappings":";;;;;;;;wBAyBgB,UAAU;;;;;;;;;wBA0EV,eAAe"}
|
package/build/normal.js
CHANGED
|
@@ -32,10 +32,19 @@ function normalCdf(x) {
|
|
|
32
32
|
}
|
|
33
33
|
return .5 + density * sum;
|
|
34
34
|
}
|
|
35
|
+
const tail = density * millsRatio(z);
|
|
36
|
+
return x > 0 ? 1 - tail : tail;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* This function gives the Mills ratio `P(Z > z) / phi(z)` for a `z` of 0 or more. It does not underflow for a large `z`.
|
|
40
|
+
* From 0.75, it is the continued fraction `1 / (z + 1 / (z + 2 / (z + ...)))`. The fraction has `1000 / z^2 + 50` terms. This count converges to the last bit.
|
|
41
|
+
* Below 0.75, it is `normalCdf(-z) / phi(z)`.
|
|
42
|
+
*/
|
|
43
|
+
function millsRatio(z) {
|
|
44
|
+
if (z < .75) return normalCdf(-z) / (halfSquareExp(z) / SQRT_2PI);
|
|
35
45
|
let fraction = z;
|
|
36
46
|
for (let k = Math.ceil(1e3 / (z * z)) + 50; k >= 1; k--) fraction = z + k / fraction;
|
|
37
|
-
|
|
38
|
-
return x > 0 ? 1 - tail : tail;
|
|
47
|
+
return 1 / fraction;
|
|
39
48
|
}
|
|
40
49
|
const A = [
|
|
41
50
|
3.3871328727963665,
|
|
@@ -133,6 +142,6 @@ function normalQuantile(p) {
|
|
|
133
142
|
const u = (normalCdf(x) - p) * SQRT_2PI / halfSquareExp(-x);
|
|
134
143
|
return x - u / (1 + x * u / 2);
|
|
135
144
|
}
|
|
136
|
-
export { normalCdf, normalQuantile };
|
|
145
|
+
export { millsRatio, normalCdf, normalQuantile };
|
|
137
146
|
|
|
138
147
|
//# sourceMappingURL=normal.js.map
|
package/build/normal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normal.js","names":[],"sources":["../src/normal.ts"],"sourcesContent":["// The standard normal law: its cumulative distribution `normalCdf` and its quantile `normalQuantile`. This file is the\n// one home of the two functions. `@mark1russell7/rigor-statistics` exports them from here. The final home is the\n// probability layer, and a move to that layer keeps these exports.\n\nconst SQRT_2PI = Math.sqrt(2 * Math.PI);\n\n/**\n * This function gives `e^(-z^2/2)` for a `z` of 0 or more, with its relative digits for a large `z`. The rounding of\n * `z^2` costs `z^2/2` ulps, so the function splits `z` into `hi + lo`. `hi` has 4 bits after the point, so `hi^2` is\n * exact for each `z` below 2^20.\n */\nfunction halfSquareExp(z: number): number {\n const hi = Math.round(z * 16) / 16;\n const lo = z - hi;\n return Math.exp((-hi * hi) / 2) * Math.exp((-lo * (z + hi)) / 2);\n}\n\n/**\n * This function gives the standard normal cumulative distribution, `P(Z <= x)`. Below 0.75 in size, `x` uses the\n * series of `0.5 + phi(x) * (x + x^3/3 + x^5/15 + ...)`. The tails use the continued fraction of the Mills ratio, so a\n * lower tail has no cancellation. On 2026-11-09 the error was at most 4.4 ulps from -38 to 9, against `mpmath`.\n * `normalCdf(-37)` is approximately 5.7e-300, not 0.\n *\n * @param x - A number. `-Infinity` gives 0 and `Infinity` gives 1. `NaN` throws a `RangeError`.\n */\nexport function normalCdf(x: number): number {\n if (Number.isNaN(x)) throw new RangeError(\"normalCdf: x is NaN.\");\n const z = Math.abs(x);\n // Below -38.5, the value is below the smallest double.\n if (z > 40) return x > 0 ? 1 : 0;\n const density = halfSquareExp(z) / SQRT_2PI;\n if (z < 0.75) {\n let term = x;\n let sum = x;\n for (let k = 1; k < 200 && Math.abs(term) > Number.EPSILON * Math.abs(sum) * 0.01; k++) {\n term *= (x * x) / (2 * k + 1);\n sum += term;\n }\n return 0.5 + density * sum;\n }\n
|
|
1
|
+
{"version":3,"file":"normal.js","names":[],"sources":["../src/normal.ts"],"sourcesContent":["// The standard normal law: its cumulative distribution `normalCdf` and its quantile `normalQuantile`. This file is the\n// one home of the two functions. `@mark1russell7/rigor-statistics` exports them from here. The final home is the\n// probability layer, and a move to that layer keeps these exports.\n\nconst SQRT_2PI = Math.sqrt(2 * Math.PI);\n\n/**\n * This function gives `e^(-z^2/2)` for a `z` of 0 or more, with its relative digits for a large `z`. The rounding of\n * `z^2` costs `z^2/2` ulps, so the function splits `z` into `hi + lo`. `hi` has 4 bits after the point, so `hi^2` is\n * exact for each `z` below 2^20.\n */\nfunction halfSquareExp(z: number): number {\n const hi = Math.round(z * 16) / 16;\n const lo = z - hi;\n return Math.exp((-hi * hi) / 2) * Math.exp((-lo * (z + hi)) / 2);\n}\n\n/**\n * This function gives the standard normal cumulative distribution, `P(Z <= x)`. Below 0.75 in size, `x` uses the\n * series of `0.5 + phi(x) * (x + x^3/3 + x^5/15 + ...)`. The tails use the continued fraction of the Mills ratio, so a\n * lower tail has no cancellation. On 2026-11-09 the error was at most 4.4 ulps from -38 to 9, against `mpmath`.\n * `normalCdf(-37)` is approximately 5.7e-300, not 0.\n *\n * @param x - A number. `-Infinity` gives 0 and `Infinity` gives 1. `NaN` throws a `RangeError`.\n */\nexport function normalCdf(x: number): number {\n if (Number.isNaN(x)) throw new RangeError(\"normalCdf: x is NaN.\");\n const z = Math.abs(x);\n // Below -38.5, the value is below the smallest double.\n if (z > 40) return x > 0 ? 1 : 0;\n const density = halfSquareExp(z) / SQRT_2PI;\n if (z < 0.75) {\n let term = x;\n let sum = x;\n for (let k = 1; k < 200 && Math.abs(term) > Number.EPSILON * Math.abs(sum) * 0.01; k++) {\n term *= (x * x) / (2 * k + 1);\n sum += term;\n }\n return 0.5 + density * sum;\n }\n const tail = density * millsRatio(z);\n return x > 0 ? 1 - tail : tail;\n}\n\n/**\n * This function gives the Mills ratio `P(Z > z) / phi(z)` for a `z` of 0 or more. It does not underflow for a large `z`.\n * From 0.75, it is the continued fraction `1 / (z + 1 / (z + 2 / (z + ...)))`. The fraction has `1000 / z^2 + 50` terms. This count converges to the last bit.\n * Below 0.75, it is `normalCdf(-z) / phi(z)`.\n */\nexport function millsRatio(z: number): number {\n if (z < 0.75) return normalCdf(-z) / (halfSquareExp(z) / SQRT_2PI);\n let fraction = z;\n for (let k = Math.ceil(1000 / (z * z)) + 50; k >= 1; k--) fraction = z + k / fraction;\n return 1 / fraction;\n}\n\n// The coefficients of PPND16 of Wichura (1988), algorithm AS 241. The first set is for |p - 0.5| <= 0.425.\nconst A = [\n 3.387132872796366608, 133.14166789178437745, 1971.5909503065514427, 13731.693765509461125, 45921.953931549871457,\n 67265.770927008700853, 33430.575583588128105, 2509.0809287301226727,\n];\nconst B = [\n 1, 42.313330701600911252, 687.1870074920579083, 5394.1960214247511077, 21213.794301586595867, 39307.89580009271061,\n 28729.085735721942674, 5226.495278852854561,\n];\n// The second set is for a tail with r = sqrt(-ln(tail)) of 5 or less.\nconst C = [\n 1.42343711074968357734, 4.6303378461565452959, 5.7694972214606914055, 3.64784832476320460504, 1.27045825245236838258,\n 0.24178072517745061177, 0.0227238449892691845833, 7.7454501427834140764e-4,\n];\nconst D = [\n 1, 2.05319162663775882187, 1.6763848301838038494, 0.68976733498510000455, 0.14810397642748007459,\n 0.0151986665636164571966, 5.475938084995344946e-4, 1.05075007164441684324e-9,\n];\n// The third set is for a far tail, with r above 5.\nconst E = [\n 6.6579046435011037772, 5.4637849111641143699, 1.7848265399172913358, 0.29656057182850489123, 0.026532189526576123093,\n 0.0012426609473880784386, 2.71155556874348757815e-5, 2.01033439929228813265e-7,\n];\nconst F = [\n 1, 0.59983220655588793769, 0.13692988092273580531, 0.0148753612908506148525, 7.868691311456132591e-4,\n 1.8463183175100546818e-5, 1.4215117583164458887e-7, 2.04426310338993978564e-15,\n];\n\n/** This function gives the value of the polynomial `c[0] + c[1] x + ...`, by the rule of Horner. */\nfunction poly(c: readonly number[], x: number): number {\n let value = 0;\n for (let i = c.length - 1; i >= 0; i--) value = value * x + c[i]!;\n return value;\n}\n\n/**\n * This function gives the quantile of the standard normal law: the `x` with `normalCdf(x) = p`. It uses PPND16 of\n * Wichura (algorithm AS 241). Below 0.075, one step of Halley with `normalCdf` follows. Above 0.5, `p` uses the lower\n * tail at `1 - p`. This subtraction is exact. On 2026-11-09 the error was at most 1.2 ulps in the tails and 5.5 ulps\n * from 0.075 to 0.925, against `mpmath`.\n *\n * @param p - The share of the law below the result, from 0 to 1. A `p` of 0 gives `-Infinity` and 1 gives `Infinity`.\n */\nexport function normalQuantile(p: number): number {\n if (!(p >= 0 && p <= 1)) throw new RangeError(`normalQuantile: p is from 0 to 1, not ${p}.`);\n if (p === 0) return Number.NEGATIVE_INFINITY;\n if (p === 1) return Number.POSITIVE_INFINITY;\n if (p > 0.5) return -normalQuantile(1 - p);\n const q = p - 0.5;\n if (q >= -0.425) {\n const r = 0.180625 - q * q;\n return (q * poly(A, r)) / poly(B, r);\n }\n let r = Math.sqrt(-Math.log(p));\n let x: number;\n if (r <= 5) {\n r -= 1.6;\n x = -poly(C, r) / poly(D, r);\n } else {\n r -= 5;\n x = -poly(E, r) / poly(F, r);\n }\n // One step of Halley. `u` is the error of the cdf over the density.\n const u = ((normalCdf(x) - p) * SQRT_2PI) / halfSquareExp(-x);\n return x - u / (1 + (x * u) / 2);\n}\n"],"mappings":";AAIA,MAAM,WAAW,KAAK,KAAK,IAAI,KAAK,EAAE;;;;;;AAOtC,SAAS,cAAc,GAAmB;CACxC,MAAM,KAAK,KAAK,MAAM,IAAI,EAAE,IAAI;CAChC,MAAM,KAAK,IAAI;CACf,OAAO,KAAK,IAAK,CAAC,KAAK,KAAM,CAAC,IAAI,KAAK,IAAK,CAAC,MAAM,IAAI,MAAO,CAAC;AACjE;;;;;;;;;AAUA,SAAgB,UAAU,GAAmB;CAC3C,IAAI,OAAO,MAAM,CAAC,GAAG,MAAM,IAAI,WAAW,sBAAsB;CAChE,MAAM,IAAI,KAAK,IAAI,CAAC;CAEpB,IAAI,IAAI,IAAI,OAAO,IAAI,IAAI,IAAI;CAC/B,MAAM,UAAU,cAAc,CAAC,IAAI;CACnC,IAAI,IAAI,KAAM;EACZ,IAAI,OAAO;EACX,IAAI,MAAM;EACV,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK,IAAI,IAAI,IAAI,OAAO,UAAU,KAAK,IAAI,GAAG,IAAI,KAAM,KAAK;GACtF,QAAS,IAAI,KAAM,IAAI,IAAI;GAC3B,OAAO;EACT;EACA,OAAO,KAAM,UAAU;CACzB;CACA,MAAM,OAAO,UAAU,WAAW,CAAC;CACnC,OAAO,IAAI,IAAI,IAAI,OAAO;AAC5B;;;;;;AAOA,SA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AK,GAAG,CAAC;CACrC;CACA,IAAI,IAAI,KAAK,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC;CAC9B,IAAI;CACJ,IAAI,KAAK,GAAG;EACV,KAAK;EACL,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC;CAC7B,OAAO;EACL,KAAK;EACL,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC;CAC7B;CAEA,MAAM,KAAM,UAAU,CAAC,IAAI,KAAK,WAAY,cAAc,CAAC,CAAC;CAC5D,OAAO,IAAI,KAAK,IAAK,IAAI,IAAK;AAChC"}
|
package/build/quantiles.d.ts
CHANGED
|
@@ -34,10 +34,13 @@ export interface BetaShares {
|
|
|
34
34
|
* This function gives the shares of the beta law with the parameters `a` and `b` below and above `x`.
|
|
35
35
|
* - The caller gives `x` and `y = 1 - x`, each with its full digits. Thus a point near 1 keeps the digits of its distance from 1.
|
|
36
36
|
* For example, `df / (df + t^2)` and `t^2 / (df + t^2)` give both shares of the Student t law with no rounding near 1.
|
|
37
|
-
* - The
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* -
|
|
37
|
+
* - The methods are those of TOMS 708 (Didonato and Morris, 1992), as `pbeta` of R. Each method gives the smaller share with its relative digits.
|
|
38
|
+
* The other share is 1 less that share.
|
|
39
|
+
* - `lambda = a - (a + b) x` comes from the smaller of `x` and `y`, with an exact sum and product. Thus a large `a + b` keeps the digits of the point.
|
|
40
|
+
* - When `a` and `b` are above 100 and `|lambda|` is `0.03 min(a, b)` or less, the asymptotic expansion (`basym`) gives the share.
|
|
41
|
+
* - Elsewhere, the continued fraction (`bfrac`) gives the share. Its terms use `lambda`, so a point near 1 with a large parameter keeps its digits.
|
|
42
|
+
* - When `a` and `b` are 10 or more, the front factor (`brcomp`) uses `x / x0 - 1` with `x0 = a / (a + b)`. Elsewhere, it uses the logs of `x` and `y` and `logBeta`.
|
|
43
|
+
* - The tests hold the relative error below 1e-12 against exact references, with parameters up to 1e13.
|
|
41
44
|
*
|
|
42
45
|
* @throws RangeError when `x` or `y` is not from 0 to 1, or when `x + y` is not 1
|
|
43
46
|
*/
|
package/build/quantiles.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quantiles.d.ts","names":[],"sources":["../src/quantiles.ts"],"mappings":";;;iBAOiB;;WAEN,QAAQ;;WAER;;EAET,IAAI;;EAEJ,SAAS;;;;;;wBAYK,SAAS;;;;;;;wBA6BT,QAAQ,WAAW;;
|
|
1
|
+
{"version":3,"file":"quantiles.d.ts","names":[],"sources":["../src/quantiles.ts"],"mappings":";;;iBAOiB;;WAEN,QAAQ;;WAER;;EAET,IAAI;;EAEJ,SAAS;;;;;;wBAYK,SAAS;;;;;;;wBA6BT,QAAQ,WAAW;;iBA8LlB;;WAEN;;WAEA;;;;;;;;;;;;;;;;wBAiBK,WAAW,WAAW,WAAW,WAAW,YAAY;;wBA6BxD,QAAQ,WAAW,WAAW;;;;;;wBA6C9B,eAAe,MAAM,kBAAkB,GAAG,SAAS"}
|
package/build/quantiles.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { normalCdf, normalQuantile } from "./normal.js";
|
|
1
|
+
import { millsRatio, normalCdf, normalQuantile } from "./normal.js";
|
|
2
2
|
// src/quantiles.ts
|
|
3
3
|
const LANCZOS = [
|
|
4
4
|
.9999999999998099,
|
|
@@ -64,43 +64,184 @@ function logBeta(a, b) {
|
|
|
64
64
|
}
|
|
65
65
|
return logGamma(p) + logGamma(q) - logGamma(p + q);
|
|
66
66
|
}
|
|
67
|
-
/**
|
|
68
|
-
function
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
67
|
+
/** The two halves of the split of Veltkamp: `hi + lo` is `value`, and each half has 26 bits at most. */
|
|
68
|
+
function split(value) {
|
|
69
|
+
const scaled = 134217729 * value;
|
|
70
|
+
const hi = scaled - (scaled - value);
|
|
71
|
+
return [hi, value - hi];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* This function gives `a - (a + b) p` with no rounding of the sum and the product before the last subtraction (Dekker).
|
|
75
|
+
* One ulp of `(a + b) p` is one ulp of `p` in the point. Near the mean, the shares of a law with a large `a + b` change by a large factor for each ulp of the point.
|
|
76
|
+
* Thus the exact sum and product keep the digits that the point gives. Above 1e290, the split overflows, and the function gives the plain value.
|
|
77
|
+
*/
|
|
78
|
+
function centered(a, b, p) {
|
|
79
|
+
const sum = a + b;
|
|
80
|
+
if (!(sum < 1e290)) return a - sum * p;
|
|
81
|
+
const sumError = a >= b ? b - (sum - a) : a - (sum - b);
|
|
82
|
+
const product = sum * p;
|
|
83
|
+
const [sumHi, sumLo] = split(sum);
|
|
84
|
+
const [pHi, pLo] = split(p);
|
|
85
|
+
const productError = sumHi * pHi - product + sumHi * pLo + sumLo * pHi + sumLo * pLo;
|
|
86
|
+
return a - product - productError - sumError * p;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* This function gives `x - log(1 + x)` for `x` above -1 (`rlog1` of TOMS 708), with its relative digits also near 0.
|
|
90
|
+
* From -0.6 to 0.6, it uses the series of `log((1 + r) / (1 - r))` with `r = x / (2 + x)`: `x - log(1 + x) = r x - 2 (r^3 / 3 + r^5 / 5 + ...)`.
|
|
91
|
+
*/
|
|
92
|
+
function rlog1(x) {
|
|
93
|
+
if (Math.abs(x) > .6) return x - Math.log1p(x);
|
|
94
|
+
const r = x / (2 + x);
|
|
95
|
+
const square = r * r;
|
|
96
|
+
let power = r * square;
|
|
97
|
+
let sum = 0;
|
|
98
|
+
for (let k = 3; k < 200; k += 2) {
|
|
99
|
+
const term = power / k;
|
|
100
|
+
sum += term;
|
|
101
|
+
if (Math.abs(term) <= 1e-17 * Math.abs(sum)) break;
|
|
102
|
+
power *= square;
|
|
103
|
+
}
|
|
104
|
+
return r * x - 2 * sum;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* This function gives the front factor `x^a y^b / B(a, b)` of the shares, for `a` and `b` of 10 or more (`brcomp` of TOMS 708).
|
|
108
|
+
* - The factor is `sqrt(b x0 / (2 pi)) exp(-(a rlog1(x / x0 - 1) + b rlog1(y / y0 - 1)) - c)`, with `x0 = a / (a + b)`, `y0 = b / (a + b)` and the Stirling corrections `c`.
|
|
109
|
+
* - `x / x0 - 1` is `-lambda / a` and `y / y0 - 1` is `lambda / b`. Thus no term of the exponent has the size of `a` or `b` near the mean.
|
|
110
|
+
* The sum of logs loses the digits of `a` times the epsilon there.
|
|
111
|
+
*/
|
|
112
|
+
function largeFront(a, b, x, y, lambda) {
|
|
113
|
+
const h = a < b ? a / b : b / a;
|
|
114
|
+
const x0 = a < b ? h / (1 + h) : 1 / (1 + h);
|
|
115
|
+
const y0 = a < b ? 1 / (1 + h) : h / (1 + h);
|
|
116
|
+
let e = -lambda / a;
|
|
117
|
+
const u = Math.abs(e) > .6 ? e - Math.log(x / x0) : rlog1(e);
|
|
118
|
+
e = lambda / b;
|
|
119
|
+
const v = Math.abs(e) > .6 ? e - Math.log(y / y0) : rlog1(e);
|
|
120
|
+
const correction = stirlingCorrection(a) + stirlingCorrection(b) - stirlingCorrection(a + b);
|
|
121
|
+
return Math.sqrt(b * x0 / (2 * Math.PI)) * Math.exp(-(a * u + b * v) - correction);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* This function gives `I_x(a, b)` by the asymptotic expansion for a large `a` and `b` (`basym` of TOMS 708), with 20 terms at most.
|
|
125
|
+
* - The caller gives `lambda = a - (a + b) x` of 0 or more, and uses the function when `a` and `b` are above 100 and `lambda` is `0.03 min(a, b)` or less.
|
|
126
|
+
* - Near the mean, the count of terms of the continued fraction grows with `sqrt(min(a, b))`. It does not converge in 10000 terms at 1e12.
|
|
127
|
+
* - `exp(z^2) erfc(z)` of TOMS 708 is `2 M(sqrt(2) z) / sqrt(2 pi)`, with the Mills ratio `M`. Thus no term underflows before the result.
|
|
128
|
+
*/
|
|
129
|
+
function asymptoticBelow(a, b, lambda) {
|
|
130
|
+
const e0 = 2 / Math.sqrt(Math.PI);
|
|
131
|
+
const e1 = 2 ** -1.5;
|
|
132
|
+
const f = a * rlog1(-lambda / a) + b * rlog1(lambda / b);
|
|
133
|
+
const t = Math.exp(-f);
|
|
134
|
+
if (t === 0) return 0;
|
|
135
|
+
const z = Math.SQRT2 * Math.sqrt(f);
|
|
136
|
+
const z2 = f + f;
|
|
137
|
+
const h = a < b ? a / b : b / a;
|
|
138
|
+
const r0 = 1 / (h + 1);
|
|
139
|
+
const r1 = (b - a) / Math.max(a, b);
|
|
140
|
+
const w0 = 1 / Math.sqrt(Math.min(a, b) * (h + 1));
|
|
141
|
+
const size = 21;
|
|
142
|
+
const a0 = new Array(size).fill(0);
|
|
143
|
+
const b0 = new Array(size).fill(0);
|
|
144
|
+
const c = new Array(size).fill(0);
|
|
145
|
+
const d = new Array(size).fill(0);
|
|
146
|
+
const first = 2 / 3 * r1;
|
|
147
|
+
a0[0] = first;
|
|
148
|
+
c[0] = -.5 * first;
|
|
149
|
+
d[0] = .5 * first;
|
|
150
|
+
let j0 = e1 * millsRatio(z);
|
|
151
|
+
let j1 = e1;
|
|
152
|
+
let sum = j0 + .5 * first * w0 * j1;
|
|
153
|
+
let s = 1;
|
|
154
|
+
let hn = 1;
|
|
155
|
+
let w = w0;
|
|
156
|
+
let znm1 = z;
|
|
157
|
+
let zn = z2;
|
|
158
|
+
for (let n = 2; n < size; n += 2) {
|
|
159
|
+
hn *= h * h;
|
|
160
|
+
a0[n - 1] = 2 * r0 * (h * hn + 1) / (n + 2);
|
|
161
|
+
s += hn;
|
|
162
|
+
a0[n] = 2 * r1 * s / (n + 3);
|
|
163
|
+
for (let i = n; i <= n + 1; i++) {
|
|
164
|
+
const r = -.5 * (i + 1);
|
|
165
|
+
b0[0] = r * a0[0];
|
|
166
|
+
for (let m = 2; m <= i; m++) {
|
|
167
|
+
let bsum = 0;
|
|
168
|
+
for (let j = 1; j < m; j++) bsum += (j * r - (m - j)) * a0[j - 1] * b0[m - j - 1];
|
|
169
|
+
b0[m - 1] = r * a0[m - 1] + bsum / m;
|
|
170
|
+
}
|
|
171
|
+
c[i - 1] = b0[i - 1] / (i + 1);
|
|
172
|
+
let dsum = 0;
|
|
173
|
+
for (let j = 1; j < i; j++) dsum += d[i - j - 1] * c[j - 1];
|
|
174
|
+
d[i - 1] = -(dsum + c[i - 1]);
|
|
175
|
+
}
|
|
176
|
+
j0 = e1 * znm1 + (n - 1) * j0;
|
|
177
|
+
j1 = e1 * zn + n * j1;
|
|
178
|
+
znm1 *= z2;
|
|
179
|
+
zn *= z2;
|
|
180
|
+
w *= w0;
|
|
181
|
+
const t0 = d[n - 1] * w * j0;
|
|
182
|
+
w *= w0;
|
|
183
|
+
const t1 = d[n] * w * j1;
|
|
184
|
+
sum += t0 + t1;
|
|
185
|
+
if (Math.abs(t0) + Math.abs(t1) <= 1e-16 * sum) break;
|
|
186
|
+
}
|
|
187
|
+
const correction = stirlingCorrection(a) + stirlingCorrection(b) - stirlingCorrection(a + b);
|
|
188
|
+
return e0 * t * Math.exp(-correction) * sum;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* This function gives the continued fraction of the incomplete beta function in the form of `bfrac` of TOMS 708 (Didonato and Morris, 1992).
|
|
192
|
+
* `I_x(a, b)` is the front factor `x^a y^b / B(a, b)` times the result.
|
|
193
|
+
* - The caller gives `lambda = a - (a + b) x`. It computes `lambda` from the smaller of `x` and `y`, so `lambda` has no cancellation.
|
|
194
|
+
* - The terms of the fraction use `lambda`, `x` and `y`, and no term subtracts two numbers near 1.
|
|
195
|
+
* The fraction of Lentz has the first term `1 - (a + b) x / (a + 1)`. With `x` near 1 and a large `a`, it loses the digits of `b` times the epsilon.
|
|
196
|
+
*/
|
|
197
|
+
function betaFraction(a, b, x, y, lambda) {
|
|
198
|
+
const c = lambda + 1;
|
|
199
|
+
const c0 = b / a;
|
|
200
|
+
const c1 = 1 / a + 1;
|
|
201
|
+
const yp1 = y + 1;
|
|
202
|
+
let p = 1;
|
|
203
|
+
let s = a + 1;
|
|
204
|
+
let an = 0;
|
|
205
|
+
let bn = 1;
|
|
206
|
+
let anp1 = 1;
|
|
207
|
+
let bnp1 = c / c1;
|
|
208
|
+
let r = c1 / c;
|
|
209
|
+
for (let n = 1; n <= 1e4; n++) {
|
|
210
|
+
let t = n / a;
|
|
211
|
+
const w = n * (b - n) * x;
|
|
212
|
+
let e = a / s;
|
|
213
|
+
const alpha = p * (p + c0) * e * e * (w * x);
|
|
214
|
+
e = (t + 1) / (c1 + t + t);
|
|
215
|
+
const beta = n + w / s + e * (c + n * yp1);
|
|
216
|
+
p = t + 1;
|
|
217
|
+
s += 2;
|
|
218
|
+
t = alpha * an + beta * anp1;
|
|
219
|
+
an = anp1;
|
|
220
|
+
anp1 = t;
|
|
221
|
+
t = alpha * bn + beta * bnp1;
|
|
222
|
+
bn = bnp1;
|
|
223
|
+
bnp1 = t;
|
|
224
|
+
const r0 = r;
|
|
225
|
+
r = anp1 / bnp1;
|
|
226
|
+
if (Math.abs(r - r0) <= 1e-16 * r) break;
|
|
227
|
+
an /= bnp1;
|
|
228
|
+
bn /= bnp1;
|
|
229
|
+
anp1 = r;
|
|
230
|
+
bnp1 = 1;
|
|
93
231
|
}
|
|
94
|
-
return
|
|
232
|
+
return r;
|
|
95
233
|
}
|
|
96
234
|
/**
|
|
97
235
|
* This function gives the shares of the beta law with the parameters `a` and `b` below and above `x`.
|
|
98
236
|
* - The caller gives `x` and `y = 1 - x`, each with its full digits. Thus a point near 1 keeps the digits of its distance from 1.
|
|
99
237
|
* For example, `df / (df + t^2)` and `t^2 / (df + t^2)` give both shares of the Student t law with no rounding near 1.
|
|
100
|
-
* - The
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* -
|
|
238
|
+
* - The methods are those of TOMS 708 (Didonato and Morris, 1992), as `pbeta` of R. Each method gives the smaller share with its relative digits.
|
|
239
|
+
* The other share is 1 less that share.
|
|
240
|
+
* - `lambda = a - (a + b) x` comes from the smaller of `x` and `y`, with an exact sum and product. Thus a large `a + b` keeps the digits of the point.
|
|
241
|
+
* - When `a` and `b` are above 100 and `|lambda|` is `0.03 min(a, b)` or less, the asymptotic expansion (`basym`) gives the share.
|
|
242
|
+
* - Elsewhere, the continued fraction (`bfrac`) gives the share. Its terms use `lambda`, so a point near 1 with a large parameter keeps its digits.
|
|
243
|
+
* - When `a` and `b` are 10 or more, the front factor (`brcomp`) uses `x / x0 - 1` with `x0 = a / (a + b)`. Elsewhere, it uses the logs of `x` and `y` and `logBeta`.
|
|
244
|
+
* - The tests hold the relative error below 1e-12 against exact references, with parameters up to 1e13.
|
|
104
245
|
*
|
|
105
246
|
* @throws RangeError when `x` or `y` is not from 0 to 1, or when `x + y` is not 1
|
|
106
247
|
*/
|
|
@@ -114,17 +255,33 @@ function betaShares(x, y, a, b) {
|
|
|
114
255
|
below: 1,
|
|
115
256
|
above: 0
|
|
116
257
|
};
|
|
258
|
+
const lambda = x <= y ? centered(a, b, x) : -centered(b, a, y);
|
|
117
259
|
const logX = x < .5 ? Math.log(x) : Math.log1p(-y);
|
|
118
260
|
const logY = y < .5 ? Math.log(y) : Math.log1p(-x);
|
|
119
|
-
const
|
|
261
|
+
const least = Math.min(a, b);
|
|
262
|
+
if (least > 100 && Math.abs(lambda) <= .03 * least) {
|
|
263
|
+
if (lambda >= 0) {
|
|
264
|
+
const below = asymptoticBelow(a, b, lambda);
|
|
265
|
+
return {
|
|
266
|
+
below,
|
|
267
|
+
above: 1 - below
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const above = asymptoticBelow(b, a, -lambda);
|
|
271
|
+
return {
|
|
272
|
+
below: 1 - above,
|
|
273
|
+
above
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
const front = least >= 10 ? largeFront(a, b, x, y, lambda) : Math.exp(a * logX + b * logY - logBeta(a, b));
|
|
120
277
|
if (x < (a + 1) / (a + b + 2)) {
|
|
121
|
-
const below = front * betaFraction(a, b, x
|
|
278
|
+
const below = front * betaFraction(a, b, x, y, lambda);
|
|
122
279
|
return {
|
|
123
280
|
below,
|
|
124
281
|
above: 1 - below
|
|
125
282
|
};
|
|
126
283
|
}
|
|
127
|
-
const above = front * betaFraction(b, a, y
|
|
284
|
+
const above = front * betaFraction(b, a, y, x, -lambda);
|
|
128
285
|
return {
|
|
129
286
|
below: 1 - above,
|
|
130
287
|
above
|
package/build/quantiles.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quantiles.js","names":[],"sources":["../src/quantiles.ts"],"sourcesContent":["// The cumulative distribution and the quantile of each family of `distributions.ts`. A design draws a uniform number u\n// in each stratum and gives the quantile of u. So the Latin hypercube and the stratified designs work with each family.\nimport type { DistributionSpec, Param } from \"./distributions.ts\";\nimport { normalCdf, normalQuantile } from \"./normal.ts\";\nimport type { Random } from \"./random.ts\";\n\n/** One point of a family: its parameters are fixed. */\nexport interface Distribution {\n /** The family of the point. */\n readonly family: DistributionSpec[\"family\"];\n /** True when the values are a finite set, as in `zipf` and `recorded`. */\n readonly discrete: boolean;\n /** This function gives the probability of a value at or below `x`. */\n cdf(x: number): number;\n /** This function gives the smallest value whose `cdf` is `u` or more. `u` is from 0 to 1. */\n quantile(u: number): number;\n}\n\nconst LANCZOS = [\n 0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012,\n 9.9843695780195716e-6, 1.5056327351493116e-7,\n];\n\n/**\n * This function gives the logarithm of the gamma function for `x` above 0 (Lanczos, g 7, 9 terms).\n * The relative error is below 1e-14, away from the zeros at 1 and 2. Below 0.5, the reflection formula gives the value.\n */\nexport function logGamma(x: number): number {\n if (x < 0.5) return Math.log(Math.PI / Math.sin(Math.PI * x)) - logGamma(1 - x);\n const y = x - 1;\n let sum = LANCZOS[0]!;\n for (let i = 1; i < 9; i++) sum += LANCZOS[i]! / (y + i);\n const t = y + 7.5;\n return 0.5 * Math.log(2 * Math.PI) + (y + 0.5) * Math.log(t) - t + Math.log(sum);\n}\n\n/** The coefficients of the Stirling series of the log of the gamma function: B(2k) / (2k (2k - 1)) for k from 1 to 8. */\nconst STIRLING = [1 / 12, -1 / 360, 1 / 1260, -1 / 1680, 1 / 1188, -691 / 360360, 1 / 156, -3617 / 122400];\n\n/**\n * This function gives the Stirling correction: `logGamma(x)` less `(x - 0.5) log x - x + log(2 pi) / 2`, for `x` of 10 or more.\n * At 10, the first term that the series omits is below 2e-18.\n */\nfunction stirlingCorrection(x: number): number {\n const square = 1 / (x * x);\n let sum = 0;\n for (let i = STIRLING.length - 1; i >= 0; i--) sum = sum * square + STIRLING[i]!;\n return sum / x;\n}\n\n/**\n * This function gives the logarithm of the beta function, `logGamma(a) + logGamma(b) - logGamma(a + b)`, for `a` and `b` above 0.\n * - When an argument is 10 or more, the Stirling series gives the difference of the large terms (as `lbeta` of R).\n * Thus the value is accurate also when `a + b` is near 1e13. There, the sum of `logGamma` loses all digits of the difference.\n * - When both arguments are below 10, the value is the sum of `logGamma`.\n */\nexport function logBeta(a: number, b: number): number {\n const p = Math.min(a, b);\n const q = Math.max(a, b);\n const share = p / (p + q);\n if (p >= 10) {\n const correction = stirlingCorrection(p) + stirlingCorrection(q) - stirlingCorrection(p + q);\n return -0.5 * Math.log(q) + 0.5 * Math.log(2 * Math.PI) + correction + (p - 0.5) * Math.log(share) + q * Math.log1p(-share);\n }\n if (q >= 10) {\n const correction = stirlingCorrection(q) - stirlingCorrection(p + q);\n return logGamma(p) + correction + p - p * Math.log(p + q) + (q - 0.5) * Math.log1p(-share);\n }\n return logGamma(p) + logGamma(q) - logGamma(p + q);\n}\n\n/** This function gives the continued fraction of the incomplete beta function (the method of Lentz). */\nfunction betaFraction(a: number, b: number, x: number): number {\n const tiny = 1e-300;\n let c = 1;\n let d = 1 - ((a + b) * x) / (a + 1);\n if (Math.abs(d) < tiny) d = tiny;\n d = 1 / d;\n let h = d;\n for (let m = 1; m <= 1000; m++) {\n const m2 = 2 * m;\n let aa = (m * (b - m) * x) / ((a + m2 - 1) * (a + m2));\n d = 1 + aa * d;\n if (Math.abs(d) < tiny) d = tiny;\n c = 1 + aa / c;\n if (Math.abs(c) < tiny) c = tiny;\n d = 1 / d;\n h *= d * c;\n aa = (-(a + m) * (a + b + m) * x) / ((a + m2) * (a + m2 + 1));\n d = 1 + aa * d;\n if (Math.abs(d) < tiny) d = tiny;\n c = 1 + aa / c;\n if (Math.abs(c) < tiny) c = tiny;\n d = 1 / d;\n const step = d * c;\n h *= step;\n if (Math.abs(step - 1) < 1e-16) break;\n }\n return h;\n}\n\n/** The shares of a beta law below and above a point. */\nexport interface BetaShares {\n /** The share of the law below the point: the regularized incomplete beta function. */\n readonly below: number;\n /** The share of the law above the point. */\n readonly above: number;\n}\n\n/**\n * This function gives the shares of the beta law with the parameters `a` and `b` below and above `x`.\n * - The caller gives `x` and `y = 1 - x`, each with its full digits. Thus a point near 1 keeps the digits of its distance from 1.\n * For example, `df / (df + t^2)` and `t^2 / (df + t^2)` give both shares of the Student t law with no rounding near 1.\n * - The continued fraction gives the smaller share with its relative digits. The other share is 1 less that share.\n * - The fraction loses digits when its point is near 1 and its first parameter is large. This is the share above `x` when `x` is above `(a + 1) / (a + b + 2)`.\n * The tests hold the relative error of that share below `2e-16 * b`, for `b` from 5e5 to 5e8.\n * - The logs of the front factor use the smaller of `x` and `y`, and the factor uses `logBeta`. Thus a parameter near 1e13 keeps the digits of the shares.\n *\n * @throws RangeError when `x` or `y` is not from 0 to 1, or when `x + y` is not 1\n */\nexport function betaShares(x: number, y: number, a: number, b: number): BetaShares {\n if (!(x >= 0 && x <= 1 && y >= 0 && y <= 1 && Math.abs(x + y - 1) <= 1e-12)) {\n throw new RangeError(`betaShares: x and y are from 0 to 1 and their sum is 1, not ${x} and ${y}.`);\n }\n if (x === 0) return { below: 0, above: 1 };\n if (y === 0) return { below: 1, above: 0 };\n const logX = x < 0.5 ? Math.log(x) : Math.log1p(-y);\n const logY = y < 0.5 ? Math.log(y) : Math.log1p(-x);\n const front = Math.exp(a * logX + b * logY - logBeta(a, b));\n if (x < (a + 1) / (a + b + 2)) {\n const below = (front * betaFraction(a, b, x)) / a;\n return { below, above: 1 - below };\n }\n const above = (front * betaFraction(b, a, y)) / b;\n return { below: 1 - above, above };\n}\n\n/** This function gives the regularized incomplete beta function: the beta cumulative distribution at `x` (`betaShares` with `1 - x`). */\nexport function betaCdf(x: number, a: number, b: number): number {\n if (Number.isNaN(x)) return Number.NaN;\n if (x <= 0) return 0;\n if (x >= 1) return 1;\n return betaShares(x, 1 - x, a, b).below;\n}\n\n/**\n * This function gives the smallest `x` from `lo` to `hi` whose `cdf` is `u` or more, by bisection.\n * The caller makes sure that `cdf(lo)` is `u` or less and `cdf(hi)` is `u` or more.\n */\nfunction bisect(cdf: (x: number) => number, u: number, lo: number, hi: number): number {\n for (let i = 0; i < 2000 && lo < hi; i++) {\n const mid = lo + (hi - lo) / 2;\n if (mid <= lo || mid >= hi) break;\n if (cdf(mid) >= u) hi = mid;\n else lo = mid;\n }\n return cdf(lo) >= u ? lo : hi;\n}\n\n/** This function examines a parameter, as `sampler` does. Both ends of a range must pass `valid`. */\nfunction checked(family: string, name: string, param: Param, valid: (value: number) => boolean, rule: string): void {\n const ends = typeof param === \"number\" ? [param] : param;\n for (const value of ends) {\n if (!Number.isFinite(value) || !valid(value)) throw new RangeError(`${family}: ${name} is ${value}, and it must be ${rule}.`);\n }\n if (typeof param !== \"number\" && param[0] > param[1]) throw new RangeError(`${family}: the range of ${name} starts above its end.`);\n}\n\n/** This function gives the value of a parameter: the number, or one draw from the range. */\nfunction valueOf(param: Param, r: Random): number {\n return typeof param === \"number\" ? param : r.range(param[0], param[1]);\n}\n\n/** This function examines `u` of a quantile. */\nfunction unit(u: number): void {\n if (!(u >= 0 && u <= 1)) throw new RangeError(`quantile: u is ${u}, and it must be from 0 to 1.`);\n}\n\n/**\n * This function gives one point of a family, with its `cdf` and its `quantile`. It draws the parameters from `r` in\n * the order of `sampler`. Thus the same seed gives the same point of the family as `sampler`.\n * The values use `Math.exp`, `Math.log` or `Math.pow` when `dependsOnLibm` of the family is true.\n */\nexport function distributionOf(spec: DistributionSpec, r: Random): Distribution {\n switch (spec.family) {\n case \"lognormal\": {\n checked(\"lognormal\", \"sigma\", spec.sigma, (v) => v >= 0, \"0 or more\");\n checked(\"lognormal\", \"mu\", spec.mu, () => true, \"finite\");\n const mu = valueOf(spec.mu, r);\n const sigma = valueOf(spec.sigma, r);\n const median = Math.exp(mu);\n return {\n family: \"lognormal\",\n discrete: false,\n cdf: (x) => (Number.isNaN(x) ? Number.NaN : x <= 0 ? 0 : sigma === 0 ? (x >= median ? 1 : 0) : normalCdf((Math.log(x) - mu) / sigma)),\n quantile: (u) => {\n unit(u);\n return sigma === 0 ? median : Math.exp(mu + sigma * normalQuantile(u));\n },\n };\n }\n case \"pareto\": {\n checked(\"pareto\", \"scale\", spec.scale, (v) => v > 0, \"above 0\");\n checked(\"pareto\", \"shape\", spec.shape, (v) => v > 0, \"above 0\");\n const scale = valueOf(spec.scale, r);\n const shape = valueOf(spec.shape, r);\n return {\n family: \"pareto\",\n discrete: false,\n cdf: (x) => (x < scale ? 0 : 1 - Math.pow(scale / x, shape)),\n quantile: (u) => {\n unit(u);\n return scale / Math.pow(1 - u, 1 / shape);\n },\n };\n }\n case \"zipf\": {\n if (!Number.isSafeInteger(spec.n) || spec.n < 1) throw new RangeError(`zipf: n is ${spec.n}, and it must be a whole number of 1 or more.`);\n checked(\"zipf\", \"exponent\", spec.exponent, (v) => v >= 0, \"0 or more\");\n const exponent = valueOf(spec.exponent, r);\n const totals: number[] = [];\n let total = 0;\n for (let rank = 1; rank <= spec.n; rank++) {\n total += Math.pow(rank, -exponent);\n totals.push(total);\n }\n return {\n family: \"zipf\",\n discrete: true,\n cdf: (x) => (x < 1 ? 0 : x >= spec.n ? 1 : totals[Math.floor(x) - 1]! / total),\n quantile: (u) => {\n unit(u);\n const at = u * total;\n let lo = 0;\n let hi = spec.n - 1;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n if (totals[mid]! >= at) hi = mid;\n else lo = mid + 1;\n }\n return lo + 1;\n },\n };\n }\n case \"beta\": {\n checked(\"beta\", \"a\", spec.a, (v) => v > 0, \"above 0\");\n checked(\"beta\", \"b\", spec.b, (v) => v > 0, \"above 0\");\n const a = valueOf(spec.a, r);\n const b = valueOf(spec.b, r);\n const cdf = (x: number): number => betaCdf(x, a, b);\n return {\n family: \"beta\",\n discrete: false,\n cdf,\n quantile: (u) => {\n unit(u);\n return u === 0 ? 0 : u === 1 ? 1 : bisect(cdf, u, 0, 1);\n },\n };\n }\n case \"mixture\": {\n if (spec.parts.length === 0) throw new RangeError(\"mixture: the list of the parts is empty.\");\n for (const { weight } of spec.parts) checked(\"mixture\", \"weight\", weight, (v) => v > 0, \"above 0\");\n const parts = spec.parts.map(({ part }) => distributionOf(part, r));\n const sum = spec.parts.reduce((s, { weight }) => s + weight, 0);\n const weights = spec.parts.map(({ weight }) => weight / sum);\n const cdf = (x: number): number => Math.min(1, parts.reduce((s, part, i) => s + weights[i]! * part.cdf(x), 0));\n return {\n family: \"mixture\",\n discrete: parts.every((part) => part.discrete),\n cdf,\n quantile: (u) => {\n unit(u);\n const ends = parts.map((part) => part.quantile(u));\n const lo = Math.min(...ends);\n const hi = Math.max(...ends);\n return lo === hi || !Number.isFinite(hi) ? hi : bisect(cdf, u, lo, hi);\n },\n };\n }\n case \"recorded\": {\n if (spec.values.length === 0) throw new RangeError(\"recorded: the list of the values is empty.\");\n for (const value of spec.values) checked(\"recorded\", \"value\", value, () => true, \"finite\");\n const sorted = [...spec.values].sort((x, y) => x - y);\n const n = sorted.length;\n return {\n family: \"recorded\",\n discrete: true,\n cdf: (x) => {\n let lo = 0;\n let hi = n;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n if (sorted[mid]! <= x) lo = mid + 1;\n else hi = mid;\n }\n return lo / n;\n },\n quantile: (u) => {\n unit(u);\n return sorted[Math.max(0, Math.ceil(u * n) - 1)]!;\n },\n };\n }\n }\n}\n"],"mappings":";;AAkBA,MAAM,UAAU;CACd;CAAqB;CAAmB;CAAqB;CAAoB;CAAqB;CAAoB;CAC1H;CAAuB;AACzB;;;;;AAMA,SAAgB,SAAS,GAAmB;CAC1C,IAAI,IAAI,IAAK,OAAO,KAAK,IAAI,KAAK,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,SAAS,IAAI,CAAC;CAC9E,MAAM,IAAI,IAAI;CACd,IAAI,MAAM,QAAQ;CAClB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,OAAO,QAAQ,MAAO,IAAI;CACtD,MAAM,IAAI,IAAI;CACd,OAAO,KAAM,KAAK,IAAI,IAAI,KAAK,EAAE,KAAK,IAAI,MAAO,KAAK,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,GAAG;AACjF;;AAGA,MAAM,WAAW;CAAC,IAAI;CAAI,KAAK;CAAK,IAAI;CAAM,KAAK;CAAM,IAAI;CAAM,OAAO;CAAQ,IAAI;CAAK,QAAQ;AAAM;;;;;AAMzG,SAAS,mBAAmB,GAAmB;CAC7C,MAAM,SAAS,KAAK,IAAI;CACxB,IAAI,MAAM;CACV,KAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KAAK,MAAM,MAAM,SAAS,SAAS;CAC7E,OAAO,MAAM;AACf;;;;;;;AAQA,SAAgB,QAAQ,GAAW,GAAmB;CACpD,MAAM,IAAI,KAAK,IAAI,GAAG,CAAC;CACvB,MAAM,I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AAQ,GAAG,MAAM,MAAM,IAAI,QAAQ,KAAM,KAAK,IAAI,CAAC,GAAG,CAAC,CAAC;GAC7G,OAAO;IACL,QAAQ;IACR,UAAU,MAAM,OAAO,SAAS,KAAK,QAAQ;IAC7C;IACA,WAAW,MAAM;KACf,KAAK,CAAC;KACN,MAAM,OAAO,MAAM,KAAK,SAAS,KAAK,SAAS,CAAC,CAAC;KACjD,MAAM,KAAK,KAAK,IAAI,GAAG,IAAI;KAC3B,MAAM,KAAK,KAAK,IAAI,GAAG,IAAI;KAC3B,OAAO,OAAO,MAAM,CAAC,OAAO,SAAS,EAAE,IAAI,KAAK,OAAO,KAAK,GAAG,IAAI,EAAE;IACvE;GACF;EACF;EACA,KAAK,YAAY;GACf,IAAI,KAAK,OAAO,WAAW,GAAG,MAAM,IAAI,WAAW,4CAA4C;GAC/F,KAAK,MAAM,SAAS,KAAK,QAAQ,QAAQ,YAAY,SAAS,aAAa,MAAM,QAAQ;GACzF,MAAM,SAAS,CAAC,GAAG,KAAK,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC;GACpD,MAAM,IAAI,OAAO;GACjB,OAAO;IACL,QAAQ;IACR,UAAU;IACV,MAAM,MAAM;KACV,IAAI,KAAK;KACT,IAAI,KAAK;KACT,OAAO,KAAK,IAAI;MACd,MAAM,MAAO,KAAK,OAAQ;MAC1B,IAAI,OAAO,QAAS,GAAG,KAAK,MAAM;WAC7B,KAAK;KACZ;KACA,OAAO,KAAK;IACd;IACA,WAAW,MAAM;KACf,KAAK,CAAC;KACN,OAAO,OAAO,KAAK,IAAI,GAAG,KAAK,KAAK,IAAI,CAAC,IAAI,CAAC;IAChD;GACF;EACF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"quantiles.js","names":[],"sources":["../src/quantiles.ts"],"sourcesContent":["// The cumulative distribution and the quantile of each family of `distributions.ts`. A design draws a uniform number u\n// in each stratum and gives the quantile of u. So the Latin hypercube and the stratified designs work with each family.\nimport type { DistributionSpec, Param } from \"./distributions.ts\";\nimport { millsRatio, normalCdf, normalQuantile } from \"./normal.ts\";\nimport type { Random } from \"./random.ts\";\n\n/** One point of a family: its parameters are fixed. */\nexport interface Distribution {\n /** The family of the point. */\n readonly family: DistributionSpec[\"family\"];\n /** True when the values are a finite set, as in `zipf` and `recorded`. */\n readonly discrete: boolean;\n /** This function gives the probability of a value at or below `x`. */\n cdf(x: number): number;\n /** This function gives the smallest value whose `cdf` is `u` or more. `u` is from 0 to 1. */\n quantile(u: number): number;\n}\n\nconst LANCZOS = [\n 0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012,\n 9.9843695780195716e-6, 1.5056327351493116e-7,\n];\n\n/**\n * This function gives the logarithm of the gamma function for `x` above 0 (Lanczos, g 7, 9 terms).\n * The relative error is below 1e-14, away from the zeros at 1 and 2. Below 0.5, the reflection formula gives the value.\n */\nexport function logGamma(x: number): number {\n if (x < 0.5) return Math.log(Math.PI / Math.sin(Math.PI * x)) - logGamma(1 - x);\n const y = x - 1;\n let sum = LANCZOS[0]!;\n for (let i = 1; i < 9; i++) sum += LANCZOS[i]! / (y + i);\n const t = y + 7.5;\n return 0.5 * Math.log(2 * Math.PI) + (y + 0.5) * Math.log(t) - t + Math.log(sum);\n}\n\n/** The coefficients of the Stirling series of the log of the gamma function: B(2k) / (2k (2k - 1)) for k from 1 to 8. */\nconst STIRLING = [1 / 12, -1 / 360, 1 / 1260, -1 / 1680, 1 / 1188, -691 / 360360, 1 / 156, -3617 / 122400];\n\n/**\n * This function gives the Stirling correction: `logGamma(x)` less `(x - 0.5) log x - x + log(2 pi) / 2`, for `x` of 10 or more.\n * At 10, the first term that the series omits is below 2e-18.\n */\nfunction stirlingCorrection(x: number): number {\n const square = 1 / (x * x);\n let sum = 0;\n for (let i = STIRLING.length - 1; i >= 0; i--) sum = sum * square + STIRLING[i]!;\n return sum / x;\n}\n\n/**\n * This function gives the logarithm of the beta function, `logGamma(a) + logGamma(b) - logGamma(a + b)`, for `a` and `b` above 0.\n * - When an argument is 10 or more, the Stirling series gives the difference of the large terms (as `lbeta` of R).\n * Thus the value is accurate also when `a + b` is near 1e13. There, the sum of `logGamma` loses all digits of the difference.\n * - When both arguments are below 10, the value is the sum of `logGamma`.\n */\nexport function logBeta(a: number, b: number): number {\n const p = Math.min(a, b);\n const q = Math.max(a, b);\n const share = p / (p + q);\n if (p >= 10) {\n const correction = stirlingCorrection(p) + stirlingCorrection(q) - stirlingCorrection(p + q);\n return -0.5 * Math.log(q) + 0.5 * Math.log(2 * Math.PI) + correction + (p - 0.5) * Math.log(share) + q * Math.log1p(-share);\n }\n if (q >= 10) {\n const correction = stirlingCorrection(q) - stirlingCorrection(p + q);\n return logGamma(p) + correction + p - p * Math.log(p + q) + (q - 0.5) * Math.log1p(-share);\n }\n return logGamma(p) + logGamma(q) - logGamma(p + q);\n}\n\n/** The two halves of the split of Veltkamp: `hi + lo` is `value`, and each half has 26 bits at most. */\nfunction split(value: number): [number, number] {\n const scaled = 134217729 * value;\n const hi = scaled - (scaled - value);\n return [hi, value - hi];\n}\n\n/**\n * This function gives `a - (a + b) p` with no rounding of the sum and the product before the last subtraction (Dekker).\n * One ulp of `(a + b) p` is one ulp of `p` in the point. Near the mean, the shares of a law with a large `a + b` change by a large factor for each ulp of the point.\n * Thus the exact sum and product keep the digits that the point gives. Above 1e290, the split overflows, and the function gives the plain value.\n */\nfunction centered(a: number, b: number, p: number): number {\n const sum = a + b;\n if (!(sum < 1e290)) return a - sum * p;\n const sumError = a >= b ? b - (sum - a) : a - (sum - b);\n const product = sum * p;\n const [sumHi, sumLo] = split(sum);\n const [pHi, pLo] = split(p);\n const productError = sumHi * pHi - product + sumHi * pLo + sumLo * pHi + sumLo * pLo;\n return a - product - productError - sumError * p;\n}\n\n/**\n * This function gives `x - log(1 + x)` for `x` above -1 (`rlog1` of TOMS 708), with its relative digits also near 0.\n * From -0.6 to 0.6, it uses the series of `log((1 + r) / (1 - r))` with `r = x / (2 + x)`: `x - log(1 + x) = r x - 2 (r^3 / 3 + r^5 / 5 + ...)`.\n */\nfunction rlog1(x: number): number {\n if (Math.abs(x) > 0.6) return x - Math.log1p(x);\n const r = x / (2 + x);\n const square = r * r;\n let power = r * square;\n let sum = 0;\n for (let k = 3; k < 200; k += 2) {\n const term = power / k;\n sum += term;\n if (Math.abs(term) <= 1e-17 * Math.abs(sum)) break;\n power *= square;\n }\n return r * x - 2 * sum;\n}\n\n/**\n * This function gives the front factor `x^a y^b / B(a, b)` of the shares, for `a` and `b` of 10 or more (`brcomp` of TOMS 708).\n * - The factor is `sqrt(b x0 / (2 pi)) exp(-(a rlog1(x / x0 - 1) + b rlog1(y / y0 - 1)) - c)`, with `x0 = a / (a + b)`, `y0 = b / (a + b)` and the Stirling corrections `c`.\n * - `x / x0 - 1` is `-lambda / a` and `y / y0 - 1` is `lambda / b`. Thus no term of the exponent has the size of `a` or `b` near the mean.\n * The sum of logs loses the digits of `a` times the epsilon there.\n */\nfunction largeFront(a: number, b: number, x: number, y: number, lambda: number): number {\n const h = a < b ? a / b : b / a;\n const x0 = a < b ? h / (1 + h) : 1 / (1 + h);\n const y0 = a < b ? 1 / (1 + h) : h / (1 + h);\n let e = -lambda / a;\n const u = Math.abs(e) > 0.6 ? e - Math.log(x / x0) : rlog1(e);\n e = lambda / b;\n const v = Math.abs(e) > 0.6 ? e - Math.log(y / y0) : rlog1(e);\n const correction = stirlingCorrection(a) + stirlingCorrection(b) - stirlingCorrection(a + b);\n return Math.sqrt((b * x0) / (2 * Math.PI)) * Math.exp(-(a * u + b * v) - correction);\n}\n\n/**\n * This function gives `I_x(a, b)` by the asymptotic expansion for a large `a` and `b` (`basym` of TOMS 708), with 20 terms at most.\n * - The caller gives `lambda = a - (a + b) x` of 0 or more, and uses the function when `a` and `b` are above 100 and `lambda` is `0.03 min(a, b)` or less.\n * - Near the mean, the count of terms of the continued fraction grows with `sqrt(min(a, b))`. It does not converge in 10000 terms at 1e12.\n * - `exp(z^2) erfc(z)` of TOMS 708 is `2 M(sqrt(2) z) / sqrt(2 pi)`, with the Mills ratio `M`. Thus no term underflows before the result.\n */\nfunction asymptoticBelow(a: number, b: number, lambda: number): number {\n const e0 = 2 / Math.sqrt(Math.PI);\n const e1 = 2 ** -1.5;\n const f = a * rlog1(-lambda / a) + b * rlog1(lambda / b);\n const t = Math.exp(-f);\n if (t === 0) return 0;\n const z0 = Math.sqrt(f);\n const z = Math.SQRT2 * z0;\n const z2 = f + f;\n const h = a < b ? a / b : b / a;\n const r0 = 1 / (h + 1);\n const r1 = (b - a) / Math.max(a, b);\n const w0 = 1 / Math.sqrt(Math.min(a, b) * (h + 1));\n const size = 21;\n const a0 = new Array<number>(size).fill(0);\n const b0 = new Array<number>(size).fill(0);\n const c = new Array<number>(size).fill(0);\n const d = new Array<number>(size).fill(0);\n const first = (2 / 3) * r1;\n a0[0] = first;\n c[0] = -0.5 * first;\n d[0] = 0.5 * first;\n let j0 = e1 * millsRatio(z);\n let j1 = e1;\n let sum = j0 + 0.5 * first * w0 * j1;\n let s = 1;\n let hn = 1;\n let w = w0;\n let znm1 = z;\n let zn = z2;\n for (let n = 2; n < size; n += 2) {\n hn *= h * h;\n a0[n - 1] = (2 * r0 * (h * hn + 1)) / (n + 2);\n s += hn;\n a0[n] = (2 * r1 * s) / (n + 3);\n for (let i = n; i <= n + 1; i++) {\n const r = -0.5 * (i + 1);\n b0[0] = r * a0[0]!;\n for (let m = 2; m <= i; m++) {\n let bsum = 0;\n for (let j = 1; j < m; j++) bsum += (j * r - (m - j)) * a0[j - 1]! * b0[m - j - 1]!;\n b0[m - 1] = r * a0[m - 1]! + bsum / m;\n }\n c[i - 1] = b0[i - 1]! / (i + 1);\n let dsum = 0;\n for (let j = 1; j < i; j++) dsum += d[i - j - 1]! * c[j - 1]!;\n d[i - 1] = -(dsum + c[i - 1]!);\n }\n j0 = e1 * znm1 + (n - 1) * j0;\n j1 = e1 * zn + n * j1;\n znm1 *= z2;\n zn *= z2;\n w *= w0;\n const t0 = d[n - 1]! * w * j0;\n w *= w0;\n const t1 = d[n]! * w * j1;\n sum += t0 + t1;\n if (Math.abs(t0) + Math.abs(t1) <= 1e-16 * sum) break;\n }\n const correction = stirlingCorrection(a) + stirlingCorrection(b) - stirlingCorrection(a + b);\n return e0 * t * Math.exp(-correction) * sum;\n}\n\n/**\n * This function gives the continued fraction of the incomplete beta function in the form of `bfrac` of TOMS 708 (Didonato and Morris, 1992).\n * `I_x(a, b)` is the front factor `x^a y^b / B(a, b)` times the result.\n * - The caller gives `lambda = a - (a + b) x`. It computes `lambda` from the smaller of `x` and `y`, so `lambda` has no cancellation.\n * - The terms of the fraction use `lambda`, `x` and `y`, and no term subtracts two numbers near 1.\n * The fraction of Lentz has the first term `1 - (a + b) x / (a + 1)`. With `x` near 1 and a large `a`, it loses the digits of `b` times the epsilon.\n */\nfunction betaFraction(a: number, b: number, x: number, y: number, lambda: number): number {\n const c = lambda + 1;\n const c0 = b / a;\n const c1 = 1 / a + 1;\n const yp1 = y + 1;\n let p = 1;\n let s = a + 1;\n let an = 0;\n let bn = 1;\n let anp1 = 1;\n let bnp1 = c / c1;\n let r = c1 / c;\n for (let n = 1; n <= 10000; n++) {\n let t = n / a;\n const w = n * (b - n) * x;\n let e = a / s;\n const alpha = p * (p + c0) * e * e * (w * x);\n e = (t + 1) / (c1 + t + t);\n const beta = n + w / s + e * (c + n * yp1);\n p = t + 1;\n s += 2;\n t = alpha * an + beta * anp1;\n an = anp1;\n anp1 = t;\n t = alpha * bn + beta * bnp1;\n bn = bnp1;\n bnp1 = t;\n const r0 = r;\n r = anp1 / bnp1;\n if (Math.abs(r - r0) <= 1e-16 * r) break;\n an /= bnp1;\n bn /= bnp1;\n anp1 = r;\n bnp1 = 1;\n }\n return r;\n}\n\n/** The shares of a beta law below and above a point. */\nexport interface BetaShares {\n /** The share of the law below the point: the regularized incomplete beta function. */\n readonly below: number;\n /** The share of the law above the point. */\n readonly above: number;\n}\n\n/**\n * This function gives the shares of the beta law with the parameters `a` and `b` below and above `x`.\n * - The caller gives `x` and `y = 1 - x`, each with its full digits. Thus a point near 1 keeps the digits of its distance from 1.\n * For example, `df / (df + t^2)` and `t^2 / (df + t^2)` give both shares of the Student t law with no rounding near 1.\n * - The methods are those of TOMS 708 (Didonato and Morris, 1992), as `pbeta` of R. Each method gives the smaller share with its relative digits.\n * The other share is 1 less that share.\n * - `lambda = a - (a + b) x` comes from the smaller of `x` and `y`, with an exact sum and product. Thus a large `a + b` keeps the digits of the point.\n * - When `a` and `b` are above 100 and `|lambda|` is `0.03 min(a, b)` or less, the asymptotic expansion (`basym`) gives the share.\n * - Elsewhere, the continued fraction (`bfrac`) gives the share. Its terms use `lambda`, so a point near 1 with a large parameter keeps its digits.\n * - When `a` and `b` are 10 or more, the front factor (`brcomp`) uses `x / x0 - 1` with `x0 = a / (a + b)`. Elsewhere, it uses the logs of `x` and `y` and `logBeta`.\n * - The tests hold the relative error below 1e-12 against exact references, with parameters up to 1e13.\n *\n * @throws RangeError when `x` or `y` is not from 0 to 1, or when `x + y` is not 1\n */\nexport function betaShares(x: number, y: number, a: number, b: number): BetaShares {\n if (!(x >= 0 && x <= 1 && y >= 0 && y <= 1 && Math.abs(x + y - 1) <= 1e-12)) {\n throw new RangeError(`betaShares: x and y are from 0 to 1 and their sum is 1, not ${x} and ${y}.`);\n }\n if (x === 0) return { below: 0, above: 1 };\n if (y === 0) return { below: 1, above: 0 };\n // lambda = a - (a + b) x, from the smaller of x and y. The share above uses the swapped fraction, with -lambda.\n const lambda = x <= y ? centered(a, b, x) : -centered(b, a, y);\n const logX = x < 0.5 ? Math.log(x) : Math.log1p(-y);\n const logY = y < 0.5 ? Math.log(y) : Math.log1p(-x);\n const least = Math.min(a, b);\n if (least > 100 && Math.abs(lambda) <= 0.03 * least) {\n if (lambda >= 0) {\n const below = asymptoticBelow(a, b, lambda);\n return { below, above: 1 - below };\n }\n const above = asymptoticBelow(b, a, -lambda);\n return { below: 1 - above, above };\n }\n const front = least >= 10 ? largeFront(a, b, x, y, lambda) : Math.exp(a * logX + b * logY - logBeta(a, b));\n if (x < (a + 1) / (a + b + 2)) {\n const below = front * betaFraction(a, b, x, y, lambda);\n return { below, above: 1 - below };\n }\n const above = front * betaFraction(b, a, y, x, -lambda);\n return { below: 1 - above, above };\n}\n\n/** This function gives the regularized incomplete beta function: the beta cumulative distribution at `x` (`betaShares` with `1 - x`). */\nexport function betaCdf(x: number, a: number, b: number): number {\n if (Number.isNaN(x)) return Number.NaN;\n if (x <= 0) return 0;\n if (x >= 1) return 1;\n return betaShares(x, 1 - x, a, b).below;\n}\n\n/**\n * This function gives the smallest `x` from `lo` to `hi` whose `cdf` is `u` or more, by bisection.\n * The caller makes sure that `cdf(lo)` is `u` or less and `cdf(hi)` is `u` or more.\n */\nfunction bisect(cdf: (x: number) => number, u: number, lo: number, hi: number): number {\n for (let i = 0; i < 2000 && lo < hi; i++) {\n const mid = lo + (hi - lo) / 2;\n if (mid <= lo || mid >= hi) break;\n if (cdf(mid) >= u) hi = mid;\n else lo = mid;\n }\n return cdf(lo) >= u ? lo : hi;\n}\n\n/** This function examines a parameter, as `sampler` does. Both ends of a range must pass `valid`. */\nfunction checked(family: string, name: string, param: Param, valid: (value: number) => boolean, rule: string): void {\n const ends = typeof param === \"number\" ? [param] : param;\n for (const value of ends) {\n if (!Number.isFinite(value) || !valid(value)) throw new RangeError(`${family}: ${name} is ${value}, and it must be ${rule}.`);\n }\n if (typeof param !== \"number\" && param[0] > param[1]) throw new RangeError(`${family}: the range of ${name} starts above its end.`);\n}\n\n/** This function gives the value of a parameter: the number, or one draw from the range. */\nfunction valueOf(param: Param, r: Random): number {\n return typeof param === \"number\" ? param : r.range(param[0], param[1]);\n}\n\n/** This function examines `u` of a quantile. */\nfunction unit(u: number): void {\n if (!(u >= 0 && u <= 1)) throw new RangeError(`quantile: u is ${u}, and it must be from 0 to 1.`);\n}\n\n/**\n * This function gives one point of a family, with its `cdf` and its `quantile`. It draws the parameters from `r` in\n * the order of `sampler`. Thus the same seed gives the same point of the family as `sampler`.\n * The values use `Math.exp`, `Math.log` or `Math.pow` when `dependsOnLibm` of the family is true.\n */\nexport function distributionOf(spec: DistributionSpec, r: Random): Distribution {\n switch (spec.family) {\n case \"lognormal\": {\n checked(\"lognormal\", \"sigma\", spec.sigma, (v) => v >= 0, \"0 or more\");\n checked(\"lognormal\", \"mu\", spec.mu, () => true, \"finite\");\n const mu = valueOf(spec.mu, r);\n const sigma = valueOf(spec.sigma, r);\n const median = Math.exp(mu);\n return {\n family: \"lognormal\",\n discrete: false,\n cdf: (x) => (Number.isNaN(x) ? Number.NaN : x <= 0 ? 0 : sigma === 0 ? (x >= median ? 1 : 0) : normalCdf((Math.log(x) - mu) / sigma)),\n quantile: (u) => {\n unit(u);\n return sigma === 0 ? median : Math.exp(mu + sigma * normalQuantile(u));\n },\n };\n }\n case \"pareto\": {\n checked(\"pareto\", \"scale\", spec.scale, (v) => v > 0, \"above 0\");\n checked(\"pareto\", \"shape\", spec.shape, (v) => v > 0, \"above 0\");\n const scale = valueOf(spec.scale, r);\n const shape = valueOf(spec.shape, r);\n return {\n family: \"pareto\",\n discrete: false,\n cdf: (x) => (x < scale ? 0 : 1 - Math.pow(scale / x, shape)),\n quantile: (u) => {\n unit(u);\n return scale / Math.pow(1 - u, 1 / shape);\n },\n };\n }\n case \"zipf\": {\n if (!Number.isSafeInteger(spec.n) || spec.n < 1) throw new RangeError(`zipf: n is ${spec.n}, and it must be a whole number of 1 or more.`);\n checked(\"zipf\", \"exponent\", spec.exponent, (v) => v >= 0, \"0 or more\");\n const exponent = valueOf(spec.exponent, r);\n const totals: number[] = [];\n let total = 0;\n for (let rank = 1; rank <= spec.n; rank++) {\n total += Math.pow(rank, -exponent);\n totals.push(total);\n }\n return {\n family: \"zipf\",\n discrete: true,\n cdf: (x) => (x < 1 ? 0 : x >= spec.n ? 1 : totals[Math.floor(x) - 1]! / total),\n quantile: (u) => {\n unit(u);\n const at = u * total;\n let lo = 0;\n let hi = spec.n - 1;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n if (totals[mid]! >= at) hi = mid;\n else lo = mid + 1;\n }\n return lo + 1;\n },\n };\n }\n case \"beta\": {\n checked(\"beta\", \"a\", spec.a, (v) => v > 0, \"above 0\");\n checked(\"beta\", \"b\", spec.b, (v) => v > 0, \"above 0\");\n const a = valueOf(spec.a, r);\n const b = valueOf(spec.b, r);\n const cdf = (x: number): number => betaCdf(x, a, b);\n return {\n family: \"beta\",\n discrete: false,\n cdf,\n quantile: (u) => {\n unit(u);\n return u === 0 ? 0 : u === 1 ? 1 : bisect(cdf, u, 0, 1);\n },\n };\n }\n case \"mixture\": {\n if (spec.parts.length === 0) throw new RangeError(\"mixture: the list of the parts is empty.\");\n for (const { weight } of spec.parts) checked(\"mixture\", \"weight\", weight, (v) => v > 0, \"above 0\");\n const parts = spec.parts.map(({ part }) => distributionOf(part, r));\n const sum = spec.parts.reduce((s, { weight }) => s + weight, 0);\n const weights = spec.parts.map(({ weight }) => weight / sum);\n const cdf = (x: number): number => Math.min(1, parts.reduce((s, part, i) => s + weights[i]! * part.cdf(x), 0));\n return {\n family: \"mixture\",\n discrete: parts.every((part) => part.discrete),\n cdf,\n quantile: (u) => {\n unit(u);\n const ends = parts.map((part) => part.quantile(u));\n const lo = Math.min(...ends);\n const hi = Math.max(...ends);\n return lo === hi || !Number.isFinite(hi) ? hi : bisect(cdf, u, lo, hi);\n },\n };\n }\n case \"recorded\": {\n if (spec.values.length === 0) throw new RangeError(\"recorded: the list of the values is empty.\");\n for (const value of spec.values) checked(\"recorded\", \"value\", value, () => true, \"finite\");\n const sorted = [...spec.values].sort((x, y) => x - y);\n const n = sorted.length;\n return {\n family: \"recorded\",\n discrete: true,\n cdf: (x) => {\n let lo = 0;\n let hi = n;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n if (sorted[mid]! <= x) lo = mid + 1;\n else hi = mid;\n }\n return lo / n;\n },\n quantile: (u) => {\n unit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,MAAM,MAAO,KAAK,OAAQ;MAC1B,IAAI,OAAO,QAAS,GAAG,KAAK,MAAM;WAC7B,KAAK;KACZ;KACA,OAAO,KAAK;IACd;IACA,WAAW,MAAM;KACf,KAAK,CAAC;KACN,OAAO,OAAO,KAAK,IAAI,GAAG,KAAK,KAAK,IAAI,CAAC,IAAI,CAAC;IAChD;GACF;EACF;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/rigor-generators",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"description": "A seeded random number generator (mulberry32) with the draws that tests and benchmarks make: integers, picks, weights, shuffles, samples, normal values and named streams. It also gives covering arrays (pairwise and of strength t), and a content hash: SHA-256 and canonical JSON (RFC 8785).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"random",
|