compute-hmean

Computes the harmonic mean over an array of values.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
compute-hmean
421.0.08 years ago8 years agoMinified + gzip package size for compute-hmean in KB

Readme

Harmonic Mean
!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coveragecoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-url
Computes the harmonic mean over an array of values.

Installation

$ npm install compute-hmean

Usage

The use the module,
var hmean = require( 'compute-hmean' );

hmean( arr )

Computes the harmonic mean.
var data = [ 1, 5, 3, 4, 16 ];

var mu = hmean( data );
// returns ~2.7088

Note: 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 hmean = require( 'compute-hmean' );

var data = new Array( 1000 );

for ( var i = 0; i < data.length; i++ ) {
	data[ i ] = Math.random() * 100;
}

console.log( hmean( data ) );

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

Notes

For arrays exceeding memory constraints, you are encouraged to use streams; see flow-hmean.

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. Rebekah Smith.