Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative Binomial Distribution #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/negative_binomial.js
@@ -0,0 +1,41 @@
import defaultSource from "./defaultSource.js";
import beta from "./beta.js";
import geometric from "./geometric.js";

export default (function sourceRandomNegativeBinomial(source) {
var G = geometric.source(source),
B = beta.source(source);

// X is the random variable and n is the success and p the probability of success
// formula is (x-1)C(n-1).(p^n).(1-p)^(x-n)

function randomNegativeBinomial(n, p) {
n = +n;
if ((p = +p) >= 1) return () => n;
if (p <= 0) return () => 0;
return function() {
var acc = 0, nn = n-1, pp = p-1;
while (nn * pp > 16 && nn * (1 - pp) > 16) {
var i = Math.floor((nn + 1) * pp),
y = B(i, nn - i + 1)();
if (y <= pp) {
acc += i;
nn -= i;
pp = (pp - y) / (1 - y);
} else {
nn = i - 1;
pp /= y;
}
}
var sign = pp < 0.5,
pFinal = sign ? pp : 1 - pp,
g = G(pFinal);
for (var s = g(), k = 0; s <= nn; ++k) s += g();
return acc + (sign ? k : nn - k);
};
}

randomNegativeBinomial.source = sourceRandomNegativeBinomial;

return randomNegativeBinomial;
})(defaultSource);