stats-lite
Advanced tools
Comparing version 1.0.3 to 2.0.0
{ | ||
"name": "stats-lite", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A light statistical package that operates on numeric Arrays.", | ||
"main": "stats.js", | ||
"directories": { | ||
"example": "examples", | ||
"test": "test" | ||
@@ -35,2 +36,5 @@ }, | ||
}, | ||
"engines": { | ||
"node": ">=2.0.0" | ||
}, | ||
"dependencies": { | ||
@@ -40,4 +44,5 @@ "isnumber": "~1.0.0" | ||
"devDependencies": { | ||
"tape": "~2.10.2" | ||
} | ||
"tape": "~4.2.0" | ||
}, | ||
"homepage": "https://github.com/brycebaril/node-stats-lite" | ||
} |
@@ -43,2 +43,4 @@ stats-lite | ||
**Compatibility Notice**: Version 2.0.0+ of this library use features that require Node.js v4.0.0 and above | ||
API | ||
@@ -69,3 +71,3 @@ === | ||
Calculate the [mean](http://en.wikipedia.org/wiki/Mean) average value of vals. | ||
Calculate the [mean](http://en.wikipedia.org/wiki/Mean) average value of `vals`. | ||
@@ -75,3 +77,3 @@ `median(vals)` | ||
Calculate the [median](http://en.wikipedia.org/wiki/Median) average value of vals. | ||
Calculate the [median](http://en.wikipedia.org/wiki/Median) average value of `vals`. | ||
@@ -81,4 +83,6 @@ `mode(vals)` | ||
Calculate the [mode](http://en.wikipedia.org/wiki/Mode_statistics) average value of vals. | ||
Calculate the [mode](http://en.wikipedia.org/wiki/Mode_statistics) average value of `vals`. | ||
If `vals` is multi-modal (contains multiple modes), `mode(vals)` will return a ES6 Set of the modes. | ||
`variance(vals)` | ||
@@ -85,0 +89,0 @@ --- |
68
stats.js
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
module.exports.numbers = numbers | ||
@@ -10,10 +12,10 @@ module.exports.sum = sum | ||
var isNumber = require("isnumber") | ||
const isNumber = require("isnumber") | ||
function numbers(vals) { | ||
var nums = [] | ||
let nums = [] | ||
if (vals == null) | ||
return nums | ||
for (var i = 0; i < vals.length; i++) { | ||
for (let i = 0; i < vals.length; i++) { | ||
if (isNumber(vals[i])) | ||
@@ -26,3 +28,3 @@ nums.push(+vals[i]) | ||
function nsort(vals) { | ||
return vals.sort(function (a, b) { return a - b }) | ||
return vals.sort(function numericSort(a, b) { return a - b }) | ||
} | ||
@@ -32,4 +34,4 @@ | ||
vals = numbers(vals) | ||
var total = 0 | ||
for (var i = 0; i < vals.length; i++) { | ||
let total = 0 | ||
for (let i = 0; i < vals.length; i++) { | ||
total += vals[i] | ||
@@ -50,3 +52,3 @@ } | ||
var half = (vals.length / 2) | 0 | ||
let half = (vals.length / 2) | 0 | ||
@@ -64,18 +66,36 @@ vals = nsort(vals) | ||
// Returns the mode of a unimodal dataset -- NaN for multi-modal or empty datasets. | ||
// Returns the mode of a unimodal dataset | ||
// If the dataset is multi-modal, returns a Set containing the modes | ||
function mode(vals) { | ||
vals = numbers(vals) | ||
if (vals.length === 0) return NaN | ||
var mode = NaN | ||
var dist = {} | ||
vals.forEach(function (n) { | ||
var me = dist[n] || 0 | ||
let mode = NaN | ||
let dist = {} | ||
for (let i = 0; i < vals.length; i++) { | ||
let value = vals[i] | ||
let me = dist[value] || 0 | ||
me++ | ||
dist[n] = me | ||
}) | ||
var rank = numbers(Object.keys(dist).sort(function (a, b) { return dist[b] - dist[a] })) | ||
dist[value] = me | ||
} | ||
let rank = numbers(Object.keys(dist).sort(function sortMembers(a, b) { return dist[b] - dist[a] })) | ||
mode = rank[0] | ||
if (dist[rank[1]] == dist[mode]) { | ||
// Multiple modes found, abort | ||
return NaN | ||
// multi-modal | ||
if (rank.length == vals.length) { | ||
// all values are modes | ||
return vals | ||
} | ||
let modes = new Set([mode]) | ||
let modeCount = dist[mode] | ||
for (let i = 1; i < rank.length; i++) { | ||
if (dist[rank[i]] == modeCount) { | ||
modes.add(rank[i]) | ||
} | ||
else { | ||
break | ||
} | ||
} | ||
return modes | ||
} | ||
@@ -88,5 +108,5 @@ return mode | ||
vals = numbers(vals) | ||
var avg = mean(vals) | ||
var diffs = [] | ||
for (var i = 0; i < vals.length; i++) { | ||
let avg = mean(vals) | ||
let diffs = [] | ||
for (let i = 0; i < vals.length; i++) { | ||
diffs.push(Math.pow((vals[i] - avg), 2)) | ||
@@ -109,8 +129,8 @@ } | ||
vals = nsort(vals) | ||
var i = (vals.length * ptile) - 0.5 | ||
let i = (vals.length * ptile) - 0.5 | ||
if ((i | 0) === i) return vals[i] | ||
// interpolated percentile -- using Estimation method | ||
var int_part = i | 0 | ||
var fract = i - int_part | ||
let int_part = i | 0 | ||
let fract = i - int_part | ||
return (1 - fract) * vals[int_part] + fract * vals[int_part + 1] | ||
} | ||
} |
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
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
7631
112
0
103
4