Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Maths/MeanAbsoluteDeviation.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { mean } from './AverageMean.js'
/**
*@function meanAbsoluteDeviation
*@description Calculates the mean absolute deviation of list of numbers
* @param {Integer} data
* @returns meanAbsoluteDeviation([2,34,5,0,-2]) = 10.480
* @url https://en.wikipedia.org/wiki/Average_absolute_deviation
* @function meanAbsoluteDeviation
* @description Calculates the mean absolute deviation (MAD) of an array
* of numbers. MAD is the average of the absolute differences between
* each value and the mean of the dataset.
* @param {number[]} arr - An array of numbers
* @returns {number} - The mean absolute deviation
* @see https://en.wikipedia.org/wiki/Average_absolute_deviation
* @example meanAbsoluteDeviation([2, 2, 3, 4, 14]) => 3.6
* @example meanAbsoluteDeviation([1, 1, 1]) => 0
*/
function meanAbsoluteDeviation(data) {
if (!Array.isArray(data)) {
throw new TypeError('Invalid Input')
const meanAbsoluteDeviation = (arr) => {
if (!Array.isArray(arr) || arr.length === 0) {
throw new TypeError('Input must be a non-empty array')
}
let absoluteSum = 0
const meanValue = mean(data)
for (const dataPoint of data) {
absoluteSum += Math.abs(dataPoint - meanValue)
if (!arr.every((n) => typeof n === 'number' && !isNaN(n))) {
throw new TypeError('All elements must be numbers')
}
return absoluteSum / data.length
const mean = arr.reduce((sum, n) => sum + n, 0) / arr.length
return arr.reduce((sum, n) => sum + Math.abs(n - mean), 0) / arr.length
}

export { meanAbsoluteDeviation }
45 changes: 37 additions & 8 deletions Maths/test/MeanAbsoluteDeviation.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
import { meanAbsoluteDeviation } from '../MeanAbsoluteDeviation.js'

describe('tests for mean absolute deviation', () => {
it('should be a function', () => {
expect(typeof meanAbsoluteDeviation).toEqual('function')
describe('meanAbsoluteDeviation', () => {
test('meanAbsoluteDeviation([2,2,3,4,14]) returns 3.6', () => {
expect(meanAbsoluteDeviation([2, 2, 3, 4, 14])).toBeCloseTo(3.6)
})

it('should throw an invalid input error', () => {
expect(() => meanAbsoluteDeviation('fgh')).toThrow()
test('meanAbsoluteDeviation([1,1,1]) returns 0', () => {
expect(meanAbsoluteDeviation([1, 1, 1])).toBe(0)
})

it('should return the mean absolute deviation of an array of numbers', () => {
const meanAbDev = meanAbsoluteDeviation([2, 34, 5, 0, -2])
expect(meanAbDev).toBe(10.479999999999999)
test('meanAbsoluteDeviation single element returns 0', () => {
expect(meanAbsoluteDeviation([5])).toBe(0)
})

test('meanAbsoluteDeviation([1,2,3,4,5]) returns 1.2', () => {
expect(meanAbsoluteDeviation([1, 2, 3, 4, 5])).toBeCloseTo(1.2)
})

test('meanAbsoluteDeviation works with negative numbers', () => {
expect(meanAbsoluteDeviation([-2, -1, 0, 1, 2])).toBeCloseTo(1.2)
})

test('meanAbsoluteDeviation([10, 20]) returns 5', () => {
expect(meanAbsoluteDeviation([10, 20])).toBeCloseTo(5)
})

test('meanAbsoluteDeviation does not mutate original array', () => {
const arr = [1, 2, 3, 4, 5]
meanAbsoluteDeviation(arr)
expect(arr).toEqual([1, 2, 3, 4, 5])
})

test('throws TypeError for empty array', () => {
expect(() => meanAbsoluteDeviation([])).toThrow(TypeError)
})

test('throws TypeError for non-array input', () => {
expect(() => meanAbsoluteDeviation('123')).toThrow(TypeError)
})

test('throws TypeError for array with non-numbers', () => {
expect(() => meanAbsoluteDeviation([1, 'two', 3])).toThrow(TypeError)
})
})
Loading