@ac-kit/math-analysis 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 +28 -0
- package/README.md +68 -0
- package/dist/_beta-power-terms.d.ts +21 -0
- package/dist/_beta-power-terms.d.ts.map +1 -0
- package/dist/_beta-power-terms.js +43 -0
- package/dist/_beta-power-terms.js.map +1 -0
- package/dist/_exp-split.d.ts +20 -0
- package/dist/_exp-split.d.ts.map +1 -0
- package/dist/_exp-split.js +44 -0
- package/dist/_exp-split.js.map +1 -0
- package/dist/_incomplete-beta.d.ts +27 -0
- package/dist/_incomplete-beta.d.ts.map +1 -0
- package/dist/_incomplete-beta.js +55 -0
- package/dist/_incomplete-beta.js.map +1 -0
- package/dist/_incomplete-gamma.d.ts +81 -0
- package/dist/_incomplete-gamma.d.ts.map +1 -0
- package/dist/_incomplete-gamma.js +155 -0
- package/dist/_incomplete-gamma.js.map +1 -0
- package/dist/_lanczos.d.ts +21 -0
- package/dist/_lanczos.d.ts.map +1 -0
- package/dist/_lanczos.js +48 -0
- package/dist/_lanczos.js.map +1 -0
- package/dist/_log1pmx.d.ts +18 -0
- package/dist/_log1pmx.d.ts.map +1 -0
- package/dist/_log1pmx.js +36 -0
- package/dist/_log1pmx.js.map +1 -0
- package/dist/_normal-quantile-guess.d.ts +14 -0
- package/dist/_normal-quantile-guess.d.ts.map +1 -0
- package/dist/_normal-quantile-guess.js +19 -0
- package/dist/_normal-quantile-guess.js.map +1 -0
- package/dist/beta.d.ts +36 -0
- package/dist/beta.d.ts.map +1 -0
- package/dist/beta.js +77 -0
- package/dist/beta.js.map +1 -0
- package/dist/bisect.d.ts +40 -0
- package/dist/bisect.d.ts.map +1 -0
- package/dist/bisect.js +72 -0
- package/dist/bisect.js.map +1 -0
- package/dist/continued-fraction-eval.d.ts +68 -0
- package/dist/continued-fraction-eval.d.ts.map +1 -0
- package/dist/continued-fraction-eval.js +86 -0
- package/dist/continued-fraction-eval.js.map +1 -0
- package/dist/convergence-error.d.ts +31 -0
- package/dist/convergence-error.d.ts.map +1 -0
- package/dist/convergence-error.js +34 -0
- package/dist/convergence-error.js.map +1 -0
- package/dist/convergence-options.d.ts +38 -0
- package/dist/convergence-options.d.ts.map +1 -0
- package/dist/convergence-options.js +2 -0
- package/dist/convergence-options.js.map +1 -0
- package/dist/convergence-result.d.ts +33 -0
- package/dist/convergence-result.d.ts.map +1 -0
- package/dist/convergence-result.js +2 -0
- package/dist/convergence-result.js.map +1 -0
- package/dist/erf.d.ts +28 -0
- package/dist/erf.d.ts.map +1 -0
- package/dist/erf.js +43 -0
- package/dist/erf.js.map +1 -0
- package/dist/erfc.d.ts +31 -0
- package/dist/erfc.d.ts.map +1 -0
- package/dist/erfc.js +48 -0
- package/dist/erfc.js.map +1 -0
- package/dist/gamma.d.ts +36 -0
- package/dist/gamma.d.ts.map +1 -0
- package/dist/gamma.js +59 -0
- package/dist/gamma.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/inverse-regularized-incomplete-beta.d.ts +34 -0
- package/dist/inverse-regularized-incomplete-beta.d.ts.map +1 -0
- package/dist/inverse-regularized-incomplete-beta.js +136 -0
- package/dist/inverse-regularized-incomplete-beta.js.map +1 -0
- package/dist/inverse-regularized-incomplete-gamma.d.ts +36 -0
- package/dist/inverse-regularized-incomplete-gamma.d.ts.map +1 -0
- package/dist/inverse-regularized-incomplete-gamma.js +149 -0
- package/dist/inverse-regularized-incomplete-gamma.js.map +1 -0
- package/dist/log-beta.d.ts +29 -0
- package/dist/log-beta.d.ts.map +1 -0
- package/dist/log-beta.js +36 -0
- package/dist/log-beta.js.map +1 -0
- package/dist/log-gamma.d.ts +29 -0
- package/dist/log-gamma.d.ts.map +1 -0
- package/dist/log-gamma.js +39 -0
- package/dist/log-gamma.js.map +1 -0
- package/dist/regularized-incomplete-beta.d.ts +41 -0
- package/dist/regularized-incomplete-beta.d.ts.map +1 -0
- package/dist/regularized-incomplete-beta.js +68 -0
- package/dist/regularized-incomplete-beta.js.map +1 -0
- package/dist/regularized-incomplete-gamma-upper.d.ts +38 -0
- package/dist/regularized-incomplete-gamma-upper.d.ts.map +1 -0
- package/dist/regularized-incomplete-gamma-upper.js +52 -0
- package/dist/regularized-incomplete-gamma-upper.js.map +1 -0
- package/dist/regularized-incomplete-gamma.d.ts +44 -0
- package/dist/regularized-incomplete-gamma.d.ts.map +1 -0
- package/dist/regularized-incomplete-gamma.js +58 -0
- package/dist/regularized-incomplete-gamma.js.map +1 -0
- package/dist/trapezoidal-rule.d.ts +35 -0
- package/dist/trapezoidal-rule.d.ts.map +1 -0
- package/dist/trapezoidal-rule.js +50 -0
- package/dist/trapezoidal-rule.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { assertIncompleteGammaDomain_, incompleteGammaScale_, } from "./_incomplete-gamma.js";
|
|
2
|
+
import { normalQuantileGuess_ } from "./_normal-quantile-guess.js";
|
|
3
|
+
import { regularizedIncompleteGammaUpper } from "./regularized-incomplete-gamma-upper.js";
|
|
4
|
+
import { regularizedIncompleteGamma } from "./regularized-incomplete-gamma.js";
|
|
5
|
+
/** Smallest starting point, so the first density evaluation is not at zero. */
|
|
6
|
+
const MIN_START_ = 1e-3;
|
|
7
|
+
/**
|
|
8
|
+
* Default relative step below which the iterate is taken as converged.
|
|
9
|
+
*
|
|
10
|
+
* Four ulps rather than one: the residual being driven to zero is itself a
|
|
11
|
+
* forward evaluation carrying tens of ulps, so a one-ulp step is not attainable
|
|
12
|
+
* — the iteration reaches the right answer in three steps and then oscillates
|
|
13
|
+
* between adjacent floats forever, which is what exhausted the budget before
|
|
14
|
+
* this was measured.
|
|
15
|
+
*/
|
|
16
|
+
const DEFAULT_TOLERANCE_ = 4 * Number.EPSILON;
|
|
17
|
+
/**
|
|
18
|
+
* A starting point good to a few digits, from which Halley's method reaches
|
|
19
|
+
* full precision in three or four steps.
|
|
20
|
+
*
|
|
21
|
+
* Two regimes, because no single approximation covers both: above `a = 1` the
|
|
22
|
+
* Wilson–Hilferty transform maps a normal quantile onto the gamma, and below it
|
|
23
|
+
* the distribution is too skewed for that, so the series and the exponential
|
|
24
|
+
* tail are inverted piecewise instead.
|
|
25
|
+
*/
|
|
26
|
+
function initialGuess_(a, p) {
|
|
27
|
+
if (a > 1) {
|
|
28
|
+
const z = normalQuantileGuess_(p);
|
|
29
|
+
return Math.max(MIN_START_, a * (1 - 1 / (9 * a) + z / (3 * Math.sqrt(a))) ** 3);
|
|
30
|
+
}
|
|
31
|
+
// `P(a, 1)` to within a few percent, which is where the two branches meet.
|
|
32
|
+
const crossover = 1 - a * (0.253 + a * 0.12);
|
|
33
|
+
return p < crossover
|
|
34
|
+
? (p / crossover) ** (1 / a)
|
|
35
|
+
: 1 - Math.log1p(-(p - crossover) / (1 - crossover));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The inverse of {@link regularizedIncompleteGamma} in its second argument: the
|
|
39
|
+
* `x` at which `P(a, x)` equals `p`.
|
|
40
|
+
*
|
|
41
|
+
* This is the quantile function — the "which value sits at the 95th
|
|
42
|
+
* percentile?" direction, where `P` answers "which percentile is this value
|
|
43
|
+
* at?". It is what the `Gamma`, `ChiSquared` and `Poisson` quantiles in
|
|
44
|
+
* `@ac-kit/math-stats` are built on, and what turns a confidence level into a
|
|
45
|
+
* critical value.
|
|
46
|
+
*
|
|
47
|
+
* Accurate to within a few ulps in the round trip: feeding the result back
|
|
48
|
+
* through `P` returns the `p` you started from to about `1e-15` relative.
|
|
49
|
+
*
|
|
50
|
+
* O(`maxIterations`) evaluations of `P` or `Q` in the worst case; three or four
|
|
51
|
+
* in practice, because convergence is cubic. O(1) memory.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const median = inverseRegularizedIncompleteGamma(3, 0.5); // 2.674060313723559
|
|
56
|
+
* regularizedIncompleteGamma(3, median); // 0.4999999999999999 — back where we started
|
|
57
|
+
* ```;
|
|
58
|
+
*
|
|
59
|
+
* @param a Shape parameter, positive.
|
|
60
|
+
* @param p Probability, in `[0, 1]`.
|
|
61
|
+
* @param options Termination criteria; `tolerance` is compared against the
|
|
62
|
+
* relative size of the refinement step and defaults to `Number.EPSILON`,
|
|
63
|
+
* `maxIterations` to 100.
|
|
64
|
+
* @returns The `x` with `P(a, x) = p`; `0` at `p = 0` and `Infinity` at `p =
|
|
65
|
+
* 1`.
|
|
66
|
+
* @throws {RangeError} When `a ≤ 0` or `p` is outside `[0, 1]`.
|
|
67
|
+
* @see {@link regularizedIncompleteGamma} for the forward direction.
|
|
68
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
69
|
+
*/
|
|
70
|
+
export function inverseRegularizedIncompleteGamma(a, p, options) {
|
|
71
|
+
// Halley's method rather than Newton's: the second-derivative term is one
|
|
72
|
+
// extra multiply, since `P`'s log-derivative is already in hand, and it turns
|
|
73
|
+
// quadratic convergence into cubic. The residual is measured against
|
|
74
|
+
// whichever of `P` and `Q` is small at the current iterate, so a quantile far
|
|
75
|
+
// out in either tail is refined against a quantity that still has digits.
|
|
76
|
+
assertIncompleteGammaDomain_("inverseRegularizedIncompleteGamma", a, 0);
|
|
77
|
+
if (!(p >= 0 && p <= 1)) {
|
|
78
|
+
throw new RangeError(`inverseRegularizedIncompleteGamma: p must be in [0, 1], got ${p}`);
|
|
79
|
+
}
|
|
80
|
+
if (p === 0) {
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
if (p === 1) {
|
|
84
|
+
return Number.POSITIVE_INFINITY;
|
|
85
|
+
}
|
|
86
|
+
const tolerance = options?.tolerance ?? DEFAULT_TOLERANCE_;
|
|
87
|
+
const maxIterations = options?.maxIterations ?? 100;
|
|
88
|
+
const { signal } = options ?? {};
|
|
89
|
+
let x = initialGuess_(a, p);
|
|
90
|
+
// Halley alone is not enough: the Wilson–Hilferty guess can be orders of
|
|
91
|
+
// magnitude out in a far tail, and an unbracketed step from there either
|
|
92
|
+
// overshoots past zero or stalls on a density that has underflowed. The
|
|
93
|
+
// bracket makes every iteration either a Halley step that provably improves
|
|
94
|
+
// or a bisection that provably halves, so the loop cannot wander.
|
|
95
|
+
let lower = 0;
|
|
96
|
+
let upper = Number.POSITIVE_INFINITY;
|
|
97
|
+
// The iterate that came closest, rather than the last one tried: past the
|
|
98
|
+
// point where the forward evaluation's own noise dominates, later iterates are
|
|
99
|
+
// not better, only different.
|
|
100
|
+
let best = x;
|
|
101
|
+
let bestResidual = Number.POSITIVE_INFINITY;
|
|
102
|
+
for (let index = 0; index < maxIterations; index++) {
|
|
103
|
+
signal?.throwIfAborted();
|
|
104
|
+
if (x <= 0) {
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
// Measured against whichever tail is the small one, so a quantile far out
|
|
108
|
+
// in either direction is refined against a value that still has digits.
|
|
109
|
+
const residual = p <= 0.5
|
|
110
|
+
? regularizedIncompleteGamma(a, x, options) - p
|
|
111
|
+
: 1 - p - regularizedIncompleteGammaUpper(a, x, options);
|
|
112
|
+
if (residual === 0) {
|
|
113
|
+
return x;
|
|
114
|
+
}
|
|
115
|
+
if (Math.abs(residual) < bestResidual) {
|
|
116
|
+
bestResidual = Math.abs(residual);
|
|
117
|
+
best = x;
|
|
118
|
+
}
|
|
119
|
+
if (residual > 0) {
|
|
120
|
+
upper = x;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
lower = x;
|
|
124
|
+
}
|
|
125
|
+
const density = incompleteGammaScale_(a, x) / x;
|
|
126
|
+
const newton = density > 0 ? residual / density : 0;
|
|
127
|
+
// Halley's correction, clamped because the curvature term overshoots
|
|
128
|
+
// wildly while the iterate is still far from the root.
|
|
129
|
+
const step = newton / (1 - 0.5 * Math.min(1, newton * ((a - 1) / x - 1)));
|
|
130
|
+
// Checked before the bracket below: once the step is under an ulp of `x` it
|
|
131
|
+
// lands back on `x` itself, which the bracket reads as an escape and answers
|
|
132
|
+
// with a bisection that throws the converged answer away.
|
|
133
|
+
if (Math.abs(step) < tolerance * x) {
|
|
134
|
+
return x - step;
|
|
135
|
+
}
|
|
136
|
+
let next = x - step;
|
|
137
|
+
if (!(next > lower && next < upper)) {
|
|
138
|
+
// Outside the bracket, so the local model is not to be trusted: halve
|
|
139
|
+
// the bracket instead, or reach for a finite upper end if there is none.
|
|
140
|
+
next = Number.isFinite(upper) ? 0.5 * (lower + upper) : 2 * x;
|
|
141
|
+
}
|
|
142
|
+
if (next === x || upper - lower < tolerance * x) {
|
|
143
|
+
return best;
|
|
144
|
+
}
|
|
145
|
+
x = next;
|
|
146
|
+
}
|
|
147
|
+
return best;
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=inverse-regularized-incomplete-gamma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inverse-regularized-incomplete-gamma.js","sourceRoot":"","sources":["../src/inverse-regularized-incomplete-gamma.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,4BAA4B,EAC5B,qBAAqB,GACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE,OAAO,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAC;AAC1F,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAE/E,+EAA+E;AAC/E,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB;;;;;;;;GAQG;AACH,MAAM,kBAAkB,GAAG,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC;AAE9C;;;;;;;;GAQG;AACH,SAAS,aAAa,CAAC,CAAS,EAAE,CAAS;IAC1C,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACX,MAAM,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC;QAElC,OAAO,IAAI,CAAC,G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,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The natural logarithm of the beta function, `ln B(a, b)`.
|
|
3
|
+
*
|
|
4
|
+
* The form to use once the shapes get large: `B(a, b)` underflows to zero from
|
|
5
|
+
* around `a + b ≈ 1500`, and this keeps full relative precision well past that.
|
|
6
|
+
* It is also what you want any time the result was going to be logged, summed
|
|
7
|
+
* with other logs, or exponentiated at the end — which is most of what a
|
|
8
|
+
* likelihood calculation does.
|
|
9
|
+
*
|
|
10
|
+
* Not a wrapper around `Math.log(beta(a, b))`: below the underflow point that
|
|
11
|
+
* is exactly what it computes, because the log of an accurate value carries
|
|
12
|
+
* only its own rounding, and above it a sum of three log-gammas takes over.
|
|
13
|
+
*
|
|
14
|
+
* O(1) time, O(1) memory.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* logBeta(500, 500); // -694.9887224857135
|
|
19
|
+
* logBeta(2000, 2000); // -2775.1235988460758 — where beta() returns 0
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @param a First shape parameter, positive.
|
|
23
|
+
* @param b Second shape parameter, positive.
|
|
24
|
+
* @returns `ln B(a, b)`, or `NaN` when either parameter is non-positive.
|
|
25
|
+
* @see {@link beta} for the value itself.
|
|
26
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
27
|
+
*/
|
|
28
|
+
export declare function logBeta(a: number, b: number): number;
|
|
29
|
+
//# sourceMappingURL=log-beta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-beta.d.ts","sourceRoot":"","sources":["../src/log-beta.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAMpD"}
|
package/dist/log-beta.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { beta } from "./beta.js";
|
|
2
|
+
import { logGamma } from "./log-gamma.js";
|
|
3
|
+
/**
|
|
4
|
+
* The natural logarithm of the beta function, `ln B(a, b)`.
|
|
5
|
+
*
|
|
6
|
+
* The form to use once the shapes get large: `B(a, b)` underflows to zero from
|
|
7
|
+
* around `a + b ≈ 1500`, and this keeps full relative precision well past that.
|
|
8
|
+
* It is also what you want any time the result was going to be logged, summed
|
|
9
|
+
* with other logs, or exponentiated at the end — which is most of what a
|
|
10
|
+
* likelihood calculation does.
|
|
11
|
+
*
|
|
12
|
+
* Not a wrapper around `Math.log(beta(a, b))`: below the underflow point that
|
|
13
|
+
* is exactly what it computes, because the log of an accurate value carries
|
|
14
|
+
* only its own rounding, and above it a sum of three log-gammas takes over.
|
|
15
|
+
*
|
|
16
|
+
* O(1) time, O(1) memory.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* logBeta(500, 500); // -694.9887224857135
|
|
21
|
+
* logBeta(2000, 2000); // -2775.1235988460758 — where beta() returns 0
|
|
22
|
+
* ```;
|
|
23
|
+
*
|
|
24
|
+
* @param a First shape parameter, positive.
|
|
25
|
+
* @param b Second shape parameter, positive.
|
|
26
|
+
* @returns `ln B(a, b)`, or `NaN` when either parameter is non-positive.
|
|
27
|
+
* @see {@link beta} for the value itself.
|
|
28
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
29
|
+
*/
|
|
30
|
+
export function logBeta(a, b) {
|
|
31
|
+
const value = beta(a, b);
|
|
32
|
+
return value > 0 && Number.isFinite(value)
|
|
33
|
+
? Math.log(value)
|
|
34
|
+
: logGamma(a) + logGamma(b) - logGamma(a + b);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=log-beta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-beta.js","sourceRoot":"","sources":["../src/log-beta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,OAAO,CAAC,CAAS,EAAE,CAAS;IAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAEzB,OAAO,KAAK,GAAG,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QACzC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;QACjB,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The natural logarithm of the gamma function, `ln Γ(x)`.
|
|
3
|
+
*
|
|
4
|
+
* Use it whenever `Γ` itself would not fit. `Γ(a)` overflows binary64 past `a ≈
|
|
5
|
+
* 171.6`, while `ln Γ(a)` stays finite for every argument a caller can
|
|
6
|
+
* represent — which is what makes the ratios of gamma functions behind
|
|
7
|
+
* {@link beta} and the probability distributions computable at all.
|
|
8
|
+
*
|
|
9
|
+
* Defined for `x > 0` only. That is not a gap: Γ alternates sign between
|
|
10
|
+
* consecutive negative integers and has a pole at each of them, so no real
|
|
11
|
+
* logarithm covers the negative reals. Use {@link gamma} there, which carries
|
|
12
|
+
* the sign.
|
|
13
|
+
*
|
|
14
|
+
* O(1) time, O(1) memory — a fixed nine-term series.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* logGamma(172); // 711.71472580229 — finite where gamma(172) is Infinity
|
|
19
|
+
* logGamma(1e6); // 12815504.56914761
|
|
20
|
+
* logGamma(-1); // NaN — no real logarithm on the negative reals
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param x A positive real.
|
|
24
|
+
* @returns `ln Γ(x)`, or `NaN` when `x ≤ 0` or `x` is `NaN`.
|
|
25
|
+
* @see {@link gamma} for the value itself, and for negative arguments.
|
|
26
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
27
|
+
*/
|
|
28
|
+
export declare function logGamma(x: number): number;
|
|
29
|
+
//# sourceMappingURL=log-gamma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-gamma.d.ts","sourceRoot":"","sources":["../src/log-gamma.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAY1C"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { LN_TWO_PI } from "@ac-kit/math-scalar";
|
|
2
|
+
import { LANCZOS_G_, lanczosSeries_ } from "./_lanczos.js";
|
|
3
|
+
/**
|
|
4
|
+
* The natural logarithm of the gamma function, `ln Γ(x)`.
|
|
5
|
+
*
|
|
6
|
+
* Use it whenever `Γ` itself would not fit. `Γ(a)` overflows binary64 past `a ≈
|
|
7
|
+
* 171.6`, while `ln Γ(a)` stays finite for every argument a caller can
|
|
8
|
+
* represent — which is what makes the ratios of gamma functions behind
|
|
9
|
+
* {@link beta} and the probability distributions computable at all.
|
|
10
|
+
*
|
|
11
|
+
* Defined for `x > 0` only. That is not a gap: Γ alternates sign between
|
|
12
|
+
* consecutive negative integers and has a pole at each of them, so no real
|
|
13
|
+
* logarithm covers the negative reals. Use {@link gamma} there, which carries
|
|
14
|
+
* the sign.
|
|
15
|
+
*
|
|
16
|
+
* O(1) time, O(1) memory — a fixed nine-term series.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* logGamma(172); // 711.71472580229 — finite where gamma(172) is Infinity
|
|
21
|
+
* logGamma(1e6); // 12815504.56914761
|
|
22
|
+
* logGamma(-1); // NaN — no real logarithm on the negative reals
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @param x A positive real.
|
|
26
|
+
* @returns `ln Γ(x)`, or `NaN` when `x ≤ 0` or `x` is `NaN`.
|
|
27
|
+
* @see {@link gamma} for the value itself, and for negative arguments.
|
|
28
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
29
|
+
*/
|
|
30
|
+
export function logGamma(x) {
|
|
31
|
+
// Written as a positive test so NaN falls through to NaN rather than passing.
|
|
32
|
+
if (!(x > 0)) {
|
|
33
|
+
return Number.NaN;
|
|
34
|
+
}
|
|
35
|
+
const z = x - 1;
|
|
36
|
+
const t = z + LANCZOS_G_ + 0.5;
|
|
37
|
+
return (LN_TWO_PI / 2 + (z + 0.5) * Math.log(t) - t + Math.log(lanczosSeries_(z)));
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=log-gamma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-gamma.js","sourceRoot":"","sources":["../src/log-gamma.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAS;IACjC,8EAA8E;IAC9E,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,GAAG,GAAG,CAAC;IAE/B,OAAO,CACN,SAAS,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CACzE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* The regularized incomplete beta `Iₓ(a, b)` — the fraction of a `Beta(a, b)`
|
|
4
|
+
* distribution lying at or below `x`.
|
|
5
|
+
*
|
|
6
|
+
* The Beta distribution describes a proportion — a success rate, a share, a
|
|
7
|
+
* probability you are uncertain about — and this is its cumulative function.
|
|
8
|
+
* Through it come the tail probabilities of Student's _t_, the _F_ distribution
|
|
9
|
+
* and the binomial, which is why it turns up far more often than its name
|
|
10
|
+
* suggests.
|
|
11
|
+
*
|
|
12
|
+
* **Regularized** is stated rather than implied: this is the ratio in `[0, 1]`,
|
|
13
|
+
* not the unnormalised integral `B(x; a, b)`. Libraries disagree on which the
|
|
14
|
+
* bare name means, and {@link regularizedIncompleteGamma} spells it out for the
|
|
15
|
+
* same reason.
|
|
16
|
+
*
|
|
17
|
+
* Relative error is a few ulps for ordinary parameters and stays within a few
|
|
18
|
+
* dozen for extreme ones.
|
|
19
|
+
*
|
|
20
|
+
* O(`maxIterations`) continued-fraction steps in the worst case, and far fewer
|
|
21
|
+
* in practice; O(1) memory.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* regularizedIncompleteBeta(2, 3, 0.5); // 0.6875
|
|
26
|
+
* regularizedIncompleteBeta(0.5, 0.5, 0.5); // 0.49999999999999944 — the arcsine law
|
|
27
|
+
* ```;
|
|
28
|
+
*
|
|
29
|
+
* @param a First shape parameter, positive.
|
|
30
|
+
* @param b Second shape parameter, positive.
|
|
31
|
+
* @param x Point of evaluation, in `[0, 1]`.
|
|
32
|
+
* @param options Termination criteria; `tolerance` is relative and defaults to
|
|
33
|
+
* `Number.EPSILON`, `maxIterations` to 1000.
|
|
34
|
+
* @returns `Iₓ(a, b)`, in `[0, 1]`.
|
|
35
|
+
* @throws {RangeError} When `a ≤ 0`, `b ≤ 0`, or `x` is outside `[0, 1]`.
|
|
36
|
+
* @throws {ConvergenceError} When the continued fraction met no tolerance.
|
|
37
|
+
* @see {@link inverseRegularizedIncompleteBeta} to go the other way.
|
|
38
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
39
|
+
*/
|
|
40
|
+
export declare function regularizedIncompleteBeta(a: number, b: number, x: number, options?: ConvergenceOptions): number;
|
|
41
|
+
//# sourceMappingURL=regularized-incomplete-beta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regularized-incomplete-beta.d.ts","sourceRoot":"","sources":["../src/regularized-incomplete-beta.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,yBAAyB,CACxC,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CAoCR"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { betaPowerTerms_ } from "./_beta-power-terms.js";
|
|
2
|
+
import { assertBetaShape_, betaContinuedFraction_, } from "./_incomplete-beta.js";
|
|
3
|
+
import { ConvergenceError } from "./convergence-error.js";
|
|
4
|
+
/**
|
|
5
|
+
* The regularized incomplete beta `Iₓ(a, b)` — the fraction of a `Beta(a, b)`
|
|
6
|
+
* distribution lying at or below `x`.
|
|
7
|
+
*
|
|
8
|
+
* The Beta distribution describes a proportion — a success rate, a share, a
|
|
9
|
+
* probability you are uncertain about — and this is its cumulative function.
|
|
10
|
+
* Through it come the tail probabilities of Student's _t_, the _F_ distribution
|
|
11
|
+
* and the binomial, which is why it turns up far more often than its name
|
|
12
|
+
* suggests.
|
|
13
|
+
*
|
|
14
|
+
* **Regularized** is stated rather than implied: this is the ratio in `[0, 1]`,
|
|
15
|
+
* not the unnormalised integral `B(x; a, b)`. Libraries disagree on which the
|
|
16
|
+
* bare name means, and {@link regularizedIncompleteGamma} spells it out for the
|
|
17
|
+
* same reason.
|
|
18
|
+
*
|
|
19
|
+
* Relative error is a few ulps for ordinary parameters and stays within a few
|
|
20
|
+
* dozen for extreme ones.
|
|
21
|
+
*
|
|
22
|
+
* O(`maxIterations`) continued-fraction steps in the worst case, and far fewer
|
|
23
|
+
* in practice; O(1) memory.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* regularizedIncompleteBeta(2, 3, 0.5); // 0.6875
|
|
28
|
+
* regularizedIncompleteBeta(0.5, 0.5, 0.5); // 0.49999999999999944 — the arcsine law
|
|
29
|
+
* ```;
|
|
30
|
+
*
|
|
31
|
+
* @param a First shape parameter, positive.
|
|
32
|
+
* @param b Second shape parameter, positive.
|
|
33
|
+
* @param x Point of evaluation, in `[0, 1]`.
|
|
34
|
+
* @param options Termination criteria; `tolerance` is relative and defaults to
|
|
35
|
+
* `Number.EPSILON`, `maxIterations` to 1000.
|
|
36
|
+
* @returns `Iₓ(a, b)`, in `[0, 1]`.
|
|
37
|
+
* @throws {RangeError} When `a ≤ 0`, `b ≤ 0`, or `x` is outside `[0, 1]`.
|
|
38
|
+
* @throws {ConvergenceError} When the continued fraction met no tolerance.
|
|
39
|
+
* @see {@link inverseRegularizedIncompleteBeta} to go the other way.
|
|
40
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
41
|
+
*/
|
|
42
|
+
export function regularizedIncompleteBeta(a, b, x, options) {
|
|
43
|
+
assertBetaShape_("regularizedIncompleteBeta", a, b);
|
|
44
|
+
if (!(x >= 0 && x <= 1)) {
|
|
45
|
+
throw new RangeError(`regularizedIncompleteBeta: x must be in [0, 1], got ${x}`);
|
|
46
|
+
}
|
|
47
|
+
if (x === 0 || x === 1) {
|
|
48
|
+
return x;
|
|
49
|
+
}
|
|
50
|
+
// Evaluated by swapping the arguments rather than by recursing: at the
|
|
51
|
+
// crossover point itself both sides land on the same branch, so a recursive
|
|
52
|
+
// call there never terminates.
|
|
53
|
+
const direct = x < (a + 1) / (a + b + 2);
|
|
54
|
+
const shapeA = direct ? a : b;
|
|
55
|
+
const shapeB = direct ? b : a;
|
|
56
|
+
const point = direct ? x : 1 - x;
|
|
57
|
+
// Carried rather than re-derived as `1 - point`, which would round away the
|
|
58
|
+
// small operand exactly when the swap was made to keep it.
|
|
59
|
+
const complement = direct ? 1 - x : x;
|
|
60
|
+
const fraction = betaContinuedFraction_(shapeA, shapeB, point, options);
|
|
61
|
+
if (!fraction.converged) {
|
|
62
|
+
throw new ConvergenceError("regularizedIncompleteBeta", fraction.iterations);
|
|
63
|
+
}
|
|
64
|
+
const front = betaPowerTerms_(shapeA, shapeB, point, complement);
|
|
65
|
+
const value = front / (shapeA * fraction.value);
|
|
66
|
+
return direct ? value : 1 - value;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=regularized-incomplete-beta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regularized-incomplete-beta.js","sourceRoot":"","sources":["../src/regularized-incomplete-beta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EACN,gBAAgB,EAChB,sBAAsB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,UAAU,yBAAyB,CACxC,CAAS,EACT,CAAS,EACT,CAAS,EACT,OAA4B;IAE5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K,GAAG,CAAC,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAEhD,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* The upper regularized incomplete gamma `Q(a, x) = 1 − P(a, x)` — the fraction
|
|
4
|
+
* of a `Gamma(a, 1)` distribution lying above `x`.
|
|
5
|
+
*
|
|
6
|
+
* This is the survival function, and in practice it is the p-value: "how often
|
|
7
|
+
* would chance alone produce a result at least this extreme?". Reach for it
|
|
8
|
+
* whenever the answer is expected to be small, which for a p-value is always.
|
|
9
|
+
*
|
|
10
|
+
* Not a convenience wrapper over {@link regularizedIncompleteGamma}. The tail is
|
|
11
|
+
* computed directly, where `1 - P` subtracts two numbers agreeing to every
|
|
12
|
+
* digit that mattered: a chi-squared p-value of `1e-12` comes out exact here
|
|
13
|
+
* and as `0` there.
|
|
14
|
+
*
|
|
15
|
+
* Relative error is within a few ulps for ordinary parameters and stays under
|
|
16
|
+
* about `5e-13` for large ones.
|
|
17
|
+
*
|
|
18
|
+
* O(`maxIterations`) calls to the underlying expansion in the worst case, and
|
|
19
|
+
* far fewer in practice; O(1) memory.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* regularizedIncompleteGammaUpper(3, 20); // 4.555149505589215e-7
|
|
24
|
+
* 1 - regularizedIncompleteGamma(3, 20); // 4.5551495053697266e-7 — diverges at the 10th digit
|
|
25
|
+
* ```;
|
|
26
|
+
*
|
|
27
|
+
* @param a Shape parameter, positive.
|
|
28
|
+
* @param x Lower limit, non-negative.
|
|
29
|
+
* @param options Termination criteria; `tolerance` is relative and defaults to
|
|
30
|
+
* `Number.EPSILON`, `maxIterations` to 1000.
|
|
31
|
+
* @returns `Q(a, x)`, in `[0, 1]`.
|
|
32
|
+
* @throws {RangeError} When `a ≤ 0` or `x < 0`.
|
|
33
|
+
* @throws {ConvergenceError} When neither expansion met the tolerance.
|
|
34
|
+
* @see {@link regularizedIncompleteGamma} when the answer is near 0.
|
|
35
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
36
|
+
*/
|
|
37
|
+
export declare function regularizedIncompleteGammaUpper(a: number, x: number, options?: ConvergenceOptions): number;
|
|
38
|
+
//# sourceMappingURL=regularized-incomplete-gamma-upper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regularized-incomplete-gamma-upper.d.ts","sourceRoot":"","sources":["../src/regularized-incomplete-gamma-upper.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,+BAA+B,CAC9C,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CAoBR"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { assertIncompleteGammaDomain_, gammaLowerSeries_, gammaUpperContinuedFraction_, preferContinuedFraction_, } from "./_incomplete-gamma.js";
|
|
2
|
+
import { ConvergenceError } from "./convergence-error.js";
|
|
3
|
+
/**
|
|
4
|
+
* The upper regularized incomplete gamma `Q(a, x) = 1 − P(a, x)` — the fraction
|
|
5
|
+
* of a `Gamma(a, 1)` distribution lying above `x`.
|
|
6
|
+
*
|
|
7
|
+
* This is the survival function, and in practice it is the p-value: "how often
|
|
8
|
+
* would chance alone produce a result at least this extreme?". Reach for it
|
|
9
|
+
* whenever the answer is expected to be small, which for a p-value is always.
|
|
10
|
+
*
|
|
11
|
+
* Not a convenience wrapper over {@link regularizedIncompleteGamma}. The tail is
|
|
12
|
+
* computed directly, where `1 - P` subtracts two numbers agreeing to every
|
|
13
|
+
* digit that mattered: a chi-squared p-value of `1e-12` comes out exact here
|
|
14
|
+
* and as `0` there.
|
|
15
|
+
*
|
|
16
|
+
* Relative error is within a few ulps for ordinary parameters and stays under
|
|
17
|
+
* about `5e-13` for large ones.
|
|
18
|
+
*
|
|
19
|
+
* O(`maxIterations`) calls to the underlying expansion in the worst case, and
|
|
20
|
+
* far fewer in practice; O(1) memory.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* regularizedIncompleteGammaUpper(3, 20); // 4.555149505589215e-7
|
|
25
|
+
* 1 - regularizedIncompleteGamma(3, 20); // 4.5551495053697266e-7 — diverges at the 10th digit
|
|
26
|
+
* ```;
|
|
27
|
+
*
|
|
28
|
+
* @param a Shape parameter, positive.
|
|
29
|
+
* @param x Lower limit, non-negative.
|
|
30
|
+
* @param options Termination criteria; `tolerance` is relative and defaults to
|
|
31
|
+
* `Number.EPSILON`, `maxIterations` to 1000.
|
|
32
|
+
* @returns `Q(a, x)`, in `[0, 1]`.
|
|
33
|
+
* @throws {RangeError} When `a ≤ 0` or `x < 0`.
|
|
34
|
+
* @throws {ConvergenceError} When neither expansion met the tolerance.
|
|
35
|
+
* @see {@link regularizedIncompleteGamma} when the answer is near 0.
|
|
36
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
37
|
+
*/
|
|
38
|
+
export function regularizedIncompleteGammaUpper(a, x, options) {
|
|
39
|
+
assertIncompleteGammaDomain_("regularizedIncompleteGammaUpper", a, x);
|
|
40
|
+
if (x === 0) {
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
const useFraction = preferContinuedFraction_(a, x);
|
|
44
|
+
const result = useFraction
|
|
45
|
+
? gammaUpperContinuedFraction_(a, x, options)
|
|
46
|
+
: gammaLowerSeries_(a, x, options);
|
|
47
|
+
if (!result.converged) {
|
|
48
|
+
throw new ConvergenceError("regularizedIncompleteGammaUpper", result.iterations);
|
|
49
|
+
}
|
|
50
|
+
return useFraction ? result.value : 1 - result.value;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=regularized-incomplete-gamma-upper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regularized-incomplete-gamma-upper.js","sourceRoot":"","sources":["../src/regularized-incomplete-gamma-upper.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,4BAA4B,EAC5B,iBAAiB,EACjB,4BAA4B,EAC5B,wBAAwB,GACxB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,+BAA+B,CAC9C,CAAS,EACT,CAAS,EACT,OAA4B;IAE5B,4BAA4B,CAAC,iCAAiC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAEtE,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACb,OAAO,CAAC,CAAC;IACV,CAAC;IAED,MAAM,WAAW,GAAG,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,MAAM,MAAM,GAAG,WAAW;QACzB,CAAC,CAAC,4BAA4B,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC;QAC7C,CAAC,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;IAEpC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACvB,MAAM,IAAI,gBAAgB,CACzB,iCAAiC,EACjC,MAAM,CAAC,UAAU,CACjB,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* The lower regularized incomplete gamma `P(a, x)` — the fraction of a
|
|
4
|
+
* `Gamma(a, 1)` distribution lying at or below `x`.
|
|
5
|
+
*
|
|
6
|
+
* In plain terms it answers "what proportion of outcomes are no bigger than
|
|
7
|
+
* this?", which makes it the cumulative distribution function of the gamma,
|
|
8
|
+
* chi-squared and Poisson families. Feed it a chi-squared statistic and you get
|
|
9
|
+
* the probability of seeing something that small.
|
|
10
|
+
*
|
|
11
|
+
* **Regularized** is spelled out because libraries disagree about whether the
|
|
12
|
+
* bare name means this ratio in `[0, 1]` or the unnormalised integral. Guessing
|
|
13
|
+
* wrong is silent and off by a factor of `Γ(a)`.
|
|
14
|
+
*
|
|
15
|
+
* Reach for {@link regularizedIncompleteGammaUpper} when the answer is near 1:
|
|
16
|
+
* `1 - P` there has already thrown away the digits the upper form keeps, which
|
|
17
|
+
* is the difference between a p-value of `1e-12` and a p-value of `0`.
|
|
18
|
+
*
|
|
19
|
+
* Relative error is within a few ulps for ordinary parameters and stays under
|
|
20
|
+
* about `5e-13` for large ones.
|
|
21
|
+
*
|
|
22
|
+
* O(`maxIterations`) calls to the underlying expansion in the worst case, and
|
|
23
|
+
* far fewer in practice; O(1) memory.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* regularizedIncompleteGamma(0.5, 1); // 0.8427007929497148 — this is erf(1)
|
|
28
|
+
* regularizedIncompleteGamma(3, 20); // 0.9999995444850495
|
|
29
|
+
* 1 - regularizedIncompleteGamma(3, 20); // 4.5551495053697266e-7 — only 9 good digits
|
|
30
|
+
* ```;
|
|
31
|
+
*
|
|
32
|
+
* @param a Shape parameter, positive.
|
|
33
|
+
* @param x Upper limit, non-negative.
|
|
34
|
+
* @param options Termination criteria; `tolerance` is relative and defaults to
|
|
35
|
+
* `Number.EPSILON`, `maxIterations` to 1000.
|
|
36
|
+
* @returns `P(a, x)`, in `[0, 1]`.
|
|
37
|
+
* @throws {RangeError} When `a ≤ 0` or `x < 0`.
|
|
38
|
+
* @throws {ConvergenceError} When neither expansion met the tolerance.
|
|
39
|
+
* @see {@link regularizedIncompleteGammaUpper} when the answer is small.
|
|
40
|
+
* @see {@link inverseRegularizedIncompleteGamma} to go the other way.
|
|
41
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
42
|
+
*/
|
|
43
|
+
export declare function regularizedIncompleteGamma(a: number, x: number, options?: ConvergenceOptions): number;
|
|
44
|
+
//# sourceMappingURL=regularized-incomplete-gamma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regularized-incomplete-gamma.d.ts","sourceRoot":"","sources":["../src/regularized-incomplete-gamma.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,0BAA0B,CACzC,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CAiBR"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { assertIncompleteGammaDomain_, gammaLowerSeries_, gammaUpperContinuedFraction_, preferContinuedFraction_, } from "./_incomplete-gamma.js";
|
|
2
|
+
import { ConvergenceError } from "./convergence-error.js";
|
|
3
|
+
/**
|
|
4
|
+
* The lower regularized incomplete gamma `P(a, x)` — the fraction of a
|
|
5
|
+
* `Gamma(a, 1)` distribution lying at or below `x`.
|
|
6
|
+
*
|
|
7
|
+
* In plain terms it answers "what proportion of outcomes are no bigger than
|
|
8
|
+
* this?", which makes it the cumulative distribution function of the gamma,
|
|
9
|
+
* chi-squared and Poisson families. Feed it a chi-squared statistic and you get
|
|
10
|
+
* the probability of seeing something that small.
|
|
11
|
+
*
|
|
12
|
+
* **Regularized** is spelled out because libraries disagree about whether the
|
|
13
|
+
* bare name means this ratio in `[0, 1]` or the unnormalised integral. Guessing
|
|
14
|
+
* wrong is silent and off by a factor of `Γ(a)`.
|
|
15
|
+
*
|
|
16
|
+
* Reach for {@link regularizedIncompleteGammaUpper} when the answer is near 1:
|
|
17
|
+
* `1 - P` there has already thrown away the digits the upper form keeps, which
|
|
18
|
+
* is the difference between a p-value of `1e-12` and a p-value of `0`.
|
|
19
|
+
*
|
|
20
|
+
* Relative error is within a few ulps for ordinary parameters and stays under
|
|
21
|
+
* about `5e-13` for large ones.
|
|
22
|
+
*
|
|
23
|
+
* O(`maxIterations`) calls to the underlying expansion in the worst case, and
|
|
24
|
+
* far fewer in practice; O(1) memory.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* regularizedIncompleteGamma(0.5, 1); // 0.8427007929497148 — this is erf(1)
|
|
29
|
+
* regularizedIncompleteGamma(3, 20); // 0.9999995444850495
|
|
30
|
+
* 1 - regularizedIncompleteGamma(3, 20); // 4.5551495053697266e-7 — only 9 good digits
|
|
31
|
+
* ```;
|
|
32
|
+
*
|
|
33
|
+
* @param a Shape parameter, positive.
|
|
34
|
+
* @param x Upper limit, non-negative.
|
|
35
|
+
* @param options Termination criteria; `tolerance` is relative and defaults to
|
|
36
|
+
* `Number.EPSILON`, `maxIterations` to 1000.
|
|
37
|
+
* @returns `P(a, x)`, in `[0, 1]`.
|
|
38
|
+
* @throws {RangeError} When `a ≤ 0` or `x < 0`.
|
|
39
|
+
* @throws {ConvergenceError} When neither expansion met the tolerance.
|
|
40
|
+
* @see {@link regularizedIncompleteGammaUpper} when the answer is small.
|
|
41
|
+
* @see {@link inverseRegularizedIncompleteGamma} to go the other way.
|
|
42
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
43
|
+
*/
|
|
44
|
+
export function regularizedIncompleteGamma(a, x, options) {
|
|
45
|
+
assertIncompleteGammaDomain_("regularizedIncompleteGamma", a, x);
|
|
46
|
+
if (x === 0) {
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
const useFraction = preferContinuedFraction_(a, x);
|
|
50
|
+
const result = useFraction
|
|
51
|
+
? gammaUpperContinuedFraction_(a, x, options)
|
|
52
|
+
: gammaLowerSeries_(a, x, options);
|
|
53
|
+
if (!result.converged) {
|
|
54
|
+
throw new ConvergenceError("regularizedIncompleteGamma", result.iterations);
|
|
55
|
+
}
|
|
56
|
+
return useFraction ? 1 - result.value : result.value;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=regularized-incomplete-gamma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regularized-incomplete-gamma.js","sourceRoot":"","sources":["../src/regularized-incomplete-gamma.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,4BAA4B,EAC5B,iBAAiB,EACjB,4BAA4B,EAC5B,wBAAwB,GACxB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,UAAU,0BAA0B,CACzC,CAAS,EACT,CAAS,EACT,OAA4B;IAE5B,4BAA4B,CAAC,4BAA4B,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAEjE,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACb,OAAO,CAAC,CAAC;IACV,CAAC;IAED,MAAM,WAAW,GAAG,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,MAAM,MAAM,GAAG,WAAW;QACzB,CAAC,CAAC,4BAA4B,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC;QAC7C,CAAC,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;IAEpC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACvB,MAAM,IAAI,gBAAgB,CAAC,4BAA4B,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;AACtD,CAAC"}
|