Skip to main content
Module

x/simplestatistics/src/root_mean_square.js

simple statistics for node & browser javascript
Go to Latest
File
/** * The Root Mean Square (RMS) is * a mean function used as a measure of the magnitude of a set * of numbers, regardless of their sign. * This is the square root of the mean of the squares of the * input numbers. * This runs in `O(n)`, linear time, with respect to the length of the array. * * @param {Array<number>} x a sample of one or more data points * @returns {number} root mean square * @throws {Error} if x is empty * @example * rootMeanSquare([-1, 1, -1, 1]); // => 1 */function rootMeanSquare(x) { if (x.length === 0) { throw new Error("rootMeanSquare requires at least one data point"); }
let sumOfSquares = 0; for (let i = 0; i < x.length; i++) { sumOfSquares += Math.pow(x[i], 2); }
return Math.sqrt(sumOfSquares / x.length);}
export default rootMeanSquare;