Skip to content

Commit

Permalink
Added harmonic mean
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanvamsi3 committed Oct 6, 2017
1 parent 06478ef commit babe98c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Math Stats [![Build Status](https://travis-ci.org/pavanvamsi3/math-stats.svg?branch=sd_and_variance)](https://travis-ci.org/pavanvamsi3/math-stats) [![Coverage Status](https://coveralls.io/repos/github/pavanvamsi3/math-stats/badge.svg?branch=master)](https://coveralls.io/github/pavanvamsi3/math-stats?branch=master)
=========

A small library that calculates mean, median and mode for your numbers
A small library that does statistics for your numbers

### Installation

Expand All @@ -20,6 +20,7 @@ var median = stats.median(numbers); // returns 2
var modes = stats.mode(moreNumbers); // returns [1]
var standardDeviation = stats.standardDeviation(numbers); // returns 0.816496580927726
var variance = stats.variance(numbers); // returns 0.6666666666666666
var harmonicMean = stats.harmonicMean(numbers); returns 1.6363636363636365
```

### Tests
Expand Down
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ let variance = numbers => {
return variance;
};

let harmonicMean = numbers => {
if (!Array.isArray(numbers)) {
return null;
}
let total = numbers.reduce(function(sum, value) {
return sum + (1/value);
}, 0);

return numbers.length/total;
};

module.exports = {
mean: mean,
median: median,
mode: mode,
standardDeviation: standardDeviation,
variance: variance
variance: variance,
harmonicMean: harmonicMean
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "math-stats",
"version": "0.2.5",
"version": "0.2.6",
"description": "Statistics for your numbers",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ describe('#stats', function() {
result = Object.is(result, NaN);
expect(result).to.equal(true);
});
it('Harmonic Mean', function() {
var result = stats.harmonicMean([1, 2, 3]);
expect(result).to.equal(1.6363636363636365);
});
});

0 comments on commit babe98c

Please sign in to comment.