diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/README.md b/lib/node_modules/@stdlib/stats/base/variancepn/README.md index 3338288f2554..0bfd12f46a3b 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/README.md +++ b/lib/node_modules/@stdlib/stats/base/variancepn/README.md @@ -98,15 +98,14 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var variancepn = require( '@stdlib/stats/base/variancepn' ); ``` -#### variancepn( N, correction, x, stride ) +#### variancepn( N, correction, x, strideX ) -Computes the [variance][variance] of a strided array `x` using a two-pass algorithm. +Computes the [variance][variance] of a strided array using a two-pass algorithm. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = variancepn( N, 1, x, 1 ); +var v = variancepn( x.length, 1, x, 1 ); // returns ~4.3333 ``` @@ -115,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 = variancepn( N, 1, x, 2 ); +var v = variancepn( 4, 1, x, 2 ); // returns 6.25 ``` @@ -135,42 +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 = variancepn( N, 1, x1, 2 ); +var v = variancepn( 4, 1, x1, 2 ); // returns 6.25 ``` -#### variancepn.ndarray( N, correction, x, stride, offset ) +#### variancepn.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a strided array using a two-pass algorithm and alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = variancepn.ndarray( N, 1, x, 1, 0 ); +var v = variancepn.ndarray( x.length, 1, 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 = variancepn.ndarray( N, 1, x, 2, 1 ); +var v = variancepn.ndarray( 4, 1, x, 2, 1 ); // returns 6.25 ``` @@ -184,6 +173,7 @@ var v = variancepn.ndarray( N, 1, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). - Depending on the environment, the typed versions ([`dvariancepn`][@stdlib/stats/strided/dvariancepn], [`svariancepn`][@stdlib/stats/strided/svariancepn], etc.) are likely to be significantly more performant. @@ -197,18 +187,12 @@ var v = variancepn.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 variancepn = require( '@stdlib/stats/base/variancepn' ); -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 = variancepn( x.length, 1, x, 1 ); @@ -265,6 +249,8 @@ console.log( v ); [@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [@stdlib/stats/strided/dvariancepn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dvariancepn diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.js index 24b5d4603b04..39accf4d64b1 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/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 variancepn = require( './../lib/variancepn.js' ); +var variancepn = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var variancepn = require( './../lib/variancepn.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/variancepn/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.ndarray.js index a60d78aca901..95fcc33e9c07 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/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 variancepn = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var variancepn = 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/variancepn/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/variancepn/docs/repl.txt index e3bffde92299..0f5b45ec364c 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/variancepn/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the variance of a strided array using a two-pass 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. @@ -30,8 +30,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -45,22 +45,19 @@ > {{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: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, 1, x1, stride ) + > {{alias}}( 3, 1, x1, 2 ) ~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 two-pass algorithm and alternative indexing semantics. @@ -88,10 +85,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -107,9 +104,8 @@ ~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 ) + > x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~4.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/variancepn/docs/types/index.d.ts index 7db62141e6a1..067b117ec1b1 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/variancepn/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 `variancepn`. @@ -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 = variancepn( 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 a two-pass 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 variance * * @example @@ -59,7 +64,7 @@ interface Routine { * var v = variancepn.ndarray( x.length, 1, 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,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/variancepn/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/variancepn/docs/types/test.ts index 146d3b4a5983..6d421920bf71 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/variancepn/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import variancepn = require( './index' ); @@ -26,6 +27,7 @@ import variancepn = require( './index' ); const x = new Float64Array( 10 ); variancepn( x.length, 1, x, 1 ); // $ExpectType number + variancepn( 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 variancepn = require( './index' ); const x = new Float64Array( 10 ); variancepn.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + variancepn.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/variancepn/examples/index.js b/lib/node_modules/@stdlib/stats/base/variancepn/examples/index.js index c128c57945e2..1604300161f2 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/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 variancepn = 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 = variancepn( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/lib/variancepn.js b/lib/node_modules/@stdlib/stats/base/variancepn/lib/accessors.js similarity index 66% rename from lib/node_modules/@stdlib/stats/base/variancepn/lib/variancepn.js rename to lib/node_modules/@stdlib/stats/base/variancepn/lib/accessors.js index e6be432823a1..70c77a3760dc 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/variancepn.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/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. @@ -20,7 +20,7 @@ // MODULES // -var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ); +var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).ndarray; // MAIN // @@ -39,18 +39,25 @@ var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ); * * @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 N = x.length; +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); * -* var v = variancepn( N, 1, x, 1 ); -* // returns ~4.3333 +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = variancepn( 4, 1, arraylike2object( x ), 2, 1 ); +* // returns 6.25 */ -function variancepn( N, correction, x, stride ) { +function variancepn( N, correction, x, strideX, offsetX ) { + var xbuf; + var get; var mu; var ix; var M2; @@ -59,29 +66,27 @@ function variancepn( N, correction, x, stride ) { var n; var i; - n = N - correction; - if ( N <= 0 || n <= 0.0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return 0.0; - } + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + // Compute an estimate for the mean: - mu = gsumpw( N, x, stride ) / N; + mu = gsumpw( N, xbuf, strideX, offsetX ) / N; + + n = N - correction; + ix = offsetX; - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } // Compute the variance... M2 = 0.0; M = 0.0; + for ( i = 0; i < N; i++ ) { - d = x[ ix ] - mu; + d = get( xbuf, ix ) - mu; M2 += d * d; M += d; - ix += stride; + ix += strideX; } return (M2/n) - ((M/N)*(M/n)); } diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/lib/index.js b/lib/node_modules/@stdlib/stats/base/variancepn/lib/index.js index 9c1e2b197154..0c47634b9930 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/index.js @@ -27,25 +27,29 @@ * var variancepn = require( '@stdlib/stats/base/variancepn' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = variancepn( N, 1, x, 1 ); +* var v = variancepn( x.length, 1, x, 1 ); * // returns ~4.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var variancepn = require( '@stdlib/stats/base/variancepn' ); * * 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 = variancepn.ndarray( N, 1, x, 2, 1 ); +* var v = variancepn.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/variancepn/lib/main.js b/lib/node_modules/@stdlib/stats/base/variancepn/lib/main.js index f20dcbfda109..a9378f2693ab 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/main.js @@ -20,14 +20,41 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var variancepn = require( './variancepn.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( variancepn, 'ndarray', ndarray ); +/** +* Computes the variance of a strided array using a two-pass algorithm. +* +* ## Method +* +* - This implementation uses a two-pass approach, as suggested by Neely (1966). +* +* ## References +* +* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958). +* - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036). +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {Object} x - input array object +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length +* @param {integer} offsetX - starting index +* @returns {number} variance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = variancepn( x.length, 1, x, 1); +* // returns ~4.3333 +*/ +function variancepn( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/variancepn/lib/ndarray.js index 6d3ee7b50e81..8e032e6811aa 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/ndarray.js @@ -21,6 +21,8 @@ // MODULES // var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).ndarray; +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -40,23 +42,21 @@ var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).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 = variancepn( N, 1, x, 2, 1 ); +* var v = variancepn( 4, 1, x, 2, 1 ); * // returns 6.25 */ -function variancepn( N, correction, x, stride, offset ) { +function variancepn( N, correction, x, strideX, offsetX ) { var mu; var ix; var M2; + var o; var M; var d; var n; @@ -66,21 +66,25 @@ function variancepn( N, correction, x, stride, offset ) { if ( N <= 0 || n <= 0.0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { + if ( N === 1 || strideX === 0 ) { return 0.0; } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } // Compute an estimate for the mean: - mu = gsumpw( N, x, stride, offset ) / N; + mu = gsumpw( N, x, strideX, offsetX ) / N; // Compute the variance... - ix = offset; + ix = offsetX; M2 = 0.0; M = 0.0; for ( i = 0; i < N; i++ ) { d = x[ ix ] - mu; M2 += d * d; M += d; - ix += stride; + ix += strideX; } return (M2/n) - ((M/N)*(M/n)); } diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/variancepn/test/test.ndarray.js index 0628463ab9a1..2747ec614540 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/test/test.ndarray.js @@ -21,8 +21,8 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var variancepn = require( './../lib/ndarray.js' ); @@ -58,6 +58,25 @@ tape( 'the function calculates the population variance of a strided array', func 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 = variancepn( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancepn( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancepn( x.length, 0, toAccessorArray( x ), 1, 0 ); + 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; @@ -77,6 +96,25 @@ tape( 'the function calculates the sample variance of a strided array', function 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 = variancepn( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancepn( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancepn( 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; @@ -92,6 +130,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` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancepn( -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 variance of `0`', function test( t ) { var x; var v; @@ -104,6 +157,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a populat t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns a population variance of `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( 1, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -119,8 +184,22 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or t.end(); }); +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancepn( 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; @@ -135,15 +214,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = variancepn( N, 1, x, 2, 0 ); + v = variancepn( 4, 1, 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 = variancepn( 4, 1, 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 +256,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = variancepn( N, 1, x, -2, 6 ); + v = variancepn( 4, 1, 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 = variancepn( 4, 1, toAccessorArray( x ), -2, 6 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -177,8 +295,19 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( x.length, 1, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -192,9 +321,29 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = variancepn( N, 1, x, 2, 1 ); + v = variancepn( 4, 1, 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 = variancepn( 4, 1, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/test/test.variancepn.js b/lib/node_modules/@stdlib/stats/base/variancepn/test/test.variancepn.js index 5b72a7c7e5be..985611269c21 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/test/test.variancepn.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/test/test.variancepn.js @@ -21,10 +21,10 @@ // 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 variancepn = require( './../lib/variancepn.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var variancepn = require( './../lib/main.js' ); // TESTS // @@ -59,6 +59,25 @@ tape( 'the function calculates the population variance of a strided array', func 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 = variancepn( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancepn( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancepn( x.length, 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; @@ -78,6 +97,25 @@ tape( 'the function calculates the sample variance of a strided array', function 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 = variancepn( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancepn( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancepn( 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; @@ -93,6 +131,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancepn( -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 variance of `0`', function test( t ) { var x; var v; @@ -105,6 +158,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a populat t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns a population variance of `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( 1, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -120,8 +185,22 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or t.end(); }); +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( x.length, x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancepn( 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; @@ -136,15 +215,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = variancepn( N, 1, x, 2 ); + v = variancepn( 4, 1, 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 = variancepn( 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 +257,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = variancepn( N, 1, x, -2 ); + v = variancepn( 4, 1, 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 = variancepn( 4, 1, toAccessorArray( x ), -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -178,10 +296,21 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancepn( x.length, 1, toAccessorArray( x ), 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -197,9 +326,33 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = variancepn( N, 1, x1, 2 ); + v = variancepn( 4, 1, 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 = variancepn( 4, 1, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end();