Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bda-research/node-ds
Browse files Browse the repository at this point in the history
  • Loading branch information
mike442144 committed Dec 26, 2017
2 parents 8570ac6 + 16af25b commit 6a02652
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Grubbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = class Grubbs {
constructor(data, alpha) {
if (!(data instanceof Array)) {
throw new Error('data must be an array');
} else if (data.length < 3) {
throw new Error('data length must be at least 3');
}
this.data = data;
this.alpha = alpha || 0.05;
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ A common data-structure and basic algorithm implemention in javascript
* [Merge-Sort](#sorting-algorithms)
* [Quick-Sort](#sorting-algorithms)
* [Heap-Sort](#sorting-algorithms)
* [Statistics Algorithms](#statistics-algorithms)
* [Grubbs](#grubbs)
* [Test](#test)
* [Contributing to node-ds](#contributing-to-node-ds)

Expand Down Expand Up @@ -242,6 +244,19 @@ Random.shuffle(a); // say [6, 9, 3, <b>1, 4, 2, 8</b>, 5, 7]
SortFamily.insertionSort(a, compare, 3, 6); // outputs [6, 9, 3, <b>8, 4, 2, 1</b>, 5, 7]
</code></pre>

### Statistics Algorithms

Find out outliers from the given data array based on some basic mathematical calculation(average、stdev).

#### Grubbs
```javascript
const Grubbs = require('node-ds/Grubbs.js');

let data = [7, 9, 2, 6, 3, 5, 7, 2, 4, 20];
let grubbs = new Grubbs(data);
grubbs.getOutliers();//outputs [9], means the number 20 is outlier
```

## Test
Like most other packages, just run test suite and check code coverage by following commands:

Expand Down
38 changes: 35 additions & 3 deletions tests/Grubbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,45 @@ const Grubbs = require('../Grubbs');
let data = null;

describe("Grubbs", function () {
beforeEach(() => {
data = [7, 9, 2, 6, 3, 5, 7, 2, 4, 20];
it('data must be an array', function () {
try {
new Grubbs(data);
} catch (e) {
e.should.be.instanceof(Error);
}
});

it('data length must be at least 3', function () {
data = [1, 100];
try {
new Grubbs(data);
} catch (e) {
e.should.be.instanceof(Error);
}
});

it('unable to tell outliers when data is distributed in two extremes', function () {
data = [1, 1, 100, 100];
let grubbs1 = new Grubbs(data);
let outliers1 = grubbs1.getOutliers();

data = [1, 1, 1, 100, 100];
let grubbs2 = new Grubbs(data);
let outliers2 = grubbs2.getOutliers();

data = [1, 1, 100, 100, 100];
let grubbs3 = new Grubbs(data);
let outliers3 = grubbs3.getOutliers();

should.equal(outliers1.length, 0);
should.equal(outliers2.length, 0);
should.equal(outliers3.length, 0);
});

it('20 should be outlier', function () {
data = [7, 9, 2, 6, 3, 5, 7, 2, 4, 20];
let grubbs = new Grubbs(data);
let outliers = grubbs.getOutliers();
console.log(outliers)
outliers.should.be.instanceof(Array);
should.equal(outliers.length, 1);
should.equal(outliers[0], 9);
Expand Down

0 comments on commit 6a02652

Please sign in to comment.