compute-nanqmean

Computes the quadratic mean (root mean square) of an array of values ignoring any values which are not numeric.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
compute-nanqmean
1.0.08 years ago8 years agoMinified + gzip package size for compute-nanqmean in KB

Readme

Quadratic Mean
!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coverage Statuscoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-url
Computes the quadratic mean (root mean square) of an array of values ignoring any values which are not numeric.

Installation

$ npm install compute-nanqmean

For use in the browser, use browserify.

Usage

To use the module,
var nanqmean = require( 'compute-nanqmean' );

nanqmean( arr )

Computes the quadratic mean (root mean square) ignoring non-numeric values.
var data = [ 2, 7, NaN, 3, -3, NaN, 9 ];

var mu = nanqmean( data );
// returns ~5.5136

Examples

var nanqmean = require( 'compute-nanqmean' );

var data = new Array( 1000 );
for ( var i = 0; i < data.length; i++ ) {
	if ( i%5 === 0 ) {
		data[ i ] = NaN;
	} else {
		data[ i ] = Math.random() * 100;
	}
}

console.log( nanqmean( data ) );

To run the example code from the top-level application directory,
$ node ./examples/index.js

Notes

  1. The algorithm to compute the quadratic mean first calculates the L2 norm before dividing by the square root of the array length. This particular implementation attempts to avoid overflow and underflow and is accurate to <1e-13 compared to the canonical formula for calculating the root mean square.
  2. The quadratic mean of an array containing non-numeric values is equal to the quadratic mean of an equivalent array which contains only the numeric values. Hence,

var d1 = [ 1, NaN, 2, 3, NaN ],
    d2 = [ 1, 2, 3 ];

console.log( nanqmean( d1 ) === nanqmean( d2 ) );
// returns true

References

  • Dahlquist, Germund and Bjorck, Ake. Numerical Methods in Scientific Computing.
  • Blue, James (1978) "A Portable Fortran Program To Find the Euclidean Norm of a Vector". ACM Transactions on Mathematical Software.
  • Higham, Nicholas J. Accuracy and Stability of Numerical Algorithms, Second Edition.

This module implements a one-pass algorithm proposed by S.J. Hammarling.

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-cov

License

MIT license.

Copyright

Copyright © 2014. Athan Reines.