jasondavies / science.js

Scientific and statistical computing in JavaScript.
http://www.jasondavies.com/
Other
886 stars 93 forks source link

mode of same-element arrays is undefined #8

Closed tmcw closed 11 years ago

tmcw commented 11 years ago

Adding the test:

assert.equal(science.stats.mode([1, 1]), 1);

Throws that science.stats.mode returns undefined, when it should return 1.

tmcw commented 11 years ago

It also looks like arrays in which the highest number is the mode are not correctly processed, like assert.equal(science.stats.mode([1, 1, 2, 2, 2]), 2);

tmcw commented 11 years ago

In simple-statistics I'm working around this by iterating up to length + 1: https://github.com/tmcw/simple-statistics/blob/master/src/simple_statistics.js#L339

jasondavies commented 11 years ago

Thanks for the bug report. I've rewritten it completely so it should be more self-explanatory, and also handle all cases correctly. Further, it prepares support for returning multiple modes in future.