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..590e653c02d5 --- /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 a [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() ); +``` + +
+ + + + + + 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(); +}); 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..3916f6ae3319 --- /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 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 arguments: the first is the value and the + second is the weight. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var mu = accumulator() + null + > mu = accumulator( 2.0, 1.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 + -------- + 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..2cf7f5b75ea3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @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 + +/// + +/** + * Weighted arithmetic mean 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. +* +* @returns accumulator function +* +* @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 +*/ +declare function incrwmean(): incrwmean_acc; + + +// 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..98035971cf43 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.ts @@ -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. +*/ + +import incrwmean = require( './index' ); + +// TESTS // + +// The function returns either a number or null... +{ + incrwmean(); // $ExpectType incrwmean_acc +} + +// The compiler throws an error if the function is provided 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 +} + +// 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 +} 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() ); 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..04cb78347a41 --- /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 +* +* 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..531a9476efe3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -0,0 +1,74 @@ +/** +* @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 +* +* 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 mu; + + wSum = 0.0; + mu = null; + + return accumulator; + + /** + * 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 or null + */ + function accumulator( x, w ) { + if ( arguments.length === 0 ) { + return mu; + } + wSum += w; + mu += (w / wSum) * (x - mu); + return mu; + } +} + + +// EXPORTS // + +module.exports = incrwmean; 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" + ] +} 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..140035f5c747 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -0,0 +1,112 @@ +/** +* @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 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' ); + + +// 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 delta; + var dataX; + var dataW; + var xwSum; + var wSum; + var acc; + var tol; + 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 ); + 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.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 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.end(); +});