stats-incremental
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "stats-incremental", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A simple tool for calculating incremental stats on numeric streams.", | ||
@@ -11,3 +11,3 @@ "main": "stats.js", | ||
"scripts": { | ||
"test": "node test/" | ||
"test": "node test/ && standard" | ||
}, | ||
@@ -39,4 +39,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"stats-lite": "^1.0.3", | ||
"tape": "~2.14.0" | ||
"standard": "~10.0.2", | ||
"stats-lite": "^2.1.0", | ||
"tape": "~4.7.0" | ||
}, | ||
@@ -43,0 +44,0 @@ "repository": { |
@@ -16,2 +16,3 @@ stats-incremental | ||
* standard_deviation | ||
* simple moving average | ||
@@ -44,3 +45,4 @@ This module can be used either with Node `streams` via a wrapper such as `through2` or without being streaming. | ||
variance: 5.851843979168881, | ||
standard_deviation: 2.419058490233107 } | ||
standard_deviation: 2.419058490233107, | ||
sma50: 6.82 } | ||
*/ | ||
@@ -84,3 +86,4 @@ | ||
variance: 0.08331362954827709, | ||
standard_deviation: 0.28864100462040576 } | ||
standard_deviation: 0.28864100462040576, | ||
sma50: 0.5422519558777934 } | ||
{ n: 200000, | ||
@@ -92,3 +95,4 @@ min: 2.0884908735752106e-7, | ||
variance: 0.08316120223669865, | ||
standard_deviation: 0.2883768406732736 } | ||
standard_deviation: 0.2883768406732736, | ||
sma50: 0.4396136475716979 } | ||
*/ | ||
@@ -101,14 +105,12 @@ | ||
`var stats = require("stats-incremental")()` | ||
--- | ||
## `const Stats = require("stats-incremental")` | ||
## `var stats = new Stats(smaBins)` | ||
Create a new incremental stats aggregator. | ||
Create a new incremental stats aggregator. The `smaBins` argument is optional (default 50) and will choose the size of recent window to retain to calculate the Simple Moving Average on the recent data. | ||
`stats.update(value)` | ||
--- | ||
## `stats.update(value)` | ||
Update the aggregator with a value. Converted to a Number via parseFloat. If this results in NaN the update is skipped. | ||
`stats.getAll()` | ||
--- | ||
## `stats.getAll()` | ||
@@ -126,40 +128,38 @@ Get a up-to-date clone of all of the stats stored. | ||
variance: 5.851843979168881, | ||
standard_deviation: 2.419058490233107 } | ||
standard_deviation: 2.419058490233107, | ||
sma50: 6.82 } | ||
``` | ||
`stats.n` | ||
--- | ||
## `stats.n` | ||
The count of observations. | ||
`stats.min` | ||
--- | ||
## `stats.min` | ||
The min value observed. | ||
`stats.max` | ||
--- | ||
## `stats.max` | ||
The max value observed. | ||
`stats.sum` | ||
--- | ||
## `stats.sum` | ||
The sum of all values observed. | ||
`stats.mean` | ||
--- | ||
## `stats.mean` | ||
The arithmetic mean of the observations. | ||
`stats.variance` | ||
--- | ||
## `stats.variance` | ||
The variance from the mean. | ||
`stats.standard_deviation` | ||
--- | ||
## `stats.standard_deviation` | ||
The standard deviation of the values from the mean. | ||
## `stats.smaXX` | ||
Get the Simple Moving Average of the recent data. Default is to store 50 recent records and expose an `sma50` property with the simple moving average. If the `Stats` object is created with an argument of a number of SMA bins, the property will reflect the number of bins, e.g. `Stats(100)` will have an `sma100` instead of `sma50` property. | ||
Alternatives | ||
@@ -166,0 +166,0 @@ --- |
47
stats.js
@@ -1,7 +0,9 @@ | ||
"use strict"; | ||
'use strict' | ||
const statslite = require('stats-lite') | ||
module.exports = Stats | ||
function Stats() { | ||
if (!(this instanceof Stats)) return new Stats() | ||
function Stats (smaBins) { | ||
if (!(this instanceof Stats)) return new Stats(smaBins) | ||
this.n = 0 | ||
@@ -12,18 +14,38 @@ this.min = Number.MAX_VALUE | ||
this.mean = 0 | ||
Object.defineProperty(this, "q", { | ||
if (smaBins == null || smaBins <= 0) { | ||
smaBins = 50 | ||
} | ||
if (smaBins !== (smaBins | 0)) { | ||
throw new Error('SMA option must be an integer') | ||
} | ||
Object.defineProperty(this, 'smaBins', { | ||
enumerable: false, | ||
writable: false, | ||
value: smaBins | ||
}) | ||
Object.defineProperty(this, '_bins', { | ||
enumerable: false, | ||
writable: false, | ||
value: [] | ||
}) | ||
Object.defineProperty(this, 'q', { | ||
enumerable: false, | ||
writable: true, | ||
value: 0 | ||
}) | ||
Object.defineProperty(this, "variance", { | ||
Object.defineProperty(this, 'variance', { | ||
enumerable: true, | ||
get: function () { return this.q / this.n } | ||
get: () => { return this.q / this.n } | ||
}) | ||
Object.defineProperty(this, "standard_deviation", { | ||
Object.defineProperty(this, 'standard_deviation', { | ||
enumerable: true, | ||
get: function () { return Math.sqrt(this.q / this.n)} | ||
get: () => { return Math.sqrt(this.q / this.n) } | ||
}) | ||
Object.defineProperty(this, `sma${smaBins}`, { | ||
enumerable: true, | ||
get: () => { return statslite.mean(this._bins) } | ||
}) | ||
} | ||
Stats.prototype.update = function update(value) { | ||
Stats.prototype.update = function update (value) { | ||
var num = parseFloat(value) | ||
@@ -41,5 +63,9 @@ if (isNaN(num)) { | ||
this.q = this.q + (num - prevMean) * (num - this.mean) | ||
this._bins.push(value) | ||
if (this._bins.length > this.smaBins) { | ||
this._bins.shift() | ||
} | ||
} | ||
Stats.prototype.getAll = function getAll() { | ||
Stats.prototype.getAll = function getAll () { | ||
if (this.n === 0) { | ||
@@ -57,3 +83,4 @@ return null | ||
} | ||
s[`sma${this.smaBins}`] = this[`sma${this.smaBins}`] | ||
return s | ||
} |
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
7720
78
3