Skip to content

Commit 6de442b

Browse files
authored
Fix failing cochran (#12)
* add failing cochran test * if all values are same, cochran should return no outliers * bump version
1 parent 95baf82 commit 6de442b

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

src/algorithms/cochran.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ type CochranOptions = {
77
alpha: number;
88
originalIndexes?: number[];
99
outlierIndexes?: number[];
10-
}
10+
};
1111

1212
export function Cochran(
1313
values: Array<number[]>,
1414
options: CochranOptions = { alpha: 0.05 }
1515
): CochranResult | null {
16+
// Early return if all values are identical
17+
const allIdentical = values.every(
18+
(sample) =>
19+
sample.every((value) => value === sample[0]) && sample[0] === values[0][0]
20+
);
21+
22+
if (allIdentical) {
23+
return {
24+
outlierIndexes: [],
25+
hasOutliers: false,
26+
};
27+
}
1628

1729
// Keep track of original indexes of values at the beginning
1830
// When we return indexes of outliers, this will be useful.
@@ -67,7 +79,7 @@ export function Cochran(
6779
{
6880
alpha: options.alpha,
6981
originalIndexes,
70-
outlierIndexes
82+
outlierIndexes,
7183
}
72-
)
84+
);
7385
}

tests/algorithms.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Algorithms', () => {
6363
// expect(output.hampel).toBeCloseTo(44.722, 3);
6464
});
6565

66-
it.skip('Q/Hampel Method (samples with no variance)', () => {
66+
it('Q/Hampel Method (samples with no variance)', () => {
6767
const samples = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
6868

6969
const q = Q(samples);

tests/cochran.test.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ describe('Cochran Algorithm', () => {
5858
});
5959

6060
it('Repeats test until there are no outliers', () => {
61-
6261
const samples = [
6362
[0.135, 0.194], // index=0 = outlier
6463
[0.187, 0.189],
@@ -70,10 +69,40 @@ describe('Cochran Algorithm', () => {
7069
[0.177, 0.186],
7170
[0.179, 0.187],
7271
[0.188, 0.196],
73-
]
72+
];
7473

7574
const output = Cochran(samples, { alpha: 0.01 });
7675
expect(output?.outlierIndexes).toContain(0);
7776
expect(output?.outlierIndexes).toContain(5);
78-
})
77+
});
78+
79+
it('Cochran Algorithm Test (New Values)', () => {
80+
const samples = [
81+
[1, 1],
82+
[1, 1],
83+
[1, 1],
84+
[1, 1],
85+
[1, 1],
86+
[1, 1],
87+
[1, 1],
88+
];
89+
90+
const output = Cochran(samples);
91+
expect(output?.hasOutliers).toBe(false); // Adjust the expectation based on your algorithm's behavior
92+
});
93+
94+
it('Cochran Algorithm Test (New Values)', () => {
95+
const samples = [
96+
[1, 1],
97+
[1, 1],
98+
[1, 1],
99+
[1, 1],
100+
[1, 1],
101+
[1, 1],
102+
[1, 1],
103+
];
104+
105+
const output = Cochran(samples);
106+
expect(output?.hasOutliers).toBe(false); // Adjust the expectation based on your algorithm's behavior
107+
});
79108
});

0 commit comments

Comments
 (0)