-
Notifications
You must be signed in to change notification settings - Fork 4
#5 - Root mean square #25
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename to |
||
| ``` | ||
|
|
||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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. | ||
|
|
||
There was a problem hiding this comment.
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
rmsas set in #5 ?