Skip to content

Commit ff59fee

Browse files
committed
if all values are same, cochran should return no outliers
1 parent ad77f20 commit ff59fee

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/algorithms/cochran.ts

Lines changed: 15 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 17 additions & 3 deletions
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,12 +69,27 @@ 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+
});
7993

8094
it('Cochran Algorithm Test (New Values)', () => {
8195
const samples = [

0 commit comments

Comments
 (0)