diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/README.md b/lib/node_modules/@stdlib/stats/incr/nanrss/README.md new file mode 100644 index 000000000000..5869646f6126 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/README.md @@ -0,0 +1,151 @@ + + +# incrnanrss + +> Compute the [residual sum of squares][residual-sum-of-squares] (RSS) incrementally, ignoring `NaN` values. + +
+ +The [**residual sum of squares**][residual-sum-of-squares] (also referred to as the **sum of squared residuals** (SSR) and the **sum of squared errors** (SSE)) is defined as + + + +```math +\mathop{\mathrm{RSS}} = \sum_{i=0}^{n-1} (y_i - x_i)^2 +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanrss = require( '@stdlib/stats/incr/nanrss' ); +``` + +#### incrnanrss() + +Returns an accumulator `function` which incrementally computes the [residual sum of squares][residual-sum-of-squares], ignoring `NaN` values. + +```javascript +var accumulator = incrnanrss(); +``` + +#### accumulator( \[x, y] ) + +If provided input values `x` and `y`, the accumulator function returns an updated [residual sum of squares][residual-sum-of-squares]. If not provided input values `x` and `y`, the accumulator function returns the current [residual sum of squares][residual-sum-of-squares]. + +```javascript +var accumulator = incrnanrss(); + +var r = accumulator( 2.0, 3.0 ); +// returns 1.0 + +r = accumulator( 1.0, NaN ); +// returns 1.0 + +r = accumulator( -1.0, -4.0 ); +// returns 10.0 + +r = accumulator( -3.0, 5.0 ); +// returns 74.0 + +r = accumulator(); +// returns 74.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. 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 bernoulli = require( '@stdlib/random/base/bernoulli' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var incrnanrss = require( '@stdlib/stats/incr/nanrss' ); + +var accumulator; +var v1; +var v2; +var i; + +// Initialize an accumulator: +accumulator = incrnanrss(); + +// For each simulated datum, update the residual sum of squares... +for ( i = 0; i < 100; i++ ) { + v1 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ); + v2 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ); + accumulator( v1, v2 ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanrss/benchmark/benchmark.js new file mode 100644 index 000000000000..4de443fa54f7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 incrnanrss = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanrss(); + 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 = incrnanrss(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu()-0.5, randu()-0.5 ); + 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/nanrss/docs/img/equation_residual_sum_of_squares.svg b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/img/equation_residual_sum_of_squares.svg new file mode 100644 index 000000000000..5a7aac9d4628 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/img/equation_residual_sum_of_squares.svg @@ -0,0 +1,52 @@ + +upper R upper S upper S equals sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis y Subscript i Baseline minus x Subscript i Baseline right-parenthesis squared + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/repl.txt new file mode 100644 index 000000000000..8057e9d62eba --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes the residual + sum of squares (RSS), ignoring `NaN` values. + + If provided input values, the accumulator function returns an updated + residual sum of squares. If not provided input values, the accumulator + function returns the current residual sum of squares. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var r = accumulator() + null + > r = accumulator( 2.0, 3.0 ) + 1.0 + > r = accumulator( 1.0, NaN ) + 1.0 + > r = accumulator( -5.0, 2.0 ) + 50.0 + > r = accumulator() + 50.0 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/types/index.d.ts new file mode 100644 index 000000000000..ba3c61b033f9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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: 4.1 + +/// + +/** +* If provided arguments, returns an updated residual sum of squares; otherwise, returns the current residual sum of squares. +* +* @param x - value +* @param y - value +* @returns residual sum of squares +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes the residual sum of squares. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanrss(); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* r = accumulator( 1.0, NaN ); +* // returns 1.0 +* +* r = accumulator( -5.0, 2.0 ); +* // returns 50.0 +* +* r = accumulator(); +* // returns 50.0 +*/ +declare function incrnanrss(): accumulator; + + +// EXPORTS // + +export = incrnanrss; diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/types/test.ts new file mode 100644 index 000000000000..274bbad6a723 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 incrnanrss from './index'; + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanrss(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnanrss( '5' ); // $ExpectError + incrnanrss( 5 ); // $ExpectError + incrnanrss( true ); // $ExpectError + incrnanrss( false ); // $ExpectError + incrnanrss( null ); // $ExpectError + incrnanrss( undefined ); // $ExpectError + incrnanrss( [] ); // $ExpectError + incrnanrss( {} ); // $ExpectError + incrnanrss( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanrss(); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanrss(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanrss/examples/index.js new file mode 100644 index 000000000000..90fba92d8fab --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 bernoulli = require( '@stdlib/random/base/bernoulli' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var incrnanrss = require( './../lib' ); + +var accumulator; +var rss; +var v1; +var v2; +var i; + +// Initialize an accumulator: +accumulator = incrnanrss(); + +// For each simulated datum, update the residual sum of squares... +console.log( '\nValue\tValue\tRSS\n' ); +for ( i = 0; i < 100; i++ ) { + v1 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ); + v2 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ); + rss = accumulator( v1, v2 ); + console.log( '%d\t%d\t%d', v1.toFixed( 3 ), v2.toFixed( 3 ), ( rss === null ) ? NaN : rss.toFixed( 3 ) ); +} +console.log( '\nFinal RSS: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanrss/lib/index.js new file mode 100644 index 000000000000..93b833c41577 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 the residual sum of squares incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanrss +* +* @example +* var incrnanrss = require( '@stdlib/stats/incr/nanrss' ); +* +* var accumulator = incrnanrss(); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* r = accumulator( 1.0, NaN ); +* // returns 1.0 +* +* r = accumulator( -5.0, 2.0 ); +* // returns 50.0 +* +* r = accumulator(); +* // returns 50.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanrss/lib/main.js new file mode 100644 index 000000000000..9d37f3617073 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/lib/main.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 incrrss = require( '@stdlib/stats/incr/rss' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes the residual sum of squares, ignoring `NaN` values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanrss(); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* r = accumulator( 1.0, NaN ); +* // returns 1.0 +* +* r = accumulator( -5.0, 2.0 ); +* // returns 50.0 +* +* r = accumulator(); +* // returns 50.0 +*/ +function incrnanrss() { + var rss = incrrss(); + return accumulator; + + /** + * If provided input values, the accumulator function returns an updated residual sum of squares. If not provided input values, the accumulator function returns the current residual sum of squares. + * + * @private + * @param {number} [x] - input value + * @param {number} [y] - input value + * @returns {(number|null)} residual sum of squares or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 || isnan( x ) || isnan( y ) ) { + return rss(); + } + return rss( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanrss; diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/package.json b/lib/node_modules/@stdlib/stats/incr/nanrss/package.json new file mode 100644 index 000000000000..d05305abef5b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/package.json @@ -0,0 +1,84 @@ +{ + "name": "@stdlib/stats/incr/nanrss", + "version": "0.0.0", + "description": "Compute the residual sum of squares (RSS) incrementally, ignoring NaN values.", + "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": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "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", + "error", + "err", + "rss", + "ssr", + "sse", + "residuals", + "deviation", + "difference", + "diff", + "delta", + "incremental", + "accumulator", + "time series", + "timeseries", + "forecasting", + "forecast", + "model", + "selection", + "evaluation", + "prediction", + "manhattan", + "cityblock", + "city-block", + "distance", + "dist" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanrss/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanrss/test/test.js new file mode 100644 index 000000000000..83838bd644f1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrss/test/test.js @@ -0,0 +1,124 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanrss = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanrss, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanrss(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanrss(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes the residual sum of squares', function test( t ) { + var expected; + var actual; + var delta; + var data; + var acc; + var sum; + var tol; + var N; + var r; + var x; + var y; + var i; + + data = [ + [ 2.0, 3.0 ], + [ 3.0, -1.0 ], + [ 2.0, 5.0 ], + [ 1.0, NaN ], + [ 4.0, -4.0 ], + [ NaN, 2.0 ], + [ 3.0, 0.0 ], + [ -4.0, 5.0 ] + ]; + N = data.length; + + acc = incrnanrss(); + + sum = 0; + for ( i = 0; i < N; i++ ) { + x = data[ i ][ 0 ]; + y = data[ i ][ 1 ]; + if ( isnan( x ) === false && isnan( y ) === false ) { + r = y - x; + sum += r * r; + } + expected = sum; + actual = acc( x, y ); + if ( actual === expected ) { + t.equal( actual, expected, 'returns expected value' ); + } else { + delta = abs( expected - actual ); + tol = 1.0 * EPS * abs( expected ); + t.equal( delta <= tol, true, 'within tolerance. Actual: '+actual+'. Expected: '+expected+'. Delta: '+delta+'. Tol: '+tol+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current residual sum of squares', function test( t ) { + var expected; + var actual; + var delta; + var data; + var acc; + var tol; + var i; + + data = [ + [ 2.0, 3.0 ], + [ 1.0, NaN ], + [ 3.0, -5.0 ], + [ NaN, 2.0 ], + [ 1.0, 10.0 ] + ]; + acc = incrnanrss(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + } + actual = acc(); + expected = 1.0 + 64.0 + 81.0; + delta = abs( expected - actual ); + tol = 1.0 * EPS * abs( expected ); + t.equal( delta <= tol, true, 'within tolerance. Actual: '+actual+'. Expected: '+expected+'. Delta: '+delta+'. Tol: '+tol+'.' ); + t.end(); +});