diff --git a/lib/node_modules/@stdlib/stats/base/variance/README.md b/lib/node_modules/@stdlib/stats/base/variance/README.md index 916d85c4a011..686d126ffdbe 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/README.md +++ b/lib/node_modules/@stdlib/stats/base/variance/README.md @@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var variance = require( '@stdlib/stats/base/variance' ); ``` -#### variance( N, correction, x, stride ) +#### variance( N, correction, x, strideX ) Computes the [variance][variance] of a strided array `x`. @@ -115,17 +115,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 strideX 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 = variance( N, 1, x, 2 ); +var v = variance( 4, 1, x, 2 ); // returns 6.25 ``` @@ -135,18 +132,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = variance( N, 1, x1, 2 ); +var v = variance( 4, 1, x1, 2 ); // returns 6.25 ``` -#### variance.ndarray( N, correction, x, stride, offset ) +#### variance.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a strided array using alternative indexing semantics. @@ -160,17 +154,14 @@ var v = variance.ndarray( N, 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 [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 the strided array starting from the second element ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -var N = floor( x.length / 2 ); -var v = variance.ndarray( N, 1, x, 2, 1 ); +var v = variance.ndarray( 4, 1, x, 2, 1 ); // returns 6.25 ``` @@ -183,6 +174,7 @@ var v = variance.ndarray( N, 1, x, 2, 1 ); ## Notes - If `N <= 0`, both functions return `NaN`. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). - 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 ([`dvariance`][@stdlib/stats/base/dvariance], [`svariance`][@stdlib/stats/base/svariance], etc.) are likely to be significantly more performant. @@ -197,18 +189,12 @@ var v = variance.ndarray( N, 1, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var variance = require( '@stdlib/stats/base/variance' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = variance( x.length, 1, x, 1 ); @@ -250,6 +236,8 @@ console.log( v ); [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray diff --git a/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.js index 03f80f2587bf..5575120b06ab 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.js @@ -21,11 +21,18 @@ // 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 variance = require( './../lib/variance.js' ); +var variance = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var variance = require( './../lib/variance.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.ndarray.js index b036eb6288c7..c02c1ba01b37 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variance/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 variance = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var variance = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/variance/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/variance/docs/repl.txt index ba1fd778e8d0..441defebff8c 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/variance/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the variance of a strided array. - 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 are at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -30,8 +30,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -45,11 +45,9 @@ > {{alias}}( x.length, 1, 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, x, 2 ) ~4.3333 // Using view offsets: @@ -57,10 +55,11 @@ > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); > stride = 2; - > {{alias}}( N, 1, x1, stride ) + > {{alias}}( 3, 1, x1, 2 ) ~4.3333 -{{alias}}.ndarray( N, correction, x, stride, offset ) + +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the variance of a strided array using alternative indexing semantics. @@ -88,10 +87,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -108,8 +107,7 @@ // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, 1, x, 2, 1 ) + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~4.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/variance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/variance/docs/types/index.d.ts index 0f7fd64a4723..4a9b59282313 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/variance/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 `variance`. @@ -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 variance * * @example @@ -41,7 +46,7 @@ interface Routine { * var v = variance( x.length, 1, 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 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 variance * * @example @@ -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 variance * * @example diff --git a/lib/node_modules/@stdlib/stats/base/variance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/variance/docs/types/test.ts index 0cb8dc2eb070..09e2570ecc52 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/variance/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import variance = require( './index' ); @@ -26,6 +27,7 @@ import variance = require( './index' ); const x = new Float64Array( 10 ); variance( x.length, 1, x, 1 ); // $ExpectType number + variance( 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 variance = require( './index' ); const x = new Float64Array( 10 ); variance.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + variance.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/variance/examples/index.js b/lib/node_modules/@stdlib/stats/base/variance/examples/index.js index 97e21fb066d8..ccf70cf0735d 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/variance/examples/index.js @@ -18,18 +18,12 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var variance = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = variance( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/variance/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/variance/lib/accessors.js new file mode 100644 index 000000000000..470dcc31b1ef --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/variance/lib/accessors.js @@ -0,0 +1,84 @@ +/** +* @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'; + +// MAIN // + +/** +* Computes the variance of a strided array using a one-pass textbook algorithm. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1.0, -2.0, 2.0 ] ); +* +* var v = variance( x.length, 1, arraylike2object( x ), 1, 0 ); +* // returns ~4.3333 +*/ +function variance( N, correction, x, strideX, offsetX ) { + var xbuf; + var get; + var S2; + var ix; + var S; + var v; + var n; + var i; + + n = N - correction; + if ( N <= 0 || n <= 0.0 ) { + return NaN; + } + + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + return get( xbuf, offsetX ); + } + ix = offsetX; + S2 = 0.0; + S = 0.0; + for ( i = 0; i < N; i++ ) { + v = get( xbuf, ix ); + S2 += v * v; + S += v; + ix += strideX; + } + return (S2 - ((S/N)*S)) / n; +} + + +// EXPORTS // + +module.exports = variance; diff --git a/lib/node_modules/@stdlib/stats/base/variance/lib/index.js b/lib/node_modules/@stdlib/stats/base/variance/lib/index.js index 9e7433c88b87..b8aeab5584b3 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/variance/lib/index.js @@ -29,7 +29,7 @@ * var x = [ 1.0, -2.0, 2.0 ]; * var N = x.length; * -* var v = variance( N, 1, x, 1 ); +* var v = variance( x.length, 1, x, 1 ); * // returns ~4.3333 * * @example @@ -39,13 +39,20 @@ * 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 = variance.ndarray( N, 1, x, 2, 1 ); +* var v = variance.ndarray( 4, 1, 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/variance/lib/main.js b/lib/node_modules/@stdlib/stats/base/variance/lib/main.js index d7ff74d08f57..590146077561 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/variance/lib/main.js @@ -20,14 +20,31 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var variance = require( './variance.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( variance, 'ndarray', ndarray ); +/** +* Computes the variance of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} 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 N = x.length; +* +* var v = variance( N, 1, x, 1 ); +* // returns ~4.3333 +*/ +function variance( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/variance/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/variance/lib/ndarray.js index 07693d8e0141..bafb38abd6c0 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variance/lib/ndarray.js @@ -20,7 +20,8 @@ // MODULES // -var variancepn = require( '@stdlib/stats/base/variancepn' ).ndarray; +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -31,21 +32,62 @@ var variancepn = require( '@stdlib/stats/base/variancepn' ).ndarray; * @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 = variance( N, 1, x, 2, 1 ); +* var v = variance( 4, 1, x, 2, 1 ); * // returns 6.25 */ -function variance( N, correction, x, stride, offset ) { - return variancepn( N, correction, x, stride, offset ); +function variance( N, correction, x, strideX, offsetX ) { + var delta2; + var delta; + var M2; + var mu; + var ix; + var v; + var n; + var o; + var i; + + if ( N <= 0 ) { + return NaN; + } + if ( N === 1 || strideX === 0 ) { + if ( N - correction <= 0 ) { + return NaN; + } + return 0.0; + } + n = N - correction; + if ( n <= 0 ) { + return NaN; + } + + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + + ix = offsetX; + M2 = 0.0; + mu = 0.0; + + for ( i = 0; i < N; i++ ) { + v = o.data[ ix ]; + delta = v - mu; + mu += delta / ( i + 1 ); + delta2 = v - mu; // Calculate second delta after mean is updated + M2 += delta * delta2; + ix += strideX; + } + + return M2 / n; } diff --git a/lib/node_modules/@stdlib/stats/base/variance/lib/variance.js b/lib/node_modules/@stdlib/stats/base/variance/lib/variance.js deleted file mode 100644 index f36a8f8494b7..000000000000 --- a/lib/node_modules/@stdlib/stats/base/variance/lib/variance.js +++ /dev/null @@ -1,51 +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 variancepn = require( '@stdlib/stats/base/variancepn' ); - - -// MAIN // - -/** -* Computes the variance of a strided array. -* -* @param {PositiveInteger} N - number of indexed elements -* @param {number} correction - degrees of freedom adjustment -* @param {NumericArray} x - input array -* @param {integer} stride - stride length -* @returns {number} variance -* -* @example -* var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; -* -* var v = variance( N, 1, x, 1 ); -* // returns ~4.3333 -*/ -function variance( N, correction, x, stride ) { - return variancepn( N, correction, x, stride ); -} - - -// EXPORTS // - -module.exports = variance; diff --git a/lib/node_modules/@stdlib/stats/base/variance/test/test.variance.js b/lib/node_modules/@stdlib/stats/base/variance/test/test.main.js similarity index 81% rename from lib/node_modules/@stdlib/stats/base/variance/test/test.variance.js rename to lib/node_modules/@stdlib/stats/base/variance/test/test.main.js index 59488887ff53..0df06e1e94aa 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/test/test.variance.js +++ b/lib/node_modules/@stdlib/stats/base/variance/test/test.main.js @@ -21,10 +21,10 @@ // 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 Float64Array = require( '@stdlib/array/float64' ); -var variance = require( './../lib/variance.js' ); +var variance = require( '@stdlib/stats/base/variance/lib/main.js' ); // TESTS // @@ -59,12 +59,12 @@ tape( 'the function calculates the population variance of a strided array', func t.end(); }); -tape( 'the function calculates the sample variance of a strided array', function test( t ) { +tape( 'the function calculates the sample variance of a strided array (accessors)', function test( t ) { var x; var v; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = variance( x.length, 1, x, 1 ); + v = variance( x.length, 1, toAccessorArray( x ), 1 ); t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); x = [ -4.0, -4.0 ]; @@ -121,7 +121,6 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -136,15 +135,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = variance( N, 1, x, 2 ); + v = variance( 4, 1, x, 2 ); + + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = variance( 4, 1, 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 N; var x; var v; @@ -159,8 +177,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = variance( N, 1, x, -2 ); + v = variance( 4, 1, x, -2 ); + + t.strictEqual( v, 6.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = variance( 4, 1, toAccessorArray( x ), -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -181,7 +219,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -197,9 +234,8 @@ 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 = variance( N, 1, x1, 2 ); + v = variance( 4, 1, x1, 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/variance/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/variance/test/test.ndarray.js index 086e610de450..4f3781db3f41 100644 --- a/lib/node_modules/@stdlib/stats/base/variance/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variance/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 variance = require( './../lib/ndarray.js' ); @@ -34,6 +34,25 @@ tape( 'main export is a function', function test( t ) { t.end(); }); +tape( 'the function calculates the sample variance of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variance( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variance( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variance( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function has an arity of 5', function test( t ) { t.strictEqual( variance.length, 5, 'has expected arity' ); t.end();