Skip to content

Commit e9c4a6a

Browse files
authoredJan 29, 2025··
fix: median absolute deviation calculation (#236)
Signed-off-by: Péter Pallos <pallosp@gmail.com> Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent c9816af commit e9c4a6a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎src/utils.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ const quantileSorted = (samples: number[], q: number) => {
265265
return samples[baseIndex]
266266
}
267267

268+
/**
269+
* Computes the median of a sample.
270+
* @param samples - the sample
271+
* @returns the median of the sample
272+
*/
273+
const median = (samples: number[]) => {
274+
return medianSorted(samples.sort((a, b) => a - b))
275+
}
276+
268277
/**
269278
* Computes the median of a sorted sample.
270279
* @param samples - the sorted sample
@@ -316,7 +325,7 @@ export const getStatisticsSorted = (samples: number[]): Statistics => {
316325
aad: absoluteDeviation(samples, average, mean),
317326
critical,
318327
df,
319-
mad: absoluteDeviation(samples, medianSorted, p50),
328+
mad: absoluteDeviation(samples, median, p50),
320329
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
321330
max: samples[df]!,
322331
mean,

‎test/utils.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { getStatisticsSorted } from '../src/utils'
4+
5+
test('median absolute deviation', () => {
6+
const stats = getStatisticsSorted([1, 2, 3])
7+
expect(stats.mad).toBe(1)
8+
})

0 commit comments

Comments
 (0)
Please sign in to comment.