Package Exports
- compute.io
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (compute.io) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Compute.io
Computation library.
Table of Contents
[Installation](#installation)
[Usage](#usage)
[Utilities](#utilities)
[roundn( x, n )](#roundn)
- polyval( coef, x )
- reverse( arr )
- shuffle( arr )
- circshift( x, k )
- diff( arr )
- find( arr, opts, clbk )
- dims( arr, max )
- issorted( arr, clbk )
- isnumeric( arr )
- isnan( arr )
- isfinite( arr )
- isinteger( arr )
- isinf( arr )
- zip( arr1, arr2,..., opts )
- unzip( arr, idx )
- linspace( start, stop, length )
- incrspace( start, stop, increment )
- logspace( a, b, length )
- datespace( start, stop, length, opts )
- Special Functions
[Arithmetic](#arithmetic)
[Relational Operations](#relational-operations)
[eq( arr, x, opts)](#eq)
[neq( arr, x, opts)](#neq)
- gt( arr, x )
- geq( arr, x )
- lt( arr, x )
- leq( arr, x )
- Trigonometry
- Geometry
[Sets](#sets)
[Discrete Mathematics](#discrete-mathematics)
- Linear Algebra
[l1norm( arr )](#l1norm)
[l2norm( arr )](#l2norm)
[linfnorm( arr )](#linfnorm)
[lpnorm( arr )](#lpnorm)
- dot( x, y )
- cross( x, y )
[Statistics](#statistics)
- min( arr )
- argmin( arr )
- nanmin( arr )
- argnanmin( arr )
- incrmin()
- mmin( arr, window )
- cmin( arr )
- max( arr )
- argmax( arr )
- nanmax( arr )
- argnanmax( arr )
- incrmax()
- mmax( arr, window )
- cmax( arr )
- range( arr )
- sum( arr )
- nansum( arr )
- incrsum()
- msum( arr, window )
- csum( arr )
- mean( arr )
- nanmean( arr )
- incrmean()
- mmean( arr, window )
- incrmmean( window )
- wmean( arr, weights )
- gmean( arr )
- nangmean( arr )
- hmean( arr )
- nanhmean( arr )
- qmean( arr )
- nanqmean( arr )
- variance( arr )
- nanvariance( arr )
- incrvariance()
- stdev( arr )
- nanstdev( arr )
- incrstdev()
- mode( arr )
- median( arr, sorted )
- quantile( arr, p, opts )
- quantiles( arr, num, opts )
- iqr( arr, opts )
- idr( arr, opts )
- midrange( arr, sorted )
- midhinge( arr, opts )
- midsummary( arr, n, opts )
- midmean( arr, sorted )
- lmidmean( arr, sorted )
- umidmean( arr, sorted )
- trimean( arr, opts )
- skewness( arr )
- kurtosis( arr )
- covariance( arr1, arr2,..., opts )
- pcorr( arr1, arr2,... )
[Information Theory](#information-theory)
[Fluent Interface](#fluent-interface)
[Tests](#tests)
[Unit](#unit)
- Coverage
[Contributing](#contributing)
[License](#license)
Installation
$ npm install compute.io
Usage
To use compute,
var compute = require( 'compute.io' );
The compute module is comprised of several smaller modules. If you want to roll your own compute, follow the links and import the individual modules.
The compute module has the following methods...
Utilities
compute.roundn( x, n )
Rounds values to the nearest multiple of 10^n
. x
may be either a single numeric value or an array of values. n
must be an integer
.
compute.roundn( Math.PI, -2 );
// returns 3.14
var x = compute.roundn( 111, 2 );
// returns 100
var data = [ 2.342, 4.943, 2.234, 7.992, 3.142 ];
compute.roundn( data, -2 );
// returns [...] where each value is rounded to nearest hundredth
Note: if provided an array
, the array
is mutated.
compute.polyval( coef, x )
Evaluates a polynomial with coefficients coef
, where x
may be a single numeric
value or an array
of numeric values.
var coef = [ 4, 2, 6, -17 ];
var x = compute.polyval( coef, [ 10, -3] );
compute.reverse( arr )
Reverses an array
in place.
var arr = [ 1, 2, 3, 4 ];
compute.reverse( arr );
// returns [ 4, 3, 2, 1 ]
Note: the array
is mutated.
compute.shuffle( arr )
Generates a random permutation of (shuffles) an array
in place.
var arr = [ 1, 2, 3, 4 ];
compute.shuffle( arr );
Note: the array
is mutated.
compute.circshift( x, k )
Circularly shifts elements/characters. x
may be an array
or a string
. k
is an integer
specifying the number of positions to shift. The sign of k
specifies the shift direction.
compute.circshift( [1,2,3,4,5], 2 );
// returns [4,5,1,2,3]
var str = compute.circshift( 'beepboop', -3 );
// returns 'pboopbee'
Note: if provided an array
, the array
, is mutated.
compute.diff( arr )
Calculates the differences between adjacent elements in an array
.
var arr = [ 2, 1, 3, 4 ];
var diff = compute.diff( arr );
// returns [ 1, -2, -1 ]
Note: the length of the returned array
is one less than the length of the original array
.
compute.find( arr, [opts,] clbk )
Finds array
elements which satisfy a test condition.
var arr = [ 2, 1, 3, 4 ];
var opts = {
'k': -2,
'returns': '*'
};
function condition( val ) {
return val < 4;
}
var results = compute.find( arr, opts, condition );
// returns [ [2,3], [1,1] ]
For further documentation, see the compute-find module.
compute.dims( arr[, max] )
Computes array
dimensions, including nested arrays
.
var data, d;
data = [ 1, 2 ];
d = compute.dims( data );
// returns [2]
data = [ [1,2], [1,2] ];
d = compute.dims( data );
// returns [2,2]
To limit the number of dimensions returned, set the max
option.
compute.issorted( arr[, comparator] )
Returns a boolean
indicating if an input array
is sorted.
var bool = compute.issorted( [ 2, 3, 5, 4 ] );
// returns false
By default, the method assumes ascending order. To impose an arbitrary sort order, provide a comparator
function.
function descending( a, b ) {
return b - a;
}
var bool = compute.issorted( [ 5, 4, 3, 2 ] );
// returns true
compute.isnumeric( arr )
Computes for each array
element whether an element is numeric. The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is numeric and 0
means that an element is not numeric.
var out = compute.isnumeric( [ 2, '3', 5, 4, null, NaN ] );
// returns [ 1, 0, 1, 1, 0, 0 ]
compute.isnan( arr )
Computes for each array
element whether an element is NaN
. The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is NaN
and 0
means that an element is not NaN
.
var out = compute.isnan( [ 2, '3', 5, 4, null ] );
// returns [ 0, 1, 0, 0, 1 ]
compute.isfinite( arr )
Computes for each array
element whether an element is a finite number
. The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is a finite number
and 0
means that an element is not a finite number
.
var out = compute.isfinite( [ 2, 1/0, 'beep', 5, 4, -1/0, null, NaN ] );
// returns [ 1, 0, 0, 1, 1, 0, 0, 0 ]
compute.isinteger( arr )
Computes for each array
element whether an element is an integer
. The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is an integer
and 0
means that an element is not an integer
.
var out = compute.isinteger( [ 2, 1/0, 'beep', 0, -4, 3.14, null, NaN ] );
// returns [ 1, 0, 0, 1, 1, 0, 0, 0 ]
compute.isinf( arr )
Computes for each array
element whether an element is infinite. The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is infinite and 0
means that an element is not infinite.
var out = compute.isinf( [ 2, 1/0, 'beep', 5, 4, -1/0 ] );
// returns [ 0, 1, 0, 0, 0, 1 ]
compute.zip( arr1, arr2,...[, opts] )
Returns an array
of arrays
, where the ith element (tuple) in the returned array
contains the ith elements of the input arrays
.
var zipped = compute.zip( [1,2], ['a','b'] );
// returns [ [1,'a'], [2,'b'] ]
For function options
, see the compute-zip module.
compute.unzip( arr[, idx] )
Unzips a zipped array (i.e., a nested array
of tuples).
var arr = [ [1,'a',3], [2,'b',4] ];
var out = compute.unzip( arr );
// returns [ [1,2], ['a','b'], [3,4] ];
To unzip specific tuple elements, you can provide an array
of indices as an optional second argument.
var arr = [ [1,'a',3], [2,'b',4] ];
var out = compute.unzip( arr, [0,2] );
// returns [ [1,2], [3,4] ];
compute.linspace( start, stop[, length] )
Generates a linearly spaced numeric array
. If a length
is not provided, the default output array
length is 100
.
var arr = compute.linspace( 0, 100, 6 );
// returns [ 0, 20, 40, 60, 80, 100 ]
compute.incrspace( start, stop[, increment] )
Generates a linearly spaced numeric array
. If an increment
is not provided, the default increment
is 1
.
var arr = compute.incrspace( 0, 11, 2 );
// returns [ 0, 2, 4, 6, 8, 10 ]
compute.logspace( a, b[, length] )
Generates a logarithmically spaced numeric array
. If a length
is not provided, the default output array
length is 10
.
var arr = compute.logspace( 0, 2, 6 );
// returns [ 1, ~2.5, ~6.31, ~15.85, ~39.81, 100 ]
compute.datespace( start, stop[, length, opts] )
Generates an array
of linearly spaced Date
objects. If a length
is not provided, the default output array
length is 100
.
var stop = '2014-12-02T07:00:54.973Z',
start = new Date( stop ) - 60000;
var arr = compute.datespace( start, stop, 6 );
/* returns [
'Mon Dec 01 2014 22:59:54 GMT-0800 (PST)',
'Mon Dec 01 2014 23:00:06 GMT-0800 (PST)',
'Mon Dec 01 2014 23:00:18 GMT-0800 (PST)',
'Mon Dec 01 2014 23:00:30 GMT-0800 (PST)',
'Mon Dec 01 2014 23:00:42 GMT-0800 (PST)',
'Mon Dec 01 2014 23:00:54 GMT-0800 (PST)'
]
*/
For function options
, see the compute-datespace module.
Special Functions
compute.abs( arr )
Computes an element-wise absolute value for each element of a numeric array
.
var data = [ 2, -4, 2, -7, 3 ];
compute.abs( data );
Note: mutates the input array
.
compute.sqrt( arr )
Computes an element-wise principal square root for each element of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
compute.sqrt( data );
Note: mutates the input array
.
compute.signum( x )
Evaluates the signum function, where x
may be a single numeric
value or a numeric array
.
var data = [ -10, -1, -0, 0, 1, 10 ];
var x = compute.signum( data );
compute.erf( x )
Evaluates the error function, where x
may be a single numeric
value or a numeric array
.
var data = [ -10, -1, 0, 1, 10 ];
var x = compute.erf( data );
compute.erfc( x )
Evaluates the complementary error function, where x
may be a single numeric
value or a numeric array
.
var data = [ -10, -1, 0, 1, 10 ];
var x = compute.erfc( data );
compute.erfinv( x )
Evaluates the inverse error function, where x
may be a single numeric
value or a numeric array
.
var data = [ -1, -0.5, 0, 0.5, 1 ];
var x = compute.erfinv( data );
compute.erfcinv( x )
Evaluates the inverse complementary error function, where x
may be a single numeric
value or a numeric array
.
var data = [ 0, 0.5, 1, 1.5, 2 ];
var x = compute.erfcinv( data );
Arithmetic
compute.add( arr, x )
Computes an element-wise addition of a numeric array
, where x
may be an array
of equal length or a numeric
value.
var data = [ 2, 4, 2, 7, 3 ];
compute.add( data, 5.5 );
Note: mutates the input array
.
compute.subtract( arr, x )
Computes an element-wise subtraction of a numeric array
, where x
may be an array
of equal length or a numeric
value.
var data = [ 2, 4, 2, 7, 3 ];
compute.subtract( data, 5.5 );
Note: mutates the input array
.
compute.multiply( arr, x )
Computes an element-wise multiplication of a numeric array
, where x
may be an array
of equal length or a numeric
value.
var data = [ 2, 4, 2, 7, 3 ];
compute.multiply( data, 5.5 );
Note: mutates the input array
.
compute.divide( arr, x )
Computes an element-wise division of a numeric array
, where x
may be an array
of equal length or a numeric
value.
var data = [ 2, 4, 2, 7, 3 ];
compute.divide( data, 5.5 );
Note: mutates the input array
.
Relational Operations
compute.eq( arr, x[, opts] )
Computes an element-wise comparison (equality) of an array
, where x
may either be an array
of equal length or a single value (of any type).
The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is equal to a compared value and 0
means that an element is not equal to a compared value.
var data = [ 2, 4, 2, 7, 3 ],
out;
out = compute.eq( data, 3 );
// returns [ 0, 0, 0, 0, 1 ]
out = compute.eq( data, [ 3, 4, 1, 7, 4 ] );
// returns [ 0, 1, 0, 1, 0 ]
For function options
, see compute-eq.
compute.neq( arr, x[, opts] )
Computes an element-wise comparison (not equal) of an array
, where x
may either be an array
of equal length or a single value (of any type).
The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is not equal to a compared value and 0
means that an element is equal to a compared value.
var data = [ 2, 4, 2, 7, 3 ],
out;
out = compute.neq( data, 3 );
// returns [ 1, 1, 1, 1, 0 ]
out = compute.neq( data, [ 3, 4, 1, 7, 4 ] );
// returns [ 1, 0, 1, 0, 1 ]
For function options
, see compute-neq.
compute.gt( arr, x )
Computes an element-wise comparison (greater than) of an array
, where x
may either be an array
of equal length or a single value (number
or string
).
The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is greater than a compared value and 0
means that an element is not greater than a compared value.
var data = [ 2, 4, 2, 7, 3 ],
out;
out = compute.gt( data, 3.14 );
// returns [ 0, 1, 0, 1, 0 ]
out = compute.gt( data, [3, 5, 1, 4, 4 ] );
// returns [ 0, 0, 1, 1, 0 ]
compute.geq( arr, x )
Computes an element-wise comparison (greater than or equal to) of an array
, where x
may either be an array
of equal length or a single value (number
or string
).
The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is greater than or equal to a compared value and 0
means that an element is not greater than or equal to a compared value.
var data = [ 2, 4, 2, 7, 3 ],
out;
out = compute.geq( data, 3.14 );
// returns [ 0, 1, 0, 1, 0 ]
out = compute.geq( data, [3, 5, 1, 7, 4 ] );
// returns [ 0, 0, 1, 1, 0 ]
compute.lt( arr, x )
Computes an element-wise comparison (less than) of an array
, where x
may either be an array
of equal length or a single value (number
or string
).
The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is less than a compared value and 0
means that an element is not less than a compared value.
var data = [ 2, 4, 2, 7, 3 ],
out;
out = compute.lt( data, 3.14 );
// returns [ 1, 0, 1, 0, 1 ]
out = compute.lt( data, [3, 5, 1, 4, 4 ] );
// returns [ 1, 1, 0, 0, 1 ]
compute.leq( arr, x )
Computes an element-wise comparison (less than or equal to) of an array
, where x
may either be an array
of equal length or a single value (number
or string
).
The function returns an array
with length equal to that of the input array
. Each output array
element is either 0
or 1
. A value of 1
means that an element is less than or equal to a compared value and 0
means that an element is not less than or equal to a compared value.
var data = [ 2, 4, 2, 7, 3 ],
out;
out = compute.leq( data, 3.14 );
// returns [ 1, 0, 1, 0, 1 ]
out = compute.leq( data, [3, 5, 1, 7, 4 ] );
// returns [ 1, 1, 0, 1, 1 ]
Trigonometry
compute.deg2rad( x )
Converts degrees to radians, where x
may be a single numeric
value or a numeric array
.
var val = compute.deg2rad( 90 );
// returns pi/2
var data = [ 0, 45, 90, 135, 180 ];
compute.deg2rad( data );
// returns [ 0, pi/4, pi/2, 3pi/4, pi ]
Note: mutates the input array
.
compute.rad2deg( x )
Converts radians to degrees, where x
may be a single numeric
value or a numeric array
.
var val = compute.rad2deg( Math.PI/2 );
// returns 90
var data = [ 0, Math.PI/4, Math.PI/2, 3*Math.PI/4, Math.PI ];
compute.rad2deg( data );
// returns [ 0, 45, 90, 135, 180 ]
Note: mutates the input array
.
Geometry
compute.hypot( a, b )
Computes the hypotenuse of a right triangle.
var a = 10,
b = 12;
var c = compute.hypot( a, b );
Sets
compute.unique( arr[, sorted] )
Removes duplicate values to determine the subset containing all unique values of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
compute.unique( data );
If the input array
is already sorted in ascending order, set the sorted
flag to true
.
Note: mutates the input array
.
Discrete Mathematics
compute.gcd( arr )
Computes the greatest common divisor (gcd) of two or more integers
.
var val = compute.gcd( [48, 18] );
// returns 6
If provided an empty array
, returns null
.
compute.lcm( arr )
Computes the least common multiple (lcm) of two or more integers
.
var val = compute.lcm( [21, 6] );
// returns 42
If provided an empty array
, returns null
.
Linear Algebra
compute.l1norm( arr )
Computes the L1 norm (Manhattan/Taxicab norm) of an array
of values.
var data = [ 2, 4, 2, 7, 3 ];
var norm = compute.l1norm( data );
compute.l2norm( arr )
Computes the L2 norm (Euclidean norm) of an array
of values.
var data = [ 2, 4, 2, 7, 3 ];
var norm = compute.l2norm( data );
compute.linfnorm( arr )
Computes the infinity norm (Chebyshev/maximum/supremum/uniform norm) of an array
of values.
var data = [ 2, 4, 2, -7, 3 ];
var norm = compute.linfnorm( data );
compute.lpnorm( arr[, p] )
Computes the Lp norm of an array
of values.
var data = [ 2, 4, 2, 7, 3 ];
// Compute the L5 norm:
var norm = compute.lpnorm( data, 5 );
compute.dot( x, y )
Computes the dot product between two arrays
of equal length.
var val = compute.dot( [1,2,3], [4,5,6] );
compute.cross( x, y )
Computes the cross product between two arrays
of length 3
.
var val = compute.cross( [1,2,3], [4,5,6] );
Statistics
compute.min( arr )
Computes the minimum value of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var min = compute.min( data );
// returns 2
compute.argmin( arr )
Computes the minimum value of a numeric array
and returns the corresponding array
indices.
var data = [ 2, 4, 2, 7, 3 ];
var idx = compute.argmin( data );
// returns [0,2]
compute.nanmin( arr )
Computes the minimum value of an array
ignoring non-numeric values.
var data = [ null, 2, 4, 2, null, 7, 3 ];
var min = compute.nanmin( data );
// returns 2
compute.argnanmin( arr )
Computes the minimum value of an array
ignoring non-numeric values and returns the corresponding array
indices.
var data = [ null, 2, 4, 2, null, 7, 3 ];
var idx = compute.argnanmin( data );
// returns [1,3]
compute.incrmin()
Returns a method to compute a minimum value incrementally.
var data = [ 2, 4, 2, 7, 3 ];
var min = compute.incrmin(),
m;
for ( var i = 0; i < data.length; i++ ) {
m = min( data[ i ] );
console.log( m );
}
console.log( min() );
compute.mmin( arr, window )
Computes a moving minimum over a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.mmin( data, 2 );
compute.cmin( arr )
Computes the cumulative minimum of a numeric array
.
var data = [ 7, 4, 2, 4, 3 ];
var arr = compute.cmin( data );
compute.max( arr )
Computes the maximum value of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var max = compute.max( data );
// returns 7
compute.argmax( arr )
Computes the maximum value of a numeric array
and returns the corresponding array
indices.
var data = [ 2, 4, 2, 7, 7, 3 ];
var idx = compute.argmax( data );
// returns [3,4]
compute.nanmax( arr )
Computes the maximum value of a numeric array
ignoring non-numeric values.
var data = [ -2, -4, null, -2, null, -7, -3 ];
var max = compute.nanmax( data );
// returns -2
compute.argnanmax( arr )
Computes the maximum value of an array
ignoring non-numeric values and returns the corresponding array
indices.
var data = [ null, -2, -4, -2, null, -7, -3 ];
var idx = compute.argnanmax( data );
// returns [1,3]
compute.incrmax()
Returns a method to compute a maximum value incrementally.
var data = [ 2, 4, 2, 7, 3 ];
var max = compute.incrmax(),
m;
for ( var i = 0; i < data.length; i++ ) {
m = max( data[ i ] );
console.log( m );
}
console.log( max() );
compute.mmax( arr, window )
Computes a moving maximum over a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.mmax( data, 2 );
compute.cmax( arr )
Computes the cumulative maximum of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.cmax( data );
compute.range( arr )
Computes the arithmetic range of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var range = compute.range( data );
compute.sum( arr )
Computes the sum of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var sum = compute.sum( data );
compute.nansum( arr )
Computes the sum of an array
ignoring any non-numeric values.
var data = [ 2, NaN, 4, 2, 7, NaN, 3 ];
var sum = compute.nansum( data );
compute.incrsum()
Returns a method to compute a sum incrementally.
var data = [ 2, 4, 2, 7, 3 ];
var sum = compute.incrsum(),
s;
for ( var i = 0; i < data.length; i++ ) {
s = sum( data[ i ] );
console.log( s );
}
console.log( sum() );
compute.msum( arr, window )
Computes a moving sum over a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.msum( data, 2 );
compute.csum( arr )
Computes the cumulative sum of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.csum( data );
compute.mean( arr )
Computes the arithmetic mean of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var mean = compute.mean( data );
compute.nanmean( arr )
Computes the arithmetic mean over an array
of values ignoring any non-numeric values.
var data = [ 2, 4, NaN, 2, 7, NaN, 3 ];
var mean = compute.nanmean( data );
compute.incrmean()
Returns a method to compute an arithmetic mean incrementally.
var data = [ 2, 4, 2, 7, 3 ];
var mean = compute.incrmean(),
mu;
for ( var i = 0; i < data.length; i++ ) {
mu = mean( data[ i ] );
console.log( mu );
}
console.log( mean() );
compute.mmean( arr, window )
Computes a moving arithmetic mean (sliding window average) over a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.mmean( data, 2 );
compute.incrmmean( window )
Returns a method to compute a moving arithmetic mean incrementally. window
sets the window size, i.e., the number of values over which to compute a moving mean.
var data = [ 2, 4, 2, 7, 3 ];
var mmean = compute.incrmmean( 3 ),
mu;
for ( var i = 0; i < data.length; i++ ) {
mu = mmean( data[ i ] );
console.log( mu );
}
console.log( mmean() );
compute.wmean( arr, weights )
Computes a weighted mean of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ],
weights = [ 1, 2, 1, 4, 0 ];
var wmean = compute.wmean( data, weights );
compute.gmean( arr )
Computes the geometric mean of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var gmean = compute.gmean( data );
compute.nangmean( arr )
Computes the geometric mean over an array
of values ignoring any non-numeric values.
var data = [ 2, 4, NaN, 2, 7, NaN, 3 ];
var gmean = compute.nangmean( data );
compute.hmean( arr )
Computes the harmonic mean of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var hmean = compute.hmean( data );
compute.nanhmean( arr )
Computes the harmonic mean over an array
of values ignoring any non-numeric values.
var data = [ 2, 4, NaN, 2, 7, NaN, 3 ];
var hmean = compute.nanhmean( data );
compute.qmean( arr )
Computes the quadratic mean (root mean square) of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var qmean = compute.qmean( data );
compute.nanqmean( arr )
Computes the quadratic mean (root mean square) over an array
of values ignoring any non-numeric values.
var data = [ 2, 4, NaN, 2, 7, NaN, 3 ];
var qmean = compute.nanqmean( data );
compute.variance( arr )
Computes the sample variance over a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var s2 = compute.variance( data );
compute.nanvariance( arr )
Computes the sample variance over an array
of values ignoring any non-numeric values.
var data = [ 2, 4, NaN, 2, 7, NaN, 3 ];
var s2 = compute.nanvariance( data );
compute.incrvariance()
Returns a method to compute a sample variance incrementally.
var data = [ 2, 4, 2, 7, 3 ];
var variance = compute.incrvariance(),
s2;
for ( var i = 0; i < data.length; i++ ) {
s2 = variance( data[ i ] );
console.log( s2 );
}
console.log( variance() );
compute.stdev( arr )
Computes the sample standard deviation of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var stdev = compute.stdev( data );
compute.nanstdev( arr )
Computes the sample standard deviation over an array
of values ignoring any non-numeric values.
var data = [ 2, 4, NaN, 2, 7, NaN, 3 ];
var stdev = compute.nanstdev( data );
compute.incrstdev()
Returns a method to compute a sample standard deviation incrementally.
var data = [ 2, 4, 2, 7, 3 ];
var stdev = compute.incrstdev(),
sigma;
for ( var i = 0; i < data.length; i++ ) {
sigma = stdev( data[ i ] );
console.log( sigma );
}
console.log( stdev() );
compute.mode( arr )
Computes the mode of an array
.
var data = [ 2, 4, 2, 7, 3 ];
var mode = compute.mode( data );
compute.median( arr[, sorted] )
Computes the median of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var median = compute.median( data );
If the input array
is already sorted in ascending order, set the sorted
flag to true
.
compute.quantile( arr, p[, opts] )
Computes a quantile for a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var q = compute.quantile( data, 0.25 );
If the input array
is already sorted in ascending order, set the sorted
option to true
.
var opts = {
'sorted': true
};
var data = [ 2, 2, 3, 4, 7 ];
var q = compute.quantile( data, 0.25, opts );
compute.quantiles( arr, num[, opts] )
Computes quantiles for a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var arr = compute.quantiles( data, 3 );
If the input array
is already sorted in ascending order, set the sorted
option to true
.
var opts = {
'sorted': true
};
var data = [ 2, 2, 3, 4, 7 ];
var arr = compute.quantiles( data, 2, opts );
compute.iqr( arr[, opts] )
Computes the interquartile range of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var iqr = compute.iqr( data );
If the input array
is already sorted in ascending order, set the sorted
options flag to true
.
compute.idr( arr[, opts] )
Computes the interdecile range of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var idr = compute.idr( data );
If the input array
is already sorted in ascending order, set the sorted
options flag to true
.
compute.midrange( arr[, sorted] )
Computes the mid-range (mid-extreme) of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var mr = compute.midrange( data );
If the input array
is already sorted in ascending order, set the sorted
flag to true
.
compute.midhinge( arr[, opts] )
Computes the midhinge of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var mh = compute.midhinge( data );
If the input array
is already sorted in ascending order, set the sorted
options flag to true
.
compute.midsummary( arr, n[, opts] )
Computes the n% midsummary of a numeric array
. n
exists on the interval [0.0, 0.50]
and specifies the proportion of values to discard in the distribution tails.
var data = [ 2, 4, 2, 7, 3 ];
var ms = compute.midsummary( data );
If the input array
is already sorted in ascending order, set the sorted
options flag to true
.
compute.midmean( arr[, sorted] )
Computes the midmean of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var mm = compute.midmean( data );
If the input array
is already sorted in ascending order, set the sorted
flag to true
.
compute.lmidmean( arr[, sorted] )
Computes the lower midmean of a numeric array
.
var data = [ 2, 4, 2, 7, 3, 7, 5 ];
var lmm = compute.lmidmean( data );
If the input array
is already sorted in ascending order, set the sorted
flag to true
.
compute.umidmean( arr[, sorted] )
Computes the upper midmean of a numeric array
.
var data = [ 2, 4, 2, 7, 3, 7, 5 ];
var umm = compute.umidmean( data );
If the input array
is already sorted in ascending order, set the sorted
flag to true
.
compute.trimean( arr[, opts] )
Computes the trimean of a numeric array
.
var data = [ 2, 4, 2, 7, 3 ];
var trimean = compute.trimean( data );
If the input array
is already sorted in ascending order, set the sorted
options flag to true
.
compute.skewness( arr )
Computes the sample skewness of an array
of values.
var data = [ 2, 4, 2, 7, 3 ];
var skew = compute.skewness( data );
compute.kurtosis( arr )
Computes the sample excess kurtosis of an array
of values.
var data = [ 2, 4, 2, 7, 3 ];
var kur = compute.kurtosis( data );
compute.covariance( arr1[, arr2,...,opts] )
Computes the covariance between one or more numeric arrays.
var x = [ 1, 2, 3, 4, 5 ],
y = [ 5, 4, 3, 2, 1 ];
var mat = compute.covariance( x, y );
// returns [[2.5,-2.5],[-2.5,2.5]]
For method options, see compute-covariance.
compute.pcorr( arr1[, arr2,...] )
Computes a correlation matrix for one or more numeric arrays.
var x = [ 1, 2, 3, 4, 5 ],
y = [ 5, 4, 3, 2, 1 ];
var mat = compute.pcorr( x, y );
// returns [[1,-1],[-1,1]]
Information Theory
compute.hamdist( a, b )
Computes the Hamming distance between two sequences of equal length.
var a = 'beep',
b = 'boop';
var dist = compute.hamdist( a, b );
var c = [ 4, 2, 3, 4 ],
d = [ 2, 4, 3, 1 ];
var dist = compute.hamdist( c, d );
Fluent Interface
For data pipelines, invoking serial methods can become verbose.
data = compute.roundn( data, -3 );
data = compute.mean( data );
data = compute.roundn( data, 0 );
...
Fluent interfaces can help alleviate this problem. Such interfaces have been popularized by libraries such as jQuery and D3 which utilize method chaining.
To create a fluent interface,
var flow = compute.flow();
A flow
pipeline should be initialized.
flow.value( data );
Once initialized, all compute methods are now available. The lone difference is that data should not be explicitly passed as an argument. For example,
flow
.value( data )
.roundn( -3 )
.mean()
.roundn( 0 );
To return the flow value
,
var mean = flow.value();
To help understand the transformations comprising a data pipeline, flow
exposes an inspect()
method, which logs the current value
to the console while maintaining the fluent interface.
flow.inspect();
The above flow
can be modified accordingly,
flow
.value( data )
.inspect()
.roundn( -3 )
.inspect()
.mean()
.inspect()
.roundn( 0 )
.inspect();
To summarize the flow
API...
flow.value( [value] )
This method is a setter/getter. If no value
is provided, returns the current flow value
. If a value
is provided, sets the flow value
.
flow.value( [ 4, 3, 6, 2 ] );
flow.inspect()
Logs the current flow value
to the console, while maintaining the fluent interface.
flow.inspect();
Notes
When creating flows, ensure that the output from one computation matches the input argument requirements for the next computation.
For large datasets, rather than loading datasets into memory, consider using file streams and utilize stream tools such as [Flow.io](https://github.com/flow-io/flow.io).
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Contributing
To contribute to compute.io, see the contributing guide.
License
Copyright
Copyright © 2014. Athan Reines.