From 910cc6300755af2c9573aa49334a23db17f67dbc Mon Sep 17 00:00:00 2001 From: Ari1009 Date: Fri, 28 Mar 2025 10:22:41 +0530 Subject: [PATCH 1/9] 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/9] 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/9] 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/9] 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/9] 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. From 2b3a1783db2bd8d4ac770db32923e68505920119 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sat, 5 Jul 2025 16:58:30 +0000 Subject: [PATCH 6/9] fix: implementation --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/stdevwd/README.md | 84 +--- .../stats/base/stdevwd/benchmark/benchmark.js | 70 +--- .../stdevwd/benchmark/benchmark.ndarray.js | 70 +--- .../@stdlib/stats/base/stdevwd/docs/repl.txt | 68 ++-- .../stats/base/stdevwd/docs/types/index.d.ts | 39 +- .../stats/base/stdevwd/docs/types/test.ts | 19 +- .../stats/base/stdevwd/examples/index.js | 14 +- .../stats/base/stdevwd/lib/accessors.js | 67 ---- .../@stdlib/stats/base/stdevwd/lib/index.js | 4 +- .../@stdlib/stats/base/stdevwd/lib/ndarray.js | 23 +- .../@stdlib/stats/base/stdevwd/lib/stdevwd.js | 14 +- .../stats/base/stdevwd/test/test.main.js | 360 ++++++++++++++++++ .../stats/base/stdevwd/test/test.ndarray.js | 189 +++++++-- .../stats/base/stdevwd/test/test.stdevwd.js | 222 ----------- 14 files changed, 620 insertions(+), 623 deletions(-) delete mode 100644 lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js create mode 100644 lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js delete mode 100644 lib/node_modules/@stdlib/stats/base/stdevwd/test/test.stdevwd.js diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/README.md b/lib/node_modules/@stdlib/stats/base/stdevwd/README.md index f233b2920d01..a87eb8f7fdf7 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/README.md +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/README.md @@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var stdevwd = require( '@stdlib/stats/base/stdevwd' ); ``` -#### stdevwd( N, correction, x, stride ) +#### stdevwd( N, correction, x, strideX ) -Computes the [standard deviation][standard-deviation] of a strided array `x` using Welford's algorithm. +Computes the [standard deviation][standard-deviation] of a strided array using Welford's algorithm. ```javascript var x = [ 1.0, -2.0, 2.0 ]; @@ -113,18 +113,15 @@ 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], [`typed array`][mdn-typed-array], or accessor array. -- **stride**: index increment for `x`. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length 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`, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`, ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; -var N = floor( x.length / 2 ); -var v = stdevwd( N, 1, x, 2 ); +var v = stdevwd( 4, 1, x, 2 ); // returns 2.5 ``` @@ -134,18 +131,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = stdevwd( N, 1, x1, 2 ); +var v = stdevwd( 4, 1, x1, 2 ); // returns 2.5 ``` -#### stdevwd.ndarray( N, correction, x, stride, offset ) +#### stdevwd.ndarray( N, correction, x, strideX, offsetX ) Computes the [standard deviation][standard-deviation] of a strided array using Welford's algorithm and alternative indexing semantics. @@ -158,37 +152,17 @@ var v = stdevwd.ndarray( x.length, 1, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [standard deviation][standard-deviation] for every other value in `x` starting from the second value +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [standard deviation][standard-deviation] for every other element in the strided array starting from the second element ```javascript -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.ndarray( N, 1, x, 2, 1 ); +var v = stdevwd.ndarray( 4, 1, x, 2, 1 ); // returns 2.5 ``` -```javascript -var stdevwd = require( '@stdlib/stats/base/stdevwd' ); - -var accessorArray = { - get(i) { - return [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ][i]; - }, - set(i, v) { - // No-op for this example - }, - length: 8 -}; - -var v = stdevwd.ndarray( 4, 1, accessorArray, 2, 1 ); -console.log( v ); -``` - @@ -199,6 +173,7 @@ console.log( v ); - If `N <= 0`, both functions return `NaN`. - If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). - Depending on the environment, the typed versions ([`dstdevwd`][@stdlib/stats/strided/dstdevwd], [`sstdevwd`][@stdlib/stats/base/sstdevwd], etc.) are likely to be significantly more performant. @@ -212,43 +187,18 @@ console.log( v ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var stdevwd = require( '@stdlib/stats/base/stdevwd' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = stdevwd( x.length, 1, x, 1 ); console.log( v ); ``` -```javascript -var stdevwd = require( '@stdlib/stats/base/stdevwd' ); - -var accessorArray = { - get(i) { - return [ 1.0, -2.0, 3.0, -4.0, 5.0 ][i]; - }, - 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. - @@ -294,6 +244,8 @@ console.log( v ); [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022 [@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 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 9f0851e66d55..6a7daa0ea303 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js @@ -21,75 +21,31 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var stdevwd = require( './../lib/stdevwd.js' ); -// FUNCTIONS // +// VARIABLES // -/** -* Creates a benchmark function for a native array. -* -* @private -* @param {PositiveInteger} len - array length -* @returns {Function} benchmark function -*/ -function createNativeBenchmark( len ) { - var x; - var i; +var options = { + 'dtype': 'generic' +}; - x = new Array( len ); - for ( i = 0; i < len; i++ ) { - x[ i ] = ( randu()*20.0 ) - 10.0; - } - 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(); - } -} +// FUNCTIONS // /** -* Creates a benchmark function for an accessor array. +* Creates a benchmark function. * * @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 - }; +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { @@ -132,11 +88,9 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); - f = createNativeBenchmark( len ); - bench( pkg+':native:len='+len, f ); - f = createAccessorBenchmark( len ); - bench( pkg+':accessors:len='+len, f ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); } } -main(); \ No newline at end of file +main(); 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 ffcedc86a162..a35722b0a1a0 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 @@ -21,75 +21,31 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var stdevwd = require( './../lib/ndarray.js' ); -// FUNCTIONS // +// VARIABLES // -/** -* Creates a benchmark function for a native array. -* -* @private -* @param {PositiveInteger} len - array length -* @returns {Function} benchmark function -*/ -function createNativeBenchmark( len ) { - var x; - var i; +var options = { + 'dtype': 'generic' +}; - x = new Array( len ); - for ( i = 0; i < len; i++ ) { - x[ i ] = ( randu()*20.0 ) - 10.0; - } - 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(); - } -} +// FUNCTIONS // /** -* Creates a benchmark function for an accessor array. +* Creates a benchmark function. * * @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 - }; +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { @@ -132,11 +88,9 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); - f = createNativeBenchmark( len ); - bench( pkg+':ndarray:native:len='+len, f ); - f = createAccessorBenchmark( len ); - bench( pkg+':ndarray:accessors:len='+len, f ); + f = createBenchmark( len ); + bench( pkg+':ndarray:len='+len, f ); } } -main(); \ No newline at end of file +main(); 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 0094c02acbc1..984e9f4e98a1 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt @@ -1,12 +1,13 @@ -{{alias}}( N, correction, x, stride ) + +{{alias}}( N, correction, x, strideX ) Computes the standard deviation of a strided array using Welford's algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed - array view or accessor array. + array view. If `N <= 0`, the function returns `NaN`. @@ -19,7 +20,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, @@ -27,11 +28,11 @@ array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). - x: Array|TypedArray|AccessorArray - Input array (regular, typed, or accessor array). + x: Array|TypedArray + Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -45,36 +46,24 @@ > {{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: + // 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 ); - > var stride = 2; - > {{alias}}( N, 1, x, stride ) + > {{alias}}( 3, 1, x, 2 ) ~2.0817 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, 1, x1, stride ) + > {{alias}}( 3, 1, x1, 2 ) ~2.0817 -{{alias}}.ndarray( N, correction, x, stride, offset ) + +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the standard deviation of a strided array using Welford's algorithm and alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the `offset` parameter supports indexing semantics based on a + buffer, the offset parameter supports indexing semantics based on a starting index. Parameters @@ -94,13 +83,13 @@ array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). - x: Array|TypedArray|AccessorArray - Input array (regular, typed, or accessor array). + x: Array|TypedArray + Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -115,20 +104,11 @@ > {{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 ); - > {{alias}}.ndarray( N, 1, x, 2, 1 ) + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~2.0817 See Also - -------- \ No newline at end of file + -------- + 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 4d79fffb05f9..74822c77fe45 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. @@ -20,27 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; /** -* Interface describing an accessor array. +* Input 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; -} +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `stdevwd`. @@ -52,7 +37,7 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns standard deviation * * @example @@ -61,7 +46,7 @@ interface Routine { * var v = stdevwd( x.length, 1, x, 1 ); * // returns ~2.0817 */ - ( N: number, correction: number, x: NumericArray, stride: number ): number; + ( N: number, correction: number, x: InputArray, strideX: number ): number; /** * Computes the standard deviation of a strided array using Welford's algorithm and alternative indexing semantics. @@ -69,8 +54,8 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns standard deviation * * @example @@ -79,7 +64,7 @@ interface Routine { * var v = stdevwd.ndarray( x.length, 1, x, 1, 0 ); * // returns ~2.0817 */ - ndarray( N: number, correction: number, x: NumericArray, stride: number, offset: number ): number; + ndarray( N: number, correction: number, x: InputArray, strideX: number, offsetX: number ): number; } /** @@ -88,7 +73,7 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns standard deviation * * @example @@ -105,5 +90,7 @@ interface Routine { */ declare var stdevwd: Routine; + // EXPORTS // -export = stdevwd; \ No newline at end of file + +export = 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 2138d1145edd..f4d526b751ab 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 @@ -27,6 +27,7 @@ import stdevwd = require( './index' ); const x = new Float64Array( 10 ); stdevwd( x.length, 1, x, 1 ); // $ExpectType number + stdevwd( x.length, 1, new AccessorArray( x ), 1 ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -47,15 +48,14 @@ import stdevwd = require( './index' ); { const x = new Float64Array( 10 ); - 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 + 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 } // The compiler throws an error if the function is provided a third argument which is not a numeric array... @@ -103,6 +103,7 @@ import stdevwd = require( './index' ); const x = new Float64Array( 10 ); stdevwd.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + stdevwd.ndarray( x.length, 1, new AccessorArray( x ), 1, 0 ); // $ExpectType number } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/examples/index.js b/lib/node_modules/@stdlib/stats/base/stdevwd/examples/index.js index 02ea6cb7e1a2..1e15a9bef0e2 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/examples/index.js @@ -18,18 +18,12 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var stdevwd = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = stdevwd( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js deleted file mode 100644 index defe85b53ad3..000000000000 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/accessors.js +++ /dev/null @@ -1,67 +0,0 @@ -/* -* @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 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/index.js b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/index.js index 3d31b99ba6a7..0c8641bb089a 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/index.js @@ -32,13 +32,11 @@ * // returns ~2.0817 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var stdevwd = require( '@stdlib/stats/base/stdevwd' ); * * 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.ndarray( N, 1, x, 2, 1 ); +* var v = stdevwd.ndarray( 4, 1, x, 2, 1 ); * // returns 2.5 */ 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 a348f9bc1173..c279156b14b0 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/ndarray.js @@ -20,9 +20,7 @@ // 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 variancewd = require( '@stdlib/stats/strided/variancewd' ).ndarray; var sqrt = require( '@stdlib/math/base/special/sqrt' ); @@ -39,26 +37,21 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * @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 +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} standard deviation -* @example -* var floor = require( '@stdlib/math/base/special/floor' ); * +* @example * 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 ); +* var v = stdevwd( 4, 1, x, 2, 1 ); * // returns 2.5 */ -function stdevwd( N, correction, x, stride, offset ) { - if ( isAccessorArray( x ) ) { - return accessors( N, correction, x, stride, offset ); - } - return sqrt( variancewd( N, correction, x, stride, offset ) ); +function stdevwd( N, correction, x, strideX, offsetX ) { + return sqrt( variancewd( N, correction, x, strideX, offsetX ) ); } // EXPORTS // -module.exports = stdevwd; \ No newline at end of file +module.exports = stdevwd; 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 8826423a38c5..91c2e166ae6f 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/lib/stdevwd.js @@ -1,4 +1,4 @@ -/* +/** * @license Apache-2.0 * * Copyright (c) 2020 The Stdlib Authors. @@ -20,6 +20,7 @@ // MODULES // +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); @@ -36,7 +37,7 @@ var ndarray = require( './ndarray.js' ); * @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 {integer} strideX - stride length * @returns {number} standard deviation * * @example @@ -45,14 +46,11 @@ var ndarray = require( './ndarray.js' ); * var v = stdevwd( x.length, 1, x, 1 ); * // returns ~2.0817 */ -function stdevwd( N, correction, x, stride ) { - return ndarray( N, correction, x, stride, 0 ); +function stdevwd( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } -// Attach the ndarray method: -stdevwd.ndarray = ndarray; - // EXPORTS // -module.exports = stdevwd; \ No newline at end of file +module.exports = stdevwd; diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js new file mode 100644 index 000000000000..d5431c8b4e45 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js @@ -0,0 +1,360 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var stdevwd = require( './../lib/main.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(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + 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; + + 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 = [ NaN, 4.0 ]; + v = stdevwd( x.length, 0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population standard deviation of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevwd( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevwd( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevwd( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { + 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 = [ -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' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevwd( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevwd( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevwd( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + 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; + + 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( -1, 1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevwd( -1, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + 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; + + 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' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns a population standard deviation of `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( 1, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + 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; + + 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+1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( x.length, x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevwd( x.length, x.length+1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a `stride` parameter', function test( t ) { + 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 + ]; + + v = stdevwd( 4, 1, x, 2 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + 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 + ]; + + v = stdevwd( 4, 1, toAccessorArray( x ), 2 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter', function test( t ) { + 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 + ]; + + v = stdevwd( 4, 1, x, -2 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + 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 + ]; + + v = stdevwd( 4, 1, toAccessorArray( x ), -2 ); + + 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; + + 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' ); + + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( x.length, 1, toAccessorArray( x ), 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var x0; + var x1; + 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 + + v = stdevwd( 4, 1, x1, 2 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessors)', function test( t ) { + var x0; + var x1; + 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 + + v = stdevwd( 4, 1, toAccessorArray( x1 ), 2 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); +}); 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 29aa2a03eb8e..18ee231bed15 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 @@ -21,9 +21,9 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var stdevwd = require( './../lib/ndarray.js' ); @@ -59,6 +59,25 @@ tape( 'the function calculates the population standard deviation of a strided ar t.end(); }); +tape( 'the function calculates the population standard deviation of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevwd( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevwd( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevwd( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { var x; var v; @@ -78,6 +97,25 @@ tape( 'the function calculates the sample standard deviation of a strided array' t.end(); }); +tape( 'the function calculates the sample standard deviation of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdevwd( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevwd( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevwd( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + 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; @@ -93,6 +131,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevwd( -1, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + 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; @@ -105,6 +158,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a populat t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns a population standard deviation of `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( 1, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + 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; @@ -120,8 +185,22 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or t.end(); }); +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevwd( x.length, x.length+1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -136,15 +215,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = stdevwd( N, 1, x, 2, 0 ); + v = stdevwd( 4, 1, x, 2, 0 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + 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 + ]; + + v = stdevwd( 4, 1, toAccessorArray( x ), 2, 0 ); 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; @@ -159,8 +257,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = stdevwd( N, 1, x, -2, 6 ); + v = stdevwd( 4, 1, x, -2, 6 ); + + t.strictEqual( v, 2.5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + 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 + ]; + + v = stdevwd( 4, 1, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); @@ -178,8 +296,19 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdevwd( x.length, 1, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -193,44 +322,30 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = stdevwd( N, 1, x, 2, 1 ); + v = stdevwd( 4, 1, x, 2, 1 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); -tape( 'the function supports accessor arrays', function test( t ) { +tape( 'the function supports an `offset` parameter (accessors)', 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 = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; - 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' ); + v = stdevwd( 4, 1, toAccessorArray( x ), 2, 1 ); + t.strictEqual( v, 2.5, '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 deleted file mode 100644 index 14269ee0a1f2..000000000000 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.stdevwd.js +++ /dev/null @@ -1,222 +0,0 @@ -/** -* @license Apache-2.0 -* -* 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. -* 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 floor = require( '@stdlib/math/base/special/floor' ); -var sqrt = require( '@stdlib/math/base/special/sqrt' ); -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var Float64Array = require( '@stdlib/array/float64' ); -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(); -}); - -tape( 'the function has an arity of 4', function test( t ) { - 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; - - 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 = [ NaN, 4.0 ]; - v = stdevwd( x.length, 0, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { - 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 = [ -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' ); - - 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; - - 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( -1, 1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - 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; - - 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' ); - - 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; - - 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+1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - 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(); -}); - - -tape( 'the function supports a negative `stride` parameter', function test( t ) { - 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; - - 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' ); - - 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(); -}); - -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 486344274e203e741d2c80c69f257c8d4d3e8b04 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sat, 5 Jul 2025 17:12:38 +0000 Subject: [PATCH 7/9] chore: update benchmark script for improved clarity --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/stdevwd/benchmark/benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6a7daa0ea303..1dce7798ef99 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/benchmark/benchmark.js @@ -25,7 +25,7 @@ var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var stdevwd = require( './../lib/stdevwd.js' ); +var stdevwd = require( './../lib/main.js' ); // VARIABLES // From 28bb8ca6d5d28905d183888a6cdb69c08f5bac5d Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 5 Jul 2025 14:16:33 -0700 Subject: [PATCH 8/9] docs: avoid redeclaration Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 984e9f4e98a1..8bc6ec26f103 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/docs/repl.txt @@ -105,7 +105,7 @@ ~2.0817 // Using offset parameter: - > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; + > x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~2.0817 From 259bd5b6c91f314510fcfc9e7d738afe0ccfe43a Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 5 Jul 2025 14:17:49 -0700 Subject: [PATCH 9/9] test: update require path Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js index d5431c8b4e45..f54420c59298 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js +++ b/lib/node_modules/@stdlib/stats/base/stdevwd/test/test.main.js @@ -25,7 +25,7 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); -var stdevwd = require( './../lib/main.js' ); +var stdevwd = require( './../lib' ); // TESTS //