Skip to content

Commit a636ffc

Browse files
committed
Implements percentiles
Fixes #26
1 parent 72ae9ab commit a636ffc

File tree

7 files changed

+296
-111
lines changed

7 files changed

+296
-111
lines changed

.eslintrc.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
module.exports = {
2-
"extends": "airbnb-base"
3-
};
2+
extends: 'airbnb-base',
3+
rules: {
4+
'arrow-parens': ['error', 'always'],
5+
'no-mixed-operators': [
6+
'error',
7+
{
8+
groups: [
9+
['&', '|', '^', '~', '<<', '>>', '>>>'],
10+
['==', '!=', '===', '!==', '>', '>=', '<', '<='],
11+
['&&', '||'],
12+
['in', 'instanceof'],
13+
],
14+
allowSamePrecedence: true,
15+
},
16+
],
17+
},
18+
};

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": true,
6+
"arrowParens": "always"
7+
}

README.md

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# stat-methods
22

3-
[![Build Status](https://travis-ci.org/boristane/stat-methods.js.svg?branch=master)](https://travis-ci.org/boristane/stat-methods.js) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/831be96eff514a60a3231a7885de3af0)](https://www.codacy.com/app/boris.tane/stat-methods.js?utm_source=github.com&utm_medium=referral&utm_content=boristane/stat-methods.js&utm_campaign=Badge_Grade) [![](https://img.shields.io/bundlephobia/min/react.svg)](https://www.npmjs.com/package/stat-methods)
3+
[![Build Status](https://travis-ci.org/boristane/stat-methods.js.svg?branch=master)](https://travis-ci.org/boristane/stat-methods.js) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/831be96eff514a60a3231a7885de3af0)](https://www.codacy.com/app/boris.tane/stat-methods.js?utm_source=github.com&utm_medium=referral&utm_content=boristane/stat-methods.js&utm_campaign=Badge_Grade) [![](https://img.shields.io/bundlephobia/min/react.svg)](https://www.npmjs.com/package/stat-methods)
44

5-
[![NPM](https://nodei.co/npm/stat-methods.png)](https://nodei.co/npm/stat-methods/)
5+
[![NPM](https://nodei.co/npm/stat-methods.png)](https://nodei.co/npm/stat-methods/)
66

77
## Getting Started
88

@@ -20,40 +20,41 @@ npm i stat-methods
2020

2121
1. [Averages and measures of central location](#Averages-and-measures-of-central-location)
2222

23-
- [mean](#mean)
24-
- [harmonicMean](#harmonicMean)
25-
- [geometricMean](#geometricMean)
26-
- [median](#median)
27-
- [medianLow](#medianLow)
28-
- [medianHigh](#medianHigh)
29-
- [medianGrouped](#medianGrouped)
30-
- [quartiles](#quartiles)
31-
- [midRange](#midRange)
32-
- [mode](#mode)
33-
- [rms](#rms)
23+
- [mean](#mean)
24+
- [harmonicMean](#harmonicMean)
25+
- [geometricMean](#geometricMean)
26+
- [median](#median)
27+
- [medianLow](#medianLow)
28+
- [medianHigh](#medianHigh)
29+
- [medianGrouped](#medianGrouped)
30+
- [quartiles](#quartiles)
31+
- [midRange](#midRange)
32+
- [mode](#mode)
33+
- [rms](#rms)
34+
- [percentile](#percentile)
3435

3536
2. [Measures of spread](#Measures-of-spread)
3637

37-
- [pVariance](#pVariance)
38-
- [pStdev](#pStdev)
39-
- [variance](#variance)
40-
- [stdev](#stdev)
41-
- [range](#range)
38+
- [pVariance](#pVariance)
39+
- [pStdev](#pStdev)
40+
- [variance](#variance)
41+
- [stdev](#stdev)
42+
- [range](#range)
4243

4344
3. [Descriptive statistics](#Descriptive-statistics)
4445

45-
- [min](#min)
46-
- [max](#max)
47-
- [product](#product)
48-
- [sum](#sum)
46+
- [min](#min)
47+
- [max](#max)
48+
- [product](#product)
49+
- [sum](#sum)
4950

5051
4. [Measures of similarity](#Measures-of-similarity)
5152

52-
- [covariance](#covariance)
53-
- [correlation](#correlation)
53+
- [covariance](#covariance)
54+
- [correlation](#correlation)
5455

5556
5. [Regressions](#Regressions)
56-
- [linReg](#linReg)
57+
- [linReg](#linReg)
5758

5859
### Averages and measures of central location
5960

@@ -72,6 +73,7 @@ These methods compute an average or typical value from a population or sample.
7273
| [midRange](#midRange) | Average of minimum and maximum |
7374
| [mode](#mode) | Modes (most common data points) of discrete data |
7475
| [rms](#rms) | Root Mean Square |
76+
| [percentile](#percentile) | Percentile |
7577

7678
Note: The methods do not require the data given to them to be sorted.
7779

@@ -133,7 +135,7 @@ Return the geometric mean of a numeric data array `arr`.
133135
The geometric mean is the nth root of the product of the `n` data points (`n` is the number of data points) For example, the geometric mean of three values `a`, `b` and `c` will be equivalent to `(a*b*c) ^ (1/3)`.
134136

135137
```js
136-
geometricMean([4, 1, 1/32]); // -> 0.5
138+
geometricMean([4, 1, 1 / 32]); // -> 0.5
137139
```
138140

139141
The geometric mean indicates the central tendency or typical value of a set of numbers and is often used when comparing different items — finding a single "figure of merit" for these items — when each item has multiple properties that have different numeric ranges.
@@ -156,13 +158,13 @@ Return the median (middle value) of a numeric data array `arr`.
156158

157159
The median is the value separating the higher half from the lower half of a data sample. The `median` method uses the “mean of middle two” method:
158160

159-
- If there is an odd number of numbers, the median is the middle one.
161+
- If there is an odd number of numbers, the median is the middle one.
160162

161163
```js
162164
median([1, 2, 3, 4, 5]); // -> 3
163165
```
164166

165-
- If there is an even number of observations, then there is no single middle value; the median is then defined as the mean of the two middle values.
167+
- If there is an even number of observations, then there is no single middle value; the median is then defined as the mean of the two middle values.
166168

167169
```js
168170
median([1, 2, 3, 4, 5, 6]); // -> 3.5
@@ -180,13 +182,13 @@ Return the low median of a data array `arr`. An optional `compareFunction` param
180182

181183
The low median is always a member of the data set. The `medianLow` method accepts both numeric and non numeric data arrays.
182184

183-
- When the number of observations is odd, the middle value is returned.
185+
- When the number of observations is odd, the middle value is returned.
184186

185187
```js
186188
medianLow([1, 2, 3, 4, 5]); // -> 3
187189
```
188190

189-
- When the number of observations is even, the smaller of the two middle values is returned.
191+
- When the number of observations is even, the smaller of the two middle values is returned.
190192

191193
```js
192194
medianLow([1, 2, 3, 4, 5, 6]); // -> 3
@@ -215,13 +217,13 @@ Return the high median of a data array `arr`. An optional `compareFunction` para
215217

216218
The high median is always a member of the data set. The `medianHigh` method accepts both numeric and non numeric data arrays.
217219

218-
- When the number of observations is odd, the middle value is returned.
220+
- When the number of observations is odd, the middle value is returned.
219221

220222
```js
221223
medianHigh([1, 2, 3, 4, 5]); // -> 3
222224
```
223225

224-
- When the number of observations is even, the larger of the two middle values is returned.
226+
- When the number of observations is even, the larger of the two middle values is returned.
225227

226228
```js
227229
medianHigh([1, 2, 3, 4, 5, 6]); // -> 4
@@ -265,14 +267,14 @@ If the data array is empty or contains a non finite `Number`, the method returns
265267
#### quartiles
266268

267269
```js
268-
quartiles(arr)
270+
quartiles(arr);
269271
```
270272

271273
Return the quartiles of a numeric data array `arr`.
272274

273-
- The first quartile (`Q1`) is defined as the middle number between the smallest number and the median of the data set.
274-
- The second quartile (`Q2`) is the median of the data.
275-
- The third quartile (`Q3`) is the middle value between the median and the highest value of the data set.
275+
- The first quartile (`Q1`) is defined as the middle number between the smallest number and the median of the data set.
276+
- The second quartile (`Q2`) is the median of the data.
277+
- The third quartile (`Q3`) is the middle value between the median and the highest value of the data set.
276278

277279
```js
278280
quartiles([2, 2, 3, 4]); // -> [2, 2.5, 3.5]
@@ -282,8 +284,8 @@ The data set if first ordered, from smallest to highest.
282284

283285
The median (`Q2`) is used to divide the ordered data set into two halves.
284286

285-
- If there are an odd number of data points in the original ordered data set the median is not included in either half.
286-
- If there are an even number of data points in the original ordered data set, the sata set is split exactly in half.
287+
- If there are an odd number of data points in the original ordered data set the median is not included in either half.
288+
- If there are an even number of data points in the original ordered data set, the sata set is split exactly in half.
287289

288290
The lower quartile value (`Q1`) is the median of the lower half of the data. The upper quartile value (`Q3`) is the median of the upper half of the data.
289291

@@ -354,6 +356,22 @@ rms([4, 1, 1, 3]); // -> 2.598076211353316;
354356

355357
If the data array is empty or contains a non-numeric value, the method returns `undefined`.
356358

359+
#### percentile
360+
361+
```js
362+
percentile(arr, k);
363+
```
364+
365+
Returns the `k^{th}` percentile of the data array.
366+
367+
A percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group3 of observations falls.
368+
369+
```js
370+
percentile([13, 20, 8, 8, 7, 10, 3, 15, 16, 6], 0.25); // -> 7
371+
```
372+
373+
If the data array is empty or contains a non-numeric value, the method returns `undefined`. If the value of `k` is non-numeric and not in the interval `[0, 1]`, the method returns `undefined`.
374+
357375
### Measures of spread
358376

359377
These methods compute a measure of the variability in a sample or population, how much the sample or population tends to deviate from the typical or average values.
@@ -544,7 +562,25 @@ sum(arr);
544562
Return the sum of all elements of a numeric data array `arr`. The method implement the [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) in order to minimise numerical error.
545563

546564
```js
547-
sum([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7]); // -> 15.3
565+
sum([
566+
0.1,
567+
0.2,
568+
0.3,
569+
0.4,
570+
0.5,
571+
0.6,
572+
0.7,
573+
0.8,
574+
0.9,
575+
1.0,
576+
1.1,
577+
1.2,
578+
1.3,
579+
1.4,
580+
1.5,
581+
1.6,
582+
1.7,
583+
]); // -> 15.3
548584
```
549585

550586
If the data array is empty or contains a non finite `Number`, the method returns `undefined`.
@@ -574,10 +610,10 @@ covariance([5, 12, 18, 23, 45], [2, 8, 18, 20, 28]); // -> 146.1
574610

575611
The `covariance` method will return `undefined` in the following cases:
576612

577-
- At least one of the arguments is not an array.
578-
- At least one of the data arrays contains at least one non finite `Number`.
579-
- At least one of the data arrays contains less than two elements.
580-
- The two data arrays do not have the same number of elements.
613+
- At least one of the arguments is not an array.
614+
- At least one of the data arrays contains at least one non finite `Number`.
615+
- At least one of the data arrays contains less than two elements.
616+
- The two data arrays do not have the same number of elements.
581617

582618
```js
583619
covariance(3, [2, 2]); // -> undefined

0 commit comments

Comments
 (0)