Skip to content

Commit 056ee49

Browse files
authored
A algorithm (#5)
* feat: a algorithm creeate result type * feat: robust value added * feat: add xiValues and robustDeviation calculation * feat: add limit, high limit & low limit * feat: create test outline * feat: completed A algorithm ✅ (with fix test issues) * style: format code style [BONUS ➕] * style: add description variable
1 parent 106351d commit 056ee49

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/algorithms/a-algorithm.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { median } from 'simple-statistics';
2+
import { AResult } from '../types';
3+
4+
/**
5+
* @param {number[]} values - an array of numbers
6+
* @returns An object with the following properties:
7+
* - robust = x*
8+
* - robustDeviation = s*
9+
* - highLimit
10+
* - lowLimit
11+
*/
12+
export function A_Algorithm(values: number[]): AResult {
13+
const robust = median(values); // robust value = median of the values
14+
const xiValues = values.map((x) => Math.abs(x - robust)); // subtract the robust value from each value
15+
const robustDeviation = median(xiValues) * 1.483;
16+
const limit = robustDeviation * 1.5;
17+
const highLimit = robust + limit;
18+
const lowLimit = robust - limit;
19+
20+
return {
21+
robust,
22+
robustDeviation,
23+
highLimit,
24+
lowLimit,
25+
};
26+
}

src/lib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ export * from './algorithms/made';
44

55
export * from './algorithms/q-hampel';
66

7+
export * from './algorithms/a-algorithm';
8+
79
export * from './grubbs';

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ export type Result = {
22
value: number;
33
uncertainty?: number;
44
};
5+
export type AResult = {
6+
robust: number;
7+
robustDeviation: number;
8+
highLimit: number;
9+
lowLimit: number;
10+
};

tests/algorithms.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
import { MADe, M_Estimator, Q, Hampel } from '../src/lib';
1+
import { MADe, M_Estimator, Q, Hampel, A_Algorithm } from '../src/lib';
22

33
describe('Algorithms', () => {
4+
it('A algorithm', () => {
5+
const samples = [
6+
0.04, 0.055, 0.178, 0.202, 0.206, 0.227, 0.228, 0.23, 0.23, 0.235, 0.236,
7+
0.237, 0.243, 0.244, 0.245, 0.2555, 0.26, 0.264, 0.267, 0.27, 0.273,
8+
0.274, 0.274, 0.278, 0.2811, 0.287, 0.287, 0.288, 0.289, 0.295, 0.296,
9+
0.311, 0.331, 0.4246,
10+
];
11+
const output = A_Algorithm(samples);
12+
13+
expect(output.robust).toBeCloseTo(0.262, 3);
14+
expect(output.robustDeviation).toBeCloseTo(0.0386, 3);
15+
expect(output.lowLimit).toBeCloseTo(0.2042, 3);
16+
expect(output.highLimit).toBeCloseTo(0.3198, 3);
17+
});
18+
419
it('MADe Method', () => {
520
const samples = [13.9, 14.12, 13.65];
621
const output = MADe(samples);

0 commit comments

Comments
 (0)