Skip to content

Add an algorithm to find mean absolute deviation #1165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2022
Merged
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
23 changes: 23 additions & 0 deletions Maths/MeanAbsoluteDeviation.js
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions Maths/test/MeanAbsoluteDeviation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { meanAbsoluteDeviation } from '../MeanAbsoluteDeviation.js'

describe('tests for mean absolute deviation', () => {
it('should be a function', () => {
expect(typeof meanAbsoluteDeviation).toEqual('function')
})

it('should throw an invalid input error', () => {
expect(() => meanAbsoluteDeviation('fgh')).toThrow()
})

it('should return the mean absolute devition of an array of numbers', () => {
const meanAbDev = meanAbsoluteDeviation([2, 34, 5, 0, -2])
expect(meanAbDev).toBe(10.479999999999999)
})
})
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy