dsh-eval 0.5.0 → 0.5.2

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/src/stats.ts CHANGED
@@ -57,6 +57,39 @@ export interface SignTestPower {
57
57
  detectable: boolean
58
58
  }
59
59
 
60
+ /**
61
+ * Coerce a paired-comparison tally to a non-negative integer. Wins and losses
62
+ * come from counting cases, so negatives, fractions, NaN and infinities are
63
+ * invalid; treating them as zero keeps the binomial sum well-defined instead
64
+ * of feeding a negative or fractional index into the pmf.
65
+ */
66
+ function countOf(value: number): number {
67
+ return Number.isFinite(value) && value > 0 ? Math.floor(value) : 0
68
+ }
69
+
70
+ /**
71
+ * Lower binomial tails `sum_{i=0}^{k} C(n,i) / 2^n` for every `k` in `0..limit`,
72
+ * built in one O(limit) pass.
73
+ *
74
+ * The log coefficient accumulates the same `ln(n-i) - ln(i+1)` term, in the same
75
+ * order, that the old per-index `logChoose` recomputation used, and the running
76
+ * sums add the same pmf values in the same order. Every entry is therefore
77
+ * bit-for-bit identical to the previous implementation, without the old O(n²)
78
+ * (signTest) and O(n³) (signTestPower) recomputation.
79
+ */
80
+ function lowerTailSums(n: number, limit: number): number[] {
81
+ const halfPow = n * Math.log(2)
82
+ const sums: number[] = []
83
+ let logCoeff = 0
84
+ let cumulative = 0
85
+ for (let index = 0; index <= limit; index++) {
86
+ if (index > 0) logCoeff += Math.log(n - (index - 1)) - Math.log(index)
87
+ cumulative += Math.exp(logCoeff - halfPow)
88
+ sums.push(cumulative)
89
+ }
90
+ return sums
91
+ }
92
+
60
93
  /**
61
94
  * Report how much a finite paired sample can ever say under an exact sign test.
62
95
  *
@@ -77,11 +110,17 @@ export function signTestPower(
77
110
  const floor = (n: number): number => n === 0 ? 1 : Math.min(1, 2 ** (1 - n))
78
111
  const bestRawP = floor(discordant)
79
112
  const bestAdjustedP = Math.min(1, bestRawP * comparisons)
113
+ // Compute every candidate tail in one pass instead of re-deriving each
114
+ // cumulative pmf from scratch; 2 ** (1 - n) underflows to 0 for large n, and
115
+ // Math.min already caps the floor at 1, so no extra overflow handling is
116
+ // needed here.
117
+ const limit = Math.floor(discordant / 2)
118
+ const tails = lowerTailSums(discordant, limit)
80
119
  // Walk from the largest significant tail down, so the reported win count is
81
120
  // the minimum that still reaches alpha rather than a clean sweep.
82
121
  let minimumWins: number | null = null
83
- for (let tail = Math.floor(discordant / 2); tail >= 0; tail--) {
84
- const adjusted = Math.min(1, signTest(tail, discordant - tail).pValue * comparisons)
122
+ for (let tail = limit; tail >= 0; tail--) {
123
+ const adjusted = Math.min(1, Math.min(1, 2 * tails[tail]!) * comparisons)
85
124
  if (adjusted <= alpha) {
86
125
  minimumWins = discordant - tail
87
126
  break
@@ -151,38 +190,23 @@ export interface BootstrapOptions {
151
190
  seed?: number
152
191
  }
153
192
 
154
- /**
155
- * Log of the binomial coefficient, accumulated rather than factorized so large
156
- * pair counts stay finite.
157
- * @param n - trials.
158
- * @param k - successes, at most n.
159
- * @returns `ln C(n, k)`.
160
- */
161
- function logChoose(n: number, k: number): number {
162
- let value = 0
163
- for (let index = 0; index < k; index++) {
164
- value += Math.log(n - index) - Math.log(index + 1)
165
- }
166
- return value
167
- }
168
-
169
193
  /**
170
194
  * Exact two-sided sign test. Under the null hypothesis a discordant pair is a
171
195
  * win or a loss with equal probability, so the win count is Binomial(n, 1/2)
172
- * and the p-value doubles the smaller tail.
196
+ * and the p-value doubles the smaller tail. Invalid tallies (negative,
197
+ * fractional, non-finite) are treated as zero so the binomial indices stay
198
+ * well-defined.
173
199
  * @param wins - pairs where B beat A.
174
200
  * @param losses - pairs where A beat B.
175
201
  * @returns the discordant count and the exact p-value in `[0, 1]`.
176
202
  */
177
203
  export function signTest(wins: number, losses: number): SignTestOutcome {
178
- const discordant = wins + losses
204
+ const positiveWins = countOf(wins)
205
+ const positiveLosses = countOf(losses)
206
+ const discordant = positiveWins + positiveLosses
179
207
  if (discordant === 0) return { discordant, pValue: 1 }
180
- const tail = Math.min(wins, losses)
181
- const halfPow = discordant * Math.log(2)
182
- let cumulative = 0
183
- for (let index = 0; index <= tail; index++) {
184
- cumulative += Math.exp(logChoose(discordant, index) - halfPow)
185
- }
208
+ const tail = Math.min(positiveWins, positiveLosses)
209
+ const cumulative = lowerTailSums(discordant, tail)[tail]!
186
210
  return { discordant, pValue: Math.min(1, 2 * cumulative) }
187
211
  }
188
212