-
Notifications
You must be signed in to change notification settings - Fork 3
/
negative_binomial.d
494 lines (416 loc) · 17 KB
/
negative_binomial.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/++
This module contains algorithms for the $(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution).
There are multiple alternative formulations of the Negative Binomial Distribution. The
formulation in this module uses the number of Bernoulli trials until `r` successes.
License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
Authors: John Michael Hall
Copyright: 2022-3 Mir Stat Authors.
+/
module mir.stat.distribution.negative_binomial;
import mir.bignum.fp: Fp;
import mir.internal.utility: isFloatingPoint;
/++
Computes the negative binomial probability mass function (PMF).
Params:
k = value to evaluate PMF (e.g. number of "heads")
r = number of successes until stopping
p = `true` probability
See_also:
$(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution)
+/
@safe pure nothrow @nogc
T negativeBinomialPMF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T)
in (r > 0, "number of failures must be larger than zero")
in (p >= 0, "p must be greater than or equal to 0")
in (p <= 1, "p must be less than or equal to 1")
{
import mir.math.common: pow;
import mir.combinatorics: binomial;
return binomial(k + r - 1, r - 1) * pow(1 - p, k) * pow(p, r);
}
///
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: shouldApprox;
4.negativeBinomialPMF(6, 3.0 / 4).shouldApprox == 0.0875988;
}
//
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: shouldApprox;
0.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.0877915;
1.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.175583;
2.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.2048468;
3.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.1820861;
4.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.1365645;
5.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.09104303;
6.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.05563741;
7.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.0317928;
8.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.0172211;
9.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.008929461;
10.negativeBinomialPMF(6, 2.0 / 3).shouldApprox == 0.00446473;
}
/++
Computes the negative binomial probability mass function (PMF) directly with extended
floating point types (e.g. `Fp!128`), which provides additional accuracy for
extreme values of `k`, `r`, or `p`.
Params:
k = value to evaluate PMF (e.g. number of "heads")
r = number of successes until stopping
p = `true` probability
See_also:
$(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution)
+/
@safe pure nothrow @nogc
T fp_negativeBinomialPMF(T)(const size_t k, const size_t r, const T p)
if (is(T == Fp!size, size_t size))
in (r > 0, "number of failures must be larger than zero")
in (cast(double) p >= 0, "p must be greater than or equal to 0")
in (cast(double) p <= 1, "p must be less than or equal to 1")
{
import mir.math.internal.fp_powi: fp_powi;
import mir.math.numeric: binomialCoefficient;
return binomialCoefficient(k + r - 1, cast(const uint) (r - 1)) * fp_powi(T(1 - cast(double) p), k) * fp_powi(p, r);
}
/// fp_binomialPMF provides accurate values for large values of `n`
version(mir_stat_test_fp)
@safe pure nothrow @nogc
unittest {
import mir.bignum.fp: Fp, fp_log;
import mir.test: shouldApprox;
1.fp_negativeBinomialPMF(1_000_000, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(1, 1_000_000, 0.75);
}
// test more values
version(mir_stat_test_fp)
@safe pure nothrow @nogc
unittest {
import mir.bignum.fp: Fp, fp_log;
import mir.test: shouldApprox;
enum size_t val = 1_000_000;
0.fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(0, val + 5, 0.75);
1.fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(1, val + 5, 0.75);
2.fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(2, val + 5, 0.75);
5.fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(5, val + 5, 0.75);
(val / 2).fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(val / 2, val + 5, 0.75);
(val - 5).fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(val - 5, val + 5, 0.75);
(val - 2).fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(val - 2, val + 5, 0.75);
(val - 1).fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(val - 1, val + 5, 0.75);
(val - 0).fp_negativeBinomialPMF(val + 5, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(val, val + 5, 0.75);
}
// using Fp!128
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.conv: to;
import mir.test: shouldApprox;
for (size_t i; i <= 5; i++) {
i.fp_negativeBinomialPMF(5, Fp!128(0.50)).to!double.shouldApprox == negativeBinomialPMF(i, 5, 0.50);
i.fp_negativeBinomialPMF(5, Fp!128(0.75)).to!double.shouldApprox == negativeBinomialPMF(i, 5, 0.75);
}
}
/++
Computes the negative binomial cumulative distribution function (CDF).
Params:
k = value to evaluate CDF (e.g. number of "heads")
r = number of successes until stopping
p = `true` probability
See_also:
$(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution)
+/
@safe pure nothrow @nogc
T negativeBinomialCDF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T)
in (r > 0, "number of failures must be larger than zero")
in (p >= 0, "p must be greater than or equal to 0")
in (p <= 1, "p must be less than or equal to 1")
{
import mir.math.common: pow;
import std.mathspecial: betaIncomplete;
if (k == 0) {
return pow(p, r);
}
return 1 - betaIncomplete(k + 1, r, 1 - p);
}
///
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: shouldApprox;
4.negativeBinomialCDF(6, 3.0 / 4).shouldApprox == 0.9218731;
}
// test multiple
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: shouldApprox;
static double sumOfnegativeBinomialPMFs(T)(size_t k, size_t r, T p) {
double result = 0.0;
for (size_t i; i <= k; i++) {
result += negativeBinomialPMF(i, r, p);
}
return result;
}
for (size_t i; i <= 10; i++) {
i.negativeBinomialCDF(5, 0.25).shouldApprox == sumOfnegativeBinomialPMFs(i, 5, 0.25);
i.negativeBinomialCDF(5, 0.50).shouldApprox == sumOfnegativeBinomialPMFs(i, 5, 0.50);
i.negativeBinomialCDF(5, 0.75).shouldApprox == sumOfnegativeBinomialPMFs(i, 5, 0.75);
i.negativeBinomialCDF(6, 0.25).shouldApprox == sumOfnegativeBinomialPMFs(i, 6, 0.25);
i.negativeBinomialCDF(6, 0.5).shouldApprox == sumOfnegativeBinomialPMFs(i, 6, 0.5);
i.negativeBinomialCDF(6, 0.75).shouldApprox == sumOfnegativeBinomialPMFs(i, 6, 0.75);
}
}
/++
Computes the negative binomial complementary cumulative distribution function (CCDF).
Params:
k = value to evaluate CCDF (e.g. number of "heads")
r = number of successes until stopping
p = `true` probability
See_also:
$(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution)
+/
@safe pure nothrow @nogc
T negativeBinomialCCDF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T)
in (r > 0, "number of failures must be larger than zero")
in (p >= 0, "p must be greater than or equal to 0")
in (p <= 1, "p must be less than or equal to 1")
{
import mir.math.common: pow;
import std.mathspecial: betaIncomplete;
if (k == 0) {
return 1 - pow(p, r);
}
return betaIncomplete(k + 1, r, 1 - p);
}
///
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: shouldApprox;
4.negativeBinomialCCDF(6, 3.0 / 4).shouldApprox == 0.07812691;
}
// test multiple
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: shouldApprox;
for (size_t i; i <= 10; i++) {
i.negativeBinomialCCDF(5, 0.25).shouldApprox == 1 - negativeBinomialCDF(i, 5, 0.25);
i.negativeBinomialCCDF(5, 0.50).shouldApprox == 1 - negativeBinomialCDF(i, 5, 0.50);
i.negativeBinomialCCDF(5, 0.75).shouldApprox == 1 - negativeBinomialCDF(i, 5, 0.75);
i.negativeBinomialCCDF(6, 0.25).shouldApprox == 1 - negativeBinomialCDF(i, 6, 0.25);
i.negativeBinomialCCDF(6, 0.5).shouldApprox == 1 - negativeBinomialCDF(i, 6, 0.5);
i.negativeBinomialCCDF(6, 0.75).shouldApprox == 1 - negativeBinomialCDF(i, 6, 0.75);
}
}
private
@safe pure nothrow @nogc
size_t negativeBinomialInvCDFSearch(T)(const size_t guess, ref T cdfGuess, const T q, const size_t r, const T p, const size_t searchIncrement)
if (isFloatingPoint!T)
in (r > 0, "number of failures must be larger than zero")
in (q >= 0, "q must be greater than or equal to 0")
in (q <= 1, "q must be less than or equal to 1")
in (p >= 0, "p must be greater than or equal to 0")
in (p <= 1, "p must be less than or equal to 1")
{
size_t guessNew = guess;
if (q <= cdfGuess) {
T cdfGuessPrevious;
while (guessNew > 0) {
cdfGuessPrevious = cdfGuess;
cdfGuess = negativeBinomialCDF(guessNew - searchIncrement, r, p);
if (q > cdfGuess) {
cdfGuess = cdfGuessPrevious;
break;
}
guessNew = guessNew > searchIncrement ? guessNew - searchIncrement : 0;
}
} else {
while (q > cdfGuess) {
guessNew = guessNew + searchIncrement;
cdfGuess = negativeBinomialCDF(guessNew, r, p);
}
}
return guessNew;
}
/++
Computes the negative binomial inverse cumulative distribution function (InvCDF).
Params:
q = value to evaluate InvCDF
r = number of successes until stopping
p = `true` probability
See_also:
$(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution)
+/
@safe pure nothrow @nogc
size_t negativeBinomialInvCDF(T)(const T q, const size_t r, const T p)
if (isFloatingPoint!T)
in (r > 0, "number of failures must be larger than zero")
in (q >= 0, "q must be greater than or equal to 0")
in (q <= 1, "q must be less than or equal to 1")
in (p >= 0, "p must be greater than or equal to 0")
in (p <= 1, "p must be less than or equal to 1")
{
import mir.math.common: floor, sqrt;
import mir.stat.distribution.normal: normalInvCDF;
if (q == 0) {
return 0;
} else if (q == 1) {
return size_t.max;
}
size_t guess = 0;
T mu = r * (1 - p) / p;
T pre_std = sqrt(r * (1 - p));
T std = pre_std / p;
T z = normalInvCDF(q);
if (r > 20 && p > 0.25 && p < 0.75) {
guess = cast(size_t) floor(mu + std * z - 0.5);
} else {
// Cornish-Fisher Approximation
guess = cast(size_t) floor(mu + std * (z + ((2 - p) / pre_std) * (z * z - 1) / 6));
}
T cdfGuess = negativeBinomialCDF(guess, r, p);
if (guess < 10_000) {
return negativeBinomialInvCDFSearch(guess, cdfGuess, q, r, p, 1);
} else {
// Faster search for large values of guess
size_t searchIncrement = cast(size_t) floor(guess * 0.001);
size_t searchIncrementPrevious;
do {
searchIncrementPrevious = searchIncrement;
guess = negativeBinomialInvCDFSearch(guess, cdfGuess, q, r, p, searchIncrement);
searchIncrement = cast(size_t) floor(searchIncrement * 0.01);
} while (searchIncrementPrevious > 0 && searchIncrement > guess * (10 * T.epsilon));
if (searchIncrementPrevious <= 1) {
return guess;
} else {
return negativeBinomialInvCDFSearch(guess, cdfGuess, q, r, p, 1);
}
}
}
///
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.test: should;
0.9.negativeBinomialInvCDF(6, 3.0 / 4).should == 4;
}
//
version(mir_stat_test)
@safe pure nothrow
unittest {
import mir.test: should;
0.negativeBinomialInvCDF(5, 0.6).should == 0;
1.negativeBinomialInvCDF(5, 0.6).should == size_t.max;
for (double x = 0.05; x < 1; x = x + 0.05) {
size_t value = x.negativeBinomialInvCDF(5, 0.6);
value.negativeBinomialCDF(5, 0.6).should!"a >= b"(x);
if (value > 0) {
(value - 1).negativeBinomialCDF(5, 0.6).should!"a < b"(x);
}
}
}
// alternate guess paths
version(mir_stat_test)
@safe pure nothrow
unittest {
import mir.test: should;
static immutable size_t[] ns = [ 25, 37, 34, 25, 25, 105];
static immutable double[] ps = [0.55, 0.2, 0.15, 0.05, 1.0e-8, 0.025];
size_t value;
for (size_t i; i < 6; i++) {
for (double x = 0.05; x < 1; x = x + 0.05) {
value = x.negativeBinomialInvCDF(ns[i], ps[i]);
negativeBinomialCDF(value, ns[i], ps[i]).should!"a >= b"(x);
if (value > 0) {
negativeBinomialCDF(value - 1, ns[i], ps[i]).should!"a < b"(x);
}
}
}
}
// more detailed guess paths
version(mir_stat_test_binom_multi)
@safe pure nothrow
unittest {
import mir.test: should;
static immutable size_t[] ns = [ 25, 37, 34, 25, 25, 105];
static immutable double[] ps = [0.55, 0.2, 0.15, 0.05, 1.0e-8, 0.025];
size_t value;
for (size_t i; i < 6; i++) {
for (double x = 0.01; x < 1; x = x + 0.01) {
value = x.negativeBinomialInvCDF(ns[i], ps[i]);
negativeBinomialCDF(value, ns[i], ps[i]).should!"a >= b"(x);
if (value > 0) {
negativeBinomialCDF(value - 1, ns[i], ps[i]).should!"a < b"(x);
}
}
}
}
/++
Computes the negative binomial log probability mass function (LPMF).
Params:
k = value to evaluate PMF (e.g. number of "heads")
r = number of successes until stopping
p = `true` probability
See_also:
$(LINK2 https://en.wikipedia.org/wiki/Negative_binomial_distribution, Negative Binomial Distribution)
+/
@safe pure nothrow @nogc
T negativeBinomialLPMF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T)
in (r > 0, "number of failures must be larger than zero")
in (p >= 0, "p must be greater than or equal to 0")
in (p <= 1, "p must be less than or equal to 1")
{
import mir.math.internal.xlogy: xlogy, xlog1py;
import mir.math.internal.log_binomial: logBinomialCoefficient;
return logBinomialCoefficient(k + r - 1, cast(const uint) (r - 1)) + xlog1py(k, -p) + xlogy(r, p);
}
///
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.math.common: exp;
import mir.test: shouldApprox;
4.negativeBinomialLPMF(6, 3.0 / 4).exp.shouldApprox == 4.negativeBinomialPMF(6, 3.0 / 4);
}
//
version(mir_stat_test)
@safe pure nothrow @nogc
unittest {
import mir.math.common: exp;
import mir.test: shouldApprox;
for (size_t i; i <= 10; i++) {
i.negativeBinomialLPMF(5, 0.5).exp.shouldApprox == negativeBinomialPMF(i, 5, 0.5);
i.negativeBinomialLPMF(5, 0.75).exp.shouldApprox == negativeBinomialPMF(i, 5, 0.75);
}
}
/// Accurate values for large values of `n`
version(mir_stat_test_fp)
@safe pure nothrow @nogc
unittest {
import mir.bignum.fp: Fp, fp_log;
import mir.test: shouldApprox;
enum size_t val = 1_000_000;
1.negativeBinomialLPMF(1_000_000, 0.75).shouldApprox == fp_negativeBinomialPMF(1, 1_000_000, Fp!128(0.75)).fp_log!double;
}
// testing more values
version(mir_stat_test_fp)
@safe pure nothrow @nogc
unittest {
import mir.bignum.fp: Fp, fp_log;
import mir.test: shouldApprox;
enum size_t val = 1_000_000;
0.negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(0, val + 5, Fp!128(0.75)).fp_log!double;
1.negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(1, val + 5, Fp!128(0.75)).fp_log!double;
2.negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(2, val + 5, Fp!128(0.75)).fp_log!double;
5.negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(5, val + 5, Fp!128(0.75)).fp_log!double;
(val / 2).negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(val / 2, val + 5, Fp!128(0.75)).fp_log!double;
(val - 5).negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(val - 5, val + 5, Fp!128(0.75)).fp_log!double;
(val - 2).negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(val - 2, val + 5, Fp!128(0.75)).fp_log!double;
(val - 1).negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(val - 1, val + 5, Fp!128(0.75)).fp_log!double;
(val - 0).negativeBinomialLPMF(val + 5, 0.75).shouldApprox == fp_negativeBinomialPMF(val, val + 5, Fp!128(0.75)).fp_log!double;
}