From a5d0d9b2701bb992232e8ce2bf0dc5251035efc1 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sat, 5 Jul 2025 19:42:37 +0000 Subject: [PATCH] feat: refactor and add protocol support to `stats/base/stdevtk` --- 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/stdevtk/README.md | 46 ++--- .../stats/base/stdevtk/benchmark/benchmark.js | 17 +- .../stdevtk/benchmark/benchmark.ndarray.js | 17 +- .../@stdlib/stats/base/stdevtk/docs/repl.txt | 36 ++-- .../stats/base/stdevtk/docs/types/index.d.ts | 19 +- .../stats/base/stdevtk/docs/types/test.ts | 3 + .../stats/base/stdevtk/examples/index.js | 14 +- .../@stdlib/stats/base/stdevtk/lib/ndarray.js | 13 +- .../@stdlib/stats/base/stdevtk/lib/stdevtk.js | 10 +- .../stats/base/stdevtk/test/test.ndarray.js | 169 ++++++++++++++++- .../stats/base/stdevtk/test/test.stdevtk.js | 173 +++++++++++++++++- 11 files changed, 402 insertions(+), 115 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/README.md b/lib/node_modules/@stdlib/stats/base/stdevtk/README.md index 0e59c49720c7..6938a102f913 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/README.md +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/README.md @@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var stdevtk = require( '@stdlib/stats/base/stdevtk' ); ``` -#### stdevtk( N, correction, x, stride ) +#### stdevtk( N, correction, x, strideX ) -Computes the [standard deviation][standard-deviation] of a strided array `x` using a one-pass textbook algorithm. +Computes the [standard deviation][standard-deviation] of a strided array using a one-pass textbook algorithm. ```javascript var x = [ 1.0, -2.0, 2.0 ]; @@ -114,17 +114,14 @@ 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]. -- **stride**: index increment for `x`. +- **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 = stdevtk( N, 1, x, 2 ); +var v = stdevtk( 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 = stdevtk( N, 1, x1, 2 ); +var v = stdevtk( 4, 1, x1, 2 ); // returns 2.5 ``` -#### stdevtk.ndarray( N, correction, x, stride, offset ) +#### stdevtk.ndarray( N, correction, x, strideX, offsetX ) Computes the [standard deviation][standard-deviation] of a strided array using a one-pass textbook algorithm and alternative indexing semantics. @@ -158,17 +152,14 @@ var v = stdevtk.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 `x` 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 = stdevtk.ndarray( N, 1, x, 2, 1 ); +var v = stdevtk.ndarray( 4, 1, x, 2, 1 ); // returns 2.5 ``` @@ -182,6 +173,7 @@ var v = stdevtk.ndarray( N, 1, x, 2, 1 ); - 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]). - Some caution should be exercised when using the one-pass textbook algorithm. Literature overwhelmingly discourages the algorithm's use for two reasons: 1) the lack of safeguards against underflow and overflow and 2) the risk of catastrophic cancellation when subtracting the two sums if the sums are large and the variance small. These concerns have merit; however, the one-pass textbook algorithm should not be dismissed outright. For data distributions with a moderately large standard deviation to mean ratio (i.e., **coefficient of variation**), the one-pass textbook algorithm may be acceptable, especially when performance is paramount and some precision loss is acceptable (including a risk of computing a negative variance due to floating-point rounding errors!). In short, no single "best" algorithm for computing the standard deviation exists. The "best" algorithm depends on the underlying data distribution, your performance requirements, and your minimum precision requirements. When evaluating which algorithm to use, consider the relative pros and cons, and choose the algorithm which best serves your needs. - Depending on the environment, the typed versions ([`dstdevtk`][@stdlib/stats/strided/dstdevtk], [`sstdevtk`][@stdlib/stats/strided/sstdevtk], etc.) are likely to be significantly more performant. @@ -196,18 +188,12 @@ var v = stdevtk.ndarray( N, 1, x, 2, 1 ); ```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 stdevtk = require( '@stdlib/stats/base/stdevtk' ); -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 = stdevtk( x.length, 1, x, 1 ); @@ -258,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 + [@ling:1974a]: https://doi.org/10.2307/2286154 diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.js index 79becfb3cb76..2e1903c37627 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.js @@ -21,13 +21,20 @@ // 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 stdevtk = require( './../lib/stdevtk.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var stdevtk = require( './../lib/stdevtk.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.ndarray.js index 650b6dbb7e04..78dd1cda8b58 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/benchmark/benchmark.ndarray.js @@ -21,13 +21,20 @@ // 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 stdevtk = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var stdevtk = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/stdevtk/docs/repl.txt index 86088980cd3f..e6c3c0df8536 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the standard deviation of a strided array using a one-pass textbook 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. @@ -31,8 +31,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -46,27 +46,24 @@ > {{alias}}( x.length, 1, x, 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 a one-pass textbook 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 @@ -89,10 +86,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -108,9 +105,8 @@ ~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 ) + > x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~2.0817 See Also diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/index.d.ts index 2fe3bd7e76a3..b6c4e80db65d 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/index.d.ts @@ -20,7 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `stdevtk`. @@ -32,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 @@ -41,7 +46,7 @@ interface Routine { * var v = stdevtk( 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 a one-pass textbook algorithm and alternative indexing semantics. @@ -49,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 @@ -59,7 +64,7 @@ interface Routine { * var v = stdevtk.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; } /** @@ -68,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 diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/test.ts index 2d4f6d658130..986a15560347 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import stdevtk = require( './index' ); @@ -26,6 +27,7 @@ import stdevtk = require( './index' ); const x = new Float64Array( 10 ); stdevtk( x.length, 1, x, 1 ); // $ExpectType number + stdevtk( 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... @@ -101,6 +103,7 @@ import stdevtk = require( './index' ); const x = new Float64Array( 10 ); stdevtk.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + stdevtk.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/stdevtk/examples/index.js b/lib/node_modules/@stdlib/stats/base/stdevtk/examples/index.js index 38701368fe61..da580e627f03 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/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 stdevtk = 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 = stdevtk( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevtk/lib/ndarray.js index 5cb715873769..c04f982c1599 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/lib/ndarray.js @@ -32,21 +32,18 @@ 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' ); -* * 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 = stdevtk( N, 1, x, 2, 1 ); +* var v = stdevtk( 4, 1, x, 2, 1 ); * // returns 2.5 */ -function stdevtk( N, correction, x, stride, offset ) { - return sqrt( variancetk( N, correction, x, stride, offset ) ); +function stdevtk( N, correction, x, strideX, offsetX ) { + return sqrt( variancetk( N, correction, x, strideX, offsetX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/lib/stdevtk.js b/lib/node_modules/@stdlib/stats/base/stdevtk/lib/stdevtk.js index 1a06a7e809d5..1996cbc367f6 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/lib/stdevtk.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/lib/stdevtk.js @@ -20,8 +20,8 @@ // MODULES // -var variancetk = require( '@stdlib/stats/strided/variancetk' ); -var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -32,7 +32,7 @@ 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 {integer} strideX - stride length * @returns {number} standard deviation * * @example @@ -41,8 +41,8 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * var v = stdevtk( x.length, 1, x, 1 ); * // returns ~2.0817 */ -function stdevtk( N, correction, x, stride ) { - return sqrt( variancetk( N, correction, x, stride ) ); +function stdevtk( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.ndarray.js index 4164fa0da840..94a864ec748e 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/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 stdevtk = 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 = stdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevtk( 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 = stdevtk( 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 = stdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevtk( 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 = stdevtk( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevtk( -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 = stdevtk( 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 = stdevtk( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevtk( 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 = stdevtk( N, 1, x, 2, 0 ); + v = stdevtk( 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 = stdevtk( 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 = stdevtk( N, 1, x, -2, 6 ); + v = stdevtk( 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 = stdevtk( 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 = stdevtk( 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,9 +322,29 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = stdevtk( N, 1, x, 2, 1 ); + v = stdevtk( 4, 1, x, 2, 1 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter (accessors)', 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 + ]; + + v = stdevtk( 4, 1, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.stdevtk.js b/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.stdevtk.js index 24287634d6e4..9c430ae508e4 100644 --- a/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.stdevtk.js +++ b/lib/node_modules/@stdlib/stats/base/stdevtk/test/test.stdevtk.js @@ -21,10 +21,10 @@ // 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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var stdevtk = require( './../lib/stdevtk.js' ); @@ -60,6 +60,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 = stdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevtk( 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; @@ -79,6 +98,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 = stdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = stdevtk( 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; @@ -94,6 +132,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 = stdevtk( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevtk( -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; @@ -106,6 +159,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 = stdevtk( 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; @@ -121,8 +186,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 = stdevtk( x.length, x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdevtk( 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 N; var x; var v; @@ -137,15 +216,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = stdevtk( N, 1, x, 2 ); + v = stdevtk( 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 = stdevtk( 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 N; var x; var v; @@ -160,8 +258,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = stdevtk( N, 1, x, -2 ); + v = stdevtk( 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 = stdevtk( 4, 1, toAccessorArray( x ), -2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); @@ -179,10 +297,21 @@ 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 = stdevtk( 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 N; var v; x0 = new Float64Array([ @@ -198,9 +327,33 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = stdevtk( N, 1, x1, 2 ); + v = stdevtk( 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 = stdevtk( 4, 1, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end();