Skip to content

Commit 1cc22c3

Browse files
committed
fix grubbs test: use sampleStandardDeviation
1 parent 55e6c11 commit 1cc22c3

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "labkar-algorithms",
3-
"version": "5.3.1",
3+
"version": "5.3.3",
44
"description": "Labkar Algorithms",
55
"main": "dist/lib.js",
66
"author": "ODTÜ PAL",

src/algorithms/q-hampel.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { jStat } = require('jstat');
22
import { median } from 'simple-statistics';
3-
import { get, sortBy, sumBy, round, sortedUniq, findIndex } from 'lodash';
3+
import { get, sortBy, sumBy, round, sortedUniq, findIndex, uniq } from 'lodash';
44

55
import { Result } from '../types';
66

@@ -22,6 +22,15 @@ export function Q(
2222
): Result {
2323
const { precision } = options;
2424

25+
if (!results.length) {
26+
throw new Error('At least one test result is required to calculate Q');
27+
}
28+
29+
// If all values are the same, return the first value
30+
if (uniq(results).length === 1) {
31+
return { value: results[0] };
32+
}
33+
2534
const values: number[] = ([] as number[]).concat(results).sort();
2635

2736
let deltas = [];
@@ -36,6 +45,8 @@ export function Q(
3645
const sortedUniqueDeltas = sortedUniq(sortedDeltas);
3746
const hMultiplier = 2 / (results.length * (results.length - 1));
3847

48+
console.log(JSON.stringify({ sortedDeltas, sortedUniqueDeltas }, null, 2));
49+
3950
const calculations: Q_Calculation[] = [];
4051

4152
for (let i = 0; i < sortedUniqueDeltas.length; i++) {
@@ -53,6 +64,8 @@ export function Q(
5364
});
5465
}
5566

67+
console.log(calculations);
68+
5669
// START OF Q Calc.
5770
const firstParameter = calculations[0].h1 * 0.75 + 0.25;
5871
const secondParameter = calculations[0].h1 * 0.375 + 0.625;

src/grubbs/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { average, standardDeviation } from 'simple-statistics';
1+
import { average, sampleStandardDeviation } from 'simple-statistics';
22
import criticalValueTable from './criticalValueTable';
33

44
export type GrubbsResult = {
@@ -17,7 +17,7 @@ export function grubbs(
1717
originOptions: { alpha: number }
1818
): GrubbsResult[] {
1919
if (typeof originDataSet === 'undefined') {
20-
throw new Error('dataSet MUST be passed');
20+
throw new Error('dataSet MUST be passed');
2121
} else if (originDataSet.filter(isValidData).length > 500) {
2222
throw new Error('dataSet.length MUST less than 500');
2323
} else if (originDataSet.filter(isValidData).length <= 2) {
@@ -58,7 +58,7 @@ export function grubbs(
5858
done = true;
5959
currentRound = {};
6060
currentRound.dataSet = dataSet.slice();
61-
currentRound.stdev = standardDeviation(
61+
currentRound.stdev = sampleStandardDeviation(
6262
currentRound.dataSet.filter(isValidData)
6363
);
6464
currentRound.average =

tests/algorithms.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ describe('Algorithms', () => {
6363
// expect(output.hampel).toBeCloseTo(44.722, 3);
6464
});
6565

66+
it('Q/Hampel Method (samples with no variance)', () => {
67+
const samples = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
68+
69+
const q = Q(samples);
70+
const hampel = Hampel(samples, q.value);
71+
72+
expect(q.value).toBe(1);
73+
expect(hampel.value).toBe(1);
74+
});
75+
76+
it.only('Q/Hampel Method (samples with low variance)', () => {
77+
const samples = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1, 1, 1, 1, 1, 1];
78+
79+
const q = Q(samples);
80+
const hampel = Hampel(samples, q.value);
81+
82+
expect(q.value).toBeCloseTo(1.0040785270846, 4);
83+
expect(hampel.value).toBeCloseTo(0.040785270845889, 4);
84+
});
85+
6686
it('Hampel Method', () => {
6787
const samples = [
6888
41.41, 39.22, 47.29, 82.46, 45.24, 49.96, 38.2, 45.41, 39.82, 48.17,

tests/grubbs.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ describe('Grubbs test', () => {
88
});
99

1010
expect(result[0].outlierIndexes).toEqual([4]);
11-
expect(result[0].stdev).toEqual(29.7);
11+
expect(result[0].stdev).toBeCloseTo(31.31, 2);
12+
});
13+
14+
it('Only -60 is outlier in this group', () => {
15+
const results = grubbs([-60, -55.6, -54.7, -54.6, -54.5, -54.4, -54.1], {
16+
alpha: 0.01,
17+
});
18+
19+
// console.log(results);
20+
21+
// Find all outlier indexes
22+
const outlierIndexes = results
23+
.map((result) => result.outlierIndexes)
24+
.flat();
25+
26+
expect(outlierIndexes).toEqual([0]);
1227
});
1328

1429
it.skip('calculates for %1', () => {

0 commit comments

Comments
 (0)