From 910cc6300755af2c9573aa49334a23db17f67dbc Mon Sep 17 00:00:00 2001 From: Ari1009 Date: Fri, 28 Mar 2025 10:22:41 +0530 Subject: [PATCH 1/5] accessor_ndarray --- .../stats/base/stdevwd/benchmark/benchmark.js | 61 ++++- .../stdevwd/benchmark/benchmark.ndarray.js | 61 ++++- .../stats/base/stdevwd/lib/accessors.js | 67 +++++ .../@stdlib/stats/base/stdevwd/lib/ndarray.js | 25 +- .../@stdlib/stats/base/stdevwd/lib/stdevwd.js | 23 +- .../stats/base/stdevwd/test/test.ndarray.js | 34 +++ .../stats/base/stdevwd/test/test.stdevwd.js | 257 +++++++++--------- 7 files changed, 360 insertions(+), 168 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js index 73ebd0677e56..9f0851e66d55 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js @@ -31,19 +31,19 @@ var stdevwd = require( './../lib/stdevwd.js' ); // FUNCTIONS // /** -* Creates a benchmark function. +* Creates a benchmark function for a native array. * * @private * @param {PositiveInteger} len - array length * @returns {Function} benchmark function */ -function createBenchmark( len ) { +function createNativeBenchmark( len ) { var x; var i; - x = []; + x = new Array( len ); for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); + x[ i ] = ( randu()*20.0 ) - 10.0; } return benchmark; @@ -67,6 +67,51 @@ function createBenchmark( len ) { } } +/** +* Creates a benchmark function for an accessor array. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createAccessorBenchmark( len ) { + var data; + var x; + var i; + + data = new Array( len ); + for ( i = 0; i < len; i++ ) { + data[ i ] = ( randu()*20.0 ) - 10.0; + } + x = { + 'get': function get( i ) { + return data[ i ]; + }, + 'set': function set() {}, + 'length': data.length + }; + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = stdevwd( x.length, 1, x, 1 ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + // MAIN // @@ -87,9 +132,11 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); - f = createBenchmark( len ); - bench( pkg+':len='+len, f ); + f = createNativeBenchmark( len ); + bench( pkg+':native:len='+len, f ); + f = createAccessorBenchmark( len ); + bench( pkg+':accessors:len='+len, f ); } } -main(); +main(); \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.ndarray.js index 040cda910a37..ffcedc86a162 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.ndarray.js @@ -31,19 +31,19 @@ var stdevwd = require( './../lib/ndarray.js' ); // FUNCTIONS // /** -* Creates a benchmark function. +* Creates a benchmark function for a native array. * * @private * @param {PositiveInteger} len - array length * @returns {Function} benchmark function */ -function createBenchmark( len ) { +function createNativeBenchmark( len ) { var x; var i; - x = []; + x = new Array( len ); for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); + x[ i ] = ( randu()*20.0 ) - 10.0; } return benchmark; @@ -67,6 +67,51 @@ function createBenchmark( len ) { } } +/** +* Creates a benchmark function for an accessor array. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createAccessorBenchmark( len ) { + var data; + var x; + var i; + + data = new Array( len ); + for ( i = 0; i < len; i++ ) { + data[ i ] = ( randu()*20.0 ) - 10.0; + } + x = { + 'get': function get( i ) { + return data[ i ]; + }, + 'set': function set() {}, + 'length': data.length + }; + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = stdevwd( x.length, 1, x, 1, 0 ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + // MAIN // @@ -87,9 +132,11 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); - f = createBenchmark( len ); - bench( pkg+':ndarray:len='+len, f ); + f = createNativeBenchmark( len ); + bench( pkg+':ndarray:native:len='+len, f ); + f = createAccessorBenchmark( len ); + bench( pkg+':ndarray:accessors:len='+len, f ); } } -main(); +main(); \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js new file mode 100644 index 000000000000..a4238927cf4f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 sqrt = require( '@stdlib/math/base/special/sqrt' ); // Fixed "req" -> "require" + +// MAIN // + +/** +* Computes the standard deviation of a strided array using Welford's algorithm and accessors. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {Object} x - input array object (with a `get` method) +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - starting index +* @returns {number} standard deviation +*/ +function stdevwd( N, correction, x, stride, offset ) { + var delta; + var mean; + var M2; + var ix; + var v; + var i; + + if ( N <= correction ) { + return NaN; + } + if ( N === 1 ) { + return 0.0; + } + ix = offset; + mean = 0.0; + M2 = 0.0; + for ( i = 0; i < N; i++ ) { + v = x.get( ix ); + delta = v - mean; + mean += delta / (i + 1); + M2 += delta * (v - mean); + ix += stride; + } + return sqrt( M2 / ( N - correction ) ); +} + +// EXPORTS // + +module.exports = stdevwd; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js index 0b03592656f9..2258d19ca72c 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2024 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. @@ -20,6 +20,8 @@ // MODULES // +var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var accessors = require( './accessors.js' ); var variancewd = require( '@stdlib/stats/base/variancewd' ).ndarray; var sqrt = require( '@stdlib/math/base/special/sqrt' ); @@ -29,32 +31,21 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); /** * Computes the standard deviation of a strided array using Welford's algorithm. * -* ## References -* -* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). -* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). -* * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {NumericArray} x - input array * @param {integer} stride - stride length * @param {NonNegativeInteger} offset - starting index * @returns {number} standard deviation -* -* @example -* var floor = require( '@stdlib/math/base/special/floor' ); -* -* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* var N = floor( x.length / 2 ); -* -* var v = stdevwd( N, 1, x, 2, 1 ); -* // returns 2.5 */ function stdevwd( N, correction, x, stride, offset ) { - return sqrt( variancewd( N, correction, x, stride, offset ) ); + if ( isAccessorArray( x ) ) { + return accessors( N, correction, x, stride, offset ); + } + return sqrt( variancewd( N, correction, x, stride, offset ) ); } // EXPORTS // -module.exports = stdevwd; +module.exports = stdevwd; // Correct export \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js index e3aeb8783a61..472916f037a2 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2024 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. @@ -20,8 +20,7 @@ // MODULES // -var variancewd = require( '@stdlib/stats/base/variancewd' ); -var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -29,28 +28,20 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); /** * Computes the standard deviation of a strided array using Welford's algorithm. * -* ## References -* -* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). -* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). -* * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {NumericArray} x - input array * @param {integer} stride - stride length * @returns {number} standard deviation -* -* @example -* var x = [ 1.0, -2.0, 2.0 ]; -* -* var v = stdevwd( x.length, 1, x, 1 ); -* // returns ~2.0817 */ function stdevwd( N, correction, x, stride ) { - return sqrt( variancewd( N, correction, x, stride ) ); + return ndarray( N, correction, x, stride, 0 ); } +// Attach the ndarray method: +stdevwd.ndarray = ndarray; + // EXPORTS // -module.exports = stdevwd; +module.exports = stdevwd; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.ndarray.js index 0b5022acab72..29aa2a03eb8e 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.ndarray.js @@ -200,3 +200,37 @@ tape( 'the function supports an `offset` parameter', function test( t ) { t.end(); }); + +tape( 'the function supports accessor arrays', function test( t ) { + var x; + var v; + + x = { + 'get': function get( i ) { + return [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ][ i ]; + }, + 'set': function set() {}, + 'length': 6 + }; + v = stdevwd( x.length, 1, x, 1, 0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles accessor arrays with negative stride', function test( t ) { + var x; + var v; + + x = { + 'get': function get( i ) { + return [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ][ i ]; + }, + 'set': function set() {}, + 'length': 6 + }; + v = stdevwd( 3, 1, x, -2, 5 ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + t.end(); +}); \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.stdevwd.js b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.stdevwd.js index dcf840ce4032..14269ee0a1f2 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.stdevwd.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.stdevwd.js @@ -31,177 +31,192 @@ var stdevwd = require( './../lib/stdevwd.js' ); // TESTS // tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof stdevwd, 'function', 'main export is a function' ); - t.end(); + t.ok( true, __filename ); + t.strictEqual( typeof stdevwd, 'function', 'main export is a function' ); + t.end(); }); tape( 'the function has an arity of 4', function test( t ) { - t.strictEqual( stdevwd.length, 4, 'has expected arity' ); - t.end(); + t.strictEqual( stdevwd.length, 4, 'has expected arity' ); + t.end(); }); tape( 'the function calculates the population standard deviation of a strided array', function test( t ) { - var x; - var v; + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = stdevwd( x.length, 0, x, 1 ); - t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevwd( x.length, 0, x, 1 ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); - x = [ -4.0, -4.0 ]; - v = stdevwd( x.length, 0, x, 1 ); - t.strictEqual( v, 0.0, 'returns expected value' ); + x = [ -4.0, -4.0 ]; + v = stdevwd( x.length, 0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ NaN, 4.0 ]; - v = stdevwd( x.length, 0, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + x = [ NaN, 4.0 ]; + v = stdevwd( x.length, 0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { - var x; - var v; + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = stdevwd( x.length, 1, x, 1 ); - t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevwd( x.length, 1, x, 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); - x = [ -4.0, -4.0 ]; - v = stdevwd( x.length, 1, x, 1 ); - t.strictEqual( v, 0.0, 'returns expected value' ); + x = [ -4.0, -4.0 ]; + v = stdevwd( x.length, 1, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ NaN, 4.0 ]; - v = stdevwd( x.length, 1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + x = [ NaN, 4.0 ]; + v = stdevwd( x.length, 1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { - var x; - var v; + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = stdevwd( 0, 1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + v = stdevwd( 0, 1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - v = stdevwd( -1, 1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + v = stdevwd( -1, 1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'if provided an `N` parameter equal to `1`, the function returns a population standard deviation of `0`', function test( t ) { - var x; - var v; + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = stdevwd( 1, 0, x, 1 ); - t.strictEqual( v, 0.0, 'returns expected value' ); + v = stdevwd( 1, 0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { - var x; - var v; + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = stdevwd( x.length, x.length, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + v = stdevwd( x.length, x.length, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - v = stdevwd( x.length, x.length+1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + v = stdevwd( x.length, x.length+1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; - var x; - var v; - - x = [ - 1.0, // 0 - 2.0, - 2.0, // 1 - -7.0, - -2.0, // 2 - 3.0, - 4.0, // 3 - 2.0 - ]; - - N = floor( x.length / 2 ); - v = stdevwd( N, 1, x, 2 ); - - t.strictEqual( v, 2.5, 'returns expected value' ); - t.end(); + var N; + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + N = floor( x.length / 2 ); + v = stdevwd( N, 1, x, 2 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); }); + tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; - var x; - var v; - - x = [ - 1.0, // 3 - 2.0, - 2.0, // 2 - -7.0, - -2.0, // 1 - 3.0, - 4.0, // 0 - 2.0 - ]; - - N = floor( x.length / 2 ); - v = stdevwd( N, 1, x, -2 ); - - t.strictEqual( v, 2.5, 'returns expected value' ); - t.end(); + var x; + var v; + + x = [ + 1.0, // 3 (index 6) + 2.0, + 2.0, // 2 (index 4) + -7.0, + -2.0, // 1 (index 2) + 3.0, + 4.0, // 0 (index 0) + 2.0 + ]; + + // Use ndarray with offset=0 (4.0 at index 6): + v = stdevwd.ndarray( 4, 1, x, -2, 6 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); }); tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { - var x; - var v; + var x; + var v; - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = stdevwd( x.length, 1, x, 0 ); - t.strictEqual( v, 0.0, 'returns expected value' ); + v = stdevwd( x.length, 1, x, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'the function supports view offsets', function test( t ) { - var x0; - var x1; - var N; - var v; - - x0 = new Float64Array([ - 2.0, - 1.0, // 0 - 2.0, - -2.0, // 1 - -2.0, - 2.0, // 2 - 3.0, - 4.0, // 3 - 6.0 - ]); - - x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - - v = stdevwd( N, 1, x1, 2 ); - t.strictEqual( v, 2.5, 'returns expected value' ); - - t.end(); + var x0; + var x1; + var N; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + N = floor(x1.length / 2); + + v = stdevwd( N, 1, x1, 2 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); }); + +tape( 'the function supports accessor arrays', function test( t ) { + var x; + var v; + + x = { + 'get': function get( i ) { + return [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ][ i ]; + }, + 'set': function set() {}, + 'length': 6 + }; + v = stdevwd( x.length, 1, x, 1 ); + t.strictEqual( v, sqrt(53.5/(x.length-1)), 'returns expected value' ); + + t.end(); +}); \ No newline at end of file From d066a91f9d63de7d958f2ab5a010dbd53eca40d1 Mon Sep 17 00:00:00 2001 From: Ari1009 Date: Fri, 28 Mar 2025 12:19:30 +0530 Subject: [PATCH 2/5] documentaion --- .../@stdlib/stats/base/stdevwd/README.md | 31 ++++++++++++++++++- .../stats/base/stdevwd/lib/accessors.js | 4 +-- .../@stdlib/stats/base/stdevwd/lib/ndarray.js | 17 ++++++++-- .../@stdlib/stats/base/stdevwd/lib/stdevwd.js | 15 +++++++-- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/README.md b/lib/node_modules/@stdlib/stats/base/stdevwd/README.md index cf40262beb09..70e487776565 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/README.md +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/README.md @@ -113,7 +113,7 @@ The function has the following parameters: - **N**: number of indexed elements. - **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). -- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or accessor array. - **stride**: index increment for `x`. The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`, @@ -172,6 +172,20 @@ var v = stdevwd.ndarray( N, 1, x, 2, 1 ); // returns 2.5 ``` +```javascript +var accessorArray = { + 'get': function get(i) { + return [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ][i]; + }, + 'set': function set(i, v) { + // No-op for this example + }, + 'length': 8 +}; +var v = stdevwd.ndarray( 4, 1, accessorArray, 2, 1 ); +console.log( v ); +``` + @@ -213,6 +227,21 @@ var v = stdevwd( x.length, 1, x, 1 ); console.log( v ); ``` +```javascript +var accessorArray = { + 'get': function get(i) { + return [ 1.0, -2.0, 3.0, -4.0, 5.0 ][i]; + }, + 'set': function set(i, v) { + // No-op for this example + }, + 'length': 5 +}; +var v = stdevwd( accessorArray.length, 1, accessorArray, 1 ); +console.log( v ); +``` +- The function supports accessor arrays, which provide an alternative way to define strided arrays using `get` and `set` methods for data access. + diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js index a4238927cf4f..0db3e32e0d77 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js @@ -1,7 +1,7 @@ -/** +/* * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2020 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. diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js index 2258d19ca72c..a348f9bc1173 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2020 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. @@ -31,12 +31,25 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); /** * Computes the standard deviation of a strided array using Welford's algorithm. * +* ## References +* +* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). +* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). +* * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {NumericArray} x - input array * @param {integer} stride - stride length * @param {NonNegativeInteger} offset - starting index * @returns {number} standard deviation +* @example +* var floor = require( '@stdlib/math/base/special/floor' ); +* +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* var N = floor( x.length / 2 ); +* +* var v = stdevwd( N, 1, x, 2, 1 ); +* // returns 2.5 */ function stdevwd( N, correction, x, stride, offset ) { if ( isAccessorArray( x ) ) { @@ -48,4 +61,4 @@ function stdevwd( N, correction, x, stride, offset ) { // EXPORTS // -module.exports = stdevwd; // Correct export \ No newline at end of file +module.exports = stdevwd; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js index 472916f037a2..8826423a38c5 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js @@ -1,7 +1,7 @@ -/** +/* * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2020 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. @@ -28,11 +28,22 @@ var ndarray = require( './ndarray.js' ); /** * Computes the standard deviation of a strided array using Welford's algorithm. * +* ## References +* +* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). +* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). +* * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {NumericArray} x - input array * @param {integer} stride - stride length * @returns {number} standard deviation +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = stdevwd( x.length, 1, x, 1 ); +* // returns ~2.0817 */ function stdevwd( N, correction, x, stride ) { return ndarray( N, correction, x, stride, 0 ); From 314e1ae2ecc57215e23965f7a0218eda31e094e6 Mon Sep 17 00:00:00 2001 From: Ari1009 Date: Thu, 3 Apr 2025 08:30:01 +0530 Subject: [PATCH 3/5] final_tes --- .../@stdlib/stats/base/stdevwd/docs/repl.txt | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt index 97e16cd66337..0094c02acbc1 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt @@ -1,4 +1,3 @@ - {{alias}}( N, correction, x, stride ) Computes the standard deviation of a strided array using Welford's algorithm. @@ -7,7 +6,7 @@ at runtime. Indexing is relative to the first index. To introduce an offset, use a typed - array view. + array view or accessor array. If `N <= 0`, the function returns `NaN`. @@ -20,7 +19,7 @@ Degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the standard deviation according to `N - c` where `c` corresponds to - the provided degrees of freedom adjustment. When computing the standard + the provided degrees of freedom adjustment.When computing the standard deviation of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample standard deviation, @@ -28,8 +27,8 @@ array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). - x: Array|TypedArray - Input array. + x: Array|TypedArray|AccessorArray + Input array (regular, typed, or accessor array). stride: integer Index increment. @@ -46,6 +45,15 @@ > {{alias}}( x.length, 1, x, 1 ) ~2.0817 + // Using accessor array: + > var accessorArray = { + > 'get': (i) => [1.0, -2.0, 2.0][i], + > 'set': (i,v) => {}, + > 'length': 3 + > }; + > {{alias}}( accessorArray.length, 1, accessorArray, 1 ) + ~2.0817 + // Using `N` and `stride` parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); @@ -86,8 +94,8 @@ array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). - x: Array|TypedArray - Input array. + x: Array|TypedArray|AccessorArray + Input array (regular, typed, or accessor array). stride: integer Index increment. @@ -107,6 +115,15 @@ > {{alias}}.ndarray( x.length, 1, x, 1, 0 ) ~2.0817 + // Using accessor array: + > var accessorArray = { + > 'get': (i) => [1.0, -2.0, 2.0][i], + > 'set': (i,v) => {}, + > 'length': 3 + > }; + > {{alias}}.ndarray( accessorArray.length, 1, accessorArray, 1, 0 ) + ~2.0817 + // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); @@ -114,5 +131,4 @@ ~2.0817 See Also - -------- - + -------- \ No newline at end of file From c6442863fc6394465eb4db3c50737d97fb8cb9e9 Mon Sep 17 00:00:00 2001 From: Ari1009 Date: Thu, 3 Apr 2025 08:31:37 +0530 Subject: [PATCH 4/5] final --- .../stats/base/stdevwd/docs/types/index.d.ts | 21 ++++++++++++++ .../stats/base/stdevwd/docs/types/test.ts | 28 +++++++++++++------ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts index 5d2ae57995a1..2b98cd8e91f4 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts @@ -22,6 +22,27 @@ import { NumericArray } from '@stdlib/types/array'; +/** +* Interface describing an accessor array. +*/ +interface AccessorArray { + /** + * Returns an array element. + */ + get( index: number ): number; + + /** + * Sets an array element. + */ + set( index: number, value: number ): void; + + /** + * Number of elements. + */ + length: number; +} + + /** * Interface describing `stdevwd`. */ diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts index 2af2b66461f1..96c001860eb5 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts @@ -28,6 +28,17 @@ import stdevwd = require( './index' ); stdevwd( x.length, 1, x, 1 ); // $ExpectType number } +// The function supports accessor arrays... +{ + const accessorArray = { + get: ( i: number ) => [1.0, -2.0, 3.0][i], + set: ( i: number, v: number ) => { /* no-op */ }, + length: 3 + }; + stdevwd( accessorArray.length, 1, accessorArray, 1 ); // $ExpectType number + stdevwd.ndarray( accessorArray.length, 1, accessorArray, 1, 0 ); // $ExpectType number +} + // The compiler throws an error if the function is provided a first argument which is not a number... { const x = new Float64Array( 10 ); @@ -46,14 +57,15 @@ import stdevwd = require( './index' ); { const x = new Float64Array( 10 ); - stdevwd( x.length, '10', x, 1 ); // $ExpectError - stdevwd( x.length, true, x, 1 ); // $ExpectError - stdevwd( x.length, false, x, 1 ); // $ExpectError - stdevwd( x.length, null, x, 1 ); // $ExpectError - stdevwd( x.length, undefined, x, 1 ); // $ExpectError - stdevwd( x.length, [], x, 1 ); // $ExpectError - stdevwd( x.length, {}, x, 1 ); // $ExpectError - stdevwd( x.length, ( x: number ): number => x, x, 1 ); // $ExpectError + stdevwd( x.length, 1, 10, 1 ); // $ExpectError + stdevwd( x.length, 1, '10', 1 ); // $ExpectError + stdevwd( x.length, 1, true, 1 ); // $ExpectError + stdevwd( x.length, 1, false, 1 ); // $ExpectError + stdevwd( x.length, 1, null, 1 ); // $ExpectError + stdevwd( x.length, 1, undefined, 1 ); // $ExpectError + stdevwd( x.length, 1, [ '1' ], 1 ); // $ExpectError + stdevwd( x.length, 1, {}, 1 ); // $ExpectError (plain object lacks accessor methods) + stdevwd( x.length, 1, ( x: number ): number => x, 1 ); // $ExpectError } // The compiler throws an error if the function is provided a third argument which is not a numeric array... From 0c6b1ac38394bfb2e699a8adee0802b3acc60ec8 Mon Sep 17 00:00:00 2001 From: Ari1009 Date: Fri, 4 Apr 2025 09:18:00 +0530 Subject: [PATCH 5/5] fix --- .../@stdlib/stats/base/stdevwd/README.md | 19 ++++++---- .../stats/base/stdevwd/docs/types/index.d.ts | 35 +++++++++---------- .../stats/base/stdevwd/docs/types/test.ts | 12 +------ .../stats/base/stdevwd/lib/accessors.js | 2 +- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/README.md b/lib/node_modules/@stdlib/stats/base/stdevwd/README.md index d0f8e97c9aa9..359dc5946982 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/README.md +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/README.md @@ -173,15 +173,18 @@ var v = stdevwd.ndarray( N, 1, x, 2, 1 ); ``` ```javascript +var stdevwd = require( '@stdlib/stats/base/stdevwd' ); + var accessorArray = { - 'get': function get(i) { + get(i) { return [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ][i]; }, - 'set': function set(i, v) { + set(i, v) { // No-op for this example }, - 'length': 8 + length: 8 }; + var v = stdevwd.ndarray( 4, 1, accessorArray, 2, 1 ); console.log( v ); ``` @@ -228,18 +231,22 @@ console.log( v ); ``` ```javascript +var stdevwd = require( '@stdlib/stats/base/stdevwd' ); + var accessorArray = { - 'get': function get(i) { + get(i) { return [ 1.0, -2.0, 3.0, -4.0, 5.0 ][i]; }, - 'set': function set(i, v) { + set(i, v) { // No-op for this example }, - 'length': 5 + length: 5 }; + var v = stdevwd( accessorArray.length, 1, accessorArray, 1 ); console.log( v ); ``` + - The function supports accessor arrays, which provide an alternative way to define strided arrays using `get` and `set` methods for data access. diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts index 2b98cd8e91f4..4d79fffb05f9 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/index.d.ts @@ -1,4 +1,4 @@ -/* +/** * @license Apache-2.0 * * Copyright (c) 2020 The Stdlib Authors. @@ -26,22 +26,21 @@ import { NumericArray } from '@stdlib/types/array'; * Interface describing an accessor array. */ interface AccessorArray { - /** - * Returns an array element. - */ - get( index: number ): number; - - /** - * Sets an array element. - */ - set( index: number, value: number ): void; - - /** - * Number of elements. - */ - length: number; -} + /** + * Returns an array element. + */ + get( index: number ): number; + + /** + * Sets an array element. + */ + set( index: number, value: number ): void; + /** + * Number of elements. + */ + length: number; +} /** * Interface describing `stdevwd`. @@ -106,7 +105,5 @@ interface Routine { */ declare var stdevwd: Routine; - // EXPORTS // - -export = stdevwd; +export = stdevwd; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts index 96c001860eb5..2138d1145edd 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import stdevwd = require( './index' ); @@ -28,17 +29,6 @@ import stdevwd = require( './index' ); stdevwd( x.length, 1, x, 1 ); // $ExpectType number } -// The function supports accessor arrays... -{ - const accessorArray = { - get: ( i: number ) => [1.0, -2.0, 3.0][i], - set: ( i: number, v: number ) => { /* no-op */ }, - length: 3 - }; - stdevwd( accessorArray.length, 1, accessorArray, 1 ); // $ExpectType number - stdevwd.ndarray( accessorArray.length, 1, accessorArray, 1, 0 ); // $ExpectType number -} - // The compiler throws an error if the function is provided a first argument which is not a number... { const x = new Float64Array( 10 ); diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js index 0db3e32e0d77..defe85b53ad3 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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.