Skip to content

Commit

Permalink
fix grubbs test: use sampleStandardDeviation
Browse files Browse the repository at this point in the history
  • Loading branch information
aozisik committed Dec 27, 2024
1 parent 55e6c11 commit 1cc22c3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "labkar-algorithms",
"version": "5.3.1",
"version": "5.3.3",
"description": "Labkar Algorithms",
"main": "dist/lib.js",
"author": "ODTÜ PAL",
Expand Down
15 changes: 14 additions & 1 deletion src/algorithms/q-hampel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { jStat } = require('jstat');
import { median } from 'simple-statistics';
import { get, sortBy, sumBy, round, sortedUniq, findIndex } from 'lodash';
import { get, sortBy, sumBy, round, sortedUniq, findIndex, uniq } from 'lodash';

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

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

if (!results.length) {
throw new Error('At least one test result is required to calculate Q');
}

// If all values are the same, return the first value
if (uniq(results).length === 1) {
return { value: results[0] };
}

const values: number[] = ([] as number[]).concat(results).sort();

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

console.log(JSON.stringify({ sortedDeltas, sortedUniqueDeltas }, null, 2));

const calculations: Q_Calculation[] = [];

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

console.log(calculations);

// START OF Q Calc.
const firstParameter = calculations[0].h1 * 0.75 + 0.25;
const secondParameter = calculations[0].h1 * 0.375 + 0.625;
Expand Down
6 changes: 3 additions & 3 deletions src/grubbs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { average, standardDeviation } from 'simple-statistics';
import { average, sampleStandardDeviation } from 'simple-statistics';
import criticalValueTable from './criticalValueTable';

export type GrubbsResult = {
Expand All @@ -17,7 +17,7 @@ export function grubbs(
originOptions: { alpha: number }
): GrubbsResult[] {
if (typeof originDataSet === 'undefined') {
throw new Error('dataSet MUST be passed');
throw new Error('dataSet MUST be passed');
} else if (originDataSet.filter(isValidData).length > 500) {
throw new Error('dataSet.length MUST less than 500');
} else if (originDataSet.filter(isValidData).length <= 2) {
Expand Down Expand Up @@ -58,7 +58,7 @@ export function grubbs(
done = true;
currentRound = {};
currentRound.dataSet = dataSet.slice();
currentRound.stdev = standardDeviation(
currentRound.stdev = sampleStandardDeviation(
currentRound.dataSet.filter(isValidData)
);
currentRound.average =
Expand Down
20 changes: 20 additions & 0 deletions tests/algorithms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ describe('Algorithms', () => {
// expect(output.hampel).toBeCloseTo(44.722, 3);
});

it('Q/Hampel Method (samples with no variance)', () => {
const samples = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

const q = Q(samples);
const hampel = Hampel(samples, q.value);

expect(q.value).toBe(1);
expect(hampel.value).toBe(1);
});

it.only('Q/Hampel Method (samples with low variance)', () => {
const samples = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1, 1, 1, 1, 1, 1];

const q = Q(samples);
const hampel = Hampel(samples, q.value);

expect(q.value).toBeCloseTo(1.0040785270846, 4);
expect(hampel.value).toBeCloseTo(0.040785270845889, 4);
});

it('Hampel Method', () => {
const samples = [
41.41, 39.22, 47.29, 82.46, 45.24, 49.96, 38.2, 45.41, 39.82, 48.17,
Expand Down
17 changes: 16 additions & 1 deletion tests/grubbs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ describe('Grubbs test', () => {
});

expect(result[0].outlierIndexes).toEqual([4]);
expect(result[0].stdev).toEqual(29.7);
expect(result[0].stdev).toBeCloseTo(31.31, 2);
});

it('Only -60 is outlier in this group', () => {
const results = grubbs([-60, -55.6, -54.7, -54.6, -54.5, -54.4, -54.1], {
alpha: 0.01,
});

// console.log(results);

// Find all outlier indexes
const outlierIndexes = results
.map((result) => result.outlierIndexes)
.flat();

expect(outlierIndexes).toEqual([0]);
});

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

0 comments on commit 1cc22c3

Please sign in to comment.