From c21c14f2946968b087f134b9b8198773c1b3c7fe Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 20:07:39 +1000 Subject: [PATCH 01/15] Add readme --- .../@stdlib/stats/incr/wmean/README.md | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/README.md diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/README.md b/lib/node_modules/@stdlib/stats/incr/wmean/README.md new file mode 100644 index 000000000000..99c0979c420d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/README.md @@ -0,0 +1,129 @@ + + +# incrwmean + +> Compute a [weighted arithmetic mean][weighted-arithmetic-mean] incrementally. + +
+ +The [weighted arithmetic mean][weighted-arithmetic-mean] is defined as + + + +
+ Equation for the weighted arithmetic mean. +
+
+ + + +
+ + + +
+ +## Usage + +```javascript +var incrwmean = require( '@stdlib/stats/incr/wmean' ); +``` + +#### incrwmean() + +Returns an accumulator `function` which incrementally computes an [weighted arithmetic mean][weighted-arithmetic-mean]. + +```javascript +var accumulator = incrwmean(); +``` + +#### accumulator( \[x, w] ) + +If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted mean. If not provided any input values, the accumulator function returns the current mean. + +```javascript +var accumulator = incrwmean(); + +var mu = accumulator( 2.0, 1.0 ); +// returns 2.0 + +mu = accumulator( 2.0, 0.5 ); +// returns 2.0 + +mu = accumulator( 3.0, 1.5 ); +// returns 2.5 + +mu = accumulator(); +// returns 2.5 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrwmean = require( '@stdlib/stats/incr/wmean' ); + +var accumulator; +var v; +var w; +var i; + +// Initialize an accumulator: +accumulator = incrwmean(); + +// For each simulated datum, update the weighted mean... +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + w = randu() * 100.0; + accumulator( v, w ); +} +console.log( accumulator() ); +``` + +
+ + + + + + From 2542759e71607ea91941f9440c67c33ad7b8dc08 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 21:05:04 +1000 Subject: [PATCH 02/15] Add examples --- .../stats/incr/wmean/examples/index.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/examples/index.js diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/wmean/examples/index.js new file mode 100644 index 000000000000..cd64d6796485 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var incrwmean = require( './../lib' ); + +var accumulator; +var mu; +var x; +var w; +var i; + +// Initialize an accumulator: +accumulator = incrwmean(); + +// For each simulated datum, update the weighted mean... +console.log( '\nValue\tWeight\tWeighted Mean\n' ); +for ( i = 0; i < 100; i++ ) { + x = randu() * 100.0; + w = randu() * 100.0; + mu = accumulator( x ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), w.toFixed( 4 ), mu.toFixed( 4 ) ); +} +console.log( '\nFinal weighted mean: %d\n', accumulator() ); From 53703365db0a465375d498a36691d42fa362fbc7 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 21:05:32 +1000 Subject: [PATCH 03/15] Add package.json --- .../@stdlib/stats/incr/wmean/package.json | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/package.json diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/package.json b/lib/node_modules/@stdlib/stats/incr/wmean/package.json new file mode 100644 index 000000000000..1b9bd002233a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/stats/incr/wmean", + "version": "0.0.0", + "description": "Compute a weighted arithmetic mean incrementally.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "incremental", + "accumulator", + "weighted" + ] +} From 279f412a9e312c11c6014160a1b90b3090d1ba56 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 21:13:06 +1000 Subject: [PATCH 04/15] Add implementation --- .../@stdlib/stats/incr/wmean/lib/index.js | 54 +++++++++++++ .../@stdlib/stats/incr/wmean/lib/main.js | 77 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js new file mode 100644 index 000000000000..957342337b4c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a weighted arithmetic mean incrementally. +* +* @module @stdlib/stats/incr/wmean +* +* @example +* var incrwmean = require( '@stdlib/stats/incr/wmean' ); +* +* var accumulator = incrwmean(); +* +* var mu = accumulator(); +* // returns null +* +* var mu = accumulator( 2.0, 1.0 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, 0.5 ); +* // returns 2.0 +* +* mu = accumulator( 3.0, 1.5 ); +* // returns 2.5 +* +* mu = accumulator(); +* // returns 2.5 +*/ + +// MODULES // + +var incrwmean = require( './main.js' ); + + +// EXPORTS // + +module.exports = incrwmean; diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js new file mode 100644 index 000000000000..058cee82d91f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Returns an accumulator function which incrementally computes a weighted arithmetic mean. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrwmean(); +* +* var mu = accumulator(); +* // returns null +* +* var mu = accumulator( 2.0, 1.0 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, 0.5 ); +* // returns 2.0 +* +* mu = accumulator( 3.0, 1.5 ); +* // returns 2.5 +* +* mu = accumulator(); +* // returns 2.5 +*/ +function incrwmean() { + var wSum; + var xwSum; + var mu; + + wSum = 0.0; + xwSum = 0.0; + mu = null; + + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated weighted mean. If not provided a value, the accumulator function returns the current weighted mean. + * + * @private + * @param {number} [x] - new value + * @param {number} [w] - new value's weight + * @returns {(number|null)} weighted mean value or null + */ + function accumulator( x, w ) { + if ( arguments.length === 0 ) { + return mu; + } + wSum += w; + xwSum += x * w; + mu = xwSum / wSum; + return mu; + } +} + + +// EXPORTS // + +module.exports = incrwmean; From 320550e3326e0ab1e256df20d797d78b890882f4 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 21:17:27 +1000 Subject: [PATCH 05/15] Add benchmarks --- .../stats/incr/wmean/benchmark/benchmark.js | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/benchmark/benchmark.js diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/wmean/benchmark/benchmark.js new file mode 100644 index 000000000000..33cd07ef2bc0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrwmean = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrwmean(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrwmean(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); From 138402e42062471208fe11045e4579ef9fc3089e Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 21:51:20 +1000 Subject: [PATCH 06/15] Add tests and add weight default of 1.0 --- .../@stdlib/stats/incr/wmean/lib/main.js | 7 +- .../@stdlib/stats/incr/wmean/test/test.js | 119 ++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index 058cee82d91f..b48fe0efed8f 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -42,8 +42,8 @@ * // returns 2.5 */ function incrwmean() { - var wSum; var xwSum; + var wSum; var mu; wSum = 0.0; @@ -57,13 +57,16 @@ function incrwmean() { * * @private * @param {number} [x] - new value - * @param {number} [w] - new value's weight + * @param {number} [w] - new value's weight (or 1.0 if not provided) * @returns {(number|null)} weighted mean value or null */ function accumulator( x, w ) { if ( arguments.length === 0 ) { return mu; } + if ( w === undefined ) { + w = 1.0 + } wSum += w; xwSum += x * w; mu = xwSum / wSum; diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js new file mode 100644 index 000000000000..9a373c0983f6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var incrwmean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof incrwmean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrwmean(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrwmean(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) { + var expected; + var actual; + var xwSum; + var wSum; + var dataX; + var dataW; + var acc; + var N; + var x; + var w; + var i; + + dataX = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + dataW = [ 1.0, 2.0, 0.1, 1.8, 9.9, 3.6 ]; + N = dataX.length; + + expected = new Array( N ); + actual = new Array( N ); + + acc = incrwmean(); + + xwSum = 0; + wSum = 0; + for ( i = 0; i < N; i++ ) { + x = dataX[ i ]; + w = dataW[ i ]; + xwSum += x * w; + wSum += w; + expected[ i ] = xwSum / wSum; + actual[ i ] = acc( x, w ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current weighted mean', function test( t ) { + var dataX; + var dataW; + var acc; + var i; + + dataX = [ 2.0, 3.0, 1.0 ]; + dataW = [ 2.0, 2.0, 1.0 ]; + acc = incrwmean(); + for ( i = 0; i < dataX.length; i++ ) { + acc( dataX[ i ], dataW[ i ] ); + } + t.equal( acc(), 2.2, 'returns the current accumulated mean' ); + t.end(); +}); + +tape( 'if weight is not provided, the accumulator function uses 1.0 as the weight', function test( t ) { + var dataX; + var acc; + var i; + + dataX = [ 2.0, 3.0, 1.0 ]; + acc = incrwmean(); + for ( i = 0; i < dataX.length; i++ ) { + acc( dataX[ i ] ); + } + t.equal( acc(), 2.0, 'returns the current accumulated mean' ); + t.end(); +}); + +tape( 'if null is passed as a weight, the accumulator function returns NaN', function test( t ) { + var acc; + + acc = incrwmean(); + acc(); + t.ok( isNaN( acc(2.0, null) ), 'returns NaN' ); + t.end(); +}); From fb07cb46a3140ee61e07493adb343c52e21e7981 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 21:54:11 +1000 Subject: [PATCH 07/15] Update examples to reflect default weight parameter --- lib/node_modules/@stdlib/stats/incr/wmean/README.md | 3 ++- lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js | 3 ++- lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js | 8 +++----- lib/node_modules/@stdlib/stats/incr/wmean/test/test.js | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/README.md b/lib/node_modules/@stdlib/stats/incr/wmean/README.md index 99c0979c420d..7e33b1b898bb 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/README.md +++ b/lib/node_modules/@stdlib/stats/incr/wmean/README.md @@ -62,7 +62,8 @@ If provided an input value `x` and a weight `w`, the accumulator function return ```javascript var accumulator = incrwmean(); -var mu = accumulator( 2.0, 1.0 ); +// Default weight is 1.0 +var mu = accumulator( 2.0 ); // returns 2.0 mu = accumulator( 2.0, 0.5 ); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js index 957342337b4c..19b8305941bb 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js @@ -31,7 +31,8 @@ * var mu = accumulator(); * // returns null * -* var mu = accumulator( 2.0, 1.0 ); +* // Default weight is 1.0 +* var mu = accumulator( 2.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index b48fe0efed8f..9d264c02daac 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -29,7 +29,8 @@ * var mu = accumulator(); * // returns null * -* var mu = accumulator( 2.0, 1.0 ); +* // Default weight is 1.0 +* var mu = accumulator( 2.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); @@ -60,13 +61,10 @@ function incrwmean() { * @param {number} [w] - new value's weight (or 1.0 if not provided) * @returns {(number|null)} weighted mean value or null */ - function accumulator( x, w ) { + function accumulator( x, w = 1.0 ) { if ( arguments.length === 0 ) { return mu; } - if ( w === undefined ) { - w = 1.0 - } wSum += w; xwSum += x * w; mu = xwSum / wSum; diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index 9a373c0983f6..32a1cc2183c6 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -46,10 +46,10 @@ tape( 'the initial accumulated value is `null`', function test( t ) { tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) { var expected; var actual; - var xwSum; - var wSum; var dataX; var dataW; + var xwSum; + var wSum; var acc; var N; var x; From c24fbc4bae7c4949903149f820c7524b106fe06f Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 22:07:21 +1000 Subject: [PATCH 08/15] Add repl --- .../@stdlib/stats/incr/wmean/docs/repl.txt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt new file mode 100644 index 000000000000..843b643bb923 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes a weighted + arithmetic mean. + + If provided parameter(s), the accumulator function returns an updated + weighted mean. If not provided a value, the accumulator function returns + the current weighted mean. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, the accumulated value is `NaN` for all future invocations. + + The accumulator accepts two parameters, the first is the value and the + second is the weight. If the weight is not provided, it defaults to 1.0. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var mu = accumulator() + null + > mu = accumulator( 2.0 ) + 2.0 + > mu = accumulator( 2.0, 0.5 ) + 2.0 + > mu = accumulator( 3.0, 1.5 ) + 2.5 + > mu = accumulator() + 2.5 + + See Also + -------- + From 013f14b9a75c2d33c2a9443f08f7ebd5398e85bf Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Sun, 24 Feb 2019 22:16:22 +1000 Subject: [PATCH 09/15] Add typescript definition --- .../stats/incr/wmean/docs/types/index.d.ts | 53 +++++++++++++++++++ .../stats/incr/wmean/docs/types/test.ts | 39 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts new file mode 100644 index 000000000000..48a286610e88 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/// + +/** +* Returns an accumulator function which incrementally computes a weighted arithmetic mean. +* +* @returns accumulator function +* +* @example* var incrwmean = require( '@stdlib/stats/incr/wmean' ); +* +* var accumulator = incrwmean(); +* +* var mu = accumulator(); +* // returns null +* +* // Default weight is 1.0 +* var mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, 0.5 ); +* // returns 2.0 +* +* mu = accumulator( 3.0, 1.5 ); +* // returns 2.5 +* +* mu = accumulator(); +* // returns 2.5 +*/ +declare function incrwmean(): ( x: number, w?: number ) => number | null; + + +// EXPORTS // + +export = incrwmean; diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts new file mode 100644 index 000000000000..cdc221491fc2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrwmean = require( './index' ); + +// TESTS // + +// The function returns either a number or null... +{ + incrwmean(); // $ExpectType (x: number, w?: number | undefined) => number | null +} + +// The compiler throws an error if the function is any arguments +{ + incrwmean( '5' ); // $ExpectError + incrwmean( 5 ); // $ExpectError + incrwmean( true ); // $ExpectError + incrwmean( false ); // $ExpectError + incrwmean( null ); // $ExpectError + incrwmean( undefined ); // $ExpectError + incrwmean( [] ); // $ExpectError + incrwmean( {} ); // $ExpectError + incrwmean( ( x: number ): number => x ); // $ExpectError +} From 2d4007b0240383bfeb9dbc75c64ba992fb3e9012 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Mon, 25 Feb 2019 13:29:02 +1000 Subject: [PATCH 10/15] Make weight parameter required --- .../@stdlib/stats/incr/wmean/README.md | 3 +-- .../@stdlib/stats/incr/wmean/docs/repl.txt | 4 ++-- .../stats/incr/wmean/docs/types/index.d.ts | 5 ++--- .../@stdlib/stats/incr/wmean/docs/types/test.ts | 2 +- .../@stdlib/stats/incr/wmean/lib/index.js | 3 +-- .../@stdlib/stats/incr/wmean/lib/main.js | 7 +++---- .../@stdlib/stats/incr/wmean/test/test.js | 15 +++++---------- 7 files changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/README.md b/lib/node_modules/@stdlib/stats/incr/wmean/README.md index 7e33b1b898bb..99c0979c420d 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/README.md +++ b/lib/node_modules/@stdlib/stats/incr/wmean/README.md @@ -62,8 +62,7 @@ If provided an input value `x` and a weight `w`, the accumulator function return ```javascript var accumulator = incrwmean(); -// Default weight is 1.0 -var mu = accumulator( 2.0 ); +var mu = accumulator( 2.0, 1.0 ); // returns 2.0 mu = accumulator( 2.0, 0.5 ); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt index 843b643bb923..f76dbba6b77a 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt @@ -11,7 +11,7 @@ `NaN`, the accumulated value is `NaN` for all future invocations. The accumulator accepts two parameters, the first is the value and the - second is the weight. If the weight is not provided, it defaults to 1.0. + second is the weight. Returns ------- @@ -23,7 +23,7 @@ > var accumulator = {{alias}}(); > var mu = accumulator() null - > mu = accumulator( 2.0 ) + > mu = accumulator( 2.0, 1.0 ) 2.0 > mu = accumulator( 2.0, 0.5 ) 2.0 diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts index 48a286610e88..758959b0081e 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -32,8 +32,7 @@ * var mu = accumulator(); * // returns null * -* // Default weight is 1.0 -* var mu = accumulator( 2.0 ); +* var mu = accumulator( 2.0, 1.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); @@ -45,7 +44,7 @@ * mu = accumulator(); * // returns 2.5 */ -declare function incrwmean(): ( x: number, w?: number ) => number | null; +declare function incrwmean(): ( x: number, w: number ) => number | null; // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts index cdc221491fc2..34bf882bf4a9 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts @@ -22,7 +22,7 @@ import incrwmean = require( './index' ); // The function returns either a number or null... { - incrwmean(); // $ExpectType (x: number, w?: number | undefined) => number | null + incrwmean(); // $ExpectType (x: number, w: number) => number | null } // The compiler throws an error if the function is any arguments diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js index 19b8305941bb..957342337b4c 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js @@ -31,8 +31,7 @@ * var mu = accumulator(); * // returns null * -* // Default weight is 1.0 -* var mu = accumulator( 2.0 ); +* var mu = accumulator( 2.0, 1.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index 9d264c02daac..223405012bc7 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -29,8 +29,7 @@ * var mu = accumulator(); * // returns null * -* // Default weight is 1.0 -* var mu = accumulator( 2.0 ); +* var mu = accumulator( 2.0, 1.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); @@ -58,10 +57,10 @@ function incrwmean() { * * @private * @param {number} [x] - new value - * @param {number} [w] - new value's weight (or 1.0 if not provided) + * @param {number} [w] - new value's weight * @returns {(number|null)} weighted mean value or null */ - function accumulator( x, w = 1.0 ) { + function accumulator( x, w ) { if ( arguments.length === 0 ) { return mu; } diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index 32a1cc2183c6..4c715a255791 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -95,25 +95,20 @@ tape( 'if not provided an input value, the accumulator function returns the curr t.end(); }); -tape( 'if weight is not provided, the accumulator function uses 1.0 as the weight', function test( t ) { - var dataX; +tape( 'if null is passed as a weight, the accumulator function returns NaN', function test( t ) { var acc; - var i; - dataX = [ 2.0, 3.0, 1.0 ]; acc = incrwmean(); - for ( i = 0; i < dataX.length; i++ ) { - acc( dataX[ i ] ); - } - t.equal( acc(), 2.0, 'returns the current accumulated mean' ); + acc(); + t.ok( isNaN( acc(2.0, null) ), 'returns NaN' ); t.end(); }); -tape( 'if null is passed as a weight, the accumulator function returns NaN', function test( t ) { +tape( 'if no weight is passed, the accumulator function returns NaN', function test( t ) { var acc; acc = incrwmean(); acc(); - t.ok( isNaN( acc(2.0, null) ), 'returns NaN' ); + t.ok( isNaN( acc(2.0) ), 'returns NaN' ); t.end(); }); From 9181f915dcabaac8bfba20f3f057abe6cc90a6d6 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Mon, 25 Feb 2019 13:49:15 +1000 Subject: [PATCH 11/15] Modify incremental algorithm Used algorithm from here http://people.ds.cam.ac.uk/fanf2/hermes/doc/antiforgery/stats.pdf --- lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js | 5 +---- lib/node_modules/@stdlib/stats/incr/wmean/test/test.js | 8 +++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index 223405012bc7..35fdcd1eb441 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -42,12 +42,10 @@ * // returns 2.5 */ function incrwmean() { - var xwSum; var wSum; var mu; wSum = 0.0; - xwSum = 0.0; mu = null; return accumulator; @@ -65,8 +63,7 @@ function incrwmean() { return mu; } wSum += w; - xwSum += x * w; - mu = xwSum / wSum; + mu += ( (w / wSum) * (x - mu) ); return mu; } } diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index 4c715a255791..1a013cf4afca 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -21,6 +21,8 @@ // MODULES // var tape = require( 'tape' ); +var EPS = require( '@stdlib/constants/math/float64-eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); var incrwmean = require( './../lib' ); @@ -46,11 +48,13 @@ tape( 'the initial accumulated value is `null`', function test( t ) { tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) { var expected; var actual; + var delta; var dataX; var dataW; var xwSum; var wSum; var acc; + var tol; var N; var x; var w; @@ -74,8 +78,10 @@ tape( 'the accumulator function incrementally computes a weighted arithmetic mea wSum += w; expected[ i ] = xwSum / wSum; actual[ i ] = acc( x, w ); + delta = abs( actual[ i ] - expected[ i ] ); + tol = EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. x: ' + x[ i ] + '. Value: ' + actual[ i ] + '. Expected: ' + expected[ i ] + '. Tolerance: ' + tol + '.' ); } - t.deepEqual( actual, expected, 'returns expected incremental results' ); t.end(); }); From 8f9d9903dcbb7ef93022be4ca2e1f8fa1f690d49 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 25 Feb 2019 16:45:50 +1000 Subject: [PATCH 12/15] Apply suggestions from code review Co-Authored-By: MatthewCochrane --- lib/node_modules/@stdlib/stats/incr/wmean/README.md | 6 +++--- lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt | 6 +++--- .../@stdlib/stats/incr/wmean/docs/types/index.d.ts | 3 ++- .../@stdlib/stats/incr/wmean/docs/types/test.ts | 2 +- lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js | 8 ++++---- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/README.md b/lib/node_modules/@stdlib/stats/incr/wmean/README.md index 99c0979c420d..590e653c02d5 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/README.md +++ b/lib/node_modules/@stdlib/stats/incr/wmean/README.md @@ -26,9 +26,9 @@ limitations under the License. The [weighted arithmetic mean][weighted-arithmetic-mean] is defined as - + -
+
Equation for the weighted arithmetic mean.
@@ -49,7 +49,7 @@ var incrwmean = require( '@stdlib/stats/incr/wmean' ); #### incrwmean() -Returns an accumulator `function` which incrementally computes an [weighted arithmetic mean][weighted-arithmetic-mean]. +Returns an accumulator `function` which incrementally computes a [weighted arithmetic mean][weighted-arithmetic-mean]. ```javascript var accumulator = incrwmean(); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt index f76dbba6b77a..3916f6ae3319 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/repl.txt @@ -3,14 +3,14 @@ Returns an accumulator function which incrementally computes a weighted arithmetic mean. - If provided parameter(s), the accumulator function returns an updated - weighted mean. If not provided a value, the accumulator function returns + If provided arguments, the accumulator function returns an updated + weighted mean. If not provided arguments, the accumulator function returns the current weighted mean. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. - The accumulator accepts two parameters, the first is the value and the + The accumulator accepts two arguments: the first is the value and the second is the weight. Returns diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts index 758959b0081e..4423742a054a 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -25,7 +25,8 @@ * * @returns accumulator function * -* @example* var incrwmean = require( '@stdlib/stats/incr/wmean' ); +* @example +* var incrwmean = require( '@stdlib/stats/incr/wmean' ); * * var accumulator = incrwmean(); * diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts index 34bf882bf4a9..ba055a2f8a31 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts @@ -25,7 +25,7 @@ import incrwmean = require( './index' ); incrwmean(); // $ExpectType (x: number, w: number) => number | null } -// The compiler throws an error if the function is any arguments +// The compiler throws an error if the function is provided arguments... { incrwmean( '5' ); // $ExpectError incrwmean( 5 ); // $ExpectError diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js index 957342337b4c..04cb78347a41 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/index.js @@ -31,7 +31,7 @@ * var mu = accumulator(); * // returns null * -* var mu = accumulator( 2.0, 1.0 ); +* mu = accumulator( 2.0, 1.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index 35fdcd1eb441..531a9476efe3 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -29,7 +29,7 @@ * var mu = accumulator(); * // returns null * -* var mu = accumulator( 2.0, 1.0 ); +* mu = accumulator( 2.0, 1.0 ); * // returns 2.0 * * mu = accumulator( 2.0, 0.5 ); @@ -51,19 +51,19 @@ function incrwmean() { return accumulator; /** - * If provided a value, the accumulator function returns an updated weighted mean. If not provided a value, the accumulator function returns the current weighted mean. + * If provided arguments, the accumulator function returns an updated weighted mean. If not provided arguments, the accumulator function returns the current weighted mean. * * @private * @param {number} [x] - new value * @param {number} [w] - new value's weight - * @returns {(number|null)} weighted mean value or null + * @returns {(number|null)} weighted mean or null */ function accumulator( x, w ) { if ( arguments.length === 0 ) { return mu; } wSum += w; - mu += ( (w / wSum) * (x - mu) ); + mu += (w / wSum) * (x - mu); return mu; } } From a861840149ff5cd81e1bfc2328ab693a9973dfa1 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Mon, 25 Feb 2019 17:08:13 +1000 Subject: [PATCH 13/15] Update tests and typescript definition --- .../@stdlib/stats/incr/wmean/docs/types/index.d.ts | 13 ++++++++++++- .../@stdlib/stats/incr/wmean/docs/types/test.ts | 2 +- .../@stdlib/stats/incr/wmean/test/test.js | 12 ++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts index 4423742a054a..0c2cbad05e10 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -20,6 +20,17 @@ /// +/** + * Accumulator function + * + * @param x value + * @param w weight + * + * @returns the incrementally computed weighted arithmetic mean. + * + */ +type incrwmean_acc = ( x: number, w: number ) => number | null; + /** * Returns an accumulator function which incrementally computes a weighted arithmetic mean. * @@ -45,7 +56,7 @@ * mu = accumulator(); * // returns 2.5 */ -declare function incrwmean(): ( x: number, w: number ) => number | null; +declare function incrwmean(): incrwmean_acc; // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts index ba055a2f8a31..7eaf0278eefb 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts @@ -22,7 +22,7 @@ import incrwmean = require( './index' ); // The function returns either a number or null... { - incrwmean(); // $ExpectType (x: number, w: number) => number | null + incrwmean(); // $ExpectType incrwmean_acc } // The compiler throws an error if the function is provided arguments... diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index 1a013cf4afca..140035f5c747 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var EPS = require( '@stdlib/constants/math/float64-eps' ); var abs = require( '@stdlib/math/base/special/abs' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); var incrwmean = require( './../lib' ); @@ -101,20 +102,11 @@ tape( 'if not provided an input value, the accumulator function returns the curr t.end(); }); -tape( 'if null is passed as a weight, the accumulator function returns NaN', function test( t ) { - var acc; - - acc = incrwmean(); - acc(); - t.ok( isNaN( acc(2.0, null) ), 'returns NaN' ); - t.end(); -}); - tape( 'if no weight is passed, the accumulator function returns NaN', function test( t ) { var acc; acc = incrwmean(); acc(); - t.ok( isNaN( acc(2.0) ), 'returns NaN' ); + t.ok( isnan( acc(2.0) ), 'returns NaN' ); t.end(); }); From 000e9290760cc1cb1645debf5c89fbaf8ec6a245 Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Mon, 25 Feb 2019 17:09:28 +1000 Subject: [PATCH 14/15] Update typescript definition --- lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts index 0c2cbad05e10..2cf7f5b75ea3 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -21,7 +21,7 @@ /// /** - * Accumulator function + * Weighted arithmetic mean accumulator function * * @param x value * @param w weight From e4f236c83373e6dac05d2388090e07d0f777f72a Mon Sep 17 00:00:00 2001 From: Matthew Cochrane Date: Mon, 25 Feb 2019 17:11:24 +1000 Subject: [PATCH 15/15] Add typescript accumulator tests --- .../@stdlib/stats/incr/wmean/docs/types/test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts index 7eaf0278eefb..98035971cf43 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts @@ -37,3 +37,18 @@ import incrwmean = require( './index' ); incrwmean( {} ); // $ExpectError incrwmean( ( x: number ): number => x ); // $ExpectError } + +// The compiler throws an error if the function is provided arguments... +{ + let acc = incrwmean(); + + acc( '5' ); // $ExpectError + acc( 5 ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( undefined ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +}