-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (39 loc) · 1.24 KB
/
index.js
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
const jStat = require('jstat');
const BinomialProportion = (count, nobs, alpha = 0.05, method = 'normal') => {
if (nobs === 0) {
return {
lowerBound: 0,
value: 0,
upperBound: 0,
};
}
const z = -1 * jStat.normal.inv(alpha / 2, 0, 1);
let p = count / nobs;
let sd;
switch (method) {
case 'normal': {
sd = z * Math.sqrt((p * (1 - p)) / nobs);
return { lowerBound: p - sd, value: p, upperBound: p + sd };
}
case 'agresti_coull': {
const pAc = (count + z ** 2 / 2) / (nobs + z ** 2);
sd = z * Math.sqrt((pAc * (1 - pAc)) / (nobs + z ** 2));
return { lowerBound: pAc - sd, value: p, upperBound: pAc + sd };
}
case 'wilson': {
const denominator = 1 + z ** 2 / nobs;
const centreAdjustedProbability = p + (z * z) / (2 * nobs);
const adjustedStandardDeviation = Math.sqrt(
(p * (1 - p) + z ** 2 / (4 * nobs)) / nobs
);
const lowerBound =
(centreAdjustedProbability - z * adjustedStandardDeviation) /
denominator;
const upperBound =
(centreAdjustedProbability + z * adjustedStandardDeviation) /
denominator;
return { lowerBound, value: p, upperBound };
}
}
};
module.exports = BinomialProportion;