diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/README.md b/lib/node_modules/@stdlib/stats/base/varianceyc/README.md index 1e4d0fe95ea4..a05b606f4f10 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/README.md +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/README.md @@ -98,14 +98,14 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var varianceyc = require( '@stdlib/stats/base/varianceyc' ); ``` -#### varianceyc( N, correction, x, stride ) +#### varianceyc( N, correction, x, strideX ) -Computes the [variance][variance] of a strided array `x` using a one-pass algorithm proposed by Youngs and Cramer. +Computes the [variance][variance] of a strided array using a one-pass algorithm proposed by Youngs and Cramer. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var v = varianceyc( x.length, 1, x, 1 ); +var v = varianceyc( x.length, 1.0, x, 1 ); // returns ~4.3333 ``` @@ -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 [variance][variance] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [variance][variance] 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 unbiased sample [variance][variance], 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 [variance][variance] 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 [variance][variance] 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 = varianceyc( N, 1, x, 2 ); +var v = varianceyc( 4, 1.0, x, 2 ); // returns 6.25 ``` @@ -134,41 +131,35 @@ 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 = varianceyc( N, 1, x1, 2 ); +var v = varianceyc( 4, 1.0, x1, 2 ); // returns 6.25 ``` -#### varianceyc.ndarray( N, correction, x, stride, offset ) +#### varianceyc.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var v = varianceyc.ndarray( x.length, 1, x, 1, 0 ); +var v = varianceyc.ndarray( x.length, 1.0, x, 1, 0 ); // returns ~4.33333 ``` 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 [variance][variance] 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 [variance][variance] 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 = varianceyc.ndarray( N, 1, x, 2, 1 ); +var v = varianceyc.ndarray( 4, 1.0, x, 2, 1 ); // returns 6.25 ``` @@ -183,6 +174,7 @@ var v = varianceyc.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`. - Depending on the environment, the typed versions ([`dvarianceyc`][@stdlib/stats/strided/dvarianceyc], [`svarianceyc`][@stdlib/stats/strided/svarianceyc], etc.) are likely to be significantly more performant. +- 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]). @@ -195,21 +187,15 @@ var v = varianceyc.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/array/uniform' ); var varianceyc = require( '@stdlib/stats/base/varianceyc' ); -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 = uniform( 10, -50.0, 50.0, { + 'dtype': 'generic' +}); console.log( x ); -var v = varianceyc( x.length, 1, x, 1 ); +var v = varianceyc( x.length, 1.0, x, 1 ); console.log( v ); ``` @@ -256,6 +242,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 + [@stdlib/stats/strided/svarianceyc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/svarianceyc [@youngs:1971a]: https://doi.org/10.1080/00401706.1971.10488826 diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.js index c07e4bfca71e..4cf64da5f3d4 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.js @@ -21,11 +21,18 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var varianceyc = require( './../lib/varianceyc.js' ); +var varianceyc = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var varianceyc = require( './../lib/varianceyc.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.0, 10.0, options ); return benchmark; function benchmark( b ) { @@ -53,7 +54,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = varianceyc( x.length, 1, x, 1 ); + v = varianceyc( x.length, 1.0, x, 1 ); if ( isnan( v ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.ndarray.js index 3f404dbf88e5..c760ff42df7f 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/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 varianceyc = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var varianceyc = 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.0, 10.0, options ); return benchmark; function benchmark( b ) { @@ -53,7 +54,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = varianceyc( x.length, 1, x, 1, 0 ); + v = varianceyc( x.length, 1.0, x, 1, 0 ); if ( isnan( v ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/varianceyc/docs/repl.txt index 6b92bcf4f75c..2511d4ec7df5 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the variance of a strided array using a one-pass algorithm proposed by Youngs and Cramer. - 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 for `x`. Returns ------- @@ -43,30 +43,27 @@ -------- // Standard Usage: > var x = [ 1.0, -2.0, 2.0 ]; - > {{alias}}( x.length, 1, x, 1 ) + > {{alias}}( x.length, 1.0, x, 1 ) ~4.3333 - // 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.0, x, 2 ) ~4.3333 // 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.0, x1, 2 ) ~4.3333 -{{alias}}.ndarray( N, correction, x, stride, offset ) + +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the variance of a strided array using a one-pass algorithm proposed by Youngs and Cramer 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,11 +86,11 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length for `x`. - offset: integer - Starting index. + offsetX: integer + Starting index for `x`. Returns ------- @@ -104,13 +101,12 @@ -------- // Standard Usage: > var x = [ 1.0, -2.0, 2.0 ]; - > {{alias}}.ndarray( x.length, 1, x, 1, 0 ) + > {{alias}}.ndarray( x.length, 1.0, x, 1, 0 ) ~4.3333 // 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.0, x, 2, 1 ) ~4.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/index.d.ts index 51da248e2f8d..de3b1949ee4b 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/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 `varianceyc`. @@ -32,16 +37,16 @@ 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 for `x` * @returns variance * * @example * var x = [ 1.0, -2.0, 2.0 ]; * - * var v = varianceyc( x.length, 1, x, 1 ); + * var v = varianceyc( x.length, 1.0, x, 1 ); * // returns ~4.3333 */ - ( N: number, correction: number, x: NumericArray, stride: number ): number; + ( N: number, correction: number, x: InputArray, strideX: number ): number; /** * Computes the variance of a strided array using a one-pass algorithm proposed by Youngs and Cramer and alternative indexing semantics. @@ -49,17 +54,17 @@ 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 for `x` + * @param offsetX - starting index for `x` * @returns variance * * @example * var x = [ 1.0, -2.0, 2.0 ]; * - * var v = varianceyc.ndarray( x.length, 1, x, 1, 0 ); + * var v = varianceyc.ndarray( x.length, 1.0, x, 1, 0 ); * // returns ~4.3333 */ - 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,19 +73,19 @@ 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 for `x` * @returns variance * * @example * var x = [ 1.0, -2.0, 2.0 ]; * -* var v = varianceyc( x.length, 1, x, 1 ); +* var v = varianceyc( x.length, 1.0, x, 1 ); * // returns ~4.3333 * * @example * var x = [ 1.0, -2.0, 2.0 ]; * -* var v = varianceyc.ndarray( x.length, 1, x, 1, 0 ); +* var v = varianceyc.ndarray( x.length, 1.0, x, 1, 0 ); * // returns ~4.3333 */ declare var varianceyc: Routine; diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/test.ts index e93995ba93eb..cae7d268af9d 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import varianceyc = require( './index' ); @@ -25,21 +26,22 @@ import varianceyc = require( './index' ); { const x = new Float64Array( 10 ); - varianceyc( x.length, 1, x, 1 ); // $ExpectType number + varianceyc( x.length, 1.0, x, 1 ); // $ExpectType number + varianceyc( x.length, 1.0, new AccessorArray( x ), 1 ); // $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 ); - varianceyc( '10', 1, x, 1 ); // $ExpectError - varianceyc( true, 1, x, 1 ); // $ExpectError - varianceyc( false, 1, x, 1 ); // $ExpectError - varianceyc( null, 1, x, 1 ); // $ExpectError - varianceyc( undefined, 1, x, 1 ); // $ExpectError - varianceyc( [], 1, x, 1 ); // $ExpectError - varianceyc( {}, 1, x, 1 ); // $ExpectError - varianceyc( ( x: number ): number => x, 1, x, 1 ); // $ExpectError + varianceyc( '10', 1.0, x, 1 ); // $ExpectError + varianceyc( true, 1.0, x, 1 ); // $ExpectError + varianceyc( false, 1.0, x, 1 ); // $ExpectError + varianceyc( null, 1.0, x, 1 ); // $ExpectError + varianceyc( undefined, 1.0, x, 1 ); // $ExpectError + varianceyc( [], 1.0, x, 1 ); // $ExpectError + varianceyc( {}, 1.0, x, 1 ); // $ExpectError + varianceyc( ( x: number ): number => x, 1.0, x, 1 ); // $ExpectError } // The compiler throws an error if the function is provided a second argument which is not a number... @@ -60,29 +62,29 @@ import varianceyc = require( './index' ); { const x = new Float64Array( 10 ); - varianceyc( x.length, 1, 10, 1 ); // $ExpectError - varianceyc( x.length, 1, '10', 1 ); // $ExpectError - varianceyc( x.length, 1, true, 1 ); // $ExpectError - varianceyc( x.length, 1, false, 1 ); // $ExpectError - varianceyc( x.length, 1, null, 1 ); // $ExpectError - varianceyc( x.length, 1, undefined, 1 ); // $ExpectError - varianceyc( x.length, 1, [ '1' ], 1 ); // $ExpectError - varianceyc( x.length, 1, {}, 1 ); // $ExpectError - varianceyc( x.length, 1, ( x: number ): number => x, 1 ); // $ExpectError + varianceyc( x.length, 1.0, 10, 1 ); // $ExpectError + varianceyc( x.length, 1.0, '10', 1 ); // $ExpectError + varianceyc( x.length, 1.0, true, 1 ); // $ExpectError + varianceyc( x.length, 1.0, false, 1 ); // $ExpectError + varianceyc( x.length, 1.0, null, 1 ); // $ExpectError + varianceyc( x.length, 1.0, undefined, 1 ); // $ExpectError + varianceyc( x.length, 1.0, [ '1' ], 1 ); // $ExpectError + varianceyc( x.length, 1.0, {}, 1 ); // $ExpectError + varianceyc( x.length, 1.0, ( x: number ): number => x, 1 ); // $ExpectError } // The compiler throws an error if the function is provided a fourth argument which is not a number... { const x = new Float64Array( 10 ); - varianceyc( x.length, 1, x, '10' ); // $ExpectError - varianceyc( x.length, 1, x, true ); // $ExpectError - varianceyc( x.length, 1, x, false ); // $ExpectError - varianceyc( x.length, 1, x, null ); // $ExpectError - varianceyc( x.length, 1, x, undefined ); // $ExpectError - varianceyc( x.length, 1, x, [] ); // $ExpectError - varianceyc( x.length, 1, x, {} ); // $ExpectError - varianceyc( x.length, 1, x, ( x: number ): number => x ); // $ExpectError + varianceyc( x.length, 1.0, x, '10' ); // $ExpectError + varianceyc( x.length, 1.0, x, true ); // $ExpectError + varianceyc( x.length, 1.0, x, false ); // $ExpectError + varianceyc( x.length, 1.0, x, null ); // $ExpectError + varianceyc( x.length, 1.0, x, undefined ); // $ExpectError + varianceyc( x.length, 1.0, x, [] ); // $ExpectError + varianceyc( x.length, 1.0, x, {} ); // $ExpectError + varianceyc( x.length, 1.0, x, ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... @@ -91,9 +93,9 @@ import varianceyc = require( './index' ); varianceyc(); // $ExpectError varianceyc( x.length ); // $ExpectError - varianceyc( x.length, 1 ); // $ExpectError - varianceyc( x.length, 1, x ); // $ExpectError - varianceyc( x.length, 1, x, 1, 10 ); // $ExpectError + varianceyc( x.length, 1.0 ); // $ExpectError + varianceyc( x.length, 1.0, x ); // $ExpectError + varianceyc( x.length, 1.0, x, 1, 10 ); // $ExpectError } // Attached to main export is an `ndarray` method which returns a number... @@ -101,20 +103,21 @@ import varianceyc = require( './index' ); const x = new Float64Array( 10 ); varianceyc.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + varianceyc.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... { const x = new Float64Array( 10 ); - varianceyc.ndarray( '10', 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( true, 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( false, 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( null, 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( undefined, 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( [], 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( {}, 1, x, 1, 0 ); // $ExpectError - varianceyc.ndarray( ( x: number ): number => x, 1, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( '10', 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( true, 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( false, 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( null, 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( undefined, 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( [], 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( {}, 1.0, x, 1, 0 ); // $ExpectError + varianceyc.ndarray( ( x: number ): number => x, 1.0, x, 1, 0 ); // $ExpectError } // The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... @@ -135,43 +138,43 @@ import varianceyc = require( './index' ); { const x = new Float64Array( 10 ); - varianceyc.ndarray( x.length, 1, 10, 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, '10', 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, true, 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, false, 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, null, 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, undefined, 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, [ '1' ], 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, {}, 1, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, ( x: number ): number => x, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, 10, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, '10', 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, true, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, false, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, null, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, undefined, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, [ '1' ], 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, {}, 1, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, ( x: number ): number => x, 1, 0 ); // $ExpectError } // The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... { const x = new Float64Array( 10 ); - varianceyc.ndarray( x.length, 1, x, '10', 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, true, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, false, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, null, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, undefined, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, [], 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, {}, 0 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, ( x: number ): number => x, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, '10', 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, true, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, false, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, null, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, undefined, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, [], 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, {}, 0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, ( x: number ): number => x, 0 ); // $ExpectError } // The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a number... { const x = new Float64Array( 10 ); - varianceyc.ndarray( x.length, 1, x, 1, '10' ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, true ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, false ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, null ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, undefined ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, [] ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, {} ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, ( x: number ): number => x ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, '10' ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, true ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, false ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, null ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, undefined ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, [] ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, {} ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... @@ -180,8 +183,8 @@ import varianceyc = require( './index' ); varianceyc.ndarray(); // $ExpectError varianceyc.ndarray( x.length ); // $ExpectError - varianceyc.ndarray( x.length, 1 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1 ); // $ExpectError - varianceyc.ndarray( x.length, 1, x, 1, 0, 10 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1 ); // $ExpectError + varianceyc.ndarray( x.length, 1.0, x, 1, 0, 10 ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/examples/index.js b/lib/node_modules/@stdlib/stats/base/varianceyc/examples/index.js index 965b058878dd..d659f7f2aa7f 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/examples/index.js @@ -18,19 +18,13 @@ '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/array/uniform' ); var varianceyc = 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 = uniform( 10, -50.0, 50.0, { + 'dtype': 'generic' +}); console.log( x ); -var v = varianceyc( x.length, 1, x, 1 ); +var v = varianceyc( x.length, 1.0, x, 1 ); console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/varianceyc.js b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/accessors.js similarity index 62% rename from lib/node_modules/@stdlib/stats/base/varianceyc/lib/varianceyc.js rename to lib/node_modules/@stdlib/stats/base/varianceyc/lib/accessors.js index 6d76e638e816..0406525482d6 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/varianceyc.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/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. @@ -31,19 +31,28 @@ * * - Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826](https://doi.org/10.1080/00401706.1971.10488826). * +* @private * @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 {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} variance * * @example -* var x = [ 1.0, -2.0, 2.0 ]; +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); * -* var v = varianceyc( x.length, 1, x, 1 ); -* // returns ~4.3333 +* var x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); +* +* var v = varianceyc( x.length, 1.0, arraylike2object( x ), 1, 0 ); +* // returns 10.7 */ -function varianceyc( N, correction, x, stride ) { +function varianceyc( N, correction, x, strideX, offsetX ) { + var xbuf; + var xget; var sum; var ix; var S; @@ -53,26 +62,22 @@ function varianceyc( N, correction, x, stride ) { var i; n = N - correction; - if ( N <= 0 || n <= 0.0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return 0.0; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - sum = x[ ix ]; - ix += stride; + + // Cache a reference to array data: + xbuf = x.data; + + // Cache a reference to an element accessor: + xget = x.accessors[ 0 ]; + + sum = xget( xbuf, offsetX ); + ix = offsetX + strideX; S = 0.0; for ( i = 2; i <= N; i++ ) { - v = x[ ix ]; + v = xget( xbuf, ix ); sum += v; d = (i*v) - sum; S += (1.0/(i*(i-1))) * d * d; - ix += stride; + ix += strideX; } return S / n; } diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/index.js b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/index.js index e90497e57e78..51c01687fbb2 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/index.js @@ -27,25 +27,29 @@ * var varianceyc = require( '@stdlib/stats/base/varianceyc' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = varianceyc( N, 1, x, 1 ); +* var v = varianceyc( 3, 1.0, x, 1 ); * // returns ~4.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var varianceyc = require( '@stdlib/stats/base/varianceyc' ); * * 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 = varianceyc.ndarray( N, 1, x, 2, 1 ); +* var v = varianceyc.ndarray( 4, 1.0, x, 2, 1 ); * // returns 6.25 */ // MODULES // +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/main.js b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/main.js index 5a6b2f4f75cf..4dfe7440899e 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/main.js @@ -20,14 +20,38 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var varianceyc = require( './varianceyc.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( varianceyc, 'ndarray', ndarray ); +/** +* Computes the variance of a strided array using a one-pass algorithm proposed by Youngs and Cramer. +* +* ## Method +* +* - This implementation uses a one-pass algorithm, as proposed by Youngs and Cramer (1971). +* +* ## References +* +* - Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826](https://doi.org/10.1080/00401706.1971.10488826). +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {number} variance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = varianceyc( x.length, 1.0, x, 1 ); +* // returns ~4.3333 +*/ +function varianceyc( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/ndarray.js index da23fbd21f16..11c3f0cfa0e5 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/lib/ndarray.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + // MAIN // /** @@ -34,20 +40,17 @@ * @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} variance * * @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 = varianceyc( N, 1, x, 2, 1 ); +* var v = varianceyc( 4, 1.0, x, 2, 1 ); * // returns 6.25 */ -function varianceyc( N, correction, x, stride, offset ) { +function varianceyc( N, correction, x, strideX, offsetX ) { var sum; var ix; var S; @@ -55,23 +58,30 @@ function varianceyc( N, correction, x, stride, offset ) { var d; var n; var i; + var o; n = N - correction; if ( N <= 0 || n <= 0.0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { + if ( N === 1 || strideX === 0 ) { return 0.0; } - sum = x[ offset ]; - ix = offset + stride; + + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + + sum = x[ offsetX ]; + ix = offsetX + strideX; S = 0.0; for ( i = 2; i <= N; i++ ) { v = x[ ix ]; sum += v; d = (i*v) - sum; S += (1.0/(i*(i-1))) * d * d; - ix += stride; + ix += strideX; } return S / n; } diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.main.js b/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.main.js new file mode 100644 index 000000000000..f3335f77fef8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.main.js @@ -0,0 +1,371 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var varianceyc = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof varianceyc, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( varianceyc.length, 4, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the population variance 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 = varianceyc( x.length, 0.0, x, 1 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = varianceyc( x.length, 0.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = varianceyc( x.length, 0.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population variance of a strided array (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = varianceyc( x.length, 0.0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = varianceyc( x.length, 0.0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = varianceyc( x.length, 0.0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance 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 = varianceyc( x.length, 1.0, x, 1 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = varianceyc( x.length, 1.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = varianceyc( x.length, 1.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance of a strided array (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = varianceyc( x.length, 1.0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = varianceyc( x.length, 1.0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = varianceyc( x.length, 1.0, 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 = varianceyc( 0, 1.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = varianceyc( -1, 1.0, 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 (accessor)`', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = varianceyc( 0, 1.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = varianceyc( -1, 1.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = toAccessorArray( [ 10.0 ] ); + v = varianceyc( 0, 0.0, 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 variance of `0`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = varianceyc( 1, 0.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 variance of `0` (accessor)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = varianceyc( 1, 0.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = toAccessorArray( [ 5.0 ] ); + v = varianceyc( 1, 0.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 = varianceyc( x.length, x.length, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = varianceyc( 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` (accessor)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = varianceyc( x.length, x.length, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected' ); + + v = varianceyc( x.length, x.length+1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected' ); + + x = toAccessorArray( [ 7.0, 8.0, 9.0 ] ); + v = varianceyc( x.length, x.length, 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 = varianceyc( 4, 1.0, x, 2 ); + + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessor)', 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 = varianceyc( 4, 1.0, toAccessorArray( x ), 2 ); + + t.strictEqual( v, 6.25, '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 = varianceyc( 4, 1.0, x, -2 ); + + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessor)', 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 = varianceyc( 4, 1.0, toAccessorArray( x ), -2 ); + + t.strictEqual( v, 6.25, '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 = varianceyc( x.length, 1.0, 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` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = varianceyc( x.length, 1.0, 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 = varianceyc( 4, 1.0, x1, 2 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessor)', 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 = varianceyc( 4, 1.0, toAccessorArray( x1 ), 2 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.ndarray.js index d479be840e45..65440b321483 100644 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.ndarray.js @@ -21,7 +21,7 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var varianceyc = require( './../lib/ndarray.js' ); @@ -44,15 +44,34 @@ tape( 'the function calculates the population variance of a strided array', func var v; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = varianceyc( x.length, 0, x, 1, 0 ); + v = varianceyc( x.length, 0.0, x, 1, 0 ); t.strictEqual( v, 53.5/x.length, 'returns expected value' ); x = [ -4.0, -4.0 ]; - v = varianceyc( x.length, 0, x, 1, 0 ); + v = varianceyc( x.length, 0.0, x, 1, 0 ); t.strictEqual( v, 0.0, 'returns expected value' ); x = [ NaN, 4.0 ]; - v = varianceyc( x.length, 0, x, 1, 0 ); + v = varianceyc( x.length, 0.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population variance of a strided array (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = varianceyc( x.length, 0.0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = varianceyc( x.length, 0.0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = varianceyc( x.length, 0.0, toAccessorArray( x ), 1, 0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); @@ -63,15 +82,34 @@ tape( 'the function calculates the sample variance of a strided array', function var v; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = varianceyc( x.length, 1, x, 1, 0 ); + v = varianceyc( x.length, 1.0, x, 1, 0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = varianceyc( x.length, 1.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = varianceyc( x.length, 1.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance of a strided array (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = varianceyc( x.length, 1.0, toAccessorArray( x ), 1, 0 ); t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); x = [ -4.0, -4.0 ]; - v = varianceyc( x.length, 1, x, 1, 0 ); + v = varianceyc( x.length, 1.0, toAccessorArray( x ), 1, 0 ); t.strictEqual( v, 0.0, 'returns expected value' ); x = [ NaN, 4.0 ]; - v = varianceyc( x.length, 1, x, 1, 0 ); + v = varianceyc( x.length, 1.0, toAccessorArray( x ), 1, 0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); @@ -83,10 +121,29 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = varianceyc( 0, 1, x, 1, 0 ); + v = varianceyc( 0, 1.0, x, 1, 0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); - v = varianceyc( -1, 1, x, 1, 0 ); + v = varianceyc( -1, 1.0, 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` (accessor)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = varianceyc( 0, 1.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = varianceyc( -1, 1.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = toAccessorArray( [ 10.0 ] ); + v = varianceyc( 0, 0.0, x, 1, 0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); @@ -98,7 +155,23 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a populat x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = varianceyc( 1, 0, x, 1, 0 ); + v = varianceyc( 1, 0.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns a population variance of `0` (accessor)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = varianceyc( 1, 0.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = toAccessorArray( [ 5.0 ] ); + v = varianceyc( 1, 0.0, x, 1, 0 ); t.strictEqual( v, 0.0, 'returns expected value' ); t.end(); @@ -119,8 +192,26 @@ 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 (accessor)`', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = varianceyc( x.length, x.length, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = varianceyc( x.length, x.length+1, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = toAccessorArray( [ 7.0, 8.0, 9.0 ] ); + v = varianceyc( x.length, x.length, 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; @@ -135,15 +226,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = varianceyc( N, 1, x, 2, 0 ); + v = varianceyc( 4, 1.0, x, 2, 0 ); + + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessor)', 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 = varianceyc( 4, 1.0, toAccessorArray( x ), 2, 0 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -158,8 +268,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = varianceyc( N, 1, x, -2, 6 ); + v = varianceyc( 4, 1.0, x, -2, 6 ); + + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessor)', 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 = varianceyc( 4, 1.0, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -171,14 +301,25 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = varianceyc( x.length, 1, x, 0, 0 ); + v = varianceyc( x.length, 1.0, x, 0, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = varianceyc( x.length, 1.0, 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; @@ -192,10 +333,27 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = varianceyc( N, 1, x, 2, 1 ); + v = varianceyc( 4, 1.0, x, 2, 1 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); }); + +tape( 'the function supports an `offset` parameter (accessor)', 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 = varianceyc( 4, 1.0, toAccessorArray( x ), 2, 1 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.varianceyc.js b/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.varianceyc.js deleted file mode 100644 index 4ff7dd76fcf8..000000000000 --- a/lib/node_modules/@stdlib/stats/base/varianceyc/test/test.varianceyc.js +++ /dev/null @@ -1,206 +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 isnan = require( '@stdlib/math/base/assert/is-nan' ); -var Float64Array = require( '@stdlib/array/float64' ); -var varianceyc = require( './../lib/varianceyc.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof varianceyc, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function has an arity of 4', function test( t ) { - t.strictEqual( varianceyc.length, 4, 'has expected arity' ); - t.end(); -}); - -tape( 'the function calculates the population variance 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 = varianceyc( x.length, 0, x, 1 ); - t.strictEqual( v, 53.5/x.length, 'returns expected value' ); - - x = [ -4.0, -4.0 ]; - v = varianceyc( x.length, 0, x, 1 ); - t.strictEqual( v, 0.0, 'returns expected value' ); - - x = [ NaN, 4.0 ]; - v = varianceyc( x.length, 0, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function calculates the sample variance 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 = varianceyc( x.length, 1, x, 1 ); - t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); - - x = [ -4.0, -4.0 ]; - v = varianceyc( x.length, 1, x, 1 ); - t.strictEqual( v, 0.0, 'returns expected value' ); - - x = [ NaN, 4.0 ]; - v = varianceyc( 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 = varianceyc( 0, 1, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - v = varianceyc( -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 variance of `0`', function test( t ) { - var x; - var v; - - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - - v = varianceyc( 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 = varianceyc( x.length, x.length, x, 1 ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - v = varianceyc( 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 = varianceyc( N, 1, x, 2 ); - - t.strictEqual( v, 6.25, '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 = varianceyc( N, 1, x, -2 ); - - t.strictEqual( v, 6.25, '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 = varianceyc( 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 = varianceyc( N, 1, x1, 2 ); - t.strictEqual( v, 6.25, 'returns expected value' ); - - t.end(); -});