From fb91879d82e4b746bdbddf312bd13dab76ad5499 Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Sat, 7 Nov 2026 18:20:31 +0530 Subject: [PATCH] feat: add mean absolute deviation algorithm --- Maths/MeanAbsoluteDeviation.js | 30 ++++++++-------- Maths/test/MeanAbsoluteDeviation.test.js | 45 +++++++++++++++++++----- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/Maths/MeanAbsoluteDeviation.js b/Maths/MeanAbsoluteDeviation.js index 55585592b6..1c3e816a22 100644 --- a/Maths/MeanAbsoluteDeviation.js +++ b/Maths/MeanAbsoluteDeviation.js @@ -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 } diff --git a/Maths/test/MeanAbsoluteDeviation.test.js b/Maths/test/MeanAbsoluteDeviation.test.js index d44eb7d06e..3707be6ed0 100644 --- a/Maths/test/MeanAbsoluteDeviation.test.js +++ b/Maths/test/MeanAbsoluteDeviation.test.js @@ -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) }) })