@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,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When an iterative routine should stop — close enough, too many tries, or
|
|
3
|
+
* cancelled.
|
|
4
|
+
*
|
|
5
|
+
* Every iterative routine in this package takes the same shape, so a caller
|
|
6
|
+
* configures them one way rather than learning a new option object per
|
|
7
|
+
* function. Every field is optional; the defaults are chosen per routine and
|
|
8
|
+
* documented on it.
|
|
9
|
+
*
|
|
10
|
+
* What `tolerance` is measured _against_ is the one thing that necessarily
|
|
11
|
+
* differs — a bracket width, a relative update, a residual — so each function
|
|
12
|
+
* states it on its own parameter rather than here.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* bisect(f, 0, 2, { tolerance: 1e-6, maxIterations: 50 });
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @see [Convergence and iteration limits](https://anthochamp.github.io/node-essentials/topics/math/calculus/convergence/)
|
|
20
|
+
*/
|
|
21
|
+
export type ConvergenceOptions = {
|
|
22
|
+
/**
|
|
23
|
+
* Threshold below which the iteration is considered converged. Each routine
|
|
24
|
+
* documents the quantity it compares against this.
|
|
25
|
+
*/
|
|
26
|
+
tolerance?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Upper bound on iterations, so an input that never converges still
|
|
29
|
+
* terminates.
|
|
30
|
+
*/
|
|
31
|
+
maxIterations?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Aborts the iteration. Checked once per iteration, so the cost is bounded by
|
|
34
|
+
* `maxIterations` rather than by the work each iteration does.
|
|
35
|
+
*/
|
|
36
|
+
signal?: AbortSignal | null;
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=convergence-options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-options.d.ts","sourceRoot":"","sources":["../src/convergence-options.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAChC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;CAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-options.js","sourceRoot":"","sources":["../src/convergence-options.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an iterative routine produced, and whether it actually got there.
|
|
3
|
+
*
|
|
4
|
+
* `converged` is the point of the type. A routine that stopped because it ran
|
|
5
|
+
* out of iterations still has a best-so-far answer worth returning, and
|
|
6
|
+
* returning it bare would make that indistinguishable from an answer that met
|
|
7
|
+
* its tolerance. **Check `converged` before trusting `value`.**
|
|
8
|
+
*
|
|
9
|
+
* Routines that return a bare number throw {@link ConvergenceError} instead,
|
|
10
|
+
* because there a caller has no way to notice the shortfall.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const result = continuedFractionEval(fraction, { maxIterations: 5 });
|
|
15
|
+
* if (!result.converged) {
|
|
16
|
+
* // result.value is the best estimate after 5 steps, not the answer.
|
|
17
|
+
* }
|
|
18
|
+
* ```;
|
|
19
|
+
*
|
|
20
|
+
* @see [Convergence and iteration limits](https://anthochamp.github.io/node-essentials/topics/math/calculus/convergence/)
|
|
21
|
+
*/
|
|
22
|
+
export type ConvergenceResult<T> = {
|
|
23
|
+
/** Best estimate available when the iteration stopped. */
|
|
24
|
+
value: T;
|
|
25
|
+
/** How many iterations ran before stopping. */
|
|
26
|
+
iterations: number;
|
|
27
|
+
/**
|
|
28
|
+
* Whether the tolerance was met. `false` means the iteration limit was
|
|
29
|
+
* reached first and {@link ConvergenceResult.value} is unconverged.
|
|
30
|
+
*/
|
|
31
|
+
converged: boolean;
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=convergence-result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-result.d.ts","sourceRoot":"","sources":["../src/convergence-result.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;IAClC,0DAA0D;IAC1D,KAAK,EAAE,CAAC,CAAC;IAET,+CAA+C;IAC/C,UAAU,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,SAAS,EAAE,OAAO,CAAC;CACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-result.js","sourceRoot":"","sources":["../src/convergence-result.ts"],"names":[],"mappings":""}
|
package/dist/erf.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error function `erf(x) = 2/√π ∫₀ˣ e^(−t²) dt` — how much of a bell curve
|
|
3
|
+
* lies within a given distance of its centre.
|
|
4
|
+
*
|
|
5
|
+
* Concretely: `erf(x)` is the probability that a standard normal measurement
|
|
6
|
+
* lands within `x√2` of its mean. It is what turns "three sigma" into a number,
|
|
7
|
+
* and it is the building block of every normal-distribution tail probability.
|
|
8
|
+
*
|
|
9
|
+
* Reach for {@link erfc} whenever the answer is near 1. `erf` saturates to
|
|
10
|
+
* exactly 1 at about `x = 6`, so `1 - erf(x)` past that point returns zero
|
|
11
|
+
* rather than a small number, and the quantity you wanted is gone.
|
|
12
|
+
*
|
|
13
|
+
* O(1) time, O(1) memory in the sense that matters to a caller: the underlying
|
|
14
|
+
* expansion is iterative but bounded, and allocates nothing.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* erf(1); // 0.8427007929497148 — ~84% within one √2-sigma
|
|
19
|
+
* erf(6); // 1 — saturated; use erfc past here
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @param x Any real.
|
|
23
|
+
* @returns `erf(x)`, in `[−1, 1]`.
|
|
24
|
+
* @see {@link erfc} for the complement, and for tail probabilities.
|
|
25
|
+
* @see [The error function](https://anthochamp.github.io/node-essentials/topics/math/functions/error-functions/)
|
|
26
|
+
*/
|
|
27
|
+
export declare function erf(x: number): number;
|
|
28
|
+
//# sourceMappingURL=erf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"erf.d.ts","sourceRoot":"","sources":["../src/erf.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAiBrC"}
|
package/dist/erf.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { regularizedIncompleteGamma } from "./regularized-incomplete-gamma.js";
|
|
2
|
+
/**
|
|
3
|
+
* The error function `erf(x) = 2/√π ∫₀ˣ e^(−t²) dt` — how much of a bell curve
|
|
4
|
+
* lies within a given distance of its centre.
|
|
5
|
+
*
|
|
6
|
+
* Concretely: `erf(x)` is the probability that a standard normal measurement
|
|
7
|
+
* lands within `x√2` of its mean. It is what turns "three sigma" into a number,
|
|
8
|
+
* and it is the building block of every normal-distribution tail probability.
|
|
9
|
+
*
|
|
10
|
+
* Reach for {@link erfc} whenever the answer is near 1. `erf` saturates to
|
|
11
|
+
* exactly 1 at about `x = 6`, so `1 - erf(x)` past that point returns zero
|
|
12
|
+
* rather than a small number, and the quantity you wanted is gone.
|
|
13
|
+
*
|
|
14
|
+
* O(1) time, O(1) memory in the sense that matters to a caller: the underlying
|
|
15
|
+
* expansion is iterative but bounded, and allocates nothing.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* erf(1); // 0.8427007929497148 — ~84% within one √2-sigma
|
|
20
|
+
* erf(6); // 1 — saturated; use erfc past here
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param x Any real.
|
|
24
|
+
* @returns `erf(x)`, in `[−1, 1]`.
|
|
25
|
+
* @see {@link erfc} for the complement, and for tail probabilities.
|
|
26
|
+
* @see [The error function](https://anthochamp.github.io/node-essentials/topics/math/functions/error-functions/)
|
|
27
|
+
*/
|
|
28
|
+
export function erf(x) {
|
|
29
|
+
if (Number.isNaN(x)) {
|
|
30
|
+
return Number.NaN;
|
|
31
|
+
}
|
|
32
|
+
// `sign(x) · P(½, x²)` rather than a dedicated rational approximation: the
|
|
33
|
+
// incomplete gamma already covers this exact integral, and a second fitted
|
|
34
|
+
// polynomial would be a second thing to keep accurate.
|
|
35
|
+
const squared = x * x;
|
|
36
|
+
// Past `|x| ≈ 1.3e154` the square overflows, long after erf itself saturated.
|
|
37
|
+
if (!Number.isFinite(squared)) {
|
|
38
|
+
return Math.sign(x);
|
|
39
|
+
}
|
|
40
|
+
const magnitude = regularizedIncompleteGamma(0.5, squared);
|
|
41
|
+
return x < 0 ? -magnitude : magnitude;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=erf.js.map
|
package/dist/erf.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"erf.js","sourceRoot":"","sources":["../src/erf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,GAAG,CAAC,CAAS;IAC5B,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,2EAA2E;IAC3E,2EAA2E;IAC3E,uDAAuD;IACvD,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;IACtB,8EAA8E;IAC9E,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACrB,CAAC;IAED,MAAM,SAAS,GAAG,0BAA0B,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAE3D,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;AACvC,CAAC"}
|
package/dist/erfc.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The complementary error function `erfc(x) = 1 − erf(x)` — how much of a bell
|
|
3
|
+
* curve lies _beyond_ a given distance from its centre.
|
|
4
|
+
*
|
|
5
|
+
* This is the one you want for a tail probability, a p-value, or any "how
|
|
6
|
+
* unlikely is this?" question. It is not a convenience wrapper: computing it as
|
|
7
|
+
* `1 - erf(x)` loses the entire answer once `erf(x)` rounds to 1, from about `x
|
|
8
|
+
* = 6`. Computed directly, it keeps full relative precision down to the
|
|
9
|
+
* underflow limit near `x = 26.5`, which is what makes a normal tail
|
|
10
|
+
* probability of `1e-100` computable at all.
|
|
11
|
+
*
|
|
12
|
+
* The rule generalises across this package: **reach for the upper form when the
|
|
13
|
+
* answer is small.**
|
|
14
|
+
*
|
|
15
|
+
* O(1) time, O(1) memory in the sense that matters to a caller: the underlying
|
|
16
|
+
* expansion is iterative but bounded, and allocates nothing.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* erfc(6); // 2.1519736712498916e-17
|
|
21
|
+
* 1 - erf(6); // 0 — the same quantity, computed the wrong way
|
|
22
|
+
* erfc(20); // 5.395865611607905e-176
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @param x Any real.
|
|
26
|
+
* @returns `erfc(x)`, in `[0, 2]`.
|
|
27
|
+
* @see {@link erf} when the answer is near 0 rather than near 1.
|
|
28
|
+
* @see [The error function](https://anthochamp.github.io/node-essentials/topics/math/functions/error-functions/)
|
|
29
|
+
*/
|
|
30
|
+
export declare function erfc(x: number): number;
|
|
31
|
+
//# sourceMappingURL=erfc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"erfc.d.ts","sourceRoot":"","sources":["../src/erfc.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAiBtC"}
|
package/dist/erfc.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { regularizedIncompleteGammaUpper } from "./regularized-incomplete-gamma-upper.js";
|
|
2
|
+
import { regularizedIncompleteGamma } from "./regularized-incomplete-gamma.js";
|
|
3
|
+
/**
|
|
4
|
+
* The complementary error function `erfc(x) = 1 − erf(x)` — how much of a bell
|
|
5
|
+
* curve lies _beyond_ a given distance from its centre.
|
|
6
|
+
*
|
|
7
|
+
* This is the one you want for a tail probability, a p-value, or any "how
|
|
8
|
+
* unlikely is this?" question. It is not a convenience wrapper: computing it as
|
|
9
|
+
* `1 - erf(x)` loses the entire answer once `erf(x)` rounds to 1, from about `x
|
|
10
|
+
* = 6`. Computed directly, it keeps full relative precision down to the
|
|
11
|
+
* underflow limit near `x = 26.5`, which is what makes a normal tail
|
|
12
|
+
* probability of `1e-100` computable at all.
|
|
13
|
+
*
|
|
14
|
+
* The rule generalises across this package: **reach for the upper form when the
|
|
15
|
+
* answer is small.**
|
|
16
|
+
*
|
|
17
|
+
* O(1) time, O(1) memory in the sense that matters to a caller: the underlying
|
|
18
|
+
* expansion is iterative but bounded, and allocates nothing.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* erfc(6); // 2.1519736712498916e-17
|
|
23
|
+
* 1 - erf(6); // 0 — the same quantity, computed the wrong way
|
|
24
|
+
* erfc(20); // 5.395865611607905e-176
|
|
25
|
+
* ```;
|
|
26
|
+
*
|
|
27
|
+
* @param x Any real.
|
|
28
|
+
* @returns `erfc(x)`, in `[0, 2]`.
|
|
29
|
+
* @see {@link erf} when the answer is near 0 rather than near 1.
|
|
30
|
+
* @see [The error function](https://anthochamp.github.io/node-essentials/topics/math/functions/error-functions/)
|
|
31
|
+
*/
|
|
32
|
+
export function erfc(x) {
|
|
33
|
+
if (Number.isNaN(x)) {
|
|
34
|
+
return Number.NaN;
|
|
35
|
+
}
|
|
36
|
+
// The positive branch goes through the upper incomplete gamma, which computes
|
|
37
|
+
// the tail directly; the negative branch is near 2 and has nothing to lose, so
|
|
38
|
+
// it takes the lower form.
|
|
39
|
+
const squared = x * x;
|
|
40
|
+
// Past `|x| ≈ 1.3e154` the square overflows, long after erfc itself saturated.
|
|
41
|
+
if (!Number.isFinite(squared)) {
|
|
42
|
+
return x < 0 ? 2 : 0;
|
|
43
|
+
}
|
|
44
|
+
return x < 0
|
|
45
|
+
? 1 + regularizedIncompleteGamma(0.5, squared)
|
|
46
|
+
: regularizedIncompleteGammaUpper(0.5, squared);
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=erfc.js.map
|
package/dist/erfc.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"erfc.js","sourceRoot":"","sources":["../src/erfc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAC;AAC1F,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,IAAI,CAAC,CAAS;IAC7B,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,8EAA8E;IAC9E,+EAA+E;IAC/E,2BAA2B;IAC3B,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;IACtB,+EAA+E;IAC/E,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtB,CAAC;IAED,OAAO,CAAC,GAAG,CAAC;QACX,CAAC,CAAC,CAAC,GAAG,0BAA0B,CAAC,GAAG,EAAE,OAAO,CAAC;QAC9C,CAAC,CAAC,+BAA+B,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAClD,CAAC"}
|
package/dist/gamma.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gamma function `Γ(x)` — the factorial, extended to every real number.
|
|
3
|
+
*
|
|
4
|
+
* `Γ(n) = (n − 1)!` at each positive integer, and it joins those points with a
|
|
5
|
+
* smooth curve, so a formula that "should" contain a factorial can be handed
|
|
6
|
+
* 2.5 as readily as 3. That is why it turns up in the normalizing constant of
|
|
7
|
+
* almost every continuous probability distribution.
|
|
8
|
+
*
|
|
9
|
+
* Reach for {@link logGamma} the moment the result might not fit: `Γ` passes the
|
|
10
|
+
* top of binary64 at about `x = 171.6`. Reach for {@link beta} rather than
|
|
11
|
+
* writing `gamma(a) * gamma(b) / gamma(a + b)` yourself, which overflows long
|
|
12
|
+
* before the ratio it computes would.
|
|
13
|
+
*
|
|
14
|
+
* Relative error is within about six ulps for ordinary arguments and stays
|
|
15
|
+
* under `5e-13` at the extremes of the representable range.
|
|
16
|
+
*
|
|
17
|
+
* O(1) time, O(1) memory — a fixed nine-term series, plus at most one
|
|
18
|
+
* reflection step for arguments below `0.5`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* gamma(5); // 23.999999999999986 — 4!, to within four ulps
|
|
23
|
+
* gamma(0.5); // 1.7724538509055152 — √π
|
|
24
|
+
* gamma(-1); // NaN — a pole
|
|
25
|
+
* gamma(172); // Infinity — past the binary64 range
|
|
26
|
+
* ```;
|
|
27
|
+
*
|
|
28
|
+
* @param x Any real other than a non-positive integer.
|
|
29
|
+
* @returns `Γ(x)`; `±Infinity` where the true value exceeds binary64, and `NaN`
|
|
30
|
+
* at the non-positive integers, where Γ has a pole whose one-sided limits
|
|
31
|
+
* disagree in sign.
|
|
32
|
+
* @see {@link logGamma} for arguments whose Γ overflows.
|
|
33
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
34
|
+
*/
|
|
35
|
+
export declare function gamma(x: number): number;
|
|
36
|
+
//# sourceMappingURL=gamma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gamma.d.ts","sourceRoot":"","sources":["../src/gamma.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CA2BvC"}
|
package/dist/gamma.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SQRT_TWO_PI } from "@ac-kit/math-scalar";
|
|
2
|
+
import { LANCZOS_G_, lanczosSeries_ } from "./_lanczos.js";
|
|
3
|
+
/**
|
|
4
|
+
* The gamma function `Γ(x)` — the factorial, extended to every real number.
|
|
5
|
+
*
|
|
6
|
+
* `Γ(n) = (n − 1)!` at each positive integer, and it joins those points with a
|
|
7
|
+
* smooth curve, so a formula that "should" contain a factorial can be handed
|
|
8
|
+
* 2.5 as readily as 3. That is why it turns up in the normalizing constant of
|
|
9
|
+
* almost every continuous probability distribution.
|
|
10
|
+
*
|
|
11
|
+
* Reach for {@link logGamma} the moment the result might not fit: `Γ` passes the
|
|
12
|
+
* top of binary64 at about `x = 171.6`. Reach for {@link beta} rather than
|
|
13
|
+
* writing `gamma(a) * gamma(b) / gamma(a + b)` yourself, which overflows long
|
|
14
|
+
* before the ratio it computes would.
|
|
15
|
+
*
|
|
16
|
+
* Relative error is within about six ulps for ordinary arguments and stays
|
|
17
|
+
* under `5e-13` at the extremes of the representable range.
|
|
18
|
+
*
|
|
19
|
+
* O(1) time, O(1) memory — a fixed nine-term series, plus at most one
|
|
20
|
+
* reflection step for arguments below `0.5`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* gamma(5); // 23.999999999999986 — 4!, to within four ulps
|
|
25
|
+
* gamma(0.5); // 1.7724538509055152 — √π
|
|
26
|
+
* gamma(-1); // NaN — a pole
|
|
27
|
+
* gamma(172); // Infinity — past the binary64 range
|
|
28
|
+
* ```;
|
|
29
|
+
*
|
|
30
|
+
* @param x Any real other than a non-positive integer.
|
|
31
|
+
* @returns `Γ(x)`; `±Infinity` where the true value exceeds binary64, and `NaN`
|
|
32
|
+
* at the non-positive integers, where Γ has a pole whose one-sided limits
|
|
33
|
+
* disagree in sign.
|
|
34
|
+
* @see {@link logGamma} for arguments whose Γ overflows.
|
|
35
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
36
|
+
*/
|
|
37
|
+
export function gamma(x) {
|
|
38
|
+
if (x < 0.5) {
|
|
39
|
+
if (Number.isInteger(x)) {
|
|
40
|
+
return Number.NaN;
|
|
41
|
+
}
|
|
42
|
+
// Euler's reflection formula, which trades an argument Lanczos cannot
|
|
43
|
+
// serve for one it can.
|
|
44
|
+
return Math.PI / (Math.sin(Math.PI * x) * gamma(1 - x));
|
|
45
|
+
}
|
|
46
|
+
// Evaluated as a product rather than as `exp(logGamma(x))`, which is the less
|
|
47
|
+
// accurate of the two because taking the log and undoing it magnifies its
|
|
48
|
+
// rounding by `ln Γ(x)`. Measured against the exact factorials: the product is
|
|
49
|
+
// 4 ulps low at `x = 5` and exact at `x = 10`, where the exponentiated log is
|
|
50
|
+
// 11 low and 19 high respectively.
|
|
51
|
+
const z = x - 1;
|
|
52
|
+
const t = z + LANCZOS_G_ + 0.5;
|
|
53
|
+
// Splitting the power in half keeps the intermediate inside binary64 for
|
|
54
|
+
// arguments whose Γ is itself still finite; `t ** (z + 0.5)` alone overflows
|
|
55
|
+
// from about `x = 142` while Γ does not until `x ≈ 171.6`.
|
|
56
|
+
const halfPower = t ** ((z + 0.5) / 2);
|
|
57
|
+
return (SQRT_TWO_PI * lanczosSeries_(z) * halfPower * (Math.exp(-t) * halfPower));
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=gamma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gamma.js","sourceRoot":"","sources":["../src/gamma.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,KAAK,CAAC,CAAS;IAC9B,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACb,IAAI,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;YACzB,OAAO,MAAM,CAAC,GAAG,CAAC;QACnB,CAAC;QAED,sEAAsE;QACtE,wBAAwB;QACxB,OAAO,IAAI,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC;IAED,8EAA8E;IAC9E,0EAA0E;IAC1E,+EAA+E;IAC/E,8EAA8E;IAC9E,mCAAmC;IACnC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,GAAG,GAAG,CAAC;IAE/B,yEAAyE;IACzE,6EAA6E;IAC7E,2DAA2D;IAC3D,MAAM,SAAS,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAEvC,OAAO,CACN,WAAW,GAAG,cAAc,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CACxE,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from "./beta.js";
|
|
2
|
+
export * from "./bisect.js";
|
|
3
|
+
export * from "./continued-fraction-eval.js";
|
|
4
|
+
export * from "./convergence-error.js";
|
|
5
|
+
export * from "./convergence-options.js";
|
|
6
|
+
export * from "./convergence-result.js";
|
|
7
|
+
export * from "./erf.js";
|
|
8
|
+
export * from "./erfc.js";
|
|
9
|
+
export * from "./gamma.js";
|
|
10
|
+
export * from "./inverse-regularized-incomplete-beta.js";
|
|
11
|
+
export * from "./inverse-regularized-incomplete-gamma.js";
|
|
12
|
+
export * from "./log-beta.js";
|
|
13
|
+
export * from "./log-gamma.js";
|
|
14
|
+
export * from "./regularized-incomplete-beta.js";
|
|
15
|
+
export * from "./regularized-incomplete-gamma-upper.js";
|
|
16
|
+
export * from "./regularized-incomplete-gamma.js";
|
|
17
|
+
export * from "./trapezoidal-rule.js";
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,0CAA0C,CAAC;AACzD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;AACxD,cAAc,mCAAmC,CAAC;AAClD,cAAc,uBAAuB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from "./beta.js";
|
|
2
|
+
export * from "./bisect.js";
|
|
3
|
+
export * from "./continued-fraction-eval.js";
|
|
4
|
+
export * from "./convergence-error.js";
|
|
5
|
+
export * from "./convergence-options.js";
|
|
6
|
+
export * from "./convergence-result.js";
|
|
7
|
+
export * from "./erf.js";
|
|
8
|
+
export * from "./erfc.js";
|
|
9
|
+
export * from "./gamma.js";
|
|
10
|
+
export * from "./inverse-regularized-incomplete-beta.js";
|
|
11
|
+
export * from "./inverse-regularized-incomplete-gamma.js";
|
|
12
|
+
export * from "./log-beta.js";
|
|
13
|
+
export * from "./log-gamma.js";
|
|
14
|
+
export * from "./regularized-incomplete-beta.js";
|
|
15
|
+
export * from "./regularized-incomplete-gamma-upper.js";
|
|
16
|
+
export * from "./regularized-incomplete-gamma.js";
|
|
17
|
+
export * from "./trapezoidal-rule.js";
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,0CAA0C,CAAC;AACzD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;AACxD,cAAc,mCAAmC,CAAC;AAClD,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* The inverse of {@link regularizedIncompleteBeta} in its third argument: the
|
|
4
|
+
* `x` at which `Iₓ(a, b)` equals `p`.
|
|
5
|
+
*
|
|
6
|
+
* The quantile function of a `Beta(a, b)` distribution, and what the `Beta`,
|
|
7
|
+
* `F` and `StudentT` quantiles in `@ac-kit/math-stats` are built on. Use it to
|
|
8
|
+
* turn a confidence level into a critical value, or a percentile into the
|
|
9
|
+
* proportion that sits there.
|
|
10
|
+
*
|
|
11
|
+
* Accurate to within a few ulps in the round trip: feeding the result back
|
|
12
|
+
* through `Iₓ` returns the `p` you started from to about `1e-15` relative.
|
|
13
|
+
*
|
|
14
|
+
* O(`maxIterations`) evaluations of `Iₓ` in the worst case; three or four in
|
|
15
|
+
* practice, because convergence is cubic. O(1) memory.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* inverseRegularizedIncompleteBeta(2, 3, 0.5); // 0.38572756813238934
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @param a First shape parameter, positive.
|
|
23
|
+
* @param b Second shape parameter, positive.
|
|
24
|
+
* @param p Probability, in `[0, 1]`.
|
|
25
|
+
* @param options Termination criteria; `tolerance` is compared against the
|
|
26
|
+
* relative size of the refinement step and defaults to `Number.EPSILON`,
|
|
27
|
+
* `maxIterations` to 100.
|
|
28
|
+
* @returns The `x` with `Iₓ(a, b) = p`; `0` at `p = 0` and `1` at `p = 1`.
|
|
29
|
+
* @throws {RangeError} When `a ≤ 0`, `b ≤ 0`, or `p` is outside `[0, 1]`.
|
|
30
|
+
* @see {@link regularizedIncompleteBeta} for the forward direction.
|
|
31
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
32
|
+
*/
|
|
33
|
+
export declare function inverseRegularizedIncompleteBeta(a: number, b: number, p: number, options?: ConvergenceOptions): number;
|
|
34
|
+
//# sourceMappingURL=inverse-regularized-incomplete-beta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inverse-regularized-incomplete-beta.d.ts","sourceRoot":"","sources":["../src/inverse-regularized-incomplete-beta.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAuCnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,gCAAgC,CAC/C,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CAwFR"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { betaPowerTerms_ } from "./_beta-power-terms.js";
|
|
2
|
+
import { assertBetaShape_ } from "./_incomplete-beta.js";
|
|
3
|
+
import { normalQuantileGuess_ } from "./_normal-quantile-guess.js";
|
|
4
|
+
import { regularizedIncompleteBeta } from "./regularized-incomplete-beta.js";
|
|
5
|
+
/**
|
|
6
|
+
* Default relative step below which the iterate is taken as converged. Four
|
|
7
|
+
* ulps, for the reason given on the gamma inverse's own constant.
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_TOLERANCE_ = 4 * Number.EPSILON;
|
|
10
|
+
/**
|
|
11
|
+
* A starting point good to a few digits.
|
|
12
|
+
*
|
|
13
|
+
* With both shapes at or above 1 the distribution is close enough to normal for
|
|
14
|
+
* the Carter–Wilson transform of a normal quantile to land nearby. Below that
|
|
15
|
+
* the mass piles up at one or both endpoints, so each end is inverted from its
|
|
16
|
+
* own leading power term instead.
|
|
17
|
+
*/
|
|
18
|
+
function initialGuess_(a, b, p) {
|
|
19
|
+
if (a >= 1 && b >= 1) {
|
|
20
|
+
const z = normalQuantileGuess_(p);
|
|
21
|
+
const squared = (z * z - 3) / 6;
|
|
22
|
+
const harmonic = 2 / (1 / (2 * a - 1) + 1 / (2 * b - 1));
|
|
23
|
+
const correction = (z * Math.sqrt(squared + harmonic)) / harmonic -
|
|
24
|
+
(1 / (2 * b - 1) - 1 / (2 * a - 1)) *
|
|
25
|
+
(squared + 5 / 6 - 2 / (3 * harmonic));
|
|
26
|
+
return a / (a + b * Math.exp(2 * correction));
|
|
27
|
+
}
|
|
28
|
+
const lowerWeight = (a / (a + b)) ** a / a;
|
|
29
|
+
const upperWeight = (b / (a + b)) ** b / b;
|
|
30
|
+
const total = lowerWeight + upperWeight;
|
|
31
|
+
return p < lowerWeight / total
|
|
32
|
+
? (a * total * p) ** (1 / a)
|
|
33
|
+
: 1 - (b * total * (1 - p)) ** (1 / b);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The inverse of {@link regularizedIncompleteBeta} in its third argument: the
|
|
37
|
+
* `x` at which `Iₓ(a, b)` equals `p`.
|
|
38
|
+
*
|
|
39
|
+
* The quantile function of a `Beta(a, b)` distribution, and what the `Beta`,
|
|
40
|
+
* `F` and `StudentT` quantiles in `@ac-kit/math-stats` are built on. Use it to
|
|
41
|
+
* turn a confidence level into a critical value, or a percentile into the
|
|
42
|
+
* proportion that sits there.
|
|
43
|
+
*
|
|
44
|
+
* Accurate to within a few ulps in the round trip: feeding the result back
|
|
45
|
+
* through `Iₓ` returns the `p` you started from to about `1e-15` relative.
|
|
46
|
+
*
|
|
47
|
+
* O(`maxIterations`) evaluations of `Iₓ` in the worst case; three or four in
|
|
48
|
+
* practice, because convergence is cubic. O(1) memory.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* inverseRegularizedIncompleteBeta(2, 3, 0.5); // 0.38572756813238934
|
|
53
|
+
* ```;
|
|
54
|
+
*
|
|
55
|
+
* @param a First shape parameter, positive.
|
|
56
|
+
* @param b Second shape parameter, positive.
|
|
57
|
+
* @param p Probability, in `[0, 1]`.
|
|
58
|
+
* @param options Termination criteria; `tolerance` is compared against the
|
|
59
|
+
* relative size of the refinement step and defaults to `Number.EPSILON`,
|
|
60
|
+
* `maxIterations` to 100.
|
|
61
|
+
* @returns The `x` with `Iₓ(a, b) = p`; `0` at `p = 0` and `1` at `p = 1`.
|
|
62
|
+
* @throws {RangeError} When `a ≤ 0`, `b ≤ 0`, or `p` is outside `[0, 1]`.
|
|
63
|
+
* @see {@link regularizedIncompleteBeta} for the forward direction.
|
|
64
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
65
|
+
*/
|
|
66
|
+
export function inverseRegularizedIncompleteBeta(a, b, p, options) {
|
|
67
|
+
// Halley's method, for the same reason as the gamma inverse: the curvature
|
|
68
|
+
// term costs one multiply and buys cubic convergence. The iterate is kept
|
|
69
|
+
// strictly inside `(0, 1)` by halving back toward the endpoint it overshot,
|
|
70
|
+
// since the density is unbounded there for shapes below 1 and a single step
|
|
71
|
+
// past the edge is unrecoverable.
|
|
72
|
+
assertBetaShape_("inverseRegularizedIncompleteBeta", a, b);
|
|
73
|
+
if (!(p >= 0 && p <= 1)) {
|
|
74
|
+
throw new RangeError(`inverseRegularizedIncompleteBeta: p must be in [0, 1], got ${p}`);
|
|
75
|
+
}
|
|
76
|
+
if (p === 0 || p === 1) {
|
|
77
|
+
return p;
|
|
78
|
+
}
|
|
79
|
+
const tolerance = options?.tolerance ?? DEFAULT_TOLERANCE_;
|
|
80
|
+
const maxIterations = options?.maxIterations ?? 100;
|
|
81
|
+
const { signal } = options ?? {};
|
|
82
|
+
// Clamped off the endpoints: for shapes below 1 the guess can land on 0 or 1
|
|
83
|
+
// outright, and the iteration has nowhere to go from there even when the true
|
|
84
|
+
// quantile is a representable float just inside.
|
|
85
|
+
let x = Math.min(Math.max(initialGuess_(a, b, p), Number.MIN_VALUE), 1 - Number.EPSILON / 2);
|
|
86
|
+
// The guess can be far out when the two shapes differ sharply and `p` is
|
|
87
|
+
// extreme — at `a = 1, b = 300, p = 1e-8` it lands nine orders of magnitude
|
|
88
|
+
// away. Bracketing makes every iteration either a Halley step that provably
|
|
89
|
+
// improves or a bisection that provably halves.
|
|
90
|
+
let lower = 0;
|
|
91
|
+
let upper = 1;
|
|
92
|
+
// The closest iterate rather than the last, for the reason given on the gamma
|
|
93
|
+
// inverse.
|
|
94
|
+
let best = x;
|
|
95
|
+
let bestResidual = Number.POSITIVE_INFINITY;
|
|
96
|
+
for (let index = 0; index < maxIterations; index++) {
|
|
97
|
+
signal?.throwIfAborted();
|
|
98
|
+
if (x <= 0 || x >= 1) {
|
|
99
|
+
return x <= 0 ? 0 : 1;
|
|
100
|
+
}
|
|
101
|
+
const complement = 1 - x;
|
|
102
|
+
const residual = regularizedIncompleteBeta(a, b, x, options) - p;
|
|
103
|
+
if (residual === 0) {
|
|
104
|
+
return x;
|
|
105
|
+
}
|
|
106
|
+
if (Math.abs(residual) < bestResidual) {
|
|
107
|
+
bestResidual = Math.abs(residual);
|
|
108
|
+
best = x;
|
|
109
|
+
}
|
|
110
|
+
if (residual > 0) {
|
|
111
|
+
upper = x;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
lower = x;
|
|
115
|
+
}
|
|
116
|
+
const density = betaPowerTerms_(a, b, x, complement) / (x * complement);
|
|
117
|
+
const newton = density > 0 ? residual / density : 0;
|
|
118
|
+
const step = newton /
|
|
119
|
+
(1 - 0.5 * Math.min(1, newton * ((a - 1) / x - (b - 1) / complement)));
|
|
120
|
+
// Checked before the bracket below, for the reason given on the gamma
|
|
121
|
+
// inverse: a sub-ulp step lands back on `x` and would be bisected away.
|
|
122
|
+
if (Math.abs(step) < tolerance * x) {
|
|
123
|
+
return x - step;
|
|
124
|
+
}
|
|
125
|
+
let next = x - step;
|
|
126
|
+
if (!(next > lower && next < upper)) {
|
|
127
|
+
next = 0.5 * (lower + upper);
|
|
128
|
+
}
|
|
129
|
+
if (next === x || upper - lower < tolerance * x) {
|
|
130
|
+
return best;
|
|
131
|
+
}
|
|
132
|
+
x = next;
|
|
133
|
+
}
|
|
134
|
+
return best;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=inverse-regularized-incomplete-beta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inverse-regularized-incomplete-beta.js","sourceRoot":"","sources":["../src/inverse-regularized-incomplete-beta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAE7E;;;GAGG;AACH,MAAM,kBAAkB,GAAG,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC;AAE9C;;;;;;;GAOG;AACH,SAAS,aAAa,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS;IACrD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACtB,MAAM,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,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,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;QAExE,sEAAsE;QACtE,wEAAwE;QACxE,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,SAAS,GAAG,CAAC,EAAE,CAAC;YACpC,OAAO,CAAC,GAAG,IAAI,CAAC;QACjB,CAAC;QAED,IAAI,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,CAAC,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,CAAC,EAAE,CAAC;YACrC,IAAI,GAAG,GAAG,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;QAC9B,CAAC;QAED,IAAI,IAAI,KAAK,CAAC,IAAI,KAAK,GAAG,KAAK,GAAG,SAAS,GAAG,CAAC,EAAE,CAAC;YACjD,OAAO,IAAI,CAAC;QACb,CAAC;QAED,CAAC,GAAG,IAAI,CAAC;IACV,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* The inverse of {@link regularizedIncompleteGamma} in its second argument: the
|
|
4
|
+
* `x` at which `P(a, x)` equals `p`.
|
|
5
|
+
*
|
|
6
|
+
* This is the quantile function — the "which value sits at the 95th
|
|
7
|
+
* percentile?" direction, where `P` answers "which percentile is this value
|
|
8
|
+
* at?". It is what the `Gamma`, `ChiSquared` and `Poisson` quantiles in
|
|
9
|
+
* `@ac-kit/math-stats` are built on, and what turns a confidence level into a
|
|
10
|
+
* critical value.
|
|
11
|
+
*
|
|
12
|
+
* Accurate to within a few ulps in the round trip: feeding the result back
|
|
13
|
+
* through `P` returns the `p` you started from to about `1e-15` relative.
|
|
14
|
+
*
|
|
15
|
+
* O(`maxIterations`) evaluations of `P` or `Q` in the worst case; three or four
|
|
16
|
+
* in practice, because convergence is cubic. O(1) memory.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const median = inverseRegularizedIncompleteGamma(3, 0.5); // 2.674060313723559
|
|
21
|
+
* regularizedIncompleteGamma(3, median); // 0.4999999999999999 — back where we started
|
|
22
|
+
* ```;
|
|
23
|
+
*
|
|
24
|
+
* @param a Shape parameter, positive.
|
|
25
|
+
* @param p Probability, in `[0, 1]`.
|
|
26
|
+
* @param options Termination criteria; `tolerance` is compared against the
|
|
27
|
+
* relative size of the refinement step and defaults to `Number.EPSILON`,
|
|
28
|
+
* `maxIterations` to 100.
|
|
29
|
+
* @returns The `x` with `P(a, x) = p`; `0` at `p = 0` and `Infinity` at `p =
|
|
30
|
+
* 1`.
|
|
31
|
+
* @throws {RangeError} When `a ≤ 0` or `p` is outside `[0, 1]`.
|
|
32
|
+
* @see {@link regularizedIncompleteGamma} for the forward direction.
|
|
33
|
+
* @see [Incomplete gamma and beta](https://anthochamp.github.io/node-essentials/topics/math/functions/incomplete-gamma-beta/)
|
|
34
|
+
*/
|
|
35
|
+
export declare function inverseRegularizedIncompleteGamma(a: number, p: number, options?: ConvergenceOptions): number;
|
|
36
|
+
//# sourceMappingURL=inverse-regularized-incomplete-gamma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inverse-regularized-incomplete-gamma.d.ts","sourceRoot":"","sources":["../src/inverse-regularized-incomplete-gamma.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AA6CnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,iCAAiC,CAChD,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CA8FR"}
|