mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
Add an algorithm to find mean absolute deviation (#1165)
This commit is contained in:
committed by
GitHub
parent
514c7c4c9f
commit
564ee8527a
23
Maths/MeanAbsoluteDeviation.js
Normal file
23
Maths/MeanAbsoluteDeviation.js
Normal file
@@ -0,0 +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 (data) {
|
||||
if (!Array.isArray(data)) {
|
||||
throw new TypeError('Invalid Input')
|
||||
}
|
||||
let absoluteSum = 0
|
||||
const meanValue = mean(data)
|
||||
for (const dataPoint of data) {
|
||||
absoluteSum += Math.abs(dataPoint - meanValue)
|
||||
}
|
||||
return absoluteSum / data.length
|
||||
}
|
||||
|
||||
export {
|
||||
meanAbsoluteDeviation
|
||||
}
|
||||
Reference in New Issue
Block a user