stats-lite
Advanced tools
Comparing version 2.0.4 to 2.1.0
{ | ||
"name": "stats-lite", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "A light statistical package that operates on numeric Arrays.", | ||
@@ -43,2 +43,3 @@ "main": "stats.js", | ||
"devDependencies": { | ||
"dice": "0.0.2", | ||
"tape": "~4.6.3" | ||
@@ -45,0 +46,0 @@ }, |
@@ -30,2 +30,3 @@ stats-lite | ||
console.log("85th percentile: %s", stats.percentile(rolls, 0.85)) | ||
console.log("histogram:", stats.histogram(rolls)) | ||
@@ -40,2 +41,6 @@ /* Your exact numbers may vary, but they should be pretty similar: | ||
85th percentile: 10 | ||
histogram { values: [ 86, 159, 253, 335, 907, 405, 339, 270, 146, 100 ], | ||
bins: 10, | ||
binWidth: 1.05, | ||
binLimits: [ 1.75, 12.25 ] } | ||
*/ | ||
@@ -101,2 +106,16 @@ | ||
`histogram(vals[, bins])` | ||
--- | ||
Build a histogram representing the distribution of the data in the provided number of `bins`. If `bins` is not set, it will choose one based on `Math.sqrt(vals.length)`. Data will look like: | ||
``` | ||
histogram { | ||
values: [ 86, 159, 253, 335, 907, 405, 339, 270, 146, 100 ], | ||
bins: 10, | ||
binWidth: 1.05, | ||
binLimits: [ 1.75, 12.25 ] | ||
} | ||
``` | ||
Where `values` are the bins and the counts of the original values falling in that range. The ranges can be calculated from the `binWidth` and `binLimits`. For example, the first bin `values[0]` in this example is from `1.75 < value <= 2.8`. The third bin `values[2]` would be `1.75 + (1.05 * 2) < value <= 1.75 + (1.05 * 3)` or `3.85 < value <= 4.9`. | ||
LICENSE | ||
@@ -103,0 +122,0 @@ ======= |
56
stats.js
@@ -11,2 +11,3 @@ "use strict"; | ||
module.exports.percentile = percentile | ||
module.exports.histogram = histogram | ||
@@ -131,1 +132,56 @@ var isNumber = require("isnumber") | ||
} | ||
function histogram (vals, bins) { | ||
if (vals == null) { | ||
return null | ||
} | ||
vals = nsort(numbers(vals)) | ||
if (vals.length === 0) { | ||
return null | ||
} | ||
if (bins == null) { | ||
// pick bins by simple method: Math.sqrt(n) | ||
bins = Math.sqrt(vals.length) | ||
} | ||
bins = Math.round(bins) | ||
if (bins < 1) { | ||
bins = 1 | ||
} | ||
var min = vals[0] | ||
var max = vals[vals.length - 1] | ||
if (min === max) { | ||
// fudge for non-variant data | ||
min = min - 0.5 | ||
max = max + 0.5 | ||
} | ||
var range = (max - min) | ||
// make the bins slightly larger by expanding the range about 10% | ||
// this helps with dumb floating point stuff | ||
var binWidth = (range + (range * 0.05)) / bins | ||
var midpoint = (min + max) / 2 | ||
// even bin count, midpoint makes an edge | ||
var leftEdge = midpoint - (binWidth * Math.floor(bins / 2)) | ||
if (bins % 2 !== 0) { | ||
// odd bin count, center middle bin on midpoint | ||
var leftEdge = (midpoint - (binWidth / 2)) - (binWidth * Math.floor(bins / 2)) | ||
} | ||
var hist = { | ||
values: Array(bins).fill(0), | ||
bins: bins, | ||
binWidth: binWidth, | ||
binLimits: [leftEdge, leftEdge + (binWidth * bins)] | ||
} | ||
var binIndex = 0 | ||
for (var i = 0; i < vals.length; i++) { | ||
while (vals[i] > (((binIndex + 1) * binWidth) + leftEdge)) { | ||
binIndex++ | ||
} | ||
hist.values[binIndex]++ | ||
} | ||
return hist | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9922
162
122
2