calc-stats
Advanced tools
Comparing version 0.4.1 to 1.0.0
const { getOrCreateIterator } = require("iter-fun"); | ||
const fasterMedian = require("faster-median"); | ||
const computeVariance = ({ count, histogram, mean }) => { | ||
return ( | ||
Object.values(histogram).reduce((sum, { n, ct }) => { | ||
return sum + ct * Math.pow(n - mean, 2); | ||
}, 0) / count | ||
); | ||
}; | ||
function calcStats( | ||
@@ -10,2 +18,3 @@ data, | ||
filter = undefined, | ||
calcCount = true, | ||
calcHistogram = true, | ||
@@ -19,10 +28,51 @@ calcMax = true, | ||
calcRange = true, | ||
calcSum = true | ||
calcStd = true, | ||
calcSum = true, | ||
calcVariance = true, | ||
calcUniques = true, | ||
stats | ||
} = { debugLevel: 0 } | ||
) { | ||
if (stats) { | ||
// validate stats argument | ||
stats.forEach(stat => { | ||
if ( | ||
![ | ||
"count", | ||
"histogram", | ||
"max", | ||
"mean", | ||
"median", | ||
"min", | ||
"mode", | ||
"modes", | ||
"range", | ||
"sum", | ||
"std", | ||
"variance", | ||
"uniques" | ||
].includes(stat) | ||
) { | ||
console.warn(`[calc-stats] skipping unknown stat "${stat}"`); | ||
} | ||
}); | ||
calcCount = stats.includes("count"); | ||
calcHistogram = stats.includes("histogram"); | ||
calcMax = stats.includes("max"); | ||
calcMean = stats.includes("mean"); | ||
calcMedian = stats.includes("median"); | ||
calcMode = stats.includes("mode"); | ||
calcModes = stats.includes("modes"); | ||
calcRange = stats.includes("range"); | ||
calcSum = stats.includes("sum"); | ||
calcStd = stats.includes("std"); | ||
calcVariance = stats.includes("variance"); | ||
calcUniques = stats.includes("uniques"); | ||
} | ||
const iter = getOrCreateIterator(data); | ||
let needCount = calcMean || calcMedian || typeof filter === "function"; | ||
let needHistogram = calcHistogram || calcMedian || calcMode || calcModes; | ||
let needSum = calcSum || calcMean; | ||
let needCount = calcCount || calcMean || calcMedian || calcVariance || calcStd || typeof filter === "function"; | ||
let needHistogram = calcHistogram || calcMedian || calcMode || calcModes || calcUniques; | ||
let needSum = calcSum || calcMean || calcVariance || calcStd; | ||
let needMin = calcMin || calcRange; | ||
@@ -73,2 +123,3 @@ let needMax = calcMax || calcRange; | ||
const results = {}; | ||
if (calcCount) results.count = count; | ||
if (calcMedian) results.median = fasterMedian({ counts: histogram, total: count }); | ||
@@ -79,3 +130,11 @@ if (calcMin) results.min = min; | ||
if (calcRange) results.range = max - min; | ||
if (calcMean) results.mean = sum / count; | ||
if (calcMean || calcVariance || calcStd) { | ||
const mean = sum / count; | ||
if (calcMean) results.mean = mean; | ||
if (calcVariance || calcStd) { | ||
const variance = computeVariance({ count, histogram, mean }); | ||
if (calcVariance) results.variance = variance; | ||
if (calcStd) results.std = Math.sqrt(variance); | ||
} | ||
} | ||
if (calcHistogram) results.histogram = histogram; | ||
@@ -100,2 +159,6 @@ if (calcMode || calcModes) { | ||
} | ||
if (calcUniques) | ||
results.uniques = Object.values(histogram) | ||
.map(({ n }) => n) | ||
.sort((a, b) => a - b); | ||
@@ -102,0 +165,0 @@ return results; |
{ | ||
"name": "calc-stats", | ||
"version": "0.4.1", | ||
"version": "1.0.0", | ||
"description": "Memory-Aware Statistical Calculations", | ||
@@ -5,0 +5,0 @@ "main": "calc-stats.js", |
@@ -31,2 +31,3 @@ # calc-stats | ||
{ | ||
count: 4950, // number of valid numerical values | ||
min: 1, | ||
@@ -39,3 +40,2 @@ max: 100, | ||
range: 99, // the difference between max and min | ||
sum: 328350, | ||
histogram: { | ||
@@ -49,3 +49,7 @@ '1': { | ||
. | ||
} | ||
}, | ||
std: 23.44970978261541, // standard deviation | ||
sum: 328350, // sum of all the valid numerical values | ||
variance: 549.8888888888889, // variance of std calculation | ||
uniques: [1, 2, 3, 4, 5, ...] // sorted array of unique values (same as histogram keys) | ||
}); | ||
@@ -87,20 +91,8 @@ */ | ||
## specify calculations | ||
If you only care about specific statistics, you can configure calcStats through an options object: | ||
If you only care about specific statistics, you can pass in a stats array | ||
```js | ||
import calcStats from "calc-stats"; | ||
// we only want the min and max | ||
const options = { | ||
calcHistogram: false, | ||
calcMax: true, | ||
calcMean: false, | ||
calcMedian: false, | ||
calcMin: true, | ||
calcMode: false, | ||
calcModes: false, | ||
calcRange: true, | ||
calcSum: false | ||
}; | ||
const results = calcStats(data, options); | ||
const results = calcStats(data, { stats: ["min", "max", "range"] }); | ||
// results is { min: 1, max: 100, range: 99 } | ||
``` |
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15930
161
0
95