@ac-kit/math-stats 0.6.0 → 0.7.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.
Files changed (141) hide show
  1. package/CHANGELOG.md +123 -0
  2. package/README.md +43 -2
  3. package/dist/_digamma.d.ts +10 -0
  4. package/dist/_digamma.d.ts.map +1 -0
  5. package/dist/_digamma.js +38 -0
  6. package/dist/_digamma.js.map +1 -0
  7. package/dist/_dilogarithm.d.ts +14 -0
  8. package/dist/_dilogarithm.d.ts.map +1 -0
  9. package/dist/_dilogarithm.js +77 -0
  10. package/dist/_dilogarithm.js.map +1 -0
  11. package/dist/_incomplete-gamma-shape-derivative.d.ts +17 -0
  12. package/dist/_incomplete-gamma-shape-derivative.d.ts.map +1 -0
  13. package/dist/_incomplete-gamma-shape-derivative.js +90 -0
  14. package/dist/_incomplete-gamma-shape-derivative.js.map +1 -0
  15. package/dist/_logistic.d.ts +14 -0
  16. package/dist/_logistic.d.ts.map +1 -0
  17. package/dist/_logistic.js +29 -0
  18. package/dist/_logistic.js.map +1 -0
  19. package/dist/_normal-tail.d.ts +43 -0
  20. package/dist/_normal-tail.d.ts.map +1 -0
  21. package/dist/_normal-tail.js +124 -0
  22. package/dist/_normal-tail.js.map +1 -0
  23. package/dist/_rank-test.d.ts +71 -0
  24. package/dist/_rank-test.d.ts.map +1 -0
  25. package/dist/_rank-test.js +130 -0
  26. package/dist/_rank-test.js.map +1 -0
  27. package/dist/average.js +2 -2
  28. package/dist/average.js.map +1 -1
  29. package/dist/bootstrap.d.ts +67 -0
  30. package/dist/bootstrap.d.ts.map +1 -0
  31. package/dist/bootstrap.js +154 -0
  32. package/dist/bootstrap.js.map +1 -0
  33. package/dist/censored-logistic-distribution.d.ts +23 -0
  34. package/dist/censored-logistic-distribution.d.ts.map +1 -0
  35. package/dist/censored-logistic-distribution.js +63 -0
  36. package/dist/censored-logistic-distribution.js.map +1 -0
  37. package/dist/censored-shifted-gamma-distribution.d.ts +23 -0
  38. package/dist/censored-shifted-gamma-distribution.d.ts.map +1 -0
  39. package/dist/censored-shifted-gamma-distribution.js +73 -0
  40. package/dist/censored-shifted-gamma-distribution.js.map +1 -0
  41. package/dist/confidence-interval.d.ts +53 -0
  42. package/dist/confidence-interval.d.ts.map +1 -1
  43. package/dist/confidence-interval.js +73 -0
  44. package/dist/confidence-interval.js.map +1 -1
  45. package/dist/correlation.d.ts +41 -0
  46. package/dist/correlation.d.ts.map +1 -1
  47. package/dist/correlation.js +168 -2
  48. package/dist/correlation.js.map +1 -1
  49. package/dist/descriptive.d.ts +129 -0
  50. package/dist/descriptive.d.ts.map +1 -0
  51. package/dist/descriptive.js +289 -0
  52. package/dist/descriptive.js.map +1 -0
  53. package/dist/forecast/_forecast-checks.d.ts +30 -0
  54. package/dist/forecast/_forecast-checks.d.ts.map +1 -0
  55. package/dist/forecast/_forecast-checks.js +119 -0
  56. package/dist/forecast/_forecast-checks.js.map +1 -0
  57. package/dist/forecast/_score-kernels.d.ts +63 -0
  58. package/dist/forecast/_score-kernels.d.ts.map +1 -0
  59. package/dist/forecast/_score-kernels.js +264 -0
  60. package/dist/forecast/_score-kernels.js.map +1 -0
  61. package/dist/forecast/brier-score.d.ts +102 -0
  62. package/dist/forecast/brier-score.d.ts.map +1 -0
  63. package/dist/forecast/brier-score.js +163 -0
  64. package/dist/forecast/brier-score.js.map +1 -0
  65. package/dist/forecast/crps-closed-form.d.ts +64 -0
  66. package/dist/forecast/crps-closed-form.d.ts.map +1 -0
  67. package/dist/forecast/crps-closed-form.js +106 -0
  68. package/dist/forecast/crps-closed-form.js.map +1 -0
  69. package/dist/forecast/crps-ensemble.d.ts +53 -0
  70. package/dist/forecast/crps-ensemble.d.ts.map +1 -0
  71. package/dist/forecast/crps-ensemble.js +121 -0
  72. package/dist/forecast/crps-ensemble.js.map +1 -0
  73. package/dist/forecast/emos.d.ts +158 -0
  74. package/dist/forecast/emos.d.ts.map +1 -0
  75. package/dist/forecast/emos.js +397 -0
  76. package/dist/forecast/emos.js.map +1 -0
  77. package/dist/forecast/exceedance-probability.d.ts +54 -0
  78. package/dist/forecast/exceedance-probability.d.ts.map +1 -0
  79. package/dist/forecast/exceedance-probability.js +125 -0
  80. package/dist/forecast/exceedance-probability.js.map +1 -0
  81. package/dist/forecast/log-score.d.ts +18 -0
  82. package/dist/forecast/log-score.d.ts.map +1 -0
  83. package/dist/forecast/log-score.js +19 -0
  84. package/dist/forecast/log-score.js.map +1 -0
  85. package/dist/forecast/logistic-regression.d.ts +123 -0
  86. package/dist/forecast/logistic-regression.d.ts.map +1 -0
  87. package/dist/forecast/logistic-regression.js +282 -0
  88. package/dist/forecast/logistic-regression.js.map +1 -0
  89. package/dist/forecast/rank-histogram.d.ts +32 -0
  90. package/dist/forecast/rank-histogram.d.ts.map +1 -0
  91. package/dist/forecast/rank-histogram.js +63 -0
  92. package/dist/forecast/rank-histogram.js.map +1 -0
  93. package/dist/forecast/reliability-diagram.d.ts +70 -0
  94. package/dist/forecast/reliability-diagram.d.ts.map +1 -0
  95. package/dist/forecast/reliability-diagram.js +126 -0
  96. package/dist/forecast/reliability-diagram.js.map +1 -0
  97. package/dist/forecast/roc-curve.d.ts +58 -0
  98. package/dist/forecast/roc-curve.d.ts.map +1 -0
  99. package/dist/forecast/roc-curve.js +124 -0
  100. package/dist/forecast/roc-curve.js.map +1 -0
  101. package/dist/index.d.ts +21 -0
  102. package/dist/index.d.ts.map +1 -1
  103. package/dist/index.js +21 -0
  104. package/dist/index.js.map +1 -1
  105. package/dist/mann-whitney-u.d.ts +27 -0
  106. package/dist/mann-whitney-u.d.ts.map +1 -0
  107. package/dist/mann-whitney-u.js +94 -0
  108. package/dist/mann-whitney-u.js.map +1 -0
  109. package/dist/multiple-regression.d.ts +123 -0
  110. package/dist/multiple-regression.d.ts.map +1 -0
  111. package/dist/multiple-regression.js +208 -0
  112. package/dist/multiple-regression.js.map +1 -0
  113. package/dist/principal-components.d.ts +66 -0
  114. package/dist/principal-components.d.ts.map +1 -0
  115. package/dist/principal-components.js +105 -0
  116. package/dist/principal-components.js.map +1 -0
  117. package/dist/quantile.d.ts +97 -9
  118. package/dist/quantile.d.ts.map +1 -1
  119. package/dist/quantile.js +298 -44
  120. package/dist/quantile.js.map +1 -1
  121. package/dist/regression.d.ts +46 -0
  122. package/dist/regression.d.ts.map +1 -1
  123. package/dist/regression.js +99 -1
  124. package/dist/regression.js.map +1 -1
  125. package/dist/sample.d.ts +29 -0
  126. package/dist/sample.d.ts.map +1 -0
  127. package/dist/sample.js +39 -0
  128. package/dist/sample.js.map +1 -0
  129. package/dist/t-digest.d.ts +136 -0
  130. package/dist/t-digest.d.ts.map +1 -0
  131. package/dist/t-digest.js +424 -0
  132. package/dist/t-digest.js.map +1 -0
  133. package/dist/truncated-normal-distribution.d.ts +28 -0
  134. package/dist/truncated-normal-distribution.d.ts.map +1 -0
  135. package/dist/truncated-normal-distribution.js +198 -0
  136. package/dist/truncated-normal-distribution.js.map +1 -0
  137. package/dist/wilcoxon-signed-rank.d.ts +42 -0
  138. package/dist/wilcoxon-signed-rank.d.ts.map +1 -0
  139. package/dist/wilcoxon-signed-rank.js +117 -0
  140. package/dist/wilcoxon-signed-rank.js.map +1 -0
  141. package/package.json +14 -10
package/CHANGELOG.md CHANGED
@@ -1,5 +1,128 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5c203e7: New descriptive statistics, accumulated exactly: `interquartileRange`,
8
+ `covariance`, `covarianceMatrix` (a `@ac-kit/math-linear` `Matrix`),
9
+ `rSquared`, `rootMeanSquareError`, `weightedMean`, `weightedVariance` and
10
+ `weightedStandardDeviation` (frequency or reliability weights), `standardize`,
11
+ `normalize`, `movingAverage`, `exponentialSmoothing` and the streaming
12
+ `ExponentialMovingAverage` (from `alpha` or a `halfLife`).
13
+ - 5c203e7: Calibration and verification of probabilistic forecasts, and three predictive
14
+ laws. `@ac-kit/math-stats` now depends on `@ac-kit/math-linear`.
15
+
16
+ - `exceedanceProbability(members, threshold, { weights, inclusive,
17
+ plottingPosition })`: the weighted share of ensemble members beyond the
18
+ threshold, or the probability through plotting positions (`"weibull"`,
19
+ `"blom"`, `"cunnane"`, `"gringorten"`, `"tukey"`, `"hazen"` or a constant in
20
+ `[0, ½]`), never `0` or `1` beyond the members. `NaN` members are missing.
21
+ - `logisticRegression(design: Matrix, outcomes, { weights, offset, penalty:
22
+ "none" | "ridge" | "firth", ridge, unpenalized, tolerance, maxIterations,
23
+ signal })`: binary or proportion outcomes by IRLS over a pivoted QR; returns
24
+ the coefficients (`0` for an aliased column), covariance, standard errors,
25
+ fitted probabilities, deviance, log likelihood, penalised log likelihood,
26
+ rank, iterations, `converged` and `separated`. Matches statsmodels' GLM
27
+ without a penalty.
28
+ - `emosFit({ location, scale }, observations, { family, criterion, scaleLink,
29
+ bound, weights, shift, start, tolerance, maxIterations, signal })`: ensemble
30
+ model output statistics for `"normal"`, `"truncated-normal"`,
31
+ `"censored-logistic"` and `"censored-shifted-gamma"` laws, by minimum CRPS or
32
+ maximum likelihood, the scale through `"variance"` (`c + d·S²`) or `"log"`
33
+ links; `start` restarts from a previous model. `emosPredict(model, location,
34
+ scale)` returns the predictive `Distribution`.
35
+ - `brierScore`, `brierSkillScore` (against the sample climatology, a fixed
36
+ probability or reference forecasts), `brierScoreDecomposition` (reliability,
37
+ resolution, uncertainty and Stephenson's within-bin terms, adding up to the
38
+ score exactly), `reliabilityDiagram` (columnar, Bröcker–Smith consistency
39
+ bars from a `RandomFn`), `rankHistogram(members: Matrix, observations)`
40
+ (ties ranked at random), `rocCurve` (columnar, at every forecast value or
41
+ given `thresholds`, with its Mann–Whitney area).
42
+ - `crpsEnsemble(members, observation, { estimator: "nrg" | "fair" | "pwm",
43
+ weights, sorted })`, which allocates no buffer per call; closed-form
44
+ `crpsNormal`, `crpsTruncatedNormal`, `crpsCensoredLogistic`,
45
+ `crpsCensoredShiftedGamma`; `logScore(distribution, observation)`, the point
46
+ mass at a censoring bound.
47
+ - `truncatedNormalDistribution(mean, standardDeviation, lower, upper)`,
48
+ accurate hundreds of standard deviations into a tail;
49
+ `censoredLogisticDistribution(location, scale, lower)` and
50
+ `censoredShiftedGammaDistribution(shape, scale, shift, lower)`, whose
51
+ `density` at the bound is the point mass there.
52
+
53
+ Every score throws a `RangeError` for mismatched lengths, probabilities or
54
+ outcomes outside `[0, 1]`, and invalid weights.
55
+ - 5c203e7: - `quantile(sorted, fraction, { method, from, to })` and `writeQuantile` take
56
+ a `method` (`QuantileMethod`): the nine Hyndman–Fan estimators under
57
+ NumPy's names (`"inverted-cdf"` … `"normal-unbiased"`) and NumPy's
58
+ `"lower"`, `"higher"`, `"nearest"` and `"midpoint"`; `"linear"` (type 7)
59
+ stays the default and the positional `from, to` form is unchanged.
60
+ - `sampleQuantile(values, fraction, { method })` takes the sample in any order
61
+ (O(n) selection) and `sampleQuantiles(values, fractions, { method })`
62
+ several fractions in one sort; a `NaN` in the sample gives `NaN`.
63
+ - `multipleRegression(design: Matrix, y, { weights, rankTolerance })`:
64
+ ordinary or weighted least squares over `@ac-kit/math-linear`'s pivoted
65
+ `QrDecomposition`, with coefficients, covariance, standard errors, t tests,
66
+ R² (uncentred without a constant column), adjusted R², the overall F test,
67
+ fitted values, residuals, leverages, log likelihood, AIC and BIC; aliased
68
+ columns get a `0` coefficient and `NaN` statistics. Matches statsmodels'
69
+ `OLS` and `WLS`.
70
+ - `linearRegression(x, y, { weights, rankTolerance })`: the least-squares
71
+ line with SciPy's `linregress` inference (`slope`, `intercept`, their
72
+ standard errors, `correlation`) and every `multipleRegression` field,
73
+ accurate for `x` far from the origin.
74
+ - 5c203e7: - `spearmanCorrelation(x, y)` (Pearson's coefficient of average ranks) and
75
+ `kendallTau(x, y)` (τ_b, tie-corrected, O(n log n)), ranking through
76
+ `@ac-kit/algo`'s `rankBy`.
77
+ - `mannWhitneyU(a, b, options)` and `wilcoxonSignedRank(sample, other,
78
+ options)` (paired sample or hypothesised median; `zeroMethod` `"wilcox"`,
79
+ `"pratt"` or `"zsplit"`): one- or two-sided p-values, exact permutation
80
+ distributions conditional on ties and zeros below 50 observations per
81
+ sample, the tie-corrected normal approximation with continuity correction
82
+ otherwise (`method` and `continuity` choose).
83
+ - `bootstrap(values, statistic, options)`: percentile, basic or BCa (default)
84
+ intervals with standard error, bias and the resampled statistics, from an
85
+ injectable `RandomFn`.
86
+ - `confidenceInterval(values, level, options)`: Student's t for the mean,
87
+ order statistics for the median, or the bootstrap for any statistic.
88
+ - `sample(distribution, count, random)` and `sampleInto(out, distribution,
89
+ random)`.
90
+ - `TDigest`: mergeable streaming quantile and CDF estimates (`add`, `merge`,
91
+ `quantile`, `cdf`, `centroids`, `fromCentroids`), k₂ scale by default.
92
+ - `principalComponents(data, options)`: PCA of a `Matrix` through the SVD,
93
+ optionally on standardised variables, with loadings, scores and explained
94
+ variance.
95
+
96
+ ### Patch Changes
97
+
98
+ - Updated dependencies [5c203e7]
99
+ - Updated dependencies [5c203e7]
100
+ - Updated dependencies [5c203e7]
101
+ - Updated dependencies [5c203e7]
102
+ - Updated dependencies [5c203e7]
103
+ - Updated dependencies [5c203e7]
104
+ - Updated dependencies [5c203e7]
105
+ - Updated dependencies [5c203e7]
106
+ - Updated dependencies [5c203e7]
107
+ - Updated dependencies [5c203e7]
108
+ - Updated dependencies [5c203e7]
109
+ - Updated dependencies [5c203e7]
110
+ - Updated dependencies [5c203e7]
111
+ - Updated dependencies [5c203e7]
112
+ - Updated dependencies [5c203e7]
113
+ - Updated dependencies [5c203e7]
114
+ - Updated dependencies [5c203e7]
115
+ - Updated dependencies [5c203e7]
116
+ - Updated dependencies [5c203e7]
117
+ - Updated dependencies [5c203e7]
118
+ - @ac-kit/algo@0.7.0
119
+ - @ac-kit/core@0.14.0
120
+ - @ac-kit/data@0.6.0
121
+ - @ac-kit/math-analysis@0.3.0
122
+ - @ac-kit/math-linear@0.5.0
123
+ - @ac-kit/math-scalar@0.5.0
124
+ - @ac-kit/math-random@0.4.2
125
+
3
126
  ## 0.6.0
4
127
 
5
128
  ### Minor Changes
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @ac-kit/math-stats
2
2
 
3
- Descriptive statistics, probability distributions and statistical inference over
4
- samples of `number`.
3
+ Descriptive statistics, probability distributions, statistical inference and
4
+ probabilistic-forecast verification over samples of `number`.
5
5
 
6
6
  ## Scope
7
7
 
@@ -17,6 +17,22 @@ distribution parameter, or a model.
17
17
  | `geometricMean(values)` | `n`-th root of the product; for ratios and growth rates |
18
18
  | `harmonicMean(values)` | Reciprocal of the mean of reciprocals; for rates over a fixed distance |
19
19
  | `midrange(values)` | `(min + max) / 2` |
20
+ | `weightedMean`, `weightedVariance`, `weightedStandardDeviation` | Weighted moments, frequency or reliability weights |
21
+ | `interquartileRange(values)` | `Q₃ − Q₁`, type-7 quartiles |
22
+ | `covariance(x, y)`, `covarianceMatrix(columns)` | Sample covariance, and the `k × k` `Matrix` of it |
23
+ | `rSquared`, `rootMeanSquareError` | Goodness of fit of any predictions |
24
+ | `standardize`, `normalize` | z-scores; min–max onto `[0, 1]` |
25
+ | `movingAverage`, `exponentialSmoothing`, `ExponentialMovingAverage` | Trailing mean, exponential smoothing, and its streaming form |
26
+ | `spearmanCorrelation`, `kendallTau` | Rank correlations: Spearman's ρ over average ranks, Kendall's τ_b in O(n log n) |
27
+ | `mannWhitneyU`, `wilcoxonSignedRank` | Rank-sum and signed-rank tests, exact under ties and zeros or asymptotic |
28
+ | `bootstrap(values, statistic, options)` | Percentile, basic or BCa bootstrap interval for any statistic |
29
+ | `confidenceInterval(values, level, options)` | Student's t for the mean, order statistics for the median, the bootstrap for any statistic |
30
+ | `sample`, `sampleInto` | Reproducible draws from a `Distribution` |
31
+ | `TDigest` | Mergeable streaming quantiles, accurate in the tails (k₁, k₂, k₃ scales) |
32
+ | `principalComponents(data, options)` | PCA through the SVD: loadings, scores, explained variance |
33
+ | `quantile(sorted, p, { method })`, `sampleQuantile`, `sampleQuantiles` | Sample quantiles by the nine Hyndman–Fan methods and NumPy's four others; sorted input, or any order by selection |
34
+ | `linearRegression(x, y, options)` | Least-squares line with SciPy's `linregress` and R's `lm` inference: standard errors, t tests, R², residuals |
35
+ | `multipleRegression(design, y, options)` | OLS or WLS over a pivoted QR: coefficients, covariance, t and F tests, adjusted R², leverages, AIC/BIC, aliased columns |
20
36
 
21
37
  ## Which mean
22
38
 
@@ -162,6 +178,8 @@ Every family is a factory returning a `Distribution` — `density`, `cdf`,
162
178
  | `gammaDistribution`, `chiSquaredDistribution` | `geometricDistribution` |
163
179
  | `studentTDistribution`, `betaDistribution` | `negativeBinomialDistribution` |
164
180
  | `fDistribution` | `hypergeometricDistribution` |
181
+ | `truncatedNormalDistribution` | |
182
+ | `censoredLogisticDistribution`, `censoredShiftedGammaDistribution` (with a point mass at the bound) | |
165
183
 
166
184
  **`survival` is not a convenience over `1 - cdf(x)`.** It is the member the rest
167
185
  is built around: a p-value _is_ an upper tail, and `1 - cdf(x)` is exactly `0`
@@ -206,6 +224,29 @@ to five groups, a nominal 95 % interval holds the true value 97–100 % of the
206
224
  time. Near-nominal coverage is reached with heterogeneous groups from about
207
225
  nine up.
208
226
 
227
+ ## Probabilistic forecasts
228
+
229
+ Calibration and verification of ensemble and probability forecasts
230
+ (`src/forecast/`).
231
+
232
+ | Symbol | Description |
233
+ | --- | --- |
234
+ | `exceedanceProbability(members, threshold, options)` | `P(X > t)` of an ensemble: the share of members, or through plotting positions (Weibull, Tukey, Gringorten, Hazen, …), weighted members, `inclusive` |
235
+ | `logisticRegression(design, outcomes, options)` | IRLS over a pivoted QR: weights, offset, no penalty, ridge or Firth's; coefficients, covariance, deviance, `converged`, `separated` |
236
+ | `emosFit(predictors, observations, options)`, `emosPredict(model, location, scale)` | Ensemble model output statistics: normal, truncated normal, censored logistic or censored shifted gamma, by minimum CRPS or log score (BFGS on analytic gradients), warm start from yesterday's model |
237
+ | `brierScore`, `brierSkillScore`, `brierScoreDecomposition` | The score, its skill against climatology or a reference, and its exact decomposition (Murphy plus Stephenson's within-bin terms) |
238
+ | `reliabilityDiagram(probabilities, outcomes, options)` | Columnar diagram with Bröcker–Smith consistency bars from a seeded `RandomFn` |
239
+ | `rankHistogram(members, observations, options)` | Talagrand histogram, ties ranked at random |
240
+ | `crpsEnsemble(members, observation, { estimator })` | CRPS of an ensemble: `"nrg"`, `"fair"` or `"pwm"`, weighted |
241
+ | `crpsNormal`, `crpsTruncatedNormal`, `crpsCensoredLogistic`, `crpsCensoredShiftedGamma` | Closed-form CRPS of each EMOS family |
242
+ | `logScore(distribution, observation)` | `−ln f(y)`, the point mass at a censoring bound |
243
+ | `rocCurve(probabilities, outcomes, options)` | Columnar ROC curve at every forecast value or given thresholds, with its Mann–Whitney area |
244
+
245
+ Every score takes case weights and throws a `RangeError` for mismatched lengths
246
+ or probabilities outside `[0, 1]`. The oracles (statsmodels, properscoring,
247
+ SciPy, mpmath) are recorded in `src/forecast/__fixtures__/` by
248
+ `scripts/record-forecast-oracles.py`.
249
+
209
250
  ## Usage
210
251
 
211
252
  ```ts
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The digamma function `ψ(x) = Γ′(x) / Γ(x)` for `x > 0`: the recurrence `ψ(x)
3
+ * = ψ(x + 1) − 1/x` up to `x ≥ 10`, then the asymptotic series `ln x − 1/(2x) −
4
+ * Σ B₂ₙ / (2n·x²ⁿ)` to seven terms, whose truncation error there is below
5
+ * `2⁻⁵³`. Absolute error a few ulps of `max(1, |ψ(x)|)`.
6
+ *
7
+ * @returns `ψ(x)`; `NaN` for `x ≤ 0` or `NaN`, `+∞` at `+∞`.
8
+ */
9
+ export declare function digamma_(x: number): number;
10
+ //# sourceMappingURL=_digamma.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_digamma.d.ts","sourceRoot":"","sources":["../src/_digamma.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAiC1C"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The digamma function `ψ(x) = Γ′(x) / Γ(x)` for `x > 0`: the recurrence `ψ(x)
3
+ * = ψ(x + 1) − 1/x` up to `x ≥ 10`, then the asymptotic series `ln x − 1/(2x) −
4
+ * Σ B₂ₙ / (2n·x²ⁿ)` to seven terms, whose truncation error there is below
5
+ * `2⁻⁵³`. Absolute error a few ulps of `max(1, |ψ(x)|)`.
6
+ *
7
+ * @returns `ψ(x)`; `NaN` for `x ≤ 0` or `NaN`, `+∞` at `+∞`.
8
+ */
9
+ export function digamma_(x) {
10
+ if (!(x > 0)) {
11
+ return Number.NaN;
12
+ }
13
+ if (x === Number.POSITIVE_INFINITY) {
14
+ return x;
15
+ }
16
+ let shift = 0;
17
+ let value = x;
18
+ while (value < 10) {
19
+ shift += 1 / value;
20
+ value += 1;
21
+ }
22
+ const inverse = 1 / value;
23
+ const inverseSquare = inverse * inverse;
24
+ // Bernoulli numbers B₂ₙ / 2n, n = 1 … 7, by Horner in 1/x².
25
+ const series = inverseSquare *
26
+ (1 / 12 -
27
+ inverseSquare *
28
+ (1 / 120 -
29
+ inverseSquare *
30
+ (1 / 252 -
31
+ inverseSquare *
32
+ (1 / 240 -
33
+ inverseSquare *
34
+ (1 / 132 -
35
+ inverseSquare * (691 / 32760 - inverseSquare / 12))))));
36
+ return Math.log(value) - 0.5 * inverse - series - shift;
37
+ }
38
+ //# sourceMappingURL=_digamma.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_digamma.js","sourceRoot":"","sources":["../src/_digamma.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAS;IACjC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IACD,IAAI,CAAC,KAAK,MAAM,CAAC,iBAAiB,EAAE,CAAC;QACpC,OAAO,CAAC,CAAC;IACV,CAAC;IAED,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,OAAO,KAAK,GAAG,EAAE,EAAE,CAAC;QACnB,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC;QACnB,KAAK,IAAI,CAAC,CAAC;IACZ,CAAC;IAED,MAAM,OAAO,GAAG,CAAC,GAAG,KAAK,CAAC;IAC1B,MAAM,aAAa,GAAG,OAAO,GAAG,OAAO,CAAC;IACxC,4DAA4D;IAC5D,MAAM,MAAM,GACX,aAAa;QACb,CAAC,CAAC,GAAG,EAAE;YACN,aAAa;gBACZ,CAAC,CAAC,GAAG,GAAG;oBACP,aAAa;wBACZ,CAAC,CAAC,GAAG,GAAG;4BACP,aAAa;gCACZ,CAAC,CAAC,GAAG,GAAG;oCACP,aAAa;wCACZ,CAAC,CAAC,GAAG,GAAG;4CACP,aAAa,GAAG,CAAC,GAAG,GAAG,KAAM,GAAG,aAAa,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEnE,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;AACzD,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The dilogarithm `Li₂(x) = −∫₀ˣ ln(1 − t)/t dt` for real `x ≤ 1`, reduced to
3
+ * `|x| ≤ ½` by the inversion and Landen identities ('t Hooft and Veltman 1979).
4
+ * Absolute error a few ulps of `max(1, |Li₂(x)|)`.
5
+ */
6
+ export declare function dilogarithm_(x: number): number;
7
+ /**
8
+ * The complete Fermi–Dirac integral of order one, `F₁(c) = ∫₀^∞ t / (1 + eᵗ⁻ᶜ)
9
+ * dt = −Li₂(−eᶜ)`: the second partial moment of the logistic distribution.
10
+ * Positive, `≈ eᶜ` far below zero and `≈ c²/2 + π²/6` far above, without
11
+ * overflow on either side.
12
+ */
13
+ export declare function fermiDirac1_(c: number): number;
14
+ //# sourceMappingURL=_dilogarithm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_dilogarithm.d.ts","sourceRoot":"","sources":["../src/_dilogarithm.ts"],"names":[],"mappings":"AAmCA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CA8B9C;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAS9C"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Coefficients `Bₙ / (n + 1)!` of `Li₂(x) = Σ Bₙ·uⁿ⁺¹ / (n + 1)!`, `u = −ln(1 −
3
+ * x)`, for n = 0, 1, 2, 4, …, 18 (the odd Bernoulli numbers past B₁ vanish).
4
+ * With `|x| ≤ ½`, `|u| ≤ ln 2` and the next term is below 10⁻²⁰.
5
+ */
6
+ const BERNOULLI_ = [
7
+ 1,
8
+ -0.25,
9
+ 1 / 36,
10
+ -1 / 3600,
11
+ 1 / 211680,
12
+ -1 / 10886400,
13
+ 1 / 526901760,
14
+ -4.064761645144226e-11,
15
+ 8.921691020456453e-13,
16
+ -1.993929586072108e-14,
17
+ 4.518980029619919e-16,
18
+ ];
19
+ const PI_SQUARED_OVER_SIX_ = (Math.PI * Math.PI) / 6;
20
+ /** `Li₂(x)` for `|x| ≤ ½`, by the Bernoulli series in `−ln(1 − x)`. */
21
+ function dilogarithmSmall_(x) {
22
+ const u = -Math.log1p(-x);
23
+ const uSquare = u * u;
24
+ // Terms n = 0 and 1 are u and −u²/4; the even ones follow in u².
25
+ let sum = 0;
26
+ for (let index = BERNOULLI_.length - 1; index >= 2; index--) {
27
+ sum = sum * uSquare + BERNOULLI_[index];
28
+ }
29
+ return u + uSquare * (-0.25 + u * sum);
30
+ }
31
+ /**
32
+ * The dilogarithm `Li₂(x) = −∫₀ˣ ln(1 − t)/t dt` for real `x ≤ 1`, reduced to
33
+ * `|x| ≤ ½` by the inversion and Landen identities ('t Hooft and Veltman 1979).
34
+ * Absolute error a few ulps of `max(1, |Li₂(x)|)`.
35
+ */
36
+ export function dilogarithm_(x) {
37
+ if (x !== x || x > 1) {
38
+ return Number.NaN;
39
+ }
40
+ if (x === 1) {
41
+ return PI_SQUARED_OVER_SIX_;
42
+ }
43
+ if (x < -1) {
44
+ // Li₂(x) = −π²/6 − ½ ln²(−x) − Li₂(1/x).
45
+ const logarithm = Math.log(-x);
46
+ return (-PI_SQUARED_OVER_SIX_ - 0.5 * logarithm * logarithm - dilogarithm_(1 / x));
47
+ }
48
+ if (x < -0.5) {
49
+ // Landen: Li₂(x) = −Li₂(x / (x − 1)) − ½ ln²(1 − x), the argument in [⅓, ½].
50
+ const logarithm = Math.log1p(-x);
51
+ return -dilogarithmSmall_(x / (x - 1)) - 0.5 * logarithm * logarithm;
52
+ }
53
+ if (x <= 0.5) {
54
+ return dilogarithmSmall_(x);
55
+ }
56
+ // Euler's reflection: Li₂(x) = π²/6 − ln x · ln(1 − x) − Li₂(1 − x).
57
+ return (PI_SQUARED_OVER_SIX_ -
58
+ Math.log(x) * Math.log1p(-x) -
59
+ dilogarithmSmall_(1 - x));
60
+ }
61
+ /**
62
+ * The complete Fermi–Dirac integral of order one, `F₁(c) = ∫₀^∞ t / (1 + eᵗ⁻ᶜ)
63
+ * dt = −Li₂(−eᶜ)`: the second partial moment of the logistic distribution.
64
+ * Positive, `≈ eᶜ` far below zero and `≈ c²/2 + π²/6` far above, without
65
+ * overflow on either side.
66
+ */
67
+ export function fermiDirac1_(c) {
68
+ if (c !== c) {
69
+ return c;
70
+ }
71
+ if (c <= 0) {
72
+ return -dilogarithm_(-Math.exp(c));
73
+ }
74
+ // −Li₂(−eᶜ) = π²/6 + c²/2 + Li₂(−e⁻ᶜ) by the inversion identity.
75
+ return PI_SQUARED_OVER_SIX_ + 0.5 * c * c + dilogarithm_(-Math.exp(-c));
76
+ }
77
+ //# sourceMappingURL=_dilogarithm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_dilogarithm.js","source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1
+ /**
2
+ * `∂P(a, x)/∂a`, the derivative of the lower regularised incomplete gamma in
3
+ * its shape, for `a > 0`, `x ≥ 0` — the term no closed form gives, needed by
4
+ * the gradient of every gamma-based score.
5
+ *
6
+ * Below `x = a + 1`, the derivative of the series `P = xᵃe⁻ˣ/Γ(a + 1) · Σ tₙ`,
7
+ * `tₙ = xⁿ / ((a + 1)…(a + n))`, term by term: `∂tₙ/∂a = −tₙ·Σⱼ 1/(a + j)`.
8
+ * Above, `−∂Q/∂a` from Legendre's continued fraction for `Q = xᵃe⁻ˣ/Γ(a) · K(a,
9
+ * x)`, its derivative carried through the modified Lentz recurrences in forward
10
+ * mode (as Moore 1982 differentiates the series). Each converges geometrically
11
+ * on its side; relative error about 1e−13.
12
+ *
13
+ * @returns The derivative (negative: a larger shape moves mass to the right);
14
+ * `0` at `x = 0` and `x = ∞`, `NaN` outside the domain.
15
+ */
16
+ export declare function incompleteGammaShapeDerivative_(a: number, x: number): number;
17
+ //# sourceMappingURL=_incomplete-gamma-shape-derivative.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_incomplete-gamma-shape-derivative.d.ts","sourceRoot":"","sources":["../src/_incomplete-gamma-shape-derivative.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,+BAA+B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAuF5E"}
@@ -0,0 +1,90 @@
1
+ import { logGamma } from "@ac-kit/math-analysis";
2
+ import { digamma_ } from "./_digamma.js";
3
+ const EPSILON_ = Number.EPSILON;
4
+ const TINY_ = 1e-300;
5
+ const MAX_TERMS_ = 2000;
6
+ /**
7
+ * `∂P(a, x)/∂a`, the derivative of the lower regularised incomplete gamma in
8
+ * its shape, for `a > 0`, `x ≥ 0` — the term no closed form gives, needed by
9
+ * the gradient of every gamma-based score.
10
+ *
11
+ * Below `x = a + 1`, the derivative of the series `P = xᵃe⁻ˣ/Γ(a + 1) · Σ tₙ`,
12
+ * `tₙ = xⁿ / ((a + 1)…(a + n))`, term by term: `∂tₙ/∂a = −tₙ·Σⱼ 1/(a + j)`.
13
+ * Above, `−∂Q/∂a` from Legendre's continued fraction for `Q = xᵃe⁻ˣ/Γ(a) · K(a,
14
+ * x)`, its derivative carried through the modified Lentz recurrences in forward
15
+ * mode (as Moore 1982 differentiates the series). Each converges geometrically
16
+ * on its side; relative error about 1e−13.
17
+ *
18
+ * @returns The derivative (negative: a larger shape moves mass to the right);
19
+ * `0` at `x = 0` and `x = ∞`, `NaN` outside the domain.
20
+ */
21
+ export function incompleteGammaShapeDerivative_(a, x) {
22
+ if (!(a > 0) || !(x >= 0)) {
23
+ return Number.NaN;
24
+ }
25
+ if (x === 0 || x === Number.POSITIVE_INFINITY) {
26
+ return 0;
27
+ }
28
+ const logX = Math.log(x);
29
+ if (x < a + 1) {
30
+ const prefactor = Math.exp(a * logX - x - logGamma(a + 1));
31
+ let term = 1;
32
+ let harmonic = 0;
33
+ let sum = 1;
34
+ let derivativeSum = 0;
35
+ for (let n = 1; n < MAX_TERMS_; n++) {
36
+ const denominator = a + n;
37
+ term *= x / denominator;
38
+ harmonic += 1 / denominator;
39
+ sum += term;
40
+ derivativeSum -= term * harmonic;
41
+ if (term <= EPSILON_ * sum &&
42
+ term * harmonic <= EPSILON_ * Math.abs(derivativeSum)) {
43
+ break;
44
+ }
45
+ }
46
+ return prefactor * ((logX - digamma_(a + 1)) * sum + derivativeSum);
47
+ }
48
+ // Q = prefactor · K, K = 1/(b₀ − 1(1 − a)/(b₁ − 2(2 − a)/(b₂ − …))), bₙ = x +
49
+ // 2n + 1 − a: modified Lentz for h = K, each recurrence differentiated in a
50
+ // (∂bₙ/∂a = −1, ∂aₙ/∂a = n for aₙ = −n(n − a)).
51
+ const prefactor = Math.exp(a * logX - x - logGamma(a));
52
+ let b = x + 1 - a;
53
+ let c = 1 / TINY_;
54
+ let dc = 0;
55
+ let d = 1 / b;
56
+ let dd = d * d;
57
+ let h = d;
58
+ let dh = dd;
59
+ for (let n = 1; n < MAX_TERMS_; n++) {
60
+ const an = -n * (n - a);
61
+ const dan = n;
62
+ b += 2;
63
+ let denominator = an * d + b;
64
+ const dDenominator = dan * d + an * dd - 1;
65
+ if (Math.abs(denominator) < TINY_) {
66
+ denominator = TINY_;
67
+ }
68
+ d = 1 / denominator;
69
+ dd = -dDenominator * d * d;
70
+ let next = b + an / c;
71
+ const dNext = -1 + dan / c - (an * dc) / (c * c);
72
+ if (Math.abs(next) < TINY_) {
73
+ next = TINY_;
74
+ }
75
+ c = next;
76
+ dc = dNext;
77
+ const delta = d * c;
78
+ const dDelta = dd * c + d * dc;
79
+ dh = dh * delta + h * dDelta;
80
+ h *= delta;
81
+ if (Math.abs(delta - 1) <= EPSILON_ &&
82
+ Math.abs(dDelta * h) <= EPSILON_ * Math.abs(dh)) {
83
+ break;
84
+ }
85
+ }
86
+ const q = prefactor * h;
87
+ const dq = q * (logX - digamma_(a)) + prefactor * dh;
88
+ return -dq;
89
+ }
90
+ //# sourceMappingURL=_incomplete-gamma-shape-derivative.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_incomplete-gamma-shape-derivative.js","sourceRoot":"","sources":["../src/_incomplete-gamma-shape-derivative.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC;AAChC,MAAM,KAAK,GAAG,MAAM,CAAC;AACrB,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,+BAA+B,CAAC,CAAS,EAAE,CAAS;IACnE,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QAC3B,OAAO,MAAM,CAAC,GAAG,CAAC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AK,EAAE,CAAC;YAC5B,IAAI,GAAG,KAAK,CAAC;QACd,CAAC;QACD,CAAC,GAAG,IAAI,CAAC;QACT,EAAE,GAAG,KAAK,CAAC;QAEX,MAAM,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC;QACpB,MAAM,MAAM,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QAE/B,EAAE,GAAG,EAAE,GAAG,KAAK,GAAG,CAAC,GAAG,MAAM,CAAC;QAC7B,CAAC,IAAI,KAAK,CAAC;QACX,IACC,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,QAAQ;YAC/B,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAC9C,CAAC;YACF,MAAM;QACP,CAAC;IACF,CAAC;IAED,MAAM,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC;IACxB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,EAAE,CAAC;IAErD,OAAO,CAAC,EAAE,CAAC;AACZ,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The standard logistic function `1 / (1 + e⁻ˣ)`, without overflow and with
3
+ * full relative precision in both tails (`1 − logistic(x)` is `logistic(−x)`,
4
+ * never a subtraction).
5
+ */
6
+ export declare function logistic_(x: number): number;
7
+ /**
8
+ * `log(1 + eˣ)`, the antiderivative of the logistic function, without overflow
9
+ * for large `x` nor loss of the tiny result for very negative `x`.
10
+ */
11
+ export declare function softplus_(x: number): number;
12
+ /** The logistic density `e⁻ˣ / (1 + e⁻ˣ)²`, symmetric and overflow-free. */
13
+ export declare function logisticDensity_(x: number): number;
14
+ //# sourceMappingURL=_logistic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_logistic.d.ts","sourceRoot":"","sources":["../src/_logistic.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAO3C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAM3C;AAED,4EAA4E;AAC5E,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAKlD"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The standard logistic function `1 / (1 + e⁻ˣ)`, without overflow and with
3
+ * full relative precision in both tails (`1 − logistic(x)` is `logistic(−x)`,
4
+ * never a subtraction).
5
+ */
6
+ export function logistic_(x) {
7
+ if (x >= 0) {
8
+ return 1 / (1 + Math.exp(-x));
9
+ }
10
+ const e = Math.exp(x);
11
+ return e / (1 + e);
12
+ }
13
+ /**
14
+ * `log(1 + eˣ)`, the antiderivative of the logistic function, without overflow
15
+ * for large `x` nor loss of the tiny result for very negative `x`.
16
+ */
17
+ export function softplus_(x) {
18
+ if (x > 0) {
19
+ return x + Math.log1p(Math.exp(-x));
20
+ }
21
+ return Math.log1p(Math.exp(x));
22
+ }
23
+ /** The logistic density `e⁻ˣ / (1 + e⁻ˣ)²`, symmetric and overflow-free. */
24
+ export function logisticDensity_(x) {
25
+ const e = Math.exp(-Math.abs(x));
26
+ const d = 1 + e;
27
+ return e / (d * d);
28
+ }
29
+ //# sourceMappingURL=_logistic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_logistic.js","sourceRoot":"","sources":["../src/_logistic.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,CAAS;IAClC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACZ,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/B,CAAC;IACD,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAEtB,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACpB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,CAAS;IAClC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACrC,CAAC;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,gBAAgB,CAAC,CAAS;IACzC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAEhB,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACpB,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The tail `Tₖ(t) = k / (t + (k + 1) / (t + (k + 2) / (t + …)))` of Laplace's
3
+ * continued fraction for Mills' ratio `R(t) = Φᶜ(t)/φ(t) = 1/(t + T₁(t))`, `t >
4
+ * 0`, by modified Lentz. `T₁` gives `R`; `T₂` the difference `R⁻¹ − t − T₁` the
5
+ * truncated-normal moments need without cancellation. Converges for every `t >
6
+ * 0`, in a few dozen terms from `t ≈ 5`.
7
+ */
8
+ export declare function millsFractionTail_(t: number, first: number): number;
9
+ /**
10
+ * Mills' ratio `R(t) = Φᶜ(t) / φ(t)` for `t ≥ 0`: `erfc` below `t = 2`, where
11
+ * `exp(t²/2)` is harmless, then `1 / (t + T₁(t))`, which never underflows.
12
+ * `R(∞) = 0`.
13
+ */
14
+ export declare function millsRatio_(t: number): number;
15
+ /**
16
+ * `T₁(t) = 1/R(t) − t` for `t ≥ 0`, without the cancellation of that
17
+ * difference: the excess of the mean of the standard normal truncated below `t`
18
+ * over `t`. `T₁(∞) = 0`.
19
+ */
20
+ export declare function millsExcess_(t: number): number;
21
+ /**
22
+ * The variance of the standard normal truncated below `t ≥ 0`, `1 − T₁(t +
23
+ * T₁)`, from the fraction as `(T₂ − T₁) / (t + T₂)` where the direct form
24
+ * cancels (it falls as `1/t²`). `0` at `t = ∞`.
25
+ */
26
+ export declare function lowerTruncatedVariance_(t: number): number;
27
+ /**
28
+ * The hazard `φ(x) / Φᶜ(x)` of the standard normal, for every `x`: `1/R(x)`
29
+ * from zero up, the direct ratio below (where `Φᶜ ≥ ½`).
30
+ */
31
+ export declare function normalHazard_(x: number): number;
32
+ /** `ln φ(x)`, the log of the standard normal density. */
33
+ export declare function logNormalDensity_(x: number): number;
34
+ /**
35
+ * `ln Φᶜ(x) = ln P(Z > x)` for the standard normal, finite wherever the
36
+ * argument is: `erfc` while it keeps its digits, `ln φ(x) − ln(x + T₁(x))` from
37
+ * Laplace's fraction past `x = 30`, `log1p` of the small lower tail below
38
+ * zero.
39
+ */
40
+ export declare function logNormalSurvival_(x: number): number;
41
+ /** `ln Φ(x)` for the standard normal: {@link logNormalSurvival_} of `−x`. */
42
+ export declare function logNormalCdf_(x: number): number;
43
+ //# sourceMappingURL=_normal-tail.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_normal-tail.d.ts","sourceRoot":"","sources":["../src/_normal-tail.ts"],"names":[],"mappings":"AASA;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CA2BnE;AAKD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAS7C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAS9C;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAazD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAM/C;AAED,yDAAyD;AACzD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAepD;AAED,6EAA6E;AAC7E,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE/C"}