From 2d862cba3897f417763f7504d9695270654566f0 Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Fri, 5 Jun 2015 17:03:20 +0200 Subject: [PATCH 01/11] [UPDATE] dotfiles --- .editorconfig | 11 ++++++++ .gitattributes | 1 + .jshintignore | 14 ++++++++++ .jshintrc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ .npmignore | 5 +++- .travis.yml | 11 ++++++-- LICENSE | 4 +-- Makefile | 37 ++++++++++++++++++++------ 8 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .jshintignore create mode 100644 .jshintrc 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..3234fbb 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/stylish.js + + # 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: From a956dc5a5a0a2f6b22094a5d7a46c20fa518203b Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Fri, 5 Jun 2015 17:03:56 +0200 Subject: [PATCH 02/11] [UPDATE] implementation --- lib/accessor.js | 43 +++++++++++++++ lib/array.js | 42 +++++++++++++++ lib/index.js | 140 +++++++++++++++++++++++++++++------------------- lib/matrix.js | 60 +++++++++++++++++++++ lib/validate.js | 58 ++++++++++++++++++++ 5 files changed, 287 insertions(+), 56 deletions(-) create mode 100644 lib/accessor.js create mode 100644 lib/array.js create mode 100644 lib/matrix.js create mode 100644 lib/validate.js diff --git a/lib/accessor.js b/lib/accessor.js new file mode 100644 index 0000000..f6f76b1 --- /dev/null +++ b/lib/accessor.js @@ -0,0 +1,43 @@ +'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} variance 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 ) ); + } else { + 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..b608eba --- /dev/null +++ b/lib/array.js @@ -0,0 +1,42 @@ +'use strict'; + +/** +* FUNCTION: stdev( arr[, bias] ) +* Computes the variance 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 variance +* @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 ) ); + } else { + 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..9f7add1 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} variance value(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..603264a --- /dev/null +++ b/lib/matrix.js @@ -0,0 +1,60 @@ +'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} variance or null +*/ +function stdev( out, mat, bias, dim ) { + var mu, delta, x, M2, + M, N, + s0, s1, + 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; + } + + for ( i = 0; i < M; i++ ) { + k = 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..6d50329 --- /dev/null +++ b/lib/validate.js @@ -0,0 +1,58 @@ +'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 +* @returns {Null|Error} null or an error +*/ +function validate( opts, options ) { + if ( !isObject( options ) ) { + return new TypeError( 'variance()::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.dim + '`.' ); + } + } + return null; +} // end FUNCTION validate() + + +// EXPORTS // + +module.exports = validate; From 7681b6761ebc97576a11d4daaadb2857998a51f8 Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Fri, 5 Jun 2015 23:59:16 +0200 Subject: [PATCH 03/11] [UPDATE] eqns --- docs/img/eqn1.svg | 56 +++++++++++++++++++++++++++++++++++++++++++++++ docs/img/eqn2.svg | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 docs/img/eqn1.svg create mode 100644 docs/img/eqn2.svg 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From c6a54b4759fabf015b3ed0f11e630285eebdf5fa Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Sat, 6 Jun 2015 00:12:23 +0200 Subject: [PATCH 04/11] [UPDATE] README --- README.md | 243 +++++++++++++++++++++++++++++++++++++++++++++++++-- package.json | 34 ++++--- 2 files changed, 258 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index bc90d79..6fdce73 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,16 @@ 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 unbiased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is defined by + +
+ Equation for the sample standard deviation. +
+
+ +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 +25,241 @@ 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 [variance](http://en.wikipedia.org/wiki/Variance). `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 = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ]; + +var s = stdev( data ); +// returns 5.067 + +data = new Int8Array( data ); +s = stdev( data ); +// returns 5.067 +``` + +For non-numeric `arrays`, provide an accessor `function` for accessing numeric `array` values + +``` javascript +var data = [ + {'x':2}, + {'x':4}, + {'x':5}, + {'x':3}, + {'x':4}, + {'x':3}, + {'x':1}, + {'x':5}, + {'x':6}, + {'x':9} +]; + +function getValue( d ) { + return d.x; +} + +var s = stdev( data, { + 'accessor': getValue +}); +// returns 5.067 +``` + +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 = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ]; + +var value = stdev( data, { + 'bias': true +}); +// returns 4.56 +``` + +The biased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is calculated as follows: + +
+ Equation for the biased sample standard deviation. +
+
+ +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 ); +/* + [ 2.5 + 2.5 + 2.5 + 2.5 + 2.5 ] +*/ +``` + +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 +}); +/* + [ 62.5, 62.5, 62.5, 62.5, 62.5 ] +*/ +``` + +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' +}); +/* + [ 10, 11, 12, 13, 14 ] +*/ + +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 = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ]; + +// Row vector: +mat = matrix( new Int8Array( data ), [1,10], 'int8' ); +s = stdev( mat ); +// returns 5.067 + +// Column vector: +mat = matrix( new Int8Array( data ), [10,1], 'int8' ); +s = stdev( mat ); +// returns 5.067 +``` + +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 ); +'use strict'; + +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 ); + + +// ---- +// 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() ); + + +// ---- +// Matrices (custom output data type)... +s = stdev( mat, { + 'dtype': 'uint8' +}); +console.log( 'Matrix (%s): %s\n', s.dtype, s.toString() ); -console.log( stdev( data ) ); ``` To run the example code from the top-level application directory, @@ -72,7 +299,7 @@ $ open reports/coverage/lcov-report/index.html ## License -[MIT license](http://opensource.org/licenses/MIT). +[MIT license](http://opensource.org/licenses/MIT). --- @@ -97,4 +324,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/package.json b/package.json index 14cd29c..101d332 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 over an array or matrix.", "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,25 @@ "bugs": { "url": "https://github.com/compute-io/stdev/issues" }, - "dependencies": {}, + "dependencies": { + "compute-array-constructors": "^1.0.0", + "compute-dtype": "^1.0.0", + "dstructs-matrix": "^1.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": "2.x.x", + "mocha": "2.x.x", "coveralls": "^2.11.1", - "istanbul": "^0.3.0" + "istanbul": "^0.3.0", + "jshint": "2.x.x", + "jshint-stylish": "^1.0.0" }, - "licenses": [ - { - "type": "MIT", - "url": "http://www.opensource.org/licenses/MIT" - } - ] + "license": "MIT" } From 21c0b9d166447982c79dd5d027979a2b6d9c3d6d Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Sat, 6 Jun 2015 00:31:05 +0200 Subject: [PATCH 05/11] [UPDATE] README, examples --- README.md | 58 +++++++++++++++++----------------------- examples/index.js | 67 ++++++++++++++++++++++++++++++++++++++++++++--- lib/index.js | 2 +- lib/validate.js | 2 ++ 4 files changed, 90 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 6fdce73..3a702c0 100644 --- a/README.md +++ b/README.md @@ -31,35 +31,28 @@ var stdev = require( 'compute-stdev' ); ### stdev( x[, opts] ) -Computes the [variance](http://en.wikipedia.org/wiki/Variance). `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). +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 = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ]; +var data = [ 1, 4, 7 ]; var s = stdev( data ); -// returns 5.067 +// returns 3 data = new Int8Array( data ); s = stdev( data ); -// returns 5.067 +// returns 3 ``` For non-numeric `arrays`, provide an accessor `function` for accessing numeric `array` values ``` javascript var data = [ - {'x':2}, - {'x':4}, - {'x':5}, - {'x':3}, - {'x':4}, - {'x':3}, {'x':1}, - {'x':5}, - {'x':6}, - {'x':9} + {'x':4}, + {'x':7} ]; function getValue( d ) { @@ -69,18 +62,18 @@ function getValue( d ) { var s = stdev( data, { 'accessor': getValue }); -// returns 5.067 +// 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 = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ]; +var data = [ 1, 4, 7 ]; var value = stdev( data, { 'bias': true }); -// returns 4.56 +// returns 3.674235 ``` The biased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is calculated as follows: @@ -119,11 +112,11 @@ mat = matrix( data, [5,5], 'int8' ); s = stdev( mat ); /* - [ 2.5 - 2.5 - 2.5 - 2.5 - 2.5 ] + [ 1.581139 + 1.581139 + 1.581139 + 1.581139 + 1.581139 ] */ ``` @@ -134,7 +127,7 @@ s = stdev( mat, { 'dim': 1 }); /* - [ 62.5, 62.5, 62.5, 62.5, 62.5 ] + [ 7.905694, 7.905694, 7.905694, 7.905694, 7.905694 ] */ ``` @@ -146,7 +139,7 @@ s = stdev( mat, { 'dtype': 'uint8' }); /* - [ 10, 11, 12, 13, 14 ] + [ 7, 7, 7, 7, 7 ] */ var dtype = s.dtype; @@ -156,17 +149,17 @@ var dtype = s.dtype; 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 = [ 2, 4, 5, 3, 4, 3, 1, 5, 6, 9 ]; +data = [ 1, 4, 7 ]; // Row vector: -mat = matrix( new Int8Array( data ), [1,10], 'int8' ); +mat = matrix( new Int8Array( data ), [1,3], 'int8' ); s = stdev( mat ); -// returns 5.067 +// returns 2 // Column vector: -mat = matrix( new Int8Array( data ), [10,1], 'int8' ); +mat = matrix( new Int8Array( data ), [3,1], 'int8' ); s = stdev( mat ); -// returns 5.067 +// 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`. @@ -191,8 +184,6 @@ s = stdev( matrix( [10,0] ) ); ## Examples ``` javascript -'use strict'; - var matrix = require( 'dstructs-matrix' ), stdev = require( './../lib' ); @@ -273,7 +264,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 @@ -297,16 +288,15 @@ $ open reports/coverage/lcov-report/index.html ``` +--- ## License [MIT license](http://opensource.org/licenses/MIT). ---- ## Copyright -Copyright © 2014. Athan Reines. - +Copyright © 2014-2015. The Compute.io Authors. [npm-image]: http://img.shields.io/npm/v/compute-stdev.svg [npm-url]: https://npmjs.org/package/compute-stdev diff --git a/examples/index.js b/examples/index.js index 9d16332..4c2fa52 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 ); + + +// ---- +// 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/index.js b/lib/index.js index 9f7add1..cea2c82 100644 --- a/lib/index.js +++ b/lib/index.js @@ -27,7 +27,7 @@ var stdev1 = require( './array.js' ), * @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} variance value(s) or null +* @returns {Number|Matrix|Null} standard deviation(s) or null */ function stdev( x, options ) { /* jshint newcap:false */ diff --git a/lib/validate.js b/lib/validate.js index 6d50329..984a0df 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -19,6 +19,8 @@ var isObject = require( 'validate.io-object' ), * @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} [opts.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 ) { From 774d70fc57dca34bf05f6fd54a4137406481aa6a Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Sat, 6 Jun 2015 00:37:13 +0200 Subject: [PATCH 06/11] [UPDATE] tests --- README.md | 2 +- test/test.accessor.js | 88 +++++++++++++++++++++ test/test.array.js | 58 ++++++++++++++ test/test.js | 175 ++++++++++++++++++++++++++++++++++++++---- test/test.matrix.js | 94 +++++++++++++++++++++++ test/test.validate.js | 147 +++++++++++++++++++++++++++++++++++ 6 files changed, 547 insertions(+), 17 deletions(-) create mode 100644 test/test.accessor.js create mode 100644 test/test.array.js create mode 100644 test/test.matrix.js create mode 100644 test/test.validate.js diff --git a/README.md b/README.md index 3a702c0..8d315bd 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ s = stdev( matrix( [10,0] ) ); ``` javascript var matrix = require( 'dstructs-matrix' ), - stdev = require( './../lib' ); + stdev = require( 'compute-stdev' ); var data, mat, 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..c4ac990 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,10 @@ +/* global describe, it, require */ +'use strict'; // MODULES // +var matrix = require( 'dstructs-matrix' ); + var // Expectation library: chai = require( 'chai' ), @@ -17,23 +21,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 +48,162 @@ 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, results; + + data = matrix( new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), [3,3] ); + expected = matrix( new Float32Array( [ 1, 1, 1 ] ), [3,1] ); + + results = stdev( data, {'dtype': 'float32'} ); + + assert.strictEqual( results.data.length, expected.data.length ); + assert.deepEqual( results.data, expected.data ); + }); + + it( 'should calculate the row standard deviations of a matrix', function test() { + var data, expected, results; + + data = matrix( new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), [3,3] ); + expected = matrix( new Float32Array( [ 3, 3, 3 ] ), [1, 3] ); + + results = stdev( data, {'dim': 1, 'dtype': 'float32'} ); + + assert.strictEqual( results.data.length, expected.data.length ); + assert.deepEqual( results.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..b991561 --- /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 ); + }); + + }); From dc9bcde2688c7466b0291469330105c8a9dfd62d Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Sat, 6 Jun 2015 16:14:29 +0200 Subject: [PATCH 07/11] [UPDATE] tweaks. --- README.md | 20 ++-- lib/accessor.js | 7 +- lib/array.js | 5 +- lib/validate.js | 2 +- package.json | 5 +- test/test.js | 37 ++++--- test/test.validate.js | 234 +++++++++++++++++++++--------------------- 7 files changed, 155 insertions(+), 155 deletions(-) diff --git a/README.md b/README.md index 8d315bd..756db31 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,14 @@ stdev > Computes the sample [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation). -The unbiased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is defined by +The biased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is defined as + +
+ Equation for the biased sample standard deviation. +
+
+ +and the unbiased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is defined as
Equation for the sample standard deviation. @@ -73,16 +80,9 @@ var data = [ 1, 4, 7 ]; var value = stdev( data, { 'bias': true }); -// returns 3.674235 +// returns 1.3333 ``` -The biased [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation) is calculated as follows: - -
- Equation for the biased sample standard deviation. -
-
- 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). @@ -296,7 +296,7 @@ $ open reports/coverage/lcov-report/index.html ## Copyright -Copyright © 2014-2015. The Compute.io Authors. +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 diff --git a/lib/accessor.js b/lib/accessor.js index f6f76b1..078ec08 100644 --- a/lib/accessor.js +++ b/lib/accessor.js @@ -7,7 +7,7 @@ * @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} variance or null +* @returns {Number|Null} variance or null */ function stdev( arr, clbk, bias ) { var len = arr.length, @@ -22,19 +22,16 @@ function stdev( arr, clbk, bias ) { 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 ) ); - } else { - return Math.sqrt( M2 / ( i - 1 ) ); } + return Math.sqrt( M2 / ( i - 1 ) ); } // end FUNCTION stdev() diff --git a/lib/array.js b/lib/array.js index b608eba..9b7b991 100644 --- a/lib/array.js +++ b/lib/array.js @@ -21,19 +21,16 @@ function stdev( arr, bias ) { 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 ) ); - } else { - return Math.sqrt( M2 / ( i - 1 ) ); } + return Math.sqrt( M2 / ( i - 1 ) ); } // end FUNCTION stdev() diff --git a/lib/validate.js b/lib/validate.js index 984a0df..94771f6 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -20,7 +20,7 @@ var isObject = require( 'validate.io-object' ), * @param {Function} [options.accessor] - accessor function for accessing array values * @param {Number} [options.dim] - dimension * @param {String} [options.dtype] - output data type -* @param {Boolean} [opts.bias=false] - Boolean indicating whether to calculate a biased or unbiased estimate of the standard deviation +* @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 ) { diff --git a/package.json b/package.json index 101d332..b74c9b8 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 or matrix.", + "description": "Computes the sample standard deviation.", "author": { "name": "Athan Reines", "email": "kgryte@gmail.com" @@ -40,8 +40,7 @@ "url": "https://github.com/compute-io/stdev/issues" }, "dependencies": { - "compute-array-constructors": "^1.0.0", - "compute-dtype": "^1.0.0", + "compute-array-constructors": "^1.0.0", "dstructs-matrix": "^1.0.0", "validate.io-array-like": "^1.0.0", "validate.io-boolean-primitive": "^1.0.0", diff --git a/test/test.js b/test/test.js index c4ac990..c11fd63 100644 --- a/test/test.js +++ b/test/test.js @@ -3,11 +3,12 @@ // MODULES // -var matrix = require( 'dstructs-matrix' ); - var // Expectation library: chai = require( 'chai' ), + // matrix data structure + matrix = require( 'dstructs-matrix' ), + // Module to be tested: stdev = require( './../lib' ); @@ -28,7 +29,7 @@ describe( 'compute-stdev', function tests() { it( 'should throw an error if the first argument is neither array-like or matrix-like', function test() { var values = [ - // '5', // valid as is array-like (length) + //'5', // valid as is array-like (length) 5, true, undefined, @@ -86,7 +87,9 @@ describe( 'compute-stdev', function tests() { function badValue( value ) { return function() { - stdev( data, {'dim': value} ); + stdev( data, { + 'dim': value + }); }; } }); @@ -105,7 +108,9 @@ describe( 'compute-stdev', function tests() { function badValue( value ) { return function() { - stdev( data, {'dim': value} ); + stdev( data, { + 'dim': value + }); }; } }); @@ -174,27 +179,29 @@ describe( 'compute-stdev', function tests() { }); it( 'should calculate the column standard deviations of a matrix', function test() { - var data, expected, results; + var data, expected, s; data = matrix( new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), [3,3] ); - expected = matrix( new Float32Array( [ 1, 1, 1 ] ), [3,1] ); + expected = matrix( new Float64Array( [ 1, 1, 1 ] ), [3,1] ); - results = stdev( data, {'dtype': 'float32'} ); + s = stdev( data, { + 'dim': 2 + }); - assert.strictEqual( results.data.length, expected.data.length ); - assert.deepEqual( results.data, expected.data ); + assert.deepEqual( s.data, expected.data ); }); it( 'should calculate the row standard deviations of a matrix', function test() { - var data, expected, results; + var data, expected, s; data = matrix( new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), [3,3] ); - expected = matrix( new Float32Array( [ 3, 3, 3 ] ), [1, 3] ); + expected = matrix( new Float64Array( [ 3, 3, 3 ] ), [1, 3] ); - results = stdev( data, {'dim': 1, 'dtype': 'float32'} ); + s = stdev( data, { + 'dim': 1 + }); - assert.strictEqual( results.data.length, expected.data.length ); - assert.deepEqual( results.data, expected.data ); + assert.deepEqual( s.data, expected.data ); }); it( 'should compute the standard deviation for a vector (matrix with one column or row)', function test() { diff --git a/test/test.validate.js b/test/test.validate.js index b991561..ca17184 100644 --- a/test/test.validate.js +++ b/test/test.validate.js @@ -15,133 +15,133 @@ var // Expectation library: var expect = chai.expect, assert = chai.assert; - // TESTS // +// TESTS // - describe( 'validate', function 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 export a function', function test() { + expect( validate ).to.be.a( 'function' ); + }); - 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 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 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 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 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 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 null if all options are valid', function test() { - var err; + 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( {}, { - 'accessor': function getValue(){}, - 'dim': 2, - 'dtype': 'int32' + 'bias': values[ i ] }); + assert.isTrue( err instanceof TypeError ); + } + }); - assert.isNull( err ); + 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 ); }); + +}); From 4e2b207f7fad2d4c23fe8004ff5c189bd96b8ffb Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sun, 7 Jun 2015 12:42:38 +0200 Subject: [PATCH 08/11] [FIX] change variance to stdev --- lib/validate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validate.js b/lib/validate.js index 94771f6..abf095a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -25,7 +25,7 @@ var isObject = require( 'validate.io-object' ), */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( 'variance()::invalid input argument. Options argument must be an object. Value: `' + options + '`.' ); + return new TypeError( 'stdev()::invalid input argument. Options argument must be an object. Value: `' + options + '`.' ); } if ( options.hasOwnProperty( 'accessor' ) ) { opts.accessor = options.accessor; From 1c0719ec2031a09203e92e2c44a37a41f698e16c Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Tue, 9 Jun 2015 15:12:21 +0200 Subject: [PATCH 09/11] [FIX] doc errors --- lib/accessor.js | 2 +- lib/array.js | 2 +- lib/validate.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/accessor.js b/lib/accessor.js index 078ec08..21f7494 100644 --- a/lib/accessor.js +++ b/lib/accessor.js @@ -7,7 +7,7 @@ * @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} variance or null +* @returns {Number|Null} standard deviation or null */ function stdev( arr, clbk, bias ) { var len = arr.length, diff --git a/lib/array.js b/lib/array.js index 9b7b991..72e45d3 100644 --- a/lib/array.js +++ b/lib/array.js @@ -2,7 +2,7 @@ /** * FUNCTION: stdev( arr[, bias] ) -* Computes the variance of an array. +* 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 variance diff --git a/lib/validate.js b/lib/validate.js index abf095a..91b25aa 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -20,7 +20,7 @@ var isObject = require( 'validate.io-object' ), * @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 +* @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 ) { @@ -48,7 +48,7 @@ function validate( opts, options ) { 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.dim + '`.' ); + return new TypeError( 'stdev()::invalid option. Bias option must be a Boolean primitive. Option: `' + opts.bias + '`.' ); } } return null; From 8d1f9bc3e37b32c2c8ea2012915d334fb6b378f4 Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Tue, 9 Jun 2015 15:12:38 +0200 Subject: [PATCH 10/11] [UPDATE] support new matrix API --- Makefile | 2 +- lib/matrix.js | 6 ++++-- package.json | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 3234fbb..c6f009c 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ ISTANBUL_HTML_REPORT_PATH ?= $(ISTANBUL_OUT)/lcov-report/index.html # JSHINT # JSHINT ?= ./node_modules/.bin/jshint -JSHINT_REPORTER ?= ./node_modules/jshint-stylish/stylish.js +JSHINT_REPORTER ?= ./node_modules/jshint-stylish diff --git a/lib/matrix.js b/lib/matrix.js index 603264a..af621db 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -8,12 +8,13 @@ * @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} variance or null +* @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 ) { @@ -32,9 +33,10 @@ function stdev( out, mat, bias, dim ) { if ( M === 0 || N === 0 ) { return null; } + o = mat.offset; for ( i = 0; i < M; i++ ) { - k = i * s0; + k = o + i * s0; mu = 0; delta = 0; M2 = 0; diff --git a/package.json b/package.json index b74c9b8..d4ea409 100644 --- a/package.json +++ b/package.json @@ -51,12 +51,12 @@ "validate.io-string-primitive": "^1.0.0" }, "devDependencies": { - "chai": "2.x.x", + "chai": "3.x.x", "mocha": "2.x.x", "coveralls": "^2.11.1", "istanbul": "^0.3.0", "jshint": "2.x.x", - "jshint-stylish": "^1.0.0" + "jshint-stylish": "^2.0.0" }, "license": "MIT" } From 6f0e808e4bf0e5240e406f0a4daa5cde953624f1 Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Mon, 22 Jun 2015 19:58:32 +0200 Subject: [PATCH 11/11] [UPDATE] update dstructs-matrix dep, README, doc --- README.md | 17 ----------------- examples/index.js | 2 +- lib/array.js | 2 +- package.json | 4 ++-- 4 files changed, 4 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 756db31..43ca075 100644 --- a/README.md +++ b/README.md @@ -192,17 +192,13 @@ var data, s, i; -// ---- // Plain arrays... var data = new Array( 100 ); for ( var i = 0; i < data.length; i++ ) { 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; @@ -215,10 +211,7 @@ for ( i = 0; i < data.length; 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++ ) { @@ -226,31 +219,21 @@ for ( i = 0; i < data.length; i++ ) { } s = stdev( data ); - -// ---- // 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() ); - -// ---- // Matrices (custom output data type)... s = stdev( mat, { 'dtype': 'uint8' }); -console.log( 'Matrix (%s): %s\n', s.dtype, s.toString() ); - ``` To run the example code from the top-level application directory, diff --git a/examples/index.js b/examples/index.js index 4c2fa52..7b4a64d 100644 --- a/examples/index.js +++ b/examples/index.js @@ -41,7 +41,7 @@ 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)... diff --git a/lib/array.js b/lib/array.js index 72e45d3..ee69607 100644 --- a/lib/array.js +++ b/lib/array.js @@ -5,7 +5,7 @@ * 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 variance +* @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 ) { diff --git a/package.json b/package.json index d4ea409..8c63058 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ }, "dependencies": { "compute-array-constructors": "^1.0.0", - "dstructs-matrix": "^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", @@ -56,7 +56,7 @@ "coveralls": "^2.11.1", "istanbul": "^0.3.0", "jshint": "2.x.x", - "jshint-stylish": "^2.0.0" + "jshint-stylish": "2.x.x" }, "license": "MIT" }