Package Exports
- compute-nanhmean
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (compute-nanhmean) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
nanhmean
Computes the harmonic mean of an array of values ignoring any values which are not numeric.
Installation
$ npm install compute-nanhmeanFor use in the browser, use browserify.
Usage
To use the module,
var nanhmean = require( 'compute-nanhmean' );nanhmean( arr )
Computes the harmonic mean ignoring non-numeric values.
var data = [ 1, 5, NaN, 3, 4, NaN, 16 ];
var mu = nanhmean( data );
// returns ~2.7088Note: only calculate the harmonic mean for positive, real numbers.
If an array contains negative numbers, the harmonic mean is nonsensical. For example, consider x = [ 3, -3, 4 ]. The harmonic mean of x is 12, while the arithmetic mean is 1.33333.... The harmonic mean should never be greater than the arithmetic mean.
Similarly, if an array contains zero values, the harmonic mean is also zero: 1/0 --> infinity and 1/infinity --> 0. For example, consider x = [ 0, 100, 1000, 10000 ]. Using the textbook definition of the harmonic mean, the mean would be 0, which, given x, does not make sense.
If an array contains elements less than or equal to 0, the function returns NaN.
Examples
var nanhmean = require( 'compute-nanhmean' );
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( nanhmean( data ) );To run the example code from the top-level application directory,
$ node ./examples/index.jsNotes
The harmonic mean of an array containing non-numeric values is equal to the harmonic 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( nanhmean( d1 ) === nanhmean( d2 ) );
// returns trueTests
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 testAll 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-covIstanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-covLicense
Copyright
Copyright © 2014. Athan Reines.