diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/README.md b/lib/node_modules/@stdlib/stats/incr/nanmda/README.md new file mode 100644 index 000000000000..7029ed5f4660 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/README.md @@ -0,0 +1,175 @@ + + +# incrnanmda + +> Compute the [mean directional accuracy][mean-directional-accuracy] (MDA) incrementally, ignoring `NaN` value. + +
+ +The [mean directional accuracy][mean-directional-accuracy] is defined as + + + +```math +\mathop{\mathrm{MDA}} = \begin{cases} 1 & \textrm{if}\ N = 1 \\\frac{1}{N} \sum_{i=1}^{N} \delta_{\mathop{\mathrm{sgn}}(\Delta f_{i,i-1}),\ \mathop{\mathrm{sgn}}(\Delta a_{i,i-1})} & \textrm{if}\ N > 1 \end{cases} +``` + + + + + +where `f_i` is the forecast value, `a_i` is the actual value, `sgn(x)` is the [signum][@stdlib/math/base/special/signum] function, and `δ` is the [Kronecker delta][@stdlib/math/base/special/kronecker-delta]. + +
+ + + +
+ +## Usage + +```javascript +var incrnanmda = require( '@stdlib/stats/incr/nanmda' ); +``` + +#### incrnanmda() + +Returns an accumulator `function` which incrementally computes the [mean directional accuracy][mean-directional-accuracy], ignoring `NaN` value. + +```javascript +var accumulator = incrnanmda(); +``` + +#### accumulator( \[f, a] ) + +If provided input values `f` and `a`, the accumulator function returns an updated [mean directional accuracy][mean-directional-accuracy]. If not provided input values `f` and `a`, the accumulator function returns the current [mean directional accuracy][mean-directional-accuracy]. + +```javascript +var accumulator = incrnanmda(); + +var m = accumulator( 2.0, 3.0 ); +// returns 1.0 + +m = accumulator( -1.0, 4.0 ); +// returns 0.5 + +m = accumulator( NaN, 4.0 ); +// returns 0.5 + +m = accumulator( -3.0, -2.0 ); +// returns ~0.67 + +m = accumulator(); +// returns ~0.67 +``` + +
+ + + +
+ +## Notes + +- Input values are type checked. If provided a NaN or a non-numeric value, the update is skipped and the accumulator returns the previously computed mean directional accuracy. 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 incrnanmda = require( '@stdlib/stats/incr/nanmda' ); + +var accumulator; +var v1; +var v2; +var i; + +// Initialize an accumulator: +accumulator = incrnanmda(); + +// For each simulated datum, update the mean directional accuracy... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v1 = NaN; + } else { + v1 = ( randu()*100.0 ) - 50.0; + } + if ( randu() < 0.2 ) { + v2 = NaN; + } else { + v2 = ( randu()*100.0 ) - 50.0; + } + accumulator( v1, v2 ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmda/benchmark/benchmark.js new file mode 100644 index 000000000000..37dc07703143 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/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 incrnanmda = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmda(); + 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 = incrnanmda(); + + 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/nanmda/docs/img/equation_mean_directional_accuracy.svg b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/img/equation_mean_directional_accuracy.svg new file mode 100644 index 000000000000..c289bdf791d2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/img/equation_mean_directional_accuracy.svg @@ -0,0 +1,115 @@ + +upper M upper D upper A equals StartLayout Enlarged left-brace 1st Row 1st Column 1 2nd Column if upper N equals 1 2nd Row 1st Column StartFraction 1 Over upper N EndFraction sigma-summation Underscript i equals 1 Overscript upper N Endscripts delta Subscript s g n left-parenthesis normal upper Delta f Sub Subscript i comma i minus 1 Subscript right-parenthesis comma s g n left-parenthesis normal upper Delta a Sub Subscript i comma i minus 1 Subscript right-parenthesis Baseline 2nd Column if upper N greater-than 1 EndLayout + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/repl.txt new file mode 100644 index 000000000000..d9576e8203d4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes the mean + directional accuracy (MDA), ignoring `NaN` value. + + If provided input values, the accumulator function returns an updated mean + directional accuracy. If not provided input values, the accumulator function + returns the current mean directional accuracy. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, the accumulated value is `NaN` for all future invocations. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var m = accumulator() + null + > m = accumulator( 2.0, 3.0 ) + 1.0 + > m = accumulator( -5.0, 4.0 ) + 0.5 + > m = accumulator( NaN, 4.0 ) + 0.5 + > m = accumulator() + 0.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/types/index.d.ts new file mode 100644 index 000000000000..1560e2cc9ed3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @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 a value, the accumulator function returns an updated mean directional accuracy. If not provided a value, the accumulator function returns the current mean directional accuracy. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param f - forecast value +* @param a - actual value +* @returns mean directional accuracy or null +*/ +type accumulator = ( f?: number, a?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes the mean directional accuracy, ignoring `NaN` value. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanmda(); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* m = accumulator( -5.0, 4.0 ); +* // returns 0.5 +* +* m = accumulator( NaN, 4.0 ); +* // returns 0.5 +* +* m = accumulator(); +* // returns 0.5 +*/ +declare function incrnanmda(): accumulator; + + +// EXPORTS // + +export = incrnanmda; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmda/docs/types/test.ts new file mode 100644 index 000000000000..12aa31807feb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/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 innancrmda = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + innancrmda(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + innancrmda( '5' ); // $ExpectError + innancrmda( 5 ); // $ExpectError + innancrmda( true ); // $ExpectError + innancrmda( false ); // $ExpectError + innancrmda( null ); // $ExpectError + innancrmda( undefined ); // $ExpectError + innancrmda( [] ); // $ExpectError + innancrmda( {} ); // $ExpectError + innancrmda( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = innancrmda(); + + 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 = innancrmda(); + + acc( '5', 2.0 ); // $ExpectError + acc( true, 2.0 ); // $ExpectError + acc( false, 2.0 ); // $ExpectError + acc( null, 2.0 ); // $ExpectError + acc( [], 2.0 ); // $ExpectError + acc( {}, 2.0 ); // $ExpectError + acc( ( x: number ): number => x, 2.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/nanmda/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmda/examples/index.js new file mode 100644 index 000000000000..176d4b68da0c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/examples/index.js @@ -0,0 +1,49 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var incrnanmda = require( './../lib' ); + +var accumulator; +var v2; +var v1; +var m; +var i; + +// Initialize an accumulator: +accumulator = incrnanmda(); + +// For each simulated datum, update the mean directional accuracy... +console.log( '\nValue\tValue\tMDA\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.1 ) { + v1 = NaN; + } else { + v1 = ( randu()*100.0 ) - 50.0; + } + if ( randu() < 0.1 ) { + v2 = NaN; + } else { + v2 = ( randu()*100.0 ) - 50.0; + } + m = accumulator( v1, v2 ); + console.log( '%d\t%d\t%d', v1.toFixed( 3 ), v2.toFixed( 3 ), m.toFixed( 3 ) ); +} +console.log( '\nFinal MDA: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmda/lib/index.js new file mode 100644 index 000000000000..dec2415dff35 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/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 mean directional accuracy (MDA) incrementally, ignoring `NaN` value. +* +* @module @stdlib/stats/incr/nanmda +* +* @example +* var incrnanmda = require( '@stdlib/stats/incr/nanmda' ); +* +* var accumulator = incrnanmda(); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* m = accumulator( -5.0, 4.0 ); +* // returns 0.5 +* +* m = accumulator( NaN, 4.0 ); +* // returns 0.5 +* +* m = accumulator(); +* // returns 0.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmda/lib/main.js new file mode 100644 index 000000000000..ccd52031a7e0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/lib/main.js @@ -0,0 +1,76 @@ +/** +* @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 incrmda = require( '@stdlib/stats/incr/mda' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes the mean directional accuracy, ignoring `NaN` value. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmda(); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* m = accumulator( -5.0, 4.0 ); +* // returns 0.5 +* +* m = accumulator( NaN, 4.0 ); +* // returns 0.5 +* +* m = accumulator(); +* // returns 0.5 +*/ +function incrnanmda() { + var mda = incrmda(); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated mean directional accuracy. If not provided a value, the accumulator function returns the current mean directional accuracy. + * + * @private + * @param {number} [f] - forecast value + * @param {number} [a] - actual value + * @returns {(number|null)} mean directional accuracy or null + */ + function accumulator( f, a ) { + if ( arguments.length === 0 || isnan( f ) || isnan( a ) || !isNumber( f ) || !isNumber( a )) { + return mda(); + } + return mda( f, a ); + } +} + + +// EXPORTS // + +module.exports = incrnanmda; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/package.json b/lib/node_modules/@stdlib/stats/incr/nanmda/package.json new file mode 100644 index 000000000000..1c934378fe82 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/stats/incr/nanmda", + "version": "0.0.0", + "description": "Compute the mean directional accuracy (MDA) 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": { + "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", + "average", + "avg", + "mean", + "direction", + "directional", + "accuracy", + "trend", + "incremental", + "accumulator", + "time series", + "timeseries", + "demand", + "forecasting", + "forecast", + "economics", + "econ", + "finance" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmda/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmda/test/test.js new file mode 100644 index 000000000000..8cb75517c35b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmda/test/test.js @@ -0,0 +1,110 @@ +/** +* @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 incrnanmda = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmda, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmda(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanmda(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes the mean directional accuracy', function test( t ) { + var expected; + var actual; + var delta; + var data; + var acc; + var tol; + var N; + var i; + + data = [ + [ 2.0, -3.0 ], // +,+ + [ 3.0, 1.0 ], // +,+ + [ NaN, 1.0 ], // +,+ + [ 2.0, 5.0 ], // -,+ + [ 4.0, -4.0 ], // +,- + [ -3.0, -1.0 ], // -,+ + [ 4.0, 5.0 ] // +,- + ]; + N = data.length; + + expected = [ + 1.0, + 2.0/2.0, + 2.0/2.0, + 2.0/3.0, + 2.0/4.0, + 2.0/5.0, + 3.0/6.0 + ]; + + acc = incrnanmda(); + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + if ( actual === expected[ i ] ) { + t.equal( actual, expected[ i ], 'returns expected value' ); + } else { + delta = abs( expected[ i ] - actual ); + tol = 1.0 * EPS * abs( expected[ i ] ); + t.equal( delta <= tol, true, 'within tolerance. Actual: '+actual+'. Expected: '+expected[ i ]+'. Delta: '+delta+'. Tol: '+tol+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current mean directional accuracy', function test( t ) { + var data; + var acc; + var i; + + data = [ + [ 2.0, -3.0 ], // +,+ + [ NaN, -3.0 ], // +,+ + [ -3.0, 5.0 ], // -,+ + [ -1.0, 14.0 ] // +,+ + ]; + acc = incrnanmda(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + } + t.equal( acc(), 2.0/3.0, 'returns expected value' ); + t.end(); +});