Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add decile function #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Calculate the [standard deviation](http://en.wikipedia.org/wiki/Standard_deviati

Calculate the value representing the desired [percentile](http://en.wikipedia.org/wiki/Percentile) `(0 < ptile <= 1)`. Uses the Estimation method to interpolate non-member percentiles.

`decile(vals, decile)`
---

Calculate the value representing the desired [decile](http://en.wikipedia.org/wiki/Decile) `(0 < decile <= 10)`. Uses the Estimation method to interpolate non-member percentiles.

`histogram(vals[, bins])`
---

Expand Down
293 changes: 293 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports.stdev = populationStdev
module.exports.sampleStdev = sampleStdev
module.exports.populationStdev = populationStdev
module.exports.percentile = percentile
module.exports.decile = decile
module.exports.firstDecile = firstDecile
module.exports.lastDecile = lastDecile
module.exports.histogram = histogram

var isNumber = require("isnumber")
Expand Down Expand Up @@ -60,7 +63,7 @@ function median(vals) {
}
else {
// Even length, average middle two elements
return (vals[half-1] + vals[half]) / 2.0
return (vals[half - 1] + vals[half]) / 2.0
}
}

Expand Down Expand Up @@ -154,7 +157,19 @@ function percentile(vals, ptile) {
return (1 - fract) * vals[int_part] + fract * vals[Math.min(int_part + 1, vals.length - 1)]
}

function histogram (vals, bins) {
function decile(vals, decile) {
if (decile == null || decile < 0) return NaN

return percentile(vals, decile / 10);
}
function firstDecile(vals) {
return decile(vals, 1);
}
function lastDecile(vals) {
return decile(vals, 9);
}

function histogram(vals, bins) {
if (vals == null) {
return null
}
Expand Down
Loading