diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/README.md b/lib/node_modules/@stdlib/stats/base/nanstdevtk/README.md index e7b6241f89c3..558601ac9ed6 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/README.md +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/README.md @@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var nanstdevtk = require( '@stdlib/stats/base/nanstdevtk' ); ``` -#### nanstdevtk( N, correction, x, stride ) +#### nanstdevtk( N, correction, x, strideX ) -Computes the [standard deviation][standard-deviation] of a strided array `x` ignoring `NaN` values and using a one-pass textbook algorithm. +Computes the [standard deviation][standard-deviation] of a strided array ignoring `NaN` values and using a one-pass textbook algorithm. ```javascript var x = [ 1.0, -2.0, NaN, 2.0 ]; @@ -114,38 +114,32 @@ 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`, ```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, NaN ]; -var N = floor( x.length / 2 ); -var v = nanstdevtk( N, 1, x, 2 ); +var v = nanstdevtk( 5, 1, x, 2 ); // returns 2.5 ``` Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. - + ```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, NaN ] ); +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = nanstdevtk( N, 1, x1, 2 ); +var v = nanstdevtk( 5, 1, x1, 2 ); // returns 2.5 ``` -#### nanstdevtk.ndarray( N, correction, x, stride, offset ) +#### nanstdevtk.ndarray( N, correction, x, strideX, offsetX ) Computes the [standard deviation][standard-deviation] of a strided array ignoring `NaN` values and using a one-pass textbook algorithm and alternative indexing semantics. @@ -158,17 +152,14 @@ var v = nanstdevtk.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, NaN, NaN ]; -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 = nanstdevtk.ndarray( N, 1, x, 2, 1 ); +var v = nanstdevtk.ndarray( 5, 1, x, 2, 1 ); // returns 2.5 ``` @@ -183,6 +174,7 @@ var v = nanstdevtk.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 and `n` corresponds to the number of non-`NaN` indexed elements), both functions return `NaN`. - 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. +- 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 ([`dnanstdevtk`][@stdlib/stats/strided/dnanstdevtk], [`snanstdevtk`][@stdlib/stats/base/snanstdevtk], etc.) are likely to be significantly more performant. @@ -196,18 +188,19 @@ var v = nanstdevtk.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 uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var nanstdevtk = require( '@stdlib/stats/base/nanstdevtk' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var v = nanstdevtk( x.length, 1, x, 1 ); @@ -258,6 +251,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/nanstdevtk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.js index 3332d88cec42..04c1717184a7 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.js @@ -21,15 +21,30 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var nanstdevtk = require( './../lib/nanstdevtk.js' ); +var nanstdevtk = require( './../lib/main.js' ); // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -38,17 +53,7 @@ var nanstdevtk = require( './../lib/nanstdevtk.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*20.0 ) - 10.0 ); - } - } + var x = filledarrayBy( len, 'generic', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.ndarray.js index 1315dd9dc22c..f6e24c1bad58 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/benchmark/benchmark.ndarray.js @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -30,6 +32,19 @@ var nanstdevtk = require( './../lib/ndarray.js' ); // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -38,17 +53,7 @@ var nanstdevtk = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*20.0 ) - 10.0 ); - } - } + var x = filledarrayBy( len, 'generic', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/repl.txt index 76f2be472456..3f08a8efb618 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/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 ignoring `NaN` values and 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. @@ -33,8 +33,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -48,20 +48,19 @@ > {{alias}}( x.length, 1, x, 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 ); - > {{alias}}( N, 1, x, 2 ) + // Using `N` and stride parameters: + > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN ]; + > {{alias}}( 4, 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 x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > {{alias}}( N, 1, x1, 2 ) + > {{alias}}( 4, 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 ignoring `NaN` values and using a one-pass textbook algorithm and alternative indexing semantics. @@ -89,10 +88,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -108,9 +107,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, NaN, NaN ]; + > {{alias}}.ndarray( 4, 1, x, 2, 1 ) ~2.0817 See Also diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/types/index.d.ts index 01d70f94db4f..8f8444404751 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/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 `nanstdevtk`. @@ -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 = nanstdevtk( 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 ignoring `NaN` values and 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 = nanstdevtk.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/nanstdevtk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/types/test.ts index fe8cdcfac9c9..552206c621ff 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import nanstdevtk = require( './index' ); @@ -26,6 +27,7 @@ import nanstdevtk = require( './index' ); const x = new Float64Array( 10 ); nanstdevtk( x.length, 1, x, 1 ); // $ExpectType number + nanstdevtk( 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 nanstdevtk = require( './index' ); const x = new Float64Array( 10 ); nanstdevtk.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + nanstdevtk.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/nanstdevtk/examples/index.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/examples/index.js index 87e0ce5aa239..8d9e49a8f386 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/examples/index.js @@ -18,22 +18,19 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var nanstdevtk = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var v = nanstdevtk( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/index.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/index.js index 0fee19c3b403..3f9143b282d9 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/index.js @@ -32,13 +32,11 @@ * // returns ~2.0817 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var nanstdevtk = require( '@stdlib/stats/base/nanstdevtk' ); * * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ]; -* var N = floor( x.length / 2 ); * -* var v = nanstdevtk.ndarray( N, 1, x, 2, 1 ); +* var v = nanstdevtk.ndarray( 5, 1, x, 2, 1 ); * // returns 2.5 */ diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/nanstdevtk.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/nanstdevtk.js index 2564fb9a31ea..3eb020d129ba 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/nanstdevtk.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/nanstdevtk.js @@ -20,8 +20,8 @@ // MODULES // -var nanvariancetk = require( '@stdlib/stats/base/nanvariancetk' ); -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 = nanstdevtk( x.length, 1, x, 1 ); * // returns ~2.0817 */ -function nanstdevtk( N, correction, x, stride ) { - return sqrt( nanvariancetk( N, correction, x, stride ) ); +function nanstdevtk( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/ndarray.js index 52a96fdb04b8..97f9a3aac2c8 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/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, NaN, NaN ]; -* var N = floor( x.length / 2 ); * -* var v = nanstdevtk( N, 1, x, 2, 1 ); +* var v = nanstdevtk( 5, 1, x, 2, 1 ); * // returns 2.5 */ -function nanstdevtk( N, correction, x, stride, offset ) { - return sqrt( nanvariancetk( N, correction, x, stride, offset ) ); +function nanstdevtk( N, correction, x, strideX, offsetX ) { + return sqrt( nanvariancetk( N, correction, x, strideX, offsetX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.nanstdevtk.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.nanstdevtk.js index a43232be82b7..fc3f4b9d3b8c 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.nanstdevtk.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.nanstdevtk.js @@ -21,11 +21,11 @@ // 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 nanstdevtk = require( './../lib/nanstdevtk.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var nanstdevtk = require( './../lib/main.js' ); // TESTS // @@ -95,6 +95,60 @@ 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 (ignoring `NaN` values) (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-6) ), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanstdevtk( 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 (ignoring `NaN` values)', function test( t ) { var x; var v; @@ -149,6 +203,60 @@ 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 (ignoring `NaN` values) (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-2) ), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, sqrt( 53.5/(x.length-7) ), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanstdevtk( 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; @@ -164,6 +272,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 = nanstdevtk( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanstdevtk( -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` provided the first element is not `NaN`', function test( t ) { var x; var v; @@ -181,6 +304,23 @@ 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` provided the first element is not `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 0, 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 sample standard deviation equal to `NaN`', function test( t ) { var x; var v; @@ -198,6 +338,23 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a sample t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns a sample standard deviation equal to `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `correction` parameter yielding a correction term less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -213,8 +370,22 @@ tape( 'if provided a `correction` parameter yielding a correction term less than t.end(); }); +tape( 'if provided a `correction` parameter yielding a correction term 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 = nanstdevtk( x.length, x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanstdevtk( 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; @@ -231,15 +402,36 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]; - N = floor( x.length / 2 ); - v = nanstdevtk( N, 1, x, 2 ); + v = nanstdevtk( 5, 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, + NaN, // 4 + NaN + ]; + + v = nanstdevtk( 5, 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; var i; @@ -256,9 +448,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]; - N = floor( x.length / 2 ); - v = nanstdevtk( N, 1, x, -2 ); + v = nanstdevtk( 5, 1, x, -2 ); t.strictEqual( v, 2.5, 'returns expected value' ); x = []; @@ -271,6 +462,37 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ + NaN, // 4 + NaN, + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = nanstdevtk( 5, 1, toAccessorArray( x ), -2 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanstdevtk( x.length, 1, toAccessorArray( x ), -1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` provided the correction term is not less than `0` and the first element is not `NaN`', function test( t ) { var x; var v; @@ -293,10 +515,31 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` provided the correction term is not less than `0` and the first element is not `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( x.length, x.length, toAccessorArray( x ), 0 ); + t.strictEqual( isnan( v ), true, '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([ @@ -314,9 +557,35 @@ 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 = nanstdevtk( N, 1, x1, 2 ); + v = nanstdevtk( 5, 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, + NaN, // 4 + NaN + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = nanstdevtk( 5, 1, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.ndarray.js index 226e3b5574c4..b80155076cb1 100644 --- a/lib/node_modules/@stdlib/stats/base/nanstdevtk/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanstdevtk/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 nanstdevtk = require( './../lib/ndarray.js' ); @@ -94,6 +94,60 @@ 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 (ignoring `NaN` values) (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-6) ), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanstdevtk( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanstdevtk( 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 (ignoring `NaN` values)', function test( t ) { var x; var v; @@ -148,6 +202,60 @@ 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 (ignoring `NaN` values) (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-2) ), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-7) ), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanstdevtk( 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; @@ -163,6 +271,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 = nanstdevtk( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanstdevtk( -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` provided the first element is not `NaN`', function test( t ) { var x; var v; @@ -180,6 +303,23 @@ 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` provided the first element is not `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 0, 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 sample standard deviation equal to `NaN`', function test( t ) { var x; var v; @@ -197,6 +337,23 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a sample t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns a sample standard deviation equal to `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( 1, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `correction` parameter yielding a correction term less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -212,8 +369,22 @@ tape( 'if provided a `correction` parameter yielding a correction term less than t.end(); }); +tape( 'if provided a `correction` parameter yielding a correction term 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 = nanstdevtk( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanstdevtk( 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; @@ -230,15 +401,36 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]; - N = floor( x.length / 2 ); - v = nanstdevtk( N, 1, x, 2, 0 ); + v = nanstdevtk( 5, 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, + NaN, // 4 + NaN + ]; + + v = nanstdevtk( 5, 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; var i; @@ -255,9 +447,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]; - N = floor( x.length / 2 ); - v = nanstdevtk( N, 1, x, -2, 8 ); + v = nanstdevtk( 5, 1, x, -2, 8 ); t.strictEqual( v, 2.5, 'returns expected value' ); x = []; @@ -270,6 +461,37 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ + NaN, // 4 + NaN, + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = nanstdevtk( 5, 1, toAccessorArray( x ), -2, 8 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanstdevtk( x.length, 1, toAccessorArray( x ), -1, x.length-1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` provided the correction term is not less than `0` and the first element is not `NaN`', function test( t ) { var x; var v; @@ -292,8 +514,29 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` provided the correction term is not less than `0` and the first element is not `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( x.length, 1, toAccessorArray( x ), 0, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanstdevtk( x.length, x.length, toAccessorArray( x ), 0, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -309,9 +552,31 @@ tape( 'the function supports an `offset` parameter', function test( t ) { NaN, NaN // 4 ]; - N = floor( x.length / 2 ); - v = nanstdevtk( N, 1, x, 2, 1 ); + v = nanstdevtk( 5, 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 + NaN, + NaN // 4 + ]; + + v = nanstdevtk( 5, 1, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end();