From ad77f209c9dbab794f36b2ea62d82a7dace6fdf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=96zisik?= Date: Fri, 14 Mar 2025 13:28:43 +0200 Subject: [PATCH 1/3] add failing cochran test --- tests/cochran.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/cochran.test.ts b/tests/cochran.test.ts index 71471e0..07ed645 100644 --- a/tests/cochran.test.ts +++ b/tests/cochran.test.ts @@ -76,4 +76,19 @@ describe('Cochran Algorithm', () => { expect(output?.outlierIndexes).toContain(0); expect(output?.outlierIndexes).toContain(5); }) + + it('Cochran Algorithm Test (New Values)', () => { + const samples = [ + [1, 1], + [1, 1], + [1, 1], + [1, 1], + [1, 1], + [1, 1], + [1, 1], + ]; + + const output = Cochran(samples); + expect(output?.hasOutliers).toBe(false); // Adjust the expectation based on your algorithm's behavior + }); }); From ff59fee22e5e86fe751e2192426ce946d7a878b1 Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Fri, 28 Mar 2025 13:16:35 +0200 Subject: [PATCH 2/3] if all values are same, cochran should return no outliers --- package-lock.json | 4 ++-- src/algorithms/cochran.ts | 18 +++++++++++++++--- tests/algorithms.test.ts | 2 +- tests/cochran.test.ts | 20 +++++++++++++++++--- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5bfe87a..b2e3d7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "labkar-algorithms", - "version": "4.0.0", + "version": "5.3.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "labkar-algorithms", - "version": "4.0.0", + "version": "5.3.4", "license": "BSD-3-Clause", "dependencies": { "jstat": "^1.9.5", diff --git a/src/algorithms/cochran.ts b/src/algorithms/cochran.ts index f0d90bf..8e0697b 100644 --- a/src/algorithms/cochran.ts +++ b/src/algorithms/cochran.ts @@ -7,12 +7,24 @@ type CochranOptions = { alpha: number; originalIndexes?: number[]; outlierIndexes?: number[]; -} +}; export function Cochran( values: Array, options: CochranOptions = { alpha: 0.05 } ): CochranResult | null { + // Early return if all values are identical + const allIdentical = values.every( + (sample) => + sample.every((value) => value === sample[0]) && sample[0] === values[0][0] + ); + + if (allIdentical) { + return { + outlierIndexes: [], + hasOutliers: false, + }; + } // Keep track of original indexes of values at the beginning // When we return indexes of outliers, this will be useful. @@ -67,7 +79,7 @@ export function Cochran( { alpha: options.alpha, originalIndexes, - outlierIndexes + outlierIndexes, } - ) + ); } diff --git a/tests/algorithms.test.ts b/tests/algorithms.test.ts index 556efc4..f8a201b 100644 --- a/tests/algorithms.test.ts +++ b/tests/algorithms.test.ts @@ -63,7 +63,7 @@ describe('Algorithms', () => { // expect(output.hampel).toBeCloseTo(44.722, 3); }); - it.skip('Q/Hampel Method (samples with no variance)', () => { + 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); diff --git a/tests/cochran.test.ts b/tests/cochran.test.ts index 07ed645..acfd446 100644 --- a/tests/cochran.test.ts +++ b/tests/cochran.test.ts @@ -58,7 +58,6 @@ describe('Cochran Algorithm', () => { }); it('Repeats test until there are no outliers', () => { - const samples = [ [0.135, 0.194], // index=0 = outlier [0.187, 0.189], @@ -70,12 +69,27 @@ describe('Cochran Algorithm', () => { [0.177, 0.186], [0.179, 0.187], [0.188, 0.196], - ] + ]; const output = Cochran(samples, { alpha: 0.01 }); expect(output?.outlierIndexes).toContain(0); expect(output?.outlierIndexes).toContain(5); - }) + }); + + it('Cochran Algorithm Test (New Values)', () => { + const samples = [ + [1, 1], + [1, 1], + [1, 1], + [1, 1], + [1, 1], + [1, 1], + [1, 1], + ]; + + const output = Cochran(samples); + expect(output?.hasOutliers).toBe(false); // Adjust the expectation based on your algorithm's behavior + }); it('Cochran Algorithm Test (New Values)', () => { const samples = [ From ce7357c3647618f741f67a482ba727f1c433a824 Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Fri, 28 Mar 2025 13:18:26 +0200 Subject: [PATCH 3/3] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c05f510..e842ab8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "labkar-algorithms", - "version": "5.3.4", + "version": "5.3.5", "description": "Labkar Algorithms", "main": "dist/lib.js", "author": "ODTÜ PAL",