From 97e9ec0f35f96c94c8d2a065c0b16e2f190faeec Mon Sep 17 00:00:00 2001 From: Tom Foster Date: Thu, 3 Jan 2019 16:21:50 -0600 Subject: [PATCH 1/2] adding root mean square --- README.md | 22 ++++++++++++++++++++++ src/central.js | 16 ++++++++++++++++ test/central.test.js | 17 +++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/README.md b/README.md index 7622f26..4b4d2a2 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ npm i stat-methods - [mean](#mean) - [harmonicMean](#harmonicMean) - [geometricMean](#geometricMean) + - [rootMeanSquare](#rootMeanSquare) - [median](#median) - [medianLow](#medianLow) - [medianHigh](#medianHigh) @@ -52,6 +53,7 @@ These methods compute an average or typical value from a population or sample. - [mean](#mean): Arithmetic mean ('average') - [harmonicMean](#harmonicMean): Harmonic mean ('subcontrary mean') - [geometricMean](#geometricMean): Geometric mean +- [rootMeanSquare](#rootMeanSquare): Root mean square - [median](#median): Median (middle value) - [medianLow](#medianLow): Low median - [medianHigh](#medianHigh): High median @@ -135,6 +137,26 @@ geometricMean([1, -2, 3, 4]); // -> undefined If the data array is empty or contains a non finite `Number`, the method returns `undefined`. +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#### rootMeanSquare + +```js +rootMeanSquare(arr); +``` + +Return the root mean square of a numeric data array `arr`. + +The root mean square is the square root of the mean square (the arithmetic mean of the squares of a set of numbers). + +```js +rootMeanSquare([4, 1, 1/32]); // -> 2.380544514916703 +``` + +If the data array is empty or contains a non finite `Number`, the method returns `undefined`. + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + #### median ```js diff --git a/src/central.js b/src/central.js index 199c409..5b140c5 100644 --- a/src/central.js +++ b/src/central.js @@ -58,6 +58,22 @@ export function geometricMean(arr) { return nthRoot(prod, arr.length); } +/** + * Return the root mean square of a numeric data array. + * The root mean square is the square root of the + * mean square (the arithmetic mean of the squares of a set of numbers). + * @param {Number[]} arr the data array + * @returns {Number} the arithmetic mean of the data array + */ +export function rootMeanSquare(arr) { + if (min(arr) === undefined) return undefined; + let arrSquareSum = 0; + for (let i = 0; i < arr.length; i += 1) { + arrSquareSum += arr[i] ** 2; + } + return Math.sqrt((arrSquareSum / arr.length)); +} + /** * Return the median (middle value) of a numeric data array. * The median is the value separating the higher half from the lower half of a data sample. diff --git a/test/central.test.js b/test/central.test.js index 46ac601..f31241a 100644 --- a/test/central.test.js +++ b/test/central.test.js @@ -3,6 +3,7 @@ import { mean, harmonicMean, geometricMean, + rootMeanSquare, median, medianLow, medianHigh, @@ -52,6 +53,22 @@ describe('Averages and measures of central location', () => { testUndefinedWithNullable(geometricMean); }); + test('Root mean square', () => { + expect(rootMeanSquare([4, 1, 1, 3])).toBe(2.598076211353316); + expect(rootMeanSquare([1, 2, 4])).toBe(2.6457513110645907); + expect(rootMeanSquare([4, 1, 1/32])).toBe(2.380544514916703); + expect(rootMeanSquare([1.1, 1.4, 3.5, 9.5])).toBe(5.139795715784821); + expect(rootMeanSquare([2.5, 0, 10])).toBe(5.951190357119041); + expect(rootMeanSquare([-1, 2, 4])).toBe(2.6457513110645907); + expect(rootMeanSquare([-1, 2, 4, 2])).toBe(2.5); + expect(rootMeanSquare(['a', 2.5, 'b', 5.75])).toBeUndefined(); + expect(rootMeanSquare([NaN, 2.5, 3, 5.75])).toBeUndefined(); + expect(rootMeanSquare([])).toBeUndefined(); + expect(rootMeanSquare(3)).toBeUndefined(); + expect(rootMeanSquare([3])).toBe(3); + testUndefinedWithNullable(rootMeanSquare); + }); + test('Median', () => { expect(median([1, 12, 3, 15, 6, 8, 9])).toBe(8); expect(median([1, -2, 3, 4, 8, 6, 5, 9])).toBe(4.5); From 6aa1f5d6e786e1ef395afcdac714a08a12bc74a5 Mon Sep 17 00:00:00 2001 From: Tom Foster Date: Thu, 3 Jan 2019 16:25:32 -0600 Subject: [PATCH 2/2] removing unneeded characters --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 4b4d2a2..a29286f 100644 --- a/README.md +++ b/README.md @@ -137,8 +137,6 @@ geometricMean([1, -2, 3, 4]); // -> undefined If the data array is empty or contains a non finite `Number`, the method returns `undefined`. -/////////////////////////////////////////////////////////////////////////////////////////////////////////// - #### rootMeanSquare ```js @@ -155,8 +153,6 @@ rootMeanSquare([4, 1, 1/32]); // -> 2.380544514916703 If the data array is empty or contains a non finite `Number`, the method returns `undefined`. -/////////////////////////////////////////////////////////////////////////////////////////////////////////// - #### median ```js