Skip to content

Commit 6522364

Browse files
authored
M-estimator & made algorithm changed & Q-Hampel Method Test Added (#2)
* fix: m-estimator & made algorithm changed. * feat: add q-hampel tests * review feedback apply
1 parent cbd200d commit 6522364

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

src/algorithms/m-estimator.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Result } from '../types';
2+
import { median } from 'simple-statistics';
23

34
export function M_Estimator(values: number[]): Result {
4-
let sumSeries = 0;
5+
let sumSeries = 0;
6+
7+
for (var i = values.length - 1; i >= 0; i--) {
8+
sumSeries += Math.abs(values[i] - median(values));
9+
}
510

6-
for (var i = values.length - 1; i >= 0; i--) {
7-
sumSeries += Math.abs(values[i] - values.length);
8-
}
11+
const mestimatorValue = (1 / ( 0.798 * values.length ) ) * sumSeries;
12+
const u = (1.25 * mestimatorValue) / Math.sqrt(values.length);
913

10-
const sd = (1 / (0.798 * values.length)) * sumSeries;
11-
const u = (1.25 * sd) / Math.sqrt(values.length);
12-
13-
return { value: sd, uncertainty: u };
14+
return { value: mestimatorValue, uncertainty: u };
1415
}

src/algorithms/made.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Result } from '../types';
2+
import { median } from 'simple-statistics';
23

34
export function MADe(values: number[]): Result {
4-
// TODO: Implement
5-
6-
const sd = 1;
7-
const u = (1.25 * sd) / Math.sqrt(values.length);
8-
9-
return { value: sd, uncertainty: u };
5+
const medianValue = median(values);
6+
const medianArr: number[] = [];
7+
values.forEach((value) => {
8+
value = Math.abs(value - medianValue);
9+
medianArr.push(value);
10+
});
11+
var result = 1.483 * median(medianArr);
12+
var u = (1.25 * result) / Math.sqrt(values.length);
13+
return { value: result, uncertainty: u };
1014
}

src/algorithms/q-hampel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { jStat } = require('jstat');
22
import { median } from 'simple-statistics';
33
import { get, sortBy, sumBy, round, sortedUniq, findIndex } from 'lodash';
44

5-
import { Result } from '../types';
5+
import { QHampelResult } from '../types';
66

77
interface Q_Options {
88
precision: number;
@@ -19,7 +19,7 @@ interface Q_Calculation {
1919
export function QHampel(
2020
results: number[],
2121
options: Q_Options = { precision: 8 }
22-
): Result {
22+
): QHampelResult {
2323
const { precision } = options;
2424

2525
const values: number[] = ([] as number[]).concat(results).sort();
@@ -76,7 +76,7 @@ export function QHampel(
7676

7777
return {
7878
value,
79-
uncertainty: hampel(results, value),
79+
hampel: hampel(results, value),
8080
};
8181
}
8282

src/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export type Result = {
22
value: number;
3-
4-
uncertainty?: number;
3+
uncertainty?: number;
4+
};
5+
export type QHampelResult = {
6+
value: number;
7+
hampel: number;
58
};

tests/algorithms.test.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1-
import { M_Estimator, QHampel } from '../src/lib';
1+
import { MADe, M_Estimator, QHampel } from '../src/lib';
22

33
describe('Algorithms', () => {
4-
it.skip('MADe Method', () => {
5-
// TODO: Implement
4+
it('MADe Method', () => {
5+
const samples = [13.9, 14.12, 13.65];
6+
const output = MADe(samples);
7+
expect(output.value).toBeCloseTo(0.32626, 3);
68
});
79

810
it('M-Estimator Method', () => {
9-
const results = [
11+
const samples = [
1012
13.9, 14.12, 13.65, 14.26, 13.9, 14.25, 0, 13.96, 13.6, 13.7, 13.76, 0,
1113
];
14+
const output = M_Estimator(samples);
1215

13-
const output = M_Estimator(results);
14-
15-
expect(output.value).toBe(3.099415);
16-
expect(output.uncertainty).toBe(1.118405);
16+
expect(output.value).toBeCloseTo(3.099415, 3);
17+
expect(output.uncertainty).toBeCloseTo(1.118405, 3);
1718
});
1819

1920
it('Q-Hampel Method', () => {
20-
const results = [
21-
13.9, 14.12, 13.65, 14.26, 13.9, 14.25, 0, 13.96, 13.6, 13.7, 13.76, 0,
21+
const samples = [
22+
41.41, 39.22, 47.29, 82.46, 45.24, 49.96, 38.2, 45.41, 39.82, 48.17,
23+
39.67, 47.55, 35.75, 46.13, 52.18, 45.15, 41.57, 43.39, 49.38, 45.67,
24+
41.08, 49.28, 49.48, 48.37, 33.96, 49.4, 24.4, 41.55, 37.43, 40.63, 49.92,
25+
47.88, 43.73, 38.1, 38.1, 46.82, 90.11, 45.74, 53.4, 42.65, 47.92, 42.02,
26+
49.47, 43.89, 50.05, 37.41, 53.64, 56.3, 47.13, 39.08, 44.73, 47, 50.53,
27+
44.22, 47.83, 46.04, 47, 36.3, 46.44, 24.79, 46.26, 39.88, 38.64, 50.19,
28+
48.61, 44.18,
2229
];
2330

24-
const output = QHampel(results);
31+
const output = QHampel(samples);
2532

26-
// TODO: Expected value is incorrect here. Look for a real life example.
27-
expect(output.value).toBe(0.4136);
33+
expect(output.value).toBeCloseTo(5.5457, 3);
34+
expect(output.hampel).toBeCloseTo(44.722, 3);
35+
2836
});
2937
});

0 commit comments

Comments
 (0)