@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
@@ -0,0 +1,63 @@
1
+ /**
2
+ * CRPS of `N(mean, sd²)` at `y` (Gneiting et al. 2005) and its partials in the
3
+ * mean and the standard deviation: `σ·[z(2Φ(z) − 1) + 2φ(z) − 1/√π]`.
4
+ */
5
+ export declare function crpsNormalKernel_(out: Float64Array, y: number, mean: number, sd: number): void;
6
+ /** Negative log density of `N(mean, sd²)` at `y`, and its partials. */
7
+ export declare function logScoreNormalKernel_(out: Float64Array, y: number, mean: number, sd: number): void;
8
+ /**
9
+ * CRPS at `y` of the normal of `mean` and `sd` truncated below `lower`, and its
10
+ * partials in `mean` and `sd`.
11
+ *
12
+ * In standard units (`z`, `a` the standardised `y` and bound, `X` the truncated
13
+ * standard normal, `h = φ(a)/Φᶜ(a) = E X`), the score is `σ·c` with `c = E|X −
14
+ * z| − ½E|X − X′| = z − h + 2∫_z^∞ (1 − F) − I`. Beyond the centre (`a ≥ 0`)
15
+ * every term is a ratio of Mills' ratios and `I` comes from Laplace's fraction
16
+ * as `(a + T₁(a))·(√2·T₁(a) − T₁(√2a)) / (√2a + T₁(√2a))`, so nothing
17
+ * underflows however far out the bound lies. The partials follow from `∂c/∂z =
18
+ * 2F(z) − 1` and `∂c/∂a = 2h·(∫_z^∞ (1 − F) − I)`. Below the bound, `y` scores
19
+ * as the bound plus the distance to it.
20
+ */
21
+ export declare function crpsTruncatedNormalKernel_(out: Float64Array, y: number, mean: number, sd: number, lower: number): void;
22
+ /**
23
+ * Negative log density at `y` of the normal truncated below `lower`, and its
24
+ * partials: `ln σ + z²/2 + ½ln 2π + ln Φᶜ(a)`. `+∞` (partials `NaN`) below the
25
+ * bound, which the law cannot produce.
26
+ */
27
+ export declare function logScoreTruncatedNormalKernel_(out: Float64Array, y: number, mean: number, sd: number, lower: number): void;
28
+ /**
29
+ * CRPS at `y` of the logistic of `location` and `scale` censored below at
30
+ * `lower`, and its partials: `s·[z − 2 ln Λ(z) − 1] − s·[softplus(a) − Λ(a)]`
31
+ * for `y ≥ lower`, with `∂/∂z = 2Λ(z) − 1` and `∂/∂a = −Λ(a)²`. Below the
32
+ * bound, `y` scores as the bound plus the distance to it.
33
+ */
34
+ export declare function crpsCensoredLogisticKernel_(out: Float64Array, y: number, location: number, scale: number, lower: number): void;
35
+ /**
36
+ * Negative log likelihood at `y` of the censored logistic, and its partials:
37
+ * the density above the bound, the point mass `Λ(a)` at or below it (where a
38
+ * censored observation is recorded).
39
+ */
40
+ export declare function logScoreCensoredLogisticKernel_(out: Float64Array, y: number, location: number, scale: number, lower: number): void;
41
+ /**
42
+ * CRPS at `y` of the censored shifted gamma (`lower + max(0, X − shift)`, `X`
43
+ * of `shape` and `scale`), and its partials in the shape, the scale and the
44
+ * shift. With `u = (y − lower + δ)/θ` and `c = δ/θ` in units of the scale and
45
+ * `Pₖ` the regularised lower incomplete gamma, Scheuerer and Hamill's (2015)
46
+ * closed form
47
+ *
48
+ * `g = u(2Pₖ(u) − 1) − c·Pₖ(c)² + k[1 + 2Pₖ(c)Pₖ₊₁(c) − Pₖ(c)² − 2Pₖ₊₁(u)] −
49
+ * Γ(k + ½)/(√π Γ(k)) · Q₂ₖ(2c)`,
50
+ *
51
+ * Score `θ·g`, with `∂g/∂u = 2Pₖ(u) − 1` and `∂g/∂c = −Pₖ(c)²`. The shape
52
+ * partial differentiates each `Pₐ` in its shape (no closed form; a series or a
53
+ * differentiated continued fraction) and the Gamma ratio through the digamma
54
+ * function. `withShape` false skips that partial (left `NaN`), the costly one.
55
+ */
56
+ export declare function crpsCensoredShiftedGammaKernel_(out: Float64Array, y: number, shape: number, scale: number, shift: number, lower: number, withShape: boolean): void;
57
+ /**
58
+ * Negative log likelihood at `y` of the censored shifted gamma, and its
59
+ * partials in shape, scale and shift: the gamma density at `y − lower + δ`
60
+ * above the bound, the point mass `Pₖ(δ/θ)` at or below it.
61
+ */
62
+ export declare function logScoreCensoredShiftedGammaKernel_(out: Float64Array, y: number, shape: number, scale: number, shift: number, lower: number): void;
63
+ //# sourceMappingURL=_score-kernels.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_score-kernels.d.ts","sourceRoot":"","sources":["../../src/forecast/_score-kernels.ts"],"names":[],"mappings":"AA6BA;;;GAGG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACR,IAAI,CASN;AAED,uEAAuE;AACvE,wBAAgB,qBAAqB,CACpC,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACR,IAAI,CAON;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,0BAA0B,CACzC,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,MAAM,GACX,IAAI,CA4CN;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAC7C,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,MAAM,GACX,IAAI,CAgBN;AA+BD;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAC1C,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACX,IAAI,CA4BN;AAED;;;;GAIG;AACH,wBAAgB,+BAA+B,CAC9C,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACX,IAAI,CAkBN;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,+BAA+B,CAC9C,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,OAAO,GAChB,IAAI,CAsCN;AAED;;;;GAIG;AACH,wBAAgB,mCAAmC,CAClD,GAAG,EAAE,YAAY,EACjB,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACX,IAAI,CAuBN"}
@@ -0,0 +1,264 @@
1
+ import { erf, erfc, logGamma, regularizedIncompleteGamma, regularizedIncompleteGammaUpper, } from "@ac-kit/math-analysis";
2
+ import { LN_TWO_PI, SQRT1_2, SQRT2, SQRT_TWO_PI } from "@ac-kit/math-scalar";
3
+ import { digamma_ } from "../_digamma.js";
4
+ import { incompleteGammaShapeDerivative_ } from "../_incomplete-gamma-shape-derivative.js";
5
+ import { logistic_, softplus_ } from "../_logistic.js";
6
+ import { logNormalSurvival_, millsExcess_, millsRatio_, normalHazard_, } from "../_normal-tail.js";
7
+ // Every kernel writes `[score, ∂/∂first, ∂/∂second, ∂/∂third]` into `out` and
8
+ // allocates nothing; the parameters are those of its own family.
9
+ const INV_SQRT_PI_ = 0.5641895835477563;
10
+ /** The standard normal density `φ(z)`. */
11
+ function density_(z) {
12
+ return Math.exp(-0.5 * z * z) / SQRT_TWO_PI;
13
+ }
14
+ /**
15
+ * CRPS of `N(mean, sd²)` at `y` (Gneiting et al. 2005) and its partials in the
16
+ * mean and the standard deviation: `σ·[z(2Φ(z) − 1) + 2φ(z) − 1/√π]`.
17
+ */
18
+ export function crpsNormalKernel_(out, y, mean, sd) {
19
+ const z = (y - mean) / sd;
20
+ const centred = erf(z * SQRT1_2);
21
+ const spread = 2 * density_(z) - INV_SQRT_PI_;
22
+ out[0] = sd * (z * centred + spread);
23
+ out[1] = -centred;
24
+ out[2] = spread;
25
+ out[3] = 0;
26
+ }
27
+ /** Negative log density of `N(mean, sd²)` at `y`, and its partials. */
28
+ export function logScoreNormalKernel_(out, y, mean, sd) {
29
+ const z = (y - mean) / sd;
30
+ out[0] = Math.log(sd) + 0.5 * z * z + 0.5 * LN_TWO_PI;
31
+ out[1] = -z / sd;
32
+ out[2] = (1 - z * z) / sd;
33
+ out[3] = 0;
34
+ }
35
+ /**
36
+ * CRPS at `y` of the normal of `mean` and `sd` truncated below `lower`, and its
37
+ * partials in `mean` and `sd`.
38
+ *
39
+ * In standard units (`z`, `a` the standardised `y` and bound, `X` the truncated
40
+ * standard normal, `h = φ(a)/Φᶜ(a) = E X`), the score is `σ·c` with `c = E|X −
41
+ * z| − ½E|X − X′| = z − h + 2∫_z^∞ (1 − F) − I`. Beyond the centre (`a ≥ 0`)
42
+ * every term is a ratio of Mills' ratios and `I` comes from Laplace's fraction
43
+ * as `(a + T₁(a))·(√2·T₁(a) − T₁(√2a)) / (√2a + T₁(√2a))`, so nothing
44
+ * underflows however far out the bound lies. The partials follow from `∂c/∂z =
45
+ * 2F(z) − 1` and `∂c/∂a = 2h·(∫_z^∞ (1 − F) − I)`. Below the bound, `y` scores
46
+ * as the bound plus the distance to it.
47
+ */
48
+ export function crpsTruncatedNormalKernel_(out, y, mean, sd, lower) {
49
+ const extra = y < lower ? lower - y : 0;
50
+ const a = (lower - mean) / sd;
51
+ const z = (Math.max(y, lower) - mean) / sd;
52
+ let hazard;
53
+ let upperTail;
54
+ let partialTail;
55
+ let halfSpread;
56
+ if (a >= 0) {
57
+ const ratioA = millsRatio_(a);
58
+ const excessA = millsExcess_(a);
59
+ const shiftedA = SQRT2 * a;
60
+ const excessShifted = millsExcess_(shiftedA);
61
+ const scale = Math.exp(-0.5 * (z - a) * (z + a)) / ratioA;
62
+ const ratioZ = millsRatio_(z);
63
+ hazard = a + excessA;
64
+ upperTail = scale * ratioZ;
65
+ partialTail = scale * ratioZ * millsExcess_(z);
66
+ halfSpread =
67
+ (hazard * (SQRT2 * excessA - excessShifted)) / (shiftedA + excessShifted);
68
+ }
69
+ else {
70
+ const mass = 0.5 * erfc(a * SQRT1_2);
71
+ const densityZ = density_(z);
72
+ const survivalZ = 0.5 * erfc(z * SQRT1_2);
73
+ hazard = density_(a) / mass;
74
+ upperTail = survivalZ / mass;
75
+ partialTail =
76
+ z >= 0
77
+ ? (densityZ * millsRatio_(z) * millsExcess_(z)) / mass
78
+ : (densityZ - z * survivalZ) / mass;
79
+ halfSpread = (0.5 * erfc(a) * INV_SQRT_PI_) / (mass * mass) - hazard;
80
+ }
81
+ const c = z - hazard + 2 * partialTail - halfSpread;
82
+ const dz = 1 - 2 * upperTail;
83
+ const da = 2 * hazard * (partialTail - halfSpread);
84
+ out[0] = sd * c + extra;
85
+ out[1] = -(dz + da);
86
+ out[2] = c - z * dz - a * da;
87
+ out[3] = 0;
88
+ }
89
+ /**
90
+ * Negative log density at `y` of the normal truncated below `lower`, and its
91
+ * partials: `ln σ + z²/2 + ½ln 2π + ln Φᶜ(a)`. `+∞` (partials `NaN`) below the
92
+ * bound, which the law cannot produce.
93
+ */
94
+ export function logScoreTruncatedNormalKernel_(out, y, mean, sd, lower) {
95
+ if (y < lower) {
96
+ out[0] = Number.POSITIVE_INFINITY;
97
+ out[1] = Number.NaN;
98
+ out[2] = Number.NaN;
99
+ out[3] = Number.NaN;
100
+ return;
101
+ }
102
+ const z = (y - mean) / sd;
103
+ const a = (lower - mean) / sd;
104
+ const hazard = normalHazard_(a);
105
+ out[0] = Math.log(sd) + 0.5 * z * z + 0.5 * LN_TWO_PI + logNormalSurvival_(a);
106
+ out[1] = (hazard - z) / sd;
107
+ out[2] = (1 - z * z + a * hazard) / sd;
108
+ out[3] = 0;
109
+ }
110
+ /** Below this logistic value, `softplus(a) − Λ(a)` is summed as `Σₙ≥₂ Λⁿ/n`. */
111
+ const SERIES_FROM_ = 0.25;
112
+ /**
113
+ * `∫_{−∞}^a Λ(t)² dt = softplus(a) − Λ(a) = −ln(1 − Λ) − Λ`: by its positive
114
+ * series in `Λ(a)` where the difference would cancel.
115
+ */
116
+ function logisticSquareIntegral_(a) {
117
+ const w = logistic_(a);
118
+ if (w >= SERIES_FROM_) {
119
+ return softplus_(a) - w;
120
+ }
121
+ let power = w * w;
122
+ let sum = 0;
123
+ for (let n = 2; n < 64; n++) {
124
+ const term = power / n;
125
+ sum += term;
126
+ if (term <= Number.EPSILON * sum) {
127
+ break;
128
+ }
129
+ power *= w;
130
+ }
131
+ return sum;
132
+ }
133
+ /**
134
+ * CRPS at `y` of the logistic of `location` and `scale` censored below at
135
+ * `lower`, and its partials: `s·[z − 2 ln Λ(z) − 1] − s·[softplus(a) − Λ(a)]`
136
+ * for `y ≥ lower`, with `∂/∂z = 2Λ(z) − 1` and `∂/∂a = −Λ(a)²`. Below the
137
+ * bound, `y` scores as the bound plus the distance to it.
138
+ */
139
+ export function crpsCensoredLogisticKernel_(out, y, location, scale, lower) {
140
+ const extra = y < lower ? lower - y : 0;
141
+ const a = (lower - location) / scale;
142
+ const z = (Math.max(y, lower) - location) / scale;
143
+ const atBound = logistic_(a);
144
+ let c;
145
+ if (a >= 0) {
146
+ // The bound above the location: c = (z − a) + 2[softplus(−z) −
147
+ // softplus(−a)] + ∫_{−∞}^{−a} Λ², each part exact, where the direct form
148
+ // cancels terms of size a into a score of size e^{−2a}.
149
+ const tail = Math.exp(-a);
150
+ c =
151
+ z -
152
+ a +
153
+ 2 * Math.log1p((tail * Math.expm1(a - z)) / (1 + tail)) +
154
+ logisticSquareIntegral_(-a);
155
+ }
156
+ else {
157
+ c = z + 2 * softplus_(-z) - 1 - logisticSquareIntegral_(a);
158
+ }
159
+ const dz = 2 * logistic_(z) - 1;
160
+ const da = -atBound * atBound;
161
+ out[0] = scale * c + extra;
162
+ out[1] = -(dz + da);
163
+ out[2] = c - z * dz - a * da;
164
+ out[3] = 0;
165
+ }
166
+ /**
167
+ * Negative log likelihood at `y` of the censored logistic, and its partials:
168
+ * the density above the bound, the point mass `Λ(a)` at or below it (where a
169
+ * censored observation is recorded).
170
+ */
171
+ export function logScoreCensoredLogisticKernel_(out, y, location, scale, lower) {
172
+ if (y <= lower) {
173
+ const a = (lower - location) / scale;
174
+ const above = logistic_(-a);
175
+ out[0] = softplus_(-a);
176
+ out[1] = above / scale;
177
+ out[2] = (a * above) / scale;
178
+ out[3] = 0;
179
+ return;
180
+ }
181
+ const z = (y - location) / scale;
182
+ const centred = 2 * logistic_(z) - 1;
183
+ out[0] = Math.log(scale) + z + 2 * softplus_(-z);
184
+ out[1] = -centred / scale;
185
+ out[2] = (1 - z * centred) / scale;
186
+ out[3] = 0;
187
+ }
188
+ /**
189
+ * CRPS at `y` of the censored shifted gamma (`lower + max(0, X − shift)`, `X`
190
+ * of `shape` and `scale`), and its partials in the shape, the scale and the
191
+ * shift. With `u = (y − lower + δ)/θ` and `c = δ/θ` in units of the scale and
192
+ * `Pₖ` the regularised lower incomplete gamma, Scheuerer and Hamill's (2015)
193
+ * closed form
194
+ *
195
+ * `g = u(2Pₖ(u) − 1) − c·Pₖ(c)² + k[1 + 2Pₖ(c)Pₖ₊₁(c) − Pₖ(c)² − 2Pₖ₊₁(u)] −
196
+ * Γ(k + ½)/(√π Γ(k)) · Q₂ₖ(2c)`,
197
+ *
198
+ * Score `θ·g`, with `∂g/∂u = 2Pₖ(u) − 1` and `∂g/∂c = −Pₖ(c)²`. The shape
199
+ * partial differentiates each `Pₐ` in its shape (no closed form; a series or a
200
+ * differentiated continued fraction) and the Gamma ratio through the digamma
201
+ * function. `withShape` false skips that partial (left `NaN`), the costly one.
202
+ */
203
+ export function crpsCensoredShiftedGammaKernel_(out, y, shape, scale, shift, lower, withShape) {
204
+ const above = y - lower;
205
+ const extra = above < 0 ? -above : 0;
206
+ const u = (Math.max(above, 0) + shift) / scale;
207
+ const c = shift / scale;
208
+ const k = shape;
209
+ const pU = regularizedIncompleteGamma(k, u);
210
+ const pNextU = regularizedIncompleteGamma(k + 1, u);
211
+ const pC = regularizedIncompleteGamma(k, c);
212
+ const pNextC = regularizedIncompleteGamma(k + 1, c);
213
+ const qDouble = regularizedIncompleteGammaUpper(2 * k, 2 * c);
214
+ const ratio = Math.exp(logGamma(k + 0.5) - logGamma(k)) * INV_SQRT_PI_;
215
+ const bracket = 1 + 2 * pC * pNextC - pC * pC - 2 * pNextU;
216
+ const g = u * (2 * pU - 1) - c * pC * pC + k * bracket - ratio * qDouble;
217
+ const dU = 2 * pU - 1;
218
+ const dC = -pC * pC;
219
+ out[0] = scale * g + extra;
220
+ out[2] = g - u * dU - c * dC;
221
+ out[3] = dU + dC;
222
+ if (!withShape) {
223
+ out[1] = Number.NaN;
224
+ return;
225
+ }
226
+ const shapeU = incompleteGammaShapeDerivative_(k, u);
227
+ const shapeNextU = incompleteGammaShapeDerivative_(k + 1, u);
228
+ const shapeC = incompleteGammaShapeDerivative_(k, c);
229
+ const shapeNextC = incompleteGammaShapeDerivative_(k + 1, c);
230
+ const shapeDouble = incompleteGammaShapeDerivative_(2 * k, 2 * c);
231
+ const dK = 2 * u * shapeU -
232
+ 2 * c * pC * shapeC +
233
+ bracket +
234
+ 2 * k * (shapeC * pNextC + pC * shapeNextC - pC * shapeC - shapeNextU) -
235
+ ratio * (digamma_(k + 0.5) - digamma_(k)) * qDouble +
236
+ 2 * ratio * shapeDouble;
237
+ out[1] = scale * dK;
238
+ }
239
+ /**
240
+ * Negative log likelihood at `y` of the censored shifted gamma, and its
241
+ * partials in shape, scale and shift: the gamma density at `y − lower + δ`
242
+ * above the bound, the point mass `Pₖ(δ/θ)` at or below it.
243
+ */
244
+ export function logScoreCensoredShiftedGammaKernel_(out, y, shape, scale, shift, lower) {
245
+ const k = shape;
246
+ if (y <= lower) {
247
+ const c = shift / scale;
248
+ const mass = regularizedIncompleteGamma(k, c);
249
+ const density = c === 0 ? 0 : Math.exp((k - 1) * Math.log(c) - c - logGamma(k));
250
+ const relative = density / mass;
251
+ out[0] = -Math.log(mass);
252
+ out[1] = -incompleteGammaShapeDerivative_(k, c) / mass;
253
+ out[2] = (c * relative) / scale;
254
+ out[3] = -relative / scale;
255
+ return;
256
+ }
257
+ const shifted = y - lower + shift;
258
+ const u = shifted / scale;
259
+ out[0] = -(k - 1) * Math.log(shifted) + u + k * Math.log(scale) + logGamma(k);
260
+ out[1] = digamma_(k) - Math.log(u);
261
+ out[2] = (k - u) / scale;
262
+ out[3] = 1 / scale - (k - 1) / shifted;
263
+ }
264
+ //# sourceMappingURL=_score-kernels.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_score-kernels.js","sourceRoot":"","sources":["../../src/forecast/_score-kernels.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,GAAG,EACH,IAAI,EACJ,QAAQ,EACR,0BAA0B,EAC1B,+BAA+B,GAC/B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,OAAO,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AC;IAC9E,GAAG,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACnC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;IACzB,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AACxC,CAAC"}
@@ -0,0 +1,102 @@
1
+ /** Case weights shared by the verification scores. */
2
+ export type ForecastWeightsOptions = {
3
+ /**
4
+ * One finite non-negative weight per case, not all zero: each score is then
5
+ * the weighted mean. `null` or absent: every case counts once.
6
+ */
7
+ readonly weights?: ArrayLike<number> | null;
8
+ };
9
+ /**
10
+ * How probability forecasts are grouped for {@link brierScoreDecomposition} and
11
+ * {@link reliabilityDiagram}:
12
+ *
13
+ * - A positive integer `K`: `K` equal bins `[k/K, (k + 1)/K)` over `[0, 1]`;
14
+ * - Strictly ascending cut points inside `(0, 1)`: one more bin than cuts;
15
+ * - `"unique"`: one bin per distinct forecast value (the issued probabilities of
16
+ * an ensemble of `m` members, say), whose within-bin terms vanish.
17
+ *
18
+ * Every bin is closed below and open above, the last closed at `1`.
19
+ */
20
+ export type ProbabilityBinning = number | ArrayLike<number> | "unique";
21
+ /**
22
+ * The Brier score of probability forecasts against binary outcomes, `Σ wᵢ (pᵢ −
23
+ * oᵢ)² / Σ wᵢ` (Brier 1950): the mean squared error of the probabilities, `0`
24
+ * for perfect forecasts, `1` for certain wrong ones. Agrees with
25
+ * properscoring's `brier_score` averaged.
26
+ *
27
+ * @param probabilities The forecast probabilities of the event, in `[0, 1]`.
28
+ * @param outcomes `1` where the event happened, `0` where not; a fractional
29
+ * outcome in `[0, 1]` (an observed frequency) is also scored.
30
+ * @param options Case weights.
31
+ * @returns The score; `NaN` for no cases.
32
+ * @throws {RangeError} When the lengths differ, a probability or outcome lies
33
+ * outside `[0, 1]`, or the weights are invalid.
34
+ */
35
+ export declare function brierScore(probabilities: ArrayLike<number>, outcomes: ArrayLike<number>, options?: ForecastWeightsOptions): number;
36
+ /** The reference of a {@link brierSkillScore}. */
37
+ export type BrierSkillScoreOptions = ForecastWeightsOptions & {
38
+ /**
39
+ * The forecasts to beat: one probability for every case (a climatological
40
+ * frequency), one per case (another system), or absent for the sample
41
+ * climatology, the weighted base rate `ō` of the outcomes themselves (whose
42
+ * Brier score is `ō(1 − ō)`).
43
+ */
44
+ readonly reference?: number | ArrayLike<number> | null;
45
+ };
46
+ /**
47
+ * The Brier skill score `1 − BS / BS_ref`: `1` for perfect forecasts, `0` for
48
+ * no better than the reference, negative for worse.
49
+ *
50
+ * @param probabilities The forecast probabilities, in `[0, 1]`.
51
+ * @param outcomes The binary outcomes, `0` or `1`.
52
+ * @param options The reference and case weights.
53
+ * @returns The skill; `NaN` when the reference scores `0` (a constant outcome
54
+ * against the sample climatology) or there are no cases.
55
+ * @throws {RangeError} As {@link brierScore}, for a reference probability
56
+ * outside `[0, 1]` or of the wrong length, and for an outcome not `0` or
57
+ * `1`.
58
+ */
59
+ export declare function brierSkillScore(probabilities: ArrayLike<number>, outcomes: ArrayLike<number>, options?: BrierSkillScoreOptions): number;
60
+ /** Options of {@link brierScoreDecomposition}. */
61
+ export type BrierScoreDecompositionOptions = ForecastWeightsOptions & {
62
+ /** The binning of the forecasts; `10` equal bins by default. */
63
+ readonly bins?: ProbabilityBinning;
64
+ };
65
+ /**
66
+ * The exact decomposition of a {@link brierScore}: `score = reliability −
67
+ * resolution + uncertainty + withinBinVariance − 2·withinBinCovariance`.
68
+ */
69
+ export type BrierScoreDecomposition = {
70
+ /** The Brier score. */
71
+ readonly score: number;
72
+ /** `Σₖ Wₖ (p̄ₖ − ōₖ)² / W`: the miscalibration of the binned forecasts. */
73
+ readonly reliability: number;
74
+ /** `Σₖ Wₖ (ōₖ − ō)² / W`: how far the bins' outcome rates spread. */
75
+ readonly resolution: number;
76
+ /** `ō(1 − ō)`: the score of the sample climatology. */
77
+ readonly uncertainty: number;
78
+ /** `Σₖ Σᵢ∈ₖ wᵢ (pᵢ − p̄ₖ)² / W`: the spread of the forecasts inside bins. */
79
+ readonly withinBinVariance: number;
80
+ /** `Σₖ Σᵢ∈ₖ wᵢ (oᵢ − ōₖ)(pᵢ − p̄ₖ) / W`: their covariance with the outcomes. */
81
+ readonly withinBinCovariance: number;
82
+ };
83
+ /**
84
+ * Murphy's (1973) decomposition of the Brier score into reliability, resolution
85
+ * and uncertainty, made exact for binned forecasts by Stephenson, Coelho and
86
+ * Jolliffe's (2008) within-bin terms: binning replaces each forecast by its
87
+ * bin's mean, and the two extra terms put back what that loses, so the parts
88
+ * add up to the score (to rounding) whatever the bins. With `bins: "unique"`
89
+ * they vanish and the classical three-term identity holds.
90
+ *
91
+ * O(n log n) for `"unique"` and cut points, O(n) for equal bins; O(bins)
92
+ * memory.
93
+ *
94
+ * @param probabilities The forecast probabilities, in `[0, 1]`.
95
+ * @param outcomes The binary outcomes, `0` or `1`.
96
+ * @param options The binning and case weights.
97
+ * @returns The parts; all `NaN` for no cases.
98
+ * @throws {RangeError} When the lengths differ, a probability lies outside `[0,
99
+ * 1]`, an outcome is not `0` or `1`, or an option is invalid.
100
+ */
101
+ export declare function brierScoreDecomposition(probabilities: ArrayLike<number>, outcomes: ArrayLike<number>, options?: BrierScoreDecompositionOptions): BrierScoreDecomposition;
102
+ //# sourceMappingURL=brier-score.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"brier-score.d.ts","sourceRoot":"","sources":["../../src/forecast/brier-score.ts"],"names":[],"mappings":"AAUA,sDAAsD;AACtD,MAAM,MAAM,sBAAsB,GAAG;IACpC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;CAC5C,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,MAAM,kBAAkB,GAAG,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,SAAS,CAAC,MAAM,CAAC,EAChC,QAAQ,EAAE,SAAS,CAAC,MAAM,CAAC,EAC3B,OAAO,CAAC,EAAE,8BAA8B,GACtC,uBAAuB,CAmFzB"}
@@ -0,0 +1,163 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { assertBinary_, assertProbabilities_, assertSameLength_, probabilityBins_, weightTotal_, } from "./_forecast-checks.js";
3
+ /**
4
+ * The Brier score of probability forecasts against binary outcomes, `Σ wᵢ (pᵢ −
5
+ * oᵢ)² / Σ wᵢ` (Brier 1950): the mean squared error of the probabilities, `0`
6
+ * for perfect forecasts, `1` for certain wrong ones. Agrees with
7
+ * properscoring's `brier_score` averaged.
8
+ *
9
+ * @param probabilities The forecast probabilities of the event, in `[0, 1]`.
10
+ * @param outcomes `1` where the event happened, `0` where not; a fractional
11
+ * outcome in `[0, 1]` (an observed frequency) is also scored.
12
+ * @param options Case weights.
13
+ * @returns The score; `NaN` for no cases.
14
+ * @throws {RangeError} When the lengths differ, a probability or outcome lies
15
+ * outside `[0, 1]`, or the weights are invalid.
16
+ */
17
+ export function brierScore(probabilities, outcomes, options) {
18
+ const count = probabilities.length;
19
+ assertSameLength_("brierScore", "probabilities", count, "outcomes", outcomes.length);
20
+ assertProbabilities_("brierScore", probabilities);
21
+ for (let index = 0; index < count; index++) {
22
+ const outcome = outcomes[index];
23
+ if (!(outcome >= 0 && outcome <= 1)) {
24
+ throw new RangeError(`brierScore: outcomes must be in [0, 1], got ${describeValue(outcome)} at ${index}`);
25
+ }
26
+ }
27
+ const weights = options?.weights ?? null;
28
+ const total = weightTotal_("brierScore", weights, count);
29
+ let sum = 0;
30
+ for (let index = 0; index < count; index++) {
31
+ const error = probabilities[index] - outcomes[index];
32
+ sum += (weights === null ? 1 : weights[index]) * error * error;
33
+ }
34
+ return count === 0 ? Number.NaN : sum / total;
35
+ }
36
+ /**
37
+ * The Brier skill score `1 − BS / BS_ref`: `1` for perfect forecasts, `0` for
38
+ * no better than the reference, negative for worse.
39
+ *
40
+ * @param probabilities The forecast probabilities, in `[0, 1]`.
41
+ * @param outcomes The binary outcomes, `0` or `1`.
42
+ * @param options The reference and case weights.
43
+ * @returns The skill; `NaN` when the reference scores `0` (a constant outcome
44
+ * against the sample climatology) or there are no cases.
45
+ * @throws {RangeError} As {@link brierScore}, for a reference probability
46
+ * outside `[0, 1]` or of the wrong length, and for an outcome not `0` or
47
+ * `1`.
48
+ */
49
+ export function brierSkillScore(probabilities, outcomes, options) {
50
+ assertBinary_("brierSkillScore", outcomes);
51
+ const score = brierScore(probabilities, outcomes, options);
52
+ const reference = options?.reference ?? null;
53
+ let referenceScore;
54
+ if (reference === null) {
55
+ const weights = options?.weights ?? null;
56
+ const total = weightTotal_("brierSkillScore", weights, outcomes.length);
57
+ let events = 0;
58
+ for (let index = 0; index < outcomes.length; index++) {
59
+ events += (weights === null ? 1 : weights[index]) * outcomes[index];
60
+ }
61
+ const rate = events / total;
62
+ referenceScore = rate * (1 - rate);
63
+ }
64
+ else if (typeof reference === "number") {
65
+ if (!(reference >= 0 && reference <= 1)) {
66
+ throw new RangeError(`brierSkillScore: reference must be in [0, 1], got ${describeValue(reference)}`);
67
+ }
68
+ const constant = new Float64Array(outcomes.length).fill(reference);
69
+ referenceScore = brierScore(constant, outcomes, options);
70
+ }
71
+ else {
72
+ referenceScore = brierScore(reference, outcomes, options);
73
+ }
74
+ return referenceScore > 0 ? 1 - score / referenceScore : Number.NaN;
75
+ }
76
+ /**
77
+ * Murphy's (1973) decomposition of the Brier score into reliability, resolution
78
+ * and uncertainty, made exact for binned forecasts by Stephenson, Coelho and
79
+ * Jolliffe's (2008) within-bin terms: binning replaces each forecast by its
80
+ * bin's mean, and the two extra terms put back what that loses, so the parts
81
+ * add up to the score (to rounding) whatever the bins. With `bins: "unique"`
82
+ * they vanish and the classical three-term identity holds.
83
+ *
84
+ * O(n log n) for `"unique"` and cut points, O(n) for equal bins; O(bins)
85
+ * memory.
86
+ *
87
+ * @param probabilities The forecast probabilities, in `[0, 1]`.
88
+ * @param outcomes The binary outcomes, `0` or `1`.
89
+ * @param options The binning and case weights.
90
+ * @returns The parts; all `NaN` for no cases.
91
+ * @throws {RangeError} When the lengths differ, a probability lies outside `[0,
92
+ * 1]`, an outcome is not `0` or `1`, or an option is invalid.
93
+ */
94
+ export function brierScoreDecomposition(probabilities, outcomes, options) {
95
+ const routine = "brierScoreDecomposition";
96
+ const count = probabilities.length;
97
+ assertSameLength_(routine, "probabilities", count, "outcomes", outcomes.length);
98
+ assertProbabilities_(routine, probabilities);
99
+ assertBinary_(routine, outcomes);
100
+ const weights = options?.weights ?? null;
101
+ const total = weightTotal_(routine, weights, count);
102
+ const bins = probabilityBins_(routine, options?.bins ?? 10, probabilities);
103
+ const binWeight = new Float64Array(bins.count);
104
+ const binForecast = new Float64Array(bins.count);
105
+ const binOutcome = new Float64Array(bins.count);
106
+ const binOf = new Int32Array(count);
107
+ let events = 0;
108
+ for (let index = 0; index < count; index++) {
109
+ const weight = weights === null ? 1 : weights[index];
110
+ const bin = bins.binOf(probabilities[index]);
111
+ binOf[index] = bin;
112
+ binWeight[bin] += weight;
113
+ binForecast[bin] += weight * probabilities[index];
114
+ binOutcome[bin] += weight * outcomes[index];
115
+ events += weight * outcomes[index];
116
+ }
117
+ if (count === 0) {
118
+ return {
119
+ score: Number.NaN,
120
+ reliability: Number.NaN,
121
+ resolution: Number.NaN,
122
+ uncertainty: Number.NaN,
123
+ withinBinVariance: Number.NaN,
124
+ withinBinCovariance: Number.NaN,
125
+ };
126
+ }
127
+ const rate = events / total;
128
+ let reliability = 0;
129
+ let resolution = 0;
130
+ for (let bin = 0; bin < bins.count; bin++) {
131
+ const weight = binWeight[bin];
132
+ if (weight > 0) {
133
+ binForecast[bin] /= weight;
134
+ binOutcome[bin] /= weight;
135
+ const miscalibration = binForecast[bin] - binOutcome[bin];
136
+ const spread = binOutcome[bin] - rate;
137
+ reliability += weight * miscalibration * miscalibration;
138
+ resolution += weight * spread * spread;
139
+ }
140
+ }
141
+ let variance = 0;
142
+ let covariance = 0;
143
+ let score = 0;
144
+ for (let index = 0; index < count; index++) {
145
+ const weight = weights === null ? 1 : weights[index];
146
+ const bin = binOf[index];
147
+ const forecastDeviation = probabilities[index] - binForecast[bin];
148
+ const error = probabilities[index] - outcomes[index];
149
+ variance += weight * forecastDeviation * forecastDeviation;
150
+ covariance +=
151
+ weight * (outcomes[index] - binOutcome[bin]) * forecastDeviation;
152
+ score += weight * error * error;
153
+ }
154
+ return {
155
+ score: score / total,
156
+ reliability: reliability / total,
157
+ resolution: resolution / total,
158
+ uncertainty: rate * (1 - rate),
159
+ withinBinVariance: variance / total,
160
+ withinBinCovariance: covariance / total,
161
+ };
162
+ }
163
+ //# sourceMappingURL=brier-score.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"brier-score.js","sourceRoot":"","sources":["../../src/forecast/brier-score.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EACN,aAAa,EACb,oBAAoB,EACpB,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,GACZ,MAAM,uBAAuB,CAAC;AAwB/B;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,UAAU,CACzB,aAAgC,EAChC,QAA2B,EAC3B,OAAgC;IAEhC,MAAM,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC;IAEnC,iBAAiB,CAChB,YAAY,EACZ,eAAe,EACf,KAAK,EACL,UAAU,EACV,QAAQ,CAAC,MAAM,CACf,CAAC;IACF,oBAAoB,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;IAClD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;QAC5C,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAE,CAAC;QAEjC,IAAI,CAAC,CAAC,OAAO,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC;Y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