wilson-interval 5.0.0 → 5.1.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.
- package/.github/dependabot.yml +11 -0
- package/README.md +8 -15
- package/package.json +2 -6
- package/src/index.js +88 -34
- package/src/index.test.js +79 -22
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "npm" # See documentation for possible values
|
|
9
|
+
directory: "/" # Location of package manifests
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "weekly"
|
package/README.md
CHANGED
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/wilson-interval)
|
|
5
5
|
[](https://www.npmjs.com/package/wilson-interval)
|
|
6
6
|
[](https://codecov.io/github/erikfox/wilson-interval)
|
|
7
|
-
|
|
8
|
-
|
|
9
7
|
[]()
|
|
10
8
|
[](https://github.com/erikfox/wilson-interval/issues)
|
|
11
9
|
|
|
@@ -27,20 +25,15 @@ import wilson from 'wilson-interval';
|
|
|
27
25
|
|
|
28
26
|
## Usage
|
|
29
27
|
|
|
30
|
-
### wilson(observed, sample[, population ][,
|
|
31
|
-
|
|
32
|
-
- `observed` - Number of observed positive outcomes.
|
|
28
|
+
### wilson(observed, sample[, population ][, confidence ]);
|
|
33
29
|
|
|
34
|
-
- `
|
|
30
|
+
- `observed` - Required, accepts `Number >= 0`. Count of observed positive outcomes as a whole number.
|
|
31
|
+
- `sample` - Required, accepts `Number > 0`. Size of sample as a whole number.
|
|
35
32
|
|
|
36
33
|
Optional arguments:
|
|
37
34
|
|
|
38
|
-
- `population` - Default `
|
|
39
|
-
|
|
40
|
-
- `options` - Default `{}`. Options object. Available parameters:
|
|
41
|
-
|
|
42
|
-
- `confidence` - Default `0.95`. Desired confidence level of interval.
|
|
43
|
-
- `precision` - Default `20`. Number of significant figures to use in calculations and output.
|
|
35
|
+
- `population` - Default `null`, accepts `Number > 0`. Total population from which sample was taken (to use **[Singleton's adjustment](https://corplingstats.wordpress.com/2012/04/30/inferential-statistics/)**<sup>[1]</sup>). Must be a positive whole number.
|
|
36
|
+
- `confidence` - Default `0.95`, accepts `1 > Number > 0`. Desired confidence level of interval as a fractional decimal value between 0 and 1.
|
|
44
37
|
|
|
45
38
|
#### Example
|
|
46
39
|
```js
|
|
@@ -49,9 +42,9 @@ return wilson(5, 100);
|
|
|
49
42
|
returns
|
|
50
43
|
```json
|
|
51
44
|
{
|
|
52
|
-
"center":
|
|
53
|
-
"high":
|
|
54
|
-
"low":
|
|
45
|
+
"center": 0.06664707421899263,
|
|
46
|
+
"high": 0.11175046929755608,
|
|
47
|
+
"low": 0.02154367914042918
|
|
55
48
|
}
|
|
56
49
|
```
|
|
57
50
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wilson-interval",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"author": "Erik Fox <eriktfox@gmail.com> (http://erikfox.dev)",
|
|
5
5
|
"description": "Used to calculate the high bound, low bound, and center of a Wilson score interval. Features support for continuity correction and Singleton's adjustment.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/erikfox/wilson-interval",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/erikfox/wilson-interval.git"
|
|
10
|
+
"url": "git+https://github.com/erikfox/wilson-interval.git"
|
|
11
11
|
},
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/erikfox/wilson-interval/issues"
|
|
@@ -36,9 +36,5 @@
|
|
|
36
36
|
"scripts": {
|
|
37
37
|
"test": "node --test src/index.test.js",
|
|
38
38
|
"test:cc": "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info src/index.test.js"
|
|
39
|
-
},
|
|
40
|
-
"dependencies": {
|
|
41
|
-
"mathjs": "^14.0.0",
|
|
42
|
-
"pnormaldist": "^1.0.0"
|
|
43
39
|
}
|
|
44
40
|
}
|
package/src/index.js
CHANGED
|
@@ -1,45 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export default function wilson(observed, sample, population = null, confidence = 0.95) {
|
|
2
|
+
observed = Number(observed);
|
|
3
|
+
sample = Number(sample);
|
|
4
|
+
confidence = Number(confidence);
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
precision = 20,
|
|
8
|
-
} = options;
|
|
9
|
-
|
|
10
|
-
const math = create(all, { number: 'BigNumber', precision });
|
|
11
|
-
|
|
12
|
-
observed = math.bignumber(observed);
|
|
13
|
-
sample = math.bignumber(sample);
|
|
14
|
-
|
|
15
|
-
const proportion = math.evaluate('observed / sample', { observed, sample });
|
|
6
|
+
if (observed < 0) {
|
|
7
|
+
throw new Error('Observed must be a non-negative whole number');
|
|
8
|
+
}
|
|
16
9
|
|
|
17
|
-
if (
|
|
18
|
-
|
|
10
|
+
if (sample <= 0) {
|
|
11
|
+
throw new Error('Sample must be a positive whole number');
|
|
19
12
|
}
|
|
20
13
|
|
|
21
|
-
|
|
14
|
+
if (confidence <= 0 || confidence >= 1) {
|
|
15
|
+
throw new Error('Confidence must be a decimal between 0 and 1');
|
|
16
|
+
}
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
if (Number.isSafeInteger(population)) {
|
|
19
|
+
if (population <= 0) {
|
|
20
|
+
throw new Error('Population must be a positive whole number');
|
|
21
|
+
}
|
|
22
|
+
sample = sample / Math.sqrt(1 - sample / population);
|
|
23
|
+
}
|
|
28
24
|
|
|
29
|
-
const
|
|
30
|
-
'(p + z * z / (2 * n) + z * sqrt(p * (1 - p) / n + z * z / (4 * (n * n)))) / (1 + z * z / n)',
|
|
31
|
-
scope,
|
|
32
|
-
));
|
|
25
|
+
const zScore = normalQuantile(1 - (1 - confidence) / 2);
|
|
33
26
|
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
));
|
|
27
|
+
const p = observed / sample;
|
|
28
|
+
const n = sample;
|
|
29
|
+
const z = zScore;
|
|
38
30
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
));
|
|
31
|
+
const high = ((p + z * z / (2 * n) + z * Math.sqrt(p * (1 - p) / n + z * z / (4 * n * n))) / (1 + z * z / n));
|
|
32
|
+
const low = ((p + z * z / (2 * n) - z * Math.sqrt(p * (1 - p) / n + z * z / (4 * n * n))) / (1 + z * z / n));
|
|
33
|
+
const center = ((p + z * z / (2 * n)) / (1 + z * z / n));
|
|
43
34
|
|
|
44
35
|
return {
|
|
45
36
|
high,
|
|
@@ -47,3 +38,66 @@ export default function wilson(observed, sample, population = false, options = {
|
|
|
47
38
|
low,
|
|
48
39
|
};
|
|
49
40
|
}
|
|
41
|
+
|
|
42
|
+
// Returns the quantile (inverse CDF) for the standard normal distribution
|
|
43
|
+
export function normalQuantile(p) {
|
|
44
|
+
if (p < 0 || p > 1) {
|
|
45
|
+
throw new Error('The probability p must be between 0 and 1');
|
|
46
|
+
}
|
|
47
|
+
if (p === 0) return -Infinity;
|
|
48
|
+
if (p === 1) return Infinity;
|
|
49
|
+
|
|
50
|
+
// Coefficients in rational approximations
|
|
51
|
+
const a = [
|
|
52
|
+
-3.969683028665376e+01,
|
|
53
|
+
2.209460984245205e+02,
|
|
54
|
+
-2.759285104469687e+02,
|
|
55
|
+
1.383577518672690e+02,
|
|
56
|
+
-3.066479806614716e+01,
|
|
57
|
+
2.506628277459239e+00
|
|
58
|
+
];
|
|
59
|
+
const b = [
|
|
60
|
+
-5.447609879822406e+01,
|
|
61
|
+
1.615858368580409e+02,
|
|
62
|
+
-1.556989798598866e+02,
|
|
63
|
+
6.680131188771972e+01,
|
|
64
|
+
-1.328068155288572e+01
|
|
65
|
+
];
|
|
66
|
+
const c = [
|
|
67
|
+
-7.784894002430293e-03,
|
|
68
|
+
-3.223964580411365e-01,
|
|
69
|
+
-2.400758277161838e+00,
|
|
70
|
+
-2.549732539343734e+00,
|
|
71
|
+
4.374664141464968e+00,
|
|
72
|
+
2.938163982698783e+00
|
|
73
|
+
];
|
|
74
|
+
const d = [
|
|
75
|
+
7.784695709041462e-03,
|
|
76
|
+
3.224671290700398e-01,
|
|
77
|
+
2.445134137142996e+00,
|
|
78
|
+
3.754408661907416e+00
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
// Define break-points
|
|
82
|
+
const plow = 0.02425;
|
|
83
|
+
const phigh = 1 - plow;
|
|
84
|
+
|
|
85
|
+
let q, r;
|
|
86
|
+
if (p < plow) {
|
|
87
|
+
// Rational approximation for lower region
|
|
88
|
+
q = Math.sqrt(-2 * Math.log(p));
|
|
89
|
+
return (((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5]) /
|
|
90
|
+
((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1);
|
|
91
|
+
} else if (phigh < p) {
|
|
92
|
+
// Rational approximation for upper region
|
|
93
|
+
q = Math.sqrt(-2 * Math.log(1 - p));
|
|
94
|
+
return -(((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5]) /
|
|
95
|
+
((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1);
|
|
96
|
+
} else {
|
|
97
|
+
// Rational approximation for central region
|
|
98
|
+
q = p - 0.5;
|
|
99
|
+
r = q * q;
|
|
100
|
+
return (((((a[0]*r + a[1])*r + a[2])*r + a[3])*r + a[4])*r + a[5])*q /
|
|
101
|
+
(((((b[0]*r + b[1])*r + b[2])*r + b[3])*r + b[4])*r + 1);
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/index.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import wilson from './index.js';
|
|
1
|
+
import wilson, { normalQuantile } from './index.js';
|
|
2
2
|
import test from 'node:test';
|
|
3
3
|
import assert from 'node:assert';
|
|
4
4
|
import { describe } from 'node:test';
|
|
@@ -9,9 +9,21 @@ describe('wilson()', () => {
|
|
|
9
9
|
const result = wilson(5, 100);
|
|
10
10
|
|
|
11
11
|
assert.deepEqual(result, {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
center: 0.06664707421899263,
|
|
13
|
+
high: 0.11175046929755608,
|
|
14
|
+
low: 0.02154367914042918
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('wilson(-1, 100)', () => {
|
|
19
|
+
assert.throws(() => wilson(-1, 100), {
|
|
20
|
+
message: 'Observed must be a non-negative whole number'
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('wilson(5, 0)', () => {
|
|
25
|
+
assert.throws(() => wilson(5, 0), {
|
|
26
|
+
message: 'Sample must be a positive whole number'
|
|
15
27
|
});
|
|
16
28
|
});
|
|
17
29
|
});
|
|
@@ -21,34 +33,79 @@ describe('wilson()', () => {
|
|
|
21
33
|
const result = wilson(5, 100, 227);
|
|
22
34
|
|
|
23
35
|
assert.deepEqual(result, {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
center: 0.050319696641804315,
|
|
37
|
+
high: 0.08456089359579015,
|
|
38
|
+
low: 0.016078499687818482
|
|
27
39
|
});
|
|
28
40
|
});
|
|
29
|
-
});
|
|
30
41
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
assert.deepEqual(result, {
|
|
36
|
-
"center": "0.0666474",
|
|
37
|
-
"high": "0.111751",
|
|
38
|
-
"low": "0.0215437",
|
|
42
|
+
test('wilson(5, 100, 0)', () => {
|
|
43
|
+
assert.throws(() => wilson(5, 100, 0), {
|
|
44
|
+
message: 'Population must be a positive whole number'
|
|
39
45
|
});
|
|
40
46
|
});
|
|
41
47
|
});
|
|
42
48
|
|
|
43
|
-
describe('handles confidence
|
|
44
|
-
test('wilson(5, 100, 227,
|
|
45
|
-
const result = wilson(5, 100, 227,
|
|
49
|
+
describe('handles confidence arg', () => {
|
|
50
|
+
test('wilson(5, 100, 227, 0.9)', () => {
|
|
51
|
+
const result = wilson(5, 100, 227, 0.9);
|
|
46
52
|
|
|
47
53
|
assert.deepEqual(result, {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
center: 0.04657481789797846,
|
|
55
|
+
high: 0.0748286439672683,
|
|
56
|
+
low: 0.018320991828688626
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('wilson(5, 100, 227, 1)', () => {
|
|
61
|
+
assert.throws(() => wilson(5, 100, 227, 1), {
|
|
62
|
+
message: 'Confidence must be a decimal between 0 and 1'
|
|
51
63
|
});
|
|
52
64
|
});
|
|
53
65
|
});
|
|
54
66
|
});
|
|
67
|
+
|
|
68
|
+
describe('normalQuantile()', () => {
|
|
69
|
+
describe('handles base args', () => {
|
|
70
|
+
test('normalQuantile(0.95)', () => {
|
|
71
|
+
const result = normalQuantile(0.95);
|
|
72
|
+
assert.strictEqual(result, 1.644853625133699);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('normalQuantile(0.01)', () => {
|
|
76
|
+
const result = normalQuantile(0.01);
|
|
77
|
+
assert.strictEqual(result, -2.326347874388028);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('normalQuantile(0.99)', () => {
|
|
81
|
+
const result = normalQuantile(0.99);
|
|
82
|
+
assert.strictEqual(result, 2.326347874388028);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('handles edge cases', () => {
|
|
87
|
+
test('normalQuantile(0)', () => {
|
|
88
|
+
const result = normalQuantile(0);
|
|
89
|
+
assert.strictEqual(result, -Infinity);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('normalQuantile(1)', () => {
|
|
93
|
+
const result = normalQuantile(1);
|
|
94
|
+
assert.strictEqual(result, Infinity);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('throws on invalid args', () => {
|
|
99
|
+
test('normalQuantile(-1)', () => {
|
|
100
|
+
assert.throws(() => normalQuantile(-1), {
|
|
101
|
+
message: 'The probability p must be between 0 and 1'
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('normalQuantile(2)', () => {
|
|
106
|
+
assert.throws(() => normalQuantile(2), {
|
|
107
|
+
message: 'The probability p must be between 0 and 1'
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|