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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ npm i stat-methods
- [mean](#mean)
- [harmonicMean](#harmonicMean)
- [geometricMean](#geometricMean)
- [rootMeanSquare](#rootMeanSquare)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rename the method to rms as set in #5 ?

- [median](#median)
- [medianLow](#medianLow)
- [medianHigh](#medianHigh)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -135,6 +137,22 @@ 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to rms throughout all files.

```

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
Expand Down
16 changes: 16 additions & 0 deletions src/central.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you are checking here but it is a bit counter-intuitive. It is not necessary to compute the min of the array when calculating its rms. Could you please use this check instead ?

if (!Array.isArray(arr) || arr.length === 0) return undefined;

let arrSquareSum = 0;
for (let i = 0; i < arr.length; i += 1) {
arrSquareSum += arr[i] ** 2;
}
return Math.sqrt((arrSquareSum / arr.length));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this approach solves the problem, it does not make use of previously defined methods in the library. We could use the literal definition of the rms:

  • Compute the mean of the squared values of the elements of the array;
  • Compute its square root if it exists.

This would give something like this:

function rms(arr) {
  const xBar = mean(arr.map(elt => elt ** 2));
  return xBar === undefined ? undefined : Math.sqrt(xBar);
}

}

/**
* 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.
Expand Down
17 changes: 17 additions & 0 deletions test/central.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
mean,
harmonicMean,
geometricMean,
rootMeanSquare,
median,
medianLow,
medianHigh,
Expand Down Expand Up @@ -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);
Expand Down