Gauss
JavaScript statistics, analytics, and set library - Node.js and web browser ready
Evented, asynchronous, and fast, Node.js is an attractive platform for data mining, statistics, and data analysis. Gauss makes it easy to calculate and explore data through JavaScript, both on Node.js and within the web browser.
License
MIT/X11 - See LICENSE
Support
Mailing list - Google Group
Getting started
Install with NPM (Node Package Manager)
Getting started with Gauss + Node.js is easy:
$ npm install gauss
var gauss = ;
Using Gauss within a web browser
Gauss requires support for ECMAScript 5 Object.defineProperty
. Compatibility is listed here. Download and include gauss.min.js:
The Bower package manager can also be used to install Gauss:
$ bower install gauss
Gauss is also Asynchronous Module Definition compatible and works with module loaders like RequireJS:
Installing development dependencies and running tests
To run Gauss's tests you'll need Vows. NPM can automatically resolve this:
$ npm install gauss --devel
To invoke the tests:
$ npm test
API
Instantiation
// List of numbersvar set = 5 1 3 2 21;// From a regular Arrayvar numbers = 8 6 7 5 3 0 9;// After instantiation, Gauss objects can be conveniently used like any Arraynumbers0 = 2;set1 = 7;
Note: To prevent unintended scope/prototype pollution, Gauss versions after 0.2.3 have removed support for monkey patching the native Array data type. Use the .toArray() method of any Gauss object to a convert to a vanilla Array.
Scope chaining
Gauss collections utilize scope chaining for converting between collection types:
var Collection = gaussCollection;var things =type: 1 age: 1type: 2 age: 2type: 1 age: 3type: 2 age: 4 ;things// Scope chained converter, converting mapped collection of ages to Vector;
Callbacks and method chaining
All of Gauss's methods accept an optional callback:
set;set;
In addition, for methods that return another Vector, method chaining makes it easy to perform calculations that flow through each other:
set; // Find the standard deviation of data set's quartiles
Finally, you can mix and match both callbacks and chaining:
set;
Collection
Collection.indexBy
.indexBy(predicate, callback)
Returns the first index of an element that matches a condition.
Collection.indicesOf
.indicesOf(element, callback)
Returns the indices of all elements that match a value.
Collection.indicesBy
.indicesBy(predicate, callback)
Returns all indices of an element that match a condition.
Collection.lastIndexBy
.lastIndexBy(predicate, callback)
Returns the last index of an element that matches a condition.
Collection.find
.find(predicate, callback)
Returns all the elements that match a condition.
var people =firstname: 'John' lastname: 'Smith'firstname: 'Jane' lastname: 'Doe'firstname: 'Mike' lastname: 'Smith'firstname: 'Susan' lastname: 'Baker';// Using a predicate Functionpeople;> firstname: 'Jane' lastname: 'Doe'// Using a condition Objectpeople;> firstname: 'John' lastname: 'Smith'firstname: 'Mike' lastname: 'Smith'
Collection.findOne
.findOne(predicate, callback)
Returns the first element that matches a condition.
// Using a predicate Functionpeople;> firstname: 'Jane' lastname: 'Doe'// Using a condition Objectpeople;> firstname: 'John' lastname: 'Smith'
Collection.split
.split(predicate[, callback])
Returns a Collection split by a condition (binomial cluster).
;> 1 3 2 4
Collection.mode
.mode(callback)
Returns the value(s) that occur the most frequently in a data set. If there is a tie, returns a Collection of values.
Collection.frequency
.frequency(element, callback)
Returns the number of occurrences of value within a data set.
Collection.distribution
.distribution(format, callback)
Returns an Object
containing the (frequency) distribution of values within the Collection. Default format: absolute
; relative
returns ratio of occurrences and total number of values in a data set.
set;>1: 12: 13: 15: 121: 1set;>1: 022: 023: 025: 0221: 02
Collection.append
.append(that, callback)
Return Collection appended with an Array.
var numbers = 1 2 3;> 1 2 3 1 2 3
Collection.unique
.unique(callback)
Return a Collection with unique values.
var numbers = 1 2 3 3 4 4;> 1 2 3 4
Collection.union
.union(array, callback)
Return the union of a Collection with another array.
var union = 'a' 'b' 'c';> 'a' 'b' 'c' 'd' 'e'
Collection.extend
.extend(methods, callback)
Returns a Collection extended with named functions.
Vector
Extends Collection methods with numerical functions.
Vector.min
.min(callback)
Returns the smallest number.
Vector.max
.max(callback)
Returns the largest number.
Vector.equal
.equal(that)
Returns true
or false
if Vector values are equal to another Vector or Array.
Vector.sum
.sum(callback)
Returns the sum of the numbers.
Vector.product
.product(callback)
Returns the product of the numbers.
Vector.push
.push(number1, ..., numberN, callback)
Returns the updated Vector with one or more elements appended to the end; performs/maintains streaming calculations.
var Vector = Vectordigits = ;// Push some numbers indigits;> 3digits;> 6// Keep on pushing; sum is updated as numbers are pusheddigits;> 6
Note: Streaming calculations like sum(), product(), variance(), and functions dependent on streaming capable functions benefit from O(1) amortized performance.
Vector.range
.range(callback)
Returns the difference between the largest and smallest value in a data set.
Vector.mean
.mean(callback)
Returns the arithmetic mean.
Vector.gmean
.gmean(callback)
Returns the geometric mean.
Vector.hmean
.hmean(callback)
Returns the harmonic mean.
Vector.qmean
.qmean(callback)
Returns the quadratic mean (RMS, root mean square).
Vector.pmean
.pmean(p, callback)
Returns the power/generalized mean given an order or power p.
// p = -1, harmonic meanset;// p = 1, arithmetic meanset;// p = 2, quadratic meanset;
Vector.median
.median(callback)
Returns the median. If there are an even amount of numbers in the data set, returns the arithmetic mean of the two middle values.
Vector.mode
.mode(callback)
Returns the value(s) that occur the most frequently in a data set. If there is a tie, returns a Vector of values.
Vector.variance
.variance(callback)
Returns a measure of how far a set of numbers are spread out from each other.
Vector.stdev
.stdev(percent, callback)
Returns the standard deviation of data set. If a percent is given, returns the standard deviation with respect to a percentile of the population.
Vector.frequency
.frequency(value, callback)
Returns the number of occurrences of value within a data set.
Vector.percentile
.percentile(value, callback)
Returns the value that below which a certain percent of observations fall within the data set.
Vector.density
.density(percent, callback)
Returns a Vector which is a percentile subset of values occurring within a data set.
Vector.distribution
.distribution(format, callback)
Returns an Object
containing the (frequency) distribution of values within the Vector. Default format: absolute
; relative
returns ratio of occurrences and total number of values in a data set.
set;>1: 12: 13: 15: 121: 1set;>1: 022: 023: 025: 0221: 02
Vector.quantile
.quantile(quantity, callback)
Returns a Vector of values that divide a frequency distribution into equal groups, each containing the same fraction of the total data set.
set; // Quartiles
Vector.sma
.sma(period, callback)
Returns a Vector of the simple moving average (SMA); unweighted means of the previous n data points. period
is the length of observation window for moving average.
var prices = 222734 22194 220847 221741 22184 221344222337 224323 222436 222933 221542 223926223816 226109 233558 240519 23753 238324239516 236338 238225 238722 236537 23187230976 23326 226805 230976 224025 221725;prices = prices;// 10-period SMAprices;> 2222475 2221283 222326899999999982226238 2230606 22423242261499 2276692 22906932307773 23211779999999997 23378612352657 23653779999999998 237113899999999982368557 2361298 23505732343225 2327734 2313121
Vector.ema
.ema(options, callback)
Returns a Vector of the exponential moving average (EMA); weighted means of the previous n data points.
options
is
- Number Length of the observation window for moving average, using the default smoothing ratio (2 / period + 1) or
- Object.period Length of the observation window for moving average
- Object.ratio Function returning a Number to be used as smoothing ratio
// 10-period EMAprices;> 2222475 2221192272727273 222447731404958722269650751314803 22331696069257568 225178967839380122796806459585646 22970659830570074 231273398613755142327720534112542 2334204073364807 2342939696389387523509906606822263 23536050860127308 23472587067376892340440760058109 23390151673202713 2326112409625676523231392442391897 23080684725593372 2291556023003094// 10-period Welles Wilder EMAprices;> 2222475 22217695 2223518552224982695 22285934255 2239292082952255881874655 22678236871895 227936531847054982290944786623495 22981883079611453 2306594477165030823146570294485276 2319728326503675 2319625493853307323186389444679765 2320035050021179 23148365450190612314328890517155 23069210014654395 22979539013188955
Vector.delta
.delta(callback)
Returns a Vector of values containing the sequential difference between numbers in a sequence.
Vector.add
.add(other, callback)
Returns a new vector which is the result of adding the input, element-wise to existing vector. Takes input of a Array of same length as the existing Vector, or a scalar.
var a = 12;var b = 34;a> 46a> 1113
Vector.subtract
.subtract(other, callback)
Returns a new vector which is the result of subtracting the input, element-wise from the existing vector. Takes input of a Array of same length as the existing Vector, or a scalar.
Vector.multiply
.multiply(other, callback)
Returns a new vector which is the result of element-wise multiplying the existing vector by the input. Takes input of a Array of same length as the existing Vector, or a scalar.
Vector.divide
.divide(other, callback)
Returns a new vector which is the result of element-wise dividing the existing vector by the input. Takes input of a Array of same length as the existing Vector, or a scalar.
Vector.extend
.extend(methods, callback)
Returns a Vector extended with named functions.
Within the function body, this
is attached to the Vector being extended and the function may take zero or more arguments.
To maintain chainability, return this
.
// Instantiate a new Vector with extensionsvar set = 14 6 9 3 187 11 1 2 2012 16 8 4 519 15 17 10 13;set> 14set> 6 9 3 187 11 1 2 2012 16 8 4 519 15 17 10 13// Extend instantiated objectsset;set>'1': 010526315789473684'2': 005263157894736842'3': 010526315789473684'4': 010526315789473684'14': 005263157894736842'15': 005263157894736842'18': 005263157894736842'-11': 005263157894736842'-10': 005263157894736842'-8': 015789473684210525'-7': 005263157894736842'-6': 005263157894736842'-4': 010526315789473684
Vector.copy
.copy(callback)
Returns a copy of the data set.
Vector.clone
.clone(callback)
Returns another instance of the Vector object and data.
Sample
By default, Vector
calculates values against the population n
. However, sample statistics functions on n - 1
are available by using the sample
modifier for the following functions:
samplemean: Functiongmean: Functionhmean: Functionqmean: Functionpmean: Functionvariance: Functionstdev: Function
Math
Vector
supports applying all the Math object methods to an entire Vector set of numbers.
For example, applying pow
primitive method on a set to calculate the quadratic mean
var squares = set; // A Vector of set's members squared> 25 1 9 4 441Math; // Sum the squares -> find average -> quadratic mean (RMS)> 9797958971132712
TimeSeries
Deprecated
Perform time series analysis. TimeSeries currently accepts time in epoch milliseconds followed by a numeric value.
var gauss = ;var set = 1315378833000 35 1315789015000 7826;
TimeSeries.times
.times(callback)
Returns a Vector of the times.
TimeSeries.values
.values(callback)
Returns a Vector of the time series values.
Using the REPL console
To experiment with Gauss or to quickly start a Node.js command-line environment for number crunching, Gauss ships with a lightweight REPL (Read–eval–print loop). Start the REPL with npm start
within the source directory, or gauss
if installed globally (via npm install -g gauss
).
For example, using the help()
function and analyzing a data file from the Gauss REPL:
$ gaussgauss>Gauss 0212/* https://github.com/wayoutmind/gauss#api */Functions: print inspect cwd clear install uninstall helpUsage:var set = 1 2 3;var times = ;version: '0.2.10'Collection: FunctionVector: FunctionTimeSeries: Functiongauss> var fs = ;gauss> var data = fs;gauss> data = data;'8''6''7''5''3''0''9'gauss> data = data;gauss> var set = data;gauss> set5428571428571429