diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8e74bf3
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.md]
+trim_trailing_whitespace = false
\ No newline at end of file
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.jshintignore b/.jshintignore
new file mode 100644
index 0000000..3163c22
--- /dev/null
+++ b/.jshintignore
@@ -0,0 +1,14 @@
+
+# Directories #
+###############
+build/
+reports/
+dist/
+
+# Node.js #
+###########
+/node_modules/
+
+# Git #
+#######
+.git*
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..d09f1fa
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,71 @@
+{
+ "bitwise": false,
+ "camelcase": false,
+ "curly": true,
+ "eqeqeq": true,
+ "es3": false,
+ "forin": true,
+ "freeze": true,
+ "immed": true,
+ "indent": 4,
+ "latedef": "nofunc",
+ "newcap": true,
+ "noarg": true,
+ "noempty": false,
+ "nonbsp": true,
+ "nonew": true,
+ "plusplus": false,
+ "quotmark": "single",
+ "undef": true,
+ "unused": true,
+ "strict": true,
+ "maxparams": 10,
+ "maxdepth": 5,
+ "maxstatements": 100,
+ "maxcomplexity": false,
+ "maxlen": 1000,
+ "asi": false,
+ "boss": false,
+ "debug": false,
+ "eqnull": false,
+ "esnext": false,
+ "evil": false,
+ "expr": false,
+ "funcscope": false,
+ "globalstrict": false,
+ "iterator": false,
+ "lastsemic": false,
+ "laxbreak": false,
+ "laxcomma": false,
+ "loopfunc": false,
+ "maxerr": 1000,
+ "moz": false,
+ "multistr": false,
+ "notypeof": false,
+ "proto": false,
+ "scripturl": false,
+ "shadow": false,
+ "sub": true,
+ "supernew": false,
+ "validthis": false,
+ "noyield": false,
+ "browser": true,
+ "browserify": true,
+ "couch": false,
+ "devel": true,
+ "dojo": false,
+ "jasmine": false,
+ "jquery": false,
+ "mocha": true,
+ "mootools": false,
+ "node": true,
+ "nonstandard": false,
+ "prototypejs": false,
+ "qunit": false,
+ "rhino": false,
+ "shelljs": false,
+ "worker": false,
+ "wsh": false,
+ "yui": false,
+ "globals": {}
+}
\ No newline at end of file
diff --git a/.npmignore b/.npmignore
index 42781b3..9db298d 100644
--- a/.npmignore
+++ b/.npmignore
@@ -13,6 +13,7 @@ examples/
reports/
support/
test/
+benchmark/
# Node.js #
###########
@@ -46,4 +47,6 @@ Desktop.ini
# Utilities #
#############
.jshintrc
-.travis.yml
\ No newline at end of file
+.jshintignore
+.travis.yml
+.editorconfig
diff --git a/.travis.yml b/.travis.yml
index 7c16620..29cff27 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,12 @@
language: node_js
node_js:
- - "0.10"
+ - '0.12'
+ - '0.11'
+ - '0.10'
+ - '0.8'
+ - 'iojs'
+before_install:
+ - npm update -g npm
after_script:
- - npm run coveralls
\ No newline at end of file
+ - npm run coveralls
+
diff --git a/LICENSE b/LICENSE
index 2fe3939..9f32cbf 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)
-Copyright (c) 2014 Athan Reines.
+Copyright (c) 2015 The Compute.io Authors. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
\ No newline at end of file
+SOFTWARE.
diff --git a/Makefile b/Makefile
index e3cad30..c6f009c 100644
--- a/Makefile
+++ b/Makefile
@@ -5,25 +5,29 @@
# Set the node.js environment to test:
NODE_ENV ?= test
+# Kernel name:
+KERNEL ?= $(shell uname -s)
+
+ifeq ($(KERNEL), Darwin)
+ OPEN ?= open
+else
+ OPEN ?= xdg-open
+endif
# NOTES #
-NOTES ?= 'TODO|FIXME'
+NOTES ?= 'TODO|FIXME|WARNING|HACK|NOTE'
# MOCHA #
-# Specify the test framework bin locations:
MOCHA ?= ./node_modules/.bin/mocha
_MOCHA ?= ./node_modules/.bin/_mocha
-
-# Specify the mocha reporter:
MOCHA_REPORTER ?= spec
# ISTANBUL #
-# Istanbul configuration:
ISTANBUL ?= ./node_modules/.bin/istanbul
ISTANBUL_OUT ?= ./reports/coverage
ISTANBUL_REPORT ?= lcov
@@ -31,6 +35,12 @@ ISTANBUL_LCOV_INFO_PATH ?= $(ISTANBUL_OUT)/lcov.info
ISTANBUL_HTML_REPORT_PATH ?= $(ISTANBUL_OUT)/lcov-report/index.html
+# JSHINT #
+
+JSHINT ?= ./node_modules/.bin/jshint
+JSHINT_REPORTER ?= ./node_modules/jshint-stylish
+
+
# FILES #
@@ -81,7 +91,8 @@ test-istanbul-mocha: node_modules
NODE_ENV=$(NODE_ENV) \
NODE_PATH=$(NODE_PATH_TEST) \
$(ISTANBUL) cover \
- --dir $(ISTANBUL_OUT) --report $(ISTANBUL_REPORT) \
+ --dir $(ISTANBUL_OUT) \
+ --report $(ISTANBUL_REPORT) \
$(_MOCHA) -- \
--reporter $(MOCHA_REPORTER) \
$(TESTS)
@@ -95,9 +106,20 @@ test-istanbul-mocha: node_modules
view-cov: view-istanbul-report
view-istanbul-report:
- open $(ISTANBUL_HTML_REPORT_PATH)
+ $(OPEN) $(ISTANBUL_HTML_REPORT_PATH)
+# LINT #
+
+.PHONY: lint lint-jshint
+
+lint: lint-jshint
+
+lint-jshint: node_modules
+ $(JSHINT) \
+ --reporter $(JSHINT_REPORTER) \
+ ./
+
# NODE #
@@ -116,7 +138,6 @@ clean-node:
# CLEAN #
-
.PHONY: clean
clean:
diff --git a/README.md b/README.md
index bc90d79..43ca075 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,23 @@ stdev
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]
-> Computes the sample standard deviation over an array of values.
+> Computes the sample [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation).
+
+The biased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is defined as
+
+
+

+
+
+
+and the unbiased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is defined as
+
+
+

+
+
+
+where `x_0, x_1,...,x_{N-1}` are individual data values and `N` is the total number of values in the data set. It is the square root of the [sample variance](http://en.wikipedia.org/wiki/Variance).
## Installation
@@ -16,23 +32,208 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
## Usage
-To use the module,
-
``` javascript
var stdev = require( 'compute-stdev' );
```
+### stdev( x[, opts] )
+
+Computes the [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation). `x` may be either an [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays), or [`matrix`](https://github.com/dstructs/matrix).
+
+For numeric `arrays`,
+
+``` javascript
+var data = [ 1, 4, 7 ];
+
+var s = stdev( data );
+// returns 3
+
+data = new Int8Array( data );
+s = stdev( data );
+// returns 3
+```
+
+For non-numeric `arrays`, provide an accessor `function` for accessing numeric `array` values
+
+``` javascript
+var data = [
+ {'x':1},
+ {'x':4},
+ {'x':7}
+];
+
+function getValue( d ) {
+ return d.x;
+}
+
+var s = stdev( data, {
+ 'accessor': getValue
+});
+// returns 2
+```
+
+By default, the function calculates the *unbiased* sample [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation). To calculate the population standard deviation (or a *biased* sample standard deviation), set the `bias` option to `true`.
+
+``` javascript
+var data = [ 1, 4, 7 ];
+
+var value = stdev( data, {
+ 'bias': true
+});
+// returns 1.3333
+```
+
+If provided a [`matrix`](https://github.com/dstructs/matrix), the function accepts the following additional `options`:
+
+* __dim__: dimension along which to compute the [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation). Default: `2` (along the columns).
+* __dtype__: output [`matrix`](https://github.com/dstructs/matrix) data type. Default: `float64`.
+
+By default, the function computes the [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) along the columns (`dim=2`).
+
+``` javascript
+var matrix = require( 'dstructs-matrix' ),
+ data,
+ mat,
+ s,
+ i;
+
+data = new Int8Array( 25 );
+for ( i = 0; i < data.length; i++ ) {
+ data[ i ] = i;
+}
+mat = matrix( data, [5,5], 'int8' );
+/*
+ [ 0 1 2 3 4
+ 5 6 7 8 9
+ 10 11 12 13 14
+ 15 16 17 18 19
+ 20 21 22 23 24 ]
+*/
+
+s = stdev( mat );
+/*
+ [ 1.581139
+ 1.581139
+ 1.581139
+ 1.581139
+ 1.581139 ]
+*/
+```
+
+To compute the [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) along the rows, set the `dim` option to `1`.
+
+``` javascript
+s = stdev( mat, {
+ 'dim': 1
+});
+/*
+ [ 7.905694, 7.905694, 7.905694, 7.905694, 7.905694 ]
+*/
+```
+
+By default, the output [`matrix`](https://github.com/dstructs/matrix) data type is `float64`. To specify a different output data type, set the `dtype` option.
+
+``` javascript
+s = stdev( mat, {
+ 'dim': 1,
+ 'dtype': 'uint8'
+});
+/*
+ [ 7, 7, 7, 7, 7 ]
+*/
+
+var dtype = s.dtype;
+// returns 'uint8'
+```
+
+If provided a [`matrix`](https://github.com/dstructs/matrix) having either dimension equal to `1`, the function treats the [`matrix`](https://github.com/dstructs/matrix) as a [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) and returns a `numeric` value.
+
+``` javascript
+data = [ 1, 4, 7 ];
+
+// Row vector:
+mat = matrix( new Int8Array( data ), [1,3], 'int8' );
+s = stdev( mat );
+// returns 2
+
+// Column vector:
+mat = matrix( new Int8Array( data ), [3,1], 'int8' );
+s = stdev( mat );
+// returns 2
+```
+
+If provided an empty [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays), or [`matrix`](https://github.com/dstructs/matrix), the function returns `null`.
+
+``` javascript
+s = stdev( [] );
+// returns null
+
+s = stdev( new Int8Array( [] ) );
+// returns null
+
+s = stdev( matrix( [0,0] ) );
+// returns null
+
+s = stdev( matrix( [0,10] ) );
+// returns null
+
+s = stdev( matrix( [10,0] ) );
+// returns null
+```
## Examples
``` javascript
-var data = new Array( 1000 );
+var matrix = require( 'dstructs-matrix' ),
+ stdev = require( 'compute-stdev' );
+var data,
+ mat,
+ s,
+ i;
+
+// Plain arrays...
+var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
- data[ i ] = Math.random()*100;
+ data[ i ] = Math.round( Math.random() * 10 + 1 );
}
+s = stdev( data );
-console.log( stdev( data ) );
+// Object arrays (accessors)...
+function getValue( d ) {
+ return d.x;
+}
+for ( i = 0; i < data.length; i++ ) {
+ data[ i ] = {
+ 'x': data[ i ]
+ };
+}
+s = stdev( data, {
+ 'accessor': getValue
+});
+
+// Typed arrays...
+data = new Int32Array( 100 );
+for ( i = 0; i < data.length; i++ ) {
+ data[ i ] = Math.round( Math.random() * 10 + 1 );
+}
+s = stdev( data );
+
+// Matrices (along rows)...
+mat = matrix( data, [10,10], 'int32' );
+s = stdev( mat, {
+ 'dim': 1
+});
+
+// Matrices (along columns)...
+s = stdev( mat, {
+ 'dim': 2
+});
+
+// Matrices (custom output data type)...
+s = stdev( mat, {
+ 'dtype': 'uint8'
+});
```
To run the example code from the top-level application directory,
@@ -46,7 +247,7 @@ $ node ./examples/index.js
### Unit
-Unit tests use the [Mocha](http://visionmedia.github.io/mocha) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
+Unit tests use the [Mocha](http://mochajs.org) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
``` bash
$ make test
@@ -70,16 +271,15 @@ $ open reports/coverage/lcov-report/index.html
```
+---
## License
-[MIT license](http://opensource.org/licenses/MIT).
+[MIT license](http://opensource.org/licenses/MIT).
----
## Copyright
-Copyright © 2014. Athan Reines.
-
+Copyright © 2014-2015. The [Compute.io](https://github.com/compute-io) Authors.
[npm-image]: http://img.shields.io/npm/v/compute-stdev.svg
[npm-url]: https://npmjs.org/package/compute-stdev
@@ -97,4 +297,4 @@ Copyright © 2014. Athan Reines.
[dev-dependencies-url]: https://david-dm.org/dev/compute-io/stdev
[github-issues-image]: http://img.shields.io/github/issues/compute-io/stdev.svg
-[github-issues-url]: https://github.com/compute-io/stdev/issues
\ No newline at end of file
+[github-issues-url]: https://github.com/compute-io/stdev/issues
diff --git a/docs/img/eqn1.svg b/docs/img/eqn1.svg
new file mode 100644
index 0000000..3822f81
--- /dev/null
+++ b/docs/img/eqn1.svg
@@ -0,0 +1,56 @@
+
+
+
+
+
diff --git a/docs/img/eqn2.svg b/docs/img/eqn2.svg
new file mode 100644
index 0000000..d2c62a1
--- /dev/null
+++ b/docs/img/eqn2.svg
@@ -0,0 +1,54 @@
+
+
+
+
+
diff --git a/examples/index.js b/examples/index.js
index 9d16332..7b4a64d 100644
--- a/examples/index.js
+++ b/examples/index.js
@@ -1,9 +1,68 @@
-var stdev = require( './../lib' );
+'use strict';
-var data = new Array( 1000 );
+var matrix = require( 'dstructs-matrix' ),
+ stdev = require( './../lib' );
+var data,
+ mat,
+ s,
+ i;
+
+// ----
+// Plain arrays...
+var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
- data[ i ] = Math.random()*100;
+ data[ i ] = Math.round( Math.random() * 10 + 1 );
+}
+s = stdev( data );
+console.log( 'Arrays: %d\n', s );
+
+
+// ----
+// Object arrays (accessors)...
+function getValue( d ) {
+ return d.x;
+}
+for ( i = 0; i < data.length; i++ ) {
+ data[ i ] = {
+ 'x': data[ i ]
+ };
+}
+s = stdev( data, {
+ 'accessor': getValue
+});
+console.log( 'Accessors: %d\n', s );
+
+
+// ----
+// Typed arrays...
+data = new Int32Array( 100 );
+for ( i = 0; i < data.length; i++ ) {
+ data[ i ] = Math.round( Math.random() * 10 + 1 );
}
+s = stdev( data );
+console.log( 'Typed arrays: %d\n', s );
+
+// ----
+// Matrices (along rows)...
+mat = matrix( data, [10,10], 'int32' );
+s = stdev( mat, {
+ 'dim': 1
+});
+console.log( 'Matrix (rows): %s\n', s.toString() );
+
+
+// ----
+// Matrices (along columns)...
+s = stdev( mat, {
+ 'dim': 2
+});
+console.log( 'Matrix (columns): %s\n', s.toString() );
+
-console.log( stdev( data ) );
\ No newline at end of file
+// ----
+// Matrices (custom output data type)...
+s = stdev( mat, {
+ 'dtype': 'uint8'
+});
+console.log( 'Matrix (%s): %s\n', s.dtype, s.toString() );
diff --git a/lib/accessor.js b/lib/accessor.js
new file mode 100644
index 0000000..21f7494
--- /dev/null
+++ b/lib/accessor.js
@@ -0,0 +1,40 @@
+'use strict';
+
+/**
+* FUNCTION: stdev( arr, clbk[, bias] )
+* Computes the standard deviation of an array using an accessor.
+*
+* @param {Number[]|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} arr - input array
+* @param {Function} accessor - accessor function for accessing array values
+* @param {Boolean} [bias=false] - boolean indicating whether to calculate a biased or unbiased estimate of the standard deviation
+* @returns {Number|Null} standard deviation or null
+*/
+function stdev( arr, clbk, bias ) {
+ var len = arr.length,
+ delta = 0,
+ mean = 0,
+ M2 = 0,
+ x, i;
+
+ if ( !len ) {
+ return null;
+ }
+ if ( len < 2 ) {
+ return 0;
+ }
+ for ( i = 0; i < len; i++ ) {
+ x = clbk( arr[ i ], i );
+ delta = x - mean;
+ mean += delta / (i+1);
+ M2 += delta * ( x - mean );
+ }
+ if ( bias ) {
+ return Math.sqrt( M2 / ( i ) );
+ }
+ return Math.sqrt( M2 / ( i - 1 ) );
+} // end FUNCTION stdev()
+
+
+// EXPORTS //
+
+module.exports = stdev;
diff --git a/lib/array.js b/lib/array.js
new file mode 100644
index 0000000..ee69607
--- /dev/null
+++ b/lib/array.js
@@ -0,0 +1,39 @@
+'use strict';
+
+/**
+* FUNCTION: stdev( arr[, bias] )
+* Computes the standard deviation of an array.
+*
+* @param {Number[]|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} arr - input array
+* @param {Boolean} [bias=false] - boolean indicating whether to calculate a biased or unbiased estimate of the standard deviation
+* @returns {Number|Null} standard deviation or null
+*/
+function stdev( arr, bias ) {
+ var len = arr.length,
+ delta = 0,
+ mean = 0,
+ M2 = 0,
+ x, i;
+
+ if ( !len ) {
+ return null;
+ }
+ if ( len < 2 ) {
+ return 0;
+ }
+ for ( i = 0; i < len; i++ ) {
+ x = arr[ i ];
+ delta = x - mean;
+ mean += delta / (i+1);
+ M2 += delta * ( x - mean );
+ }
+ if ( bias ) {
+ return Math.sqrt( M2 / ( i ) );
+ }
+ return Math.sqrt( M2 / ( i - 1 ) );
+} // end FUNCTION stdev()
+
+
+// EXPORTS //
+
+module.exports = stdev;
diff --git a/lib/index.js b/lib/index.js
index bb3ed14..cea2c82 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,66 +1,94 @@
-/**
-*
-* COMPUTE: stdev
-*
-*
-* DESCRIPTION:
-* - Computes the sample standard deviation over an array of values.
-*
-*
-* NOTES:
-* [1]
-*
-*
-* TODO:
-* [1]
-*
-*
-* LICENSE:
-* MIT
-*
-* Copyright (c) 2014. Athan Reines.
-*
-*
-* AUTHOR:
-* Athan Reines. kgryte@gmail.com. 2014.
+'use strict';
+
+// MODULES //
+
+var isArrayLike = require( 'validate.io-array-like' ),
+ isMatrixLike = require( 'validate.io-matrix-like' ),
+ ctors = require( 'compute-array-constructors' ),
+ matrix = require( 'dstructs-matrix' ).raw,
+ validate = require( './validate.js' );
+
+// FUNCTIONS //
+
+var stdev1 = require( './array.js' ),
+ stdev2 = require( './accessor.js' ),
+ stdev3 = require( './matrix.js' );
+
+
+// STDEV //
+
+/*
+* FUNCTION: stdev( x[, options] )
+* Computes the standard deviation of elements in x.
*
+* @param {Number[]|Array|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Matrix} x - input value
+* @param {Object} [opts] - function options
+* @param {Boolean} [opts.bias=false] - - boolean indicating whether to calculate a biased or unbiased estimate of the standard deviation
+* @param {Function} [opts.accessor] - accessor function for accessing array values
+* @param {Number} [opts.dim=2] - dimension along which to compute the standard deviation
+* @param {String} [opts.dtype="float64"] - output data type
+* @returns {Number|Matrix|Null} standard deviation(s) or null
*/
+function stdev( x, options ) {
+ /* jshint newcap:false */
+ var opts = {},
+ bias,
+ shape,
+ ctor,
+ err,
+ len,
+ dim,
+ dt,
+ d,
+ m;
-(function() {
- 'use strict';
-
- /**
- * FUNCTION: stdev( arr )
- * Computes the sample standard deviation over an array of values.
- *
- * @param {Array} arr - array of values
- * @returns {Number} sample standard deviation
- */
- function stdev( arr ) {
- if ( !Array.isArray( arr ) ) {
- throw new TypeError( 'stdev()::invalid input argument. Must provide an array.' );
+ if ( arguments.length > 1 ) {
+ err = validate( opts, options );
+ if ( err ) {
+ throw err;
}
- var len = arr.length,
- N = 0,
- mean = 0,
- M2 = 0,
- delta = 0;
+ }
- if ( len < 2 ) {
- return 0;
+ bias = opts.bias || false;
+
+ if ( isMatrixLike( x ) ) {
+ dt = opts.dtype || 'float64';
+ dim = opts.dim;
+
+ // Determine if provided a vector...
+ if ( x.shape[ 0 ] === 1 || x.shape[ 1 ] === 1 ) {
+ // Treat as an array-like object:
+ return stdev1( x.data );
}
- for ( var i = 0; i < len; i++ ) {
- N += 1;
- delta = arr[ i ] - mean;
- mean += delta / N;
- M2 += delta * ( arr[i] - mean );
+ if ( dim > 2 ) {
+ throw new RangeError( 'stdev()::invalid option. Dimension option exceeds number of matrix dimensions. Option: `' + dim + '`.' );
}
- return Math.sqrt( M2 / ( N-1 ) );
- } // end FUNCTION stdev()
-
+ if ( dim === void 0 || dim === 2 ) {
+ len = x.shape[ 0 ];
+ shape = [ len, 1 ];
+ } else {
+ len = x.shape[ 1 ];
+ shape = [ 1, len ];
+ }
+ ctor = ctors( dt );
+ if ( ctor === null ) {
+ throw new Error( 'stdev()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + dt + '`.' );
+ }
+ // Create an output matrix and calculate the variance(s):
+ d = new ctor( len );
+ m = matrix( d, shape, dt );
+ return stdev3( m, x, bias, dim );
+ }
+ if ( isArrayLike( x ) ) {
+ if ( opts.accessor ) {
+ return stdev2( x, opts.accessor, bias );
+ }
+ return stdev1( x, bias );
+ }
+ throw new TypeError( 'stdev()::invalid input argument. First argument must be either an array or a matrix. Value: `' + x + '`.' );
+} // end FUNCTION stdev()
- // EXPORTS //
- module.exports = stdev;
+// EXPORTS //
-})();
\ No newline at end of file
+module.exports = stdev;
diff --git a/lib/matrix.js b/lib/matrix.js
new file mode 100644
index 0000000..af621db
--- /dev/null
+++ b/lib/matrix.js
@@ -0,0 +1,62 @@
+'use strict';
+
+/**
+* FUNCTION: stdev( out, mat,[bias , dim] )
+* Computes the standard deviation along a matrix dimension
+*
+* @param {Matrix} out - output matrix
+* @param {Matrix} mat - input matrix
+* @param {Boolean} [bias=false] - boolean indicating whether to calculate a biased or unbiased estimate of the standard deviation
+* @param {Number} [dim=2] - matrix dimension along which to compute the standard deviation. If `dim=1`, compute along matrix rows. If `dim=2`, compute along matrix columns.
+* @returns {Matrix|Null} standard deviation(s) or null
+*/
+function stdev( out, mat, bias, dim ) {
+ var mu, delta, x, M2,
+ M, N,
+ s0, s1,
+ o,
+ i, j, k;
+
+ if ( dim === 1 ) {
+ // Compute along the rows...
+ M = mat.shape[ 1 ];
+ N = mat.shape[ 0 ];
+ s0 = mat.strides[ 1 ];
+ s1 = mat.strides[ 0 ];
+ } else {
+ // Compute along the columns...
+ M = mat.shape[ 0 ];
+ N = mat.shape[ 1 ];
+ s0 = mat.strides[ 0 ];
+ s1 = mat.strides[ 1 ];
+ }
+ if ( M === 0 || N === 0 ) {
+ return null;
+ }
+ o = mat.offset;
+
+ for ( i = 0; i < M; i++ ) {
+ k = o + i * s0;
+ mu = 0;
+ delta = 0;
+ M2 = 0;
+ for ( j = 0; j < N; j++ ) {
+ x = mat.data[ k + j*s1 ];
+ delta = x - mu;
+ mu += delta / (j+1);
+ M2 += delta * ( x - mu );
+ }
+ if ( bias ) {
+ out.data[ i ] = Math.sqrt( M2 / ( N ) );
+ } else {
+ out.data[ i ] = Math.sqrt( M2 / ( N - 1 ) );
+ }
+ }
+
+ return out;
+} // end FUNCTION stdev()
+
+
+// EXPORTS //
+
+module.exports = stdev;
diff --git a/lib/validate.js b/lib/validate.js
new file mode 100644
index 0000000..91b25aa
--- /dev/null
+++ b/lib/validate.js
@@ -0,0 +1,60 @@
+'use strict';
+
+// MODULES //
+
+var isObject = require( 'validate.io-object' ),
+ isFunction = require( 'validate.io-function' ),
+ isString = require( 'validate.io-string-primitive' ),
+ isBoolean = require( 'validate.io-boolean-primitive'),
+ isPositiveInteger = require( 'validate.io-positive-integer' );
+
+
+// VALIDATE //
+
+/**
+* FUNCTION: validate( opts, options )
+* Validates function options.
+*
+* @param {Object} opts - destination for validated options
+* @param {Object} options - function options
+* @param {Function} [options.accessor] - accessor function for accessing array values
+* @param {Number} [options.dim] - dimension
+* @param {String} [options.dtype] - output data type
+* @param {Boolean} [options.bias=false] - boolean indicating whether to calculate a biased or unbiased estimate of the standard deviation
+* @returns {Null|Error} null or an error
+*/
+function validate( opts, options ) {
+ if ( !isObject( options ) ) {
+ return new TypeError( 'stdev()::invalid input argument. Options argument must be an object. Value: `' + options + '`.' );
+ }
+ if ( options.hasOwnProperty( 'accessor' ) ) {
+ opts.accessor = options.accessor;
+ if ( !isFunction( opts.accessor ) ) {
+ return new TypeError( 'stdev()::invalid option. Accessor must be a function. Option: `' + opts.accessor + '`.' );
+ }
+ }
+ if ( options.hasOwnProperty( 'dim' ) ) {
+ opts.dim = options.dim;
+ if ( !isPositiveInteger( opts.dim ) ) {
+ return new TypeError( 'stdev()::invalid option. Dimension option must be a positive integer. Option: `' + opts.dim + '`.' );
+ }
+ }
+ if ( options.hasOwnProperty( 'dtype' ) ) {
+ opts.dtype = options.dtype;
+ if ( !isString( opts.dtype ) ) {
+ return new TypeError( 'stdev()::invalid option. Data type option must be a string primitive. Option: `' + opts.dtype + '`.' );
+ }
+ }
+ if ( options.hasOwnProperty( 'bias' ) ) {
+ opts.bias = options.bias;
+ if ( !isBoolean( opts.bias ) ) {
+ return new TypeError( 'stdev()::invalid option. Bias option must be a Boolean primitive. Option: `' + opts.bias + '`.' );
+ }
+ }
+ return null;
+} // end FUNCTION validate()
+
+
+// EXPORTS //
+
+module.exports = validate;
diff --git a/package.json b/package.json
index 14cd29c..8c63058 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "compute-stdev",
"version": "0.0.0",
- "description": "Computes the sample standard deviation over an array of values.",
+ "description": "Computes the sample standard deviation.",
"author": {
"name": "Athan Reines",
"email": "kgryte@gmail.com"
@@ -10,6 +10,10 @@
{
"name": "Athan Reines",
"email": "kgryte@gmail.com"
+ },
+ {
+ "name": "Philipp Burckhardt",
+ "email": "pburckhardt@outlook.com"
}
],
"scripts": {
@@ -35,17 +39,24 @@
"bugs": {
"url": "https://github.com/compute-io/stdev/issues"
},
- "dependencies": {},
+ "dependencies": {
+ "compute-array-constructors": "^1.0.0",
+ "dstructs-matrix": "^2.0.0",
+ "validate.io-array-like": "^1.0.0",
+ "validate.io-boolean-primitive": "^1.0.0",
+ "validate.io-function": "^1.0.2",
+ "validate.io-matrix-like": "^1.0.0",
+ "validate.io-object": "^1.0.4",
+ "validate.io-positive-integer": "^1.0.0",
+ "validate.io-string-primitive": "^1.0.0"
+ },
"devDependencies": {
- "chai": "1.x.x",
- "mocha": "1.x.x",
+ "chai": "3.x.x",
+ "mocha": "2.x.x",
"coveralls": "^2.11.1",
- "istanbul": "^0.3.0"
+ "istanbul": "^0.3.0",
+ "jshint": "2.x.x",
+ "jshint-stylish": "2.x.x"
},
- "licenses": [
- {
- "type": "MIT",
- "url": "http://www.opensource.org/licenses/MIT"
- }
- ]
+ "license": "MIT"
}
diff --git a/test/test.accessor.js b/test/test.accessor.js
new file mode 100644
index 0000000..e0d12f7
--- /dev/null
+++ b/test/test.accessor.js
@@ -0,0 +1,88 @@
+/* global describe, it, require */
+'use strict';
+
+// MODULES //
+
+var // Expectation library:
+ chai = require( 'chai' ),
+
+ // Module to be tested:
+ stdev = require( './../lib/accessor.js' );
+
+
+// VARIABLES //
+
+var expect = chai.expect,
+ assert = chai.assert;
+
+
+// TESTS //
+
+describe( 'accessor standard deviation', function tests() {
+
+ it( 'should export a function', function test() {
+ expect( stdev ).to.be.a( 'function' );
+ });
+
+ it( 'should compute the standard deviation using an accessor', function test() {
+ var data, expected;
+
+ data = [
+ {'x':2},
+ {'x':4},
+ {'x':5},
+ {'x':3},
+ {'x':8},
+ {'x':2}
+ ];
+ expected = 2.280350850198276;
+
+ assert.strictEqual( stdev( data, getValue ), expected );
+
+ function getValue( d ) {
+ return d.x;
+ }
+ });
+
+ it( 'should compute the (biased) standard deviation using an accessor', function test() {
+ var data, expected;
+
+ data = [
+ {'x':2},
+ {'x':4},
+ {'x':5},
+ {'x':3},
+ {'x':8},
+ {'x':2}
+ ];
+ expected = 2.0816659994661326;
+
+ assert.strictEqual( stdev( data, getValue, true ), expected );
+
+ function getValue( d ) {
+ return d.x;
+ }
+ });
+
+ it( 'should return 0 for a single element array', function test() {
+ var data, expected;
+
+ data = [ {'x':2} ];
+ expected = 0;
+
+ assert.strictEqual( stdev( data, getValue ), expected );
+
+ function getValue( d ) {
+ return d.x;
+ }
+ });
+
+ it( 'should return null if provided an empty array', function test() {
+ assert.isNull( stdev( [], getValue ) );
+
+ function getValue( d ) {
+ return d.x;
+ }
+ });
+
+});
diff --git a/test/test.array.js b/test/test.array.js
new file mode 100644
index 0000000..012fa05
--- /dev/null
+++ b/test/test.array.js
@@ -0,0 +1,58 @@
+/* global describe, it, require */
+'use strict';
+
+// MODULES //
+
+var // Expectation library:
+ chai = require( 'chai' ),
+
+ // Module to be tested:
+ stdev = require( './../lib/array.js' );
+
+
+// VARIABLES //
+
+var expect = chai.expect,
+ assert = chai.assert;
+
+
+// TESTS //
+
+describe( 'array standard deviation', function tests() {
+
+ it( 'should export a function', function test() {
+ expect( stdev ).to.be.a( 'function' );
+ });
+
+ it( 'should compute the standard deviation', function test() {
+ var data, expected;
+
+ data = [ 2, 4, 5, 3, 8, 2 ];
+ expected = 2.280350850198276;
+
+ assert.strictEqual( stdev( data ), expected );
+ });
+
+ it( 'should compute the (biased) standard deviation', function test() {
+ var data, expected;
+
+ data = [ 2, 4, 5, 3, 8, 2 ];
+ expected = 2.0816659994661326;
+
+ assert.strictEqual( stdev( data, true ), expected );
+ });
+
+ it( 'should return 0 for a single element array', function test() {
+ var data, expected;
+
+ data = [ 2 ];
+ expected = 0;
+
+ assert.strictEqual( stdev( data ), expected );
+ });
+
+ it( 'should return null if provided an empty array', function test() {
+ assert.isNull( stdev( [] ) );
+ });
+
+});
diff --git a/test/test.js b/test/test.js
index e9339e4..c11fd63 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,9 +1,14 @@
+/* global describe, it, require */
+'use strict';
// MODULES //
var // Expectation library:
chai = require( 'chai' ),
+ // matrix data structure
+ matrix = require( 'dstructs-matrix' ),
+
// Module to be tested:
stdev = require( './../lib' );
@@ -17,23 +22,22 @@ var expect = chai.expect,
// TESTS //
describe( 'compute-stdev', function tests() {
- 'use strict';
it( 'should export a function', function test() {
expect( stdev ).to.be.a( 'function' );
});
- it( 'should throw an error if provided a non-array', function test() {
+ it( 'should throw an error if the first argument is neither array-like or matrix-like', function test() {
var values = [
- '5',
- 5,
- true,
- undefined,
- null,
- NaN,
- function(){},
- {}
- ];
+ //'5', // valid as is array-like (length)
+ 5,
+ true,
+ undefined,
+ null,
+ NaN,
+ function(){},
+ {}
+ ];
for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
@@ -45,22 +49,168 @@ describe( 'compute-stdev', function tests() {
}
});
+ it( 'should throw an error if provided an unrecognized/unsupported data type option', function test() {
+ var values = [
+ 'beep',
+ 'boop'
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ expect( badValue( values[i] ) ).to.throw( Error );
+ }
+ function badValue( value ) {
+ return function() {
+ stdev( matrix( [2,2] ), {
+ 'dtype': value
+ });
+ };
+ }
+ });
+
+ it( 'should throw an error if provided a dim option which is not a positive integer', function test() {
+ var data = matrix( new Int32Array([1,2,3,4]), [2,2] );
+ var values = [
+ '5',
+ -5,
+ 2.2,
+ true,
+ undefined,
+ null,
+ NaN,
+ [],
+ {}
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ expect( badValue( values[ i ] ) ).to.throw( Error );
+ }
+
+ function badValue( value ) {
+ return function() {
+ stdev( data, {
+ 'dim': value
+ });
+ };
+ }
+ });
+
+ it( 'should throw an error if provided a dim option which exceeds matrix dimensions ( = 2 )', function test() {
+ var data = matrix( new Int32Array([1,2,3,4]), [2,2] );
+ var values = [
+ 3,
+ 4,
+ 5
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ expect( badValue( values[ i ] ) ).to.throw( RangeError );
+ }
+
+ function badValue( value ) {
+ return function() {
+ stdev( data, {
+ 'dim': value
+ });
+ };
+ }
+ });
+
it( 'should compute the sample standard deviation', function test() {
var data, expected;
data = [ 2, 4, 5, 3, 8, 2 ];
- expected = Math.sqrt( 5.2 );
+ expected = 2.280350850198276;
assert.strictEqual( stdev( data ), expected );
});
- it( 'should return 0 for a single element array', function test() {
+ it( 'should compute the sample standard deviation of a typed array', function test() {
+ var data, expected;
+
+ data = new Int8Array( [ 2, 4, 5, 3, 8, 2 ] );
+ expected = 2.280350850198276;
+
+ assert.strictEqual( stdev( data ), expected );
+ });
+
+ it( 'should compute the (biased) sample standard deviation', function test() {
+ var data, expected, actual;
+
+ data = [ 2, 4, 5, 3, 8, 2 ];
+ expected = 2.0816659994661326;
+
+ actual = stdev( data, {
+ 'bias': true
+ });
+
+ assert.strictEqual( actual, expected );
+ });
+
+ it( 'should compute the sample standard deviation using an accessor function', function test() {
+ var data, expected, actual;
+
+ data = [
+ {'x':2},
+ {'x':4},
+ {'x':5},
+ {'x':3},
+ {'x':8},
+ {'x':2}
+ ];
+ expected = 2.280350850198276;
+ actual = stdev( data, {
+ 'accessor': getValue
+ });
+
+ function getValue( d ) {
+ return d.x;
+ }
+
+ assert.strictEqual( actual, expected );
+ });
+
+ it( 'should return `null` when provided an empty array', function test() {
+ var data, expected;
+
+ data = [];
+ expected = null;
+
+ assert.strictEqual( stdev( data ), expected );
+ });
+
+ it( 'should calculate the column standard deviations of a matrix', function test() {
+ var data, expected, s;
+
+ data = matrix( new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), [3,3] );
+ expected = matrix( new Float64Array( [ 1, 1, 1 ] ), [3,1] );
+
+ s = stdev( data, {
+ 'dim': 2
+ });
+
+ assert.deepEqual( s.data, expected.data );
+ });
+
+ it( 'should calculate the row standard deviations of a matrix', function test() {
+ var data, expected, s;
+
+ data = matrix( new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), [3,3] );
+ expected = matrix( new Float64Array( [ 3, 3, 3 ] ), [1, 3] );
+
+ s = stdev( data, {
+ 'dim': 1
+ });
+
+ assert.deepEqual( s.data, expected.data );
+ });
+
+ it( 'should compute the standard deviation for a vector (matrix with one column or row)', function test() {
var data, expected;
- data = [ 2 ];
- expected = 0;
+ expected = 2.280350850198276;
+ data = matrix( new Int32Array( [ 2, 4, 5, 3, 8, 2 ] ), [6,1] );
assert.strictEqual( stdev( data ), expected );
});
-});
\ No newline at end of file
+});
diff --git a/test/test.matrix.js b/test/test.matrix.js
new file mode 100644
index 0000000..b6b180d
--- /dev/null
+++ b/test/test.matrix.js
@@ -0,0 +1,94 @@
+/* global describe, it, require */
+'use strict';
+
+// MODULES //
+
+var // Expectation library:
+ chai = require( 'chai' ),
+
+ // Matrix data structure:
+ matrix = require( 'dstructs-matrix' ),
+
+ // Module to be tested:
+ stdev = require( './../lib/matrix.js' );
+
+
+// VARIABLES //
+
+var expect = chai.expect,
+ assert = chai.assert;
+
+
+// TESTS //
+
+describe( 'matrix standard deviation', function tests() {
+
+ var data,
+ mat,
+ i;
+
+ data = new Int32Array( 25 );
+ for ( i = 0; i < data.length; i++ ) {
+ data[ i ] = i + 1;
+ }
+ mat = matrix( data, [5,5], 'int8' );
+
+
+ it( 'should export a function', function test() {
+ expect( stdev ).to.be.a( 'function' );
+ });
+
+ it( 'should compute the standard deviation along matrix columns', function test() {
+ var out, p, expected;
+
+ out = matrix( [5,1], 'float64' );
+
+ p = stdev( out, mat );
+ expected = '1.5811388300841898;1.5811388300841898;1.5811388300841898;1.5811388300841898;1.5811388300841898';
+
+ assert.strictEqual( p.toString(), expected );
+
+ p = stdev( out, mat, false, 2 );
+ expected = '1.5811388300841898;1.5811388300841898;1.5811388300841898;1.5811388300841898;1.5811388300841898';
+
+ assert.strictEqual( p.toString(), expected );
+ });
+
+ it( 'should compute the standard deviation along matrix rows', function test() {
+ var out, p, expected;
+
+ out = matrix( [1,5], 'float64' );
+
+ p = stdev( out, mat, false, 1 );
+ expected = '7.905694150420948,7.905694150420948,7.905694150420948,7.905694150420948,7.905694150420948';
+
+ assert.strictEqual( p.toString(), expected );
+ });
+
+ it( 'should compute the (biased) standard deviation along matrix rows', function test() {
+ var out, p, expected;
+
+ out = matrix( [1,5], 'float64' );
+
+ p = stdev( out, mat, true, 1 );
+ expected = '7.0710678118654755,7.0710678118654755,7.0710678118654755,7.0710678118654755,7.0710678118654755';
+
+ assert.strictEqual( p.toString(), expected );
+ });
+
+ it( 'should return null if provided a matrix having one or more zero dimensions', function test() {
+ var out, mat;
+
+ out = matrix( [0,0] );
+
+ mat = matrix( [0,10] );
+ assert.isNull( stdev( out, mat ) );
+
+ mat = matrix( [10,0] );
+ assert.isNull( stdev( out, mat ) );
+
+ mat = matrix( [0,0] );
+ assert.isNull( stdev( out, mat ) );
+ });
+
+});
diff --git a/test/test.validate.js b/test/test.validate.js
new file mode 100644
index 0000000..ca17184
--- /dev/null
+++ b/test/test.validate.js
@@ -0,0 +1,147 @@
+/* global describe, it, require */
+'use strict';
+
+// MODULES //
+
+var // Expectation library:
+ chai = require( 'chai' ),
+
+ // Module to be tested:
+ validate = require( './../lib/validate.js' );
+
+
+// VARIABLES //
+
+var expect = chai.expect,
+ assert = chai.assert;
+
+// TESTS //
+
+describe( 'validate', function tests() {
+
+ it( 'should export a function', function test() {
+ expect( validate ).to.be.a( 'function' );
+ });
+
+ it( 'should return an error if provided an options argument which is not an object', function test() {
+ var values = [
+ '5',
+ 5,
+ true,
+ undefined,
+ null,
+ NaN,
+ function(){},
+ []
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ assert.isTrue( validate( {}, values[ i ] ) instanceof TypeError );
+ }
+ });
+
+ it( 'should return an error if provided an accessor which is not a function', function test() {
+ var values, err;
+
+ values = [
+ '5',
+ 5,
+ true,
+ undefined,
+ null,
+ NaN,
+ [],
+ {}
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ err = validate( {}, {
+ 'accessor': values[ i ]
+ });
+ assert.isTrue( err instanceof TypeError );
+ }
+ });
+
+ it( 'should return an error if provided a dim option which is not a positive integer', function test() {
+ var values, err;
+
+ values = [
+ '5',
+ Math.PI,
+ -1,
+ 0,
+ true,
+ undefined,
+ null,
+ NaN,
+ [],
+ {},
+ function(){}
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ err = validate( {}, {
+ 'dim': values[ i ]
+ });
+ assert.isTrue( err instanceof TypeError );
+ }
+ });
+
+ it( 'should return an error if provided a dtype option which is not a string primitive', function test() {
+ var values, err;
+
+ values = [
+ 5,
+ true,
+ undefined,
+ null,
+ NaN,
+ [],
+ {},
+ function(){}
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ err = validate( {}, {
+ 'dtype': values[ i ]
+ });
+ assert.isTrue( err instanceof TypeError );
+ }
+ });
+
+ it( 'should throw an error if provided a bias option which is not a boolean primitive', function test() {
+ var values, err;
+
+ values = [
+ '5',
+ 5,
+ [],
+ new Boolean( false ),
+ undefined,
+ null,
+ NaN,
+ function(){},
+ {}
+ ];
+
+ for ( var i = 0; i < values.length; i++ ) {
+ err = validate( {}, {
+ 'bias': values[ i ]
+ });
+ assert.isTrue( err instanceof TypeError );
+ }
+ });
+
+ it( 'should return null if all options are valid', function test() {
+ var err;
+
+ err = validate( {}, {
+ 'accessor': function getValue(){},
+ 'dim': 2,
+ 'dtype': 'int32'
+ });
+
+ assert.isNull( err );
+ });
+
+});