diff --git a/lib/node_modules/@stdlib/stats/base/variancech/README.md b/lib/node_modules/@stdlib/stats/base/variancech/README.md index 885a0d0b0eae..a4875152ee2e 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/README.md +++ b/lib/node_modules/@stdlib/stats/base/variancech/README.md @@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var variancech = require( '@stdlib/stats/base/variancech' ); ``` -#### variancech( N, correction, x, stride ) +#### variancech( N, correction, x, strideX ) -Computes the [variance][variance] of a strided array `x` using a one-pass trial mean algorithm. +Computes the [variance][variance] of a strided array using a one-pass trial mean algorithm. ```javascript var x = [ 1.0, -2.0, 2.0 ]; @@ -114,17 +114,14 @@ The function has the following parameters: - **N**: number of indexed elements. - **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [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 = variancech( N, 1, x, 2 ); +var v = variancech( 4, 1, x, 2 ); // returns 6.25 ``` @@ -134,18 +131,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = variancech( N, 1, x1, 2 ); +var v = variancech( 4, 1, x1, 2 ); // returns 6.25 ``` -#### variancech.ndarray( N, correction, x, stride, offset ) +#### variancech.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a strided array using a one-pass trial mean algorithm and alternative indexing semantics. @@ -158,17 +152,14 @@ var v = variancech.ndarray( x.length, 1, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [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 = variancech.ndarray( N, 1, x, 2, 1 ); +var v = variancech.ndarray( 4, 1, x, 2, 1 ); // returns 6.25 ``` @@ -183,6 +174,7 @@ var v = variancech.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`. - The underlying algorithm is a specialized case of Neely's two-pass algorithm. As the variance is invariant with respect to changes in the location parameter, the underlying algorithm uses the first strided array element as a trial mean to shift subsequent data values and thus mitigate catastrophic cancellation. Accordingly, the algorithm's accuracy is best when data is **unordered** (i.e., the data is **not** sorted in either ascending or descending order such that the first value is an "extreme" value). +- 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 ([`dvariancech`][@stdlib/stats/strided/dvariancech], [`svariancech`][@stdlib/stats/strided/svariancech], etc.) are likely to be significantly more performant. @@ -196,18 +188,12 @@ var v = variancech.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 variancech = require( '@stdlib/stats/base/variancech' ); -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 = variancech( x.length, 1, x, 1 ); @@ -260,6 +246,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/svariancech]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/svariancech [@neely:1966a]: https://doi.org/10.1145/365719.365958 diff --git a/lib/node_modules/@stdlib/stats/base/variancech/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/variancech/benchmark/benchmark.js index ffbd065eb648..ac477d0789d7 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/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 variancech = require( './../lib/variancech.js' ); +var variancech = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var variancech = require( './../lib/variancech.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/variancech/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/variancech/benchmark/benchmark.ndarray.js index bd6db354fd9a..b273ff4a8d68 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/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 variancech = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var variancech = 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/variancech/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/variancech/docs/repl.txt index 9f65d159e89e..929365feac6b 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/variancech/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 trial mean algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -31,8 +31,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -46,22 +46,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 one-pass trial mean algorithm and alternative indexing semantics. @@ -89,10 +86,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -108,9 +105,8 @@ ~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/variancech/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/variancech/docs/types/index.d.ts index 9af38fe17b03..749ed66d2ca5 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/variancech/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 `variancech`. @@ -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 = variancech( 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 one-pass trial mean 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 = variancech.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/variancech/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/variancech/docs/types/test.ts index 4487f142c189..7c9a437186d3 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/variancech/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import variancech = require( './index' ); @@ -26,6 +27,7 @@ import variancech = require( './index' ); const x = new Float64Array( 10 ); variancech( x.length, 1, x, 1 ); // $ExpectType number + variancech( 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 variancech = require( './index' ); const x = new Float64Array( 10 ); variancech.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + variancech.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/variancech/examples/index.js b/lib/node_modules/@stdlib/stats/base/variancech/examples/index.js index 3b545a9c8e69..adaae05a5d6d 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/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 variancech = 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 = variancech( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/variancech/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/variancech/lib/accessors.js new file mode 100644 index 000000000000..149ba4659feb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/variancech/lib/accessors.js @@ -0,0 +1,95 @@ +/** +* @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 trial mean algorithm. +* +* ## Method +* +* - This implementation uses a one-pass trial mean approach, as suggested by Chan et al (1983). +* +* ## 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). +* - Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154](https://doi.org/10.2307/2286154). +* - Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. 1983. "Algorithms for Computing the Sample Variance: Analysis and Recommendations." _The American Statistician_ 37 (3). American Statistical Association, Taylor & Francis, Ltd.: 242–47. doi:[10.1080/00031305.1983.10483115](https://doi.org/10.1080/00031305.1983.10483115). +* - 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). +* +* @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( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = variancech( 4, 1, arraylike2object( x ), 2, 1 ); +* // returns 6.25 +*/ +function variancech( N, correction, x, strideX, offsetX ) { + var xbuf; + var get; + var mu; + var ix; + var M2; + var M; + var d; + var n; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + + n = N - correction; + ix = offsetX; + + // Use an estimate for the mean: + mu = get( xbuf, ix ); + ix += strideX; + + // Compute the variance... + M2 = 0.0; + M = 0.0; + for ( i = 1; i < N; i++ ) { + d = get( xbuf, ix ) - mu; + M2 += d * d; + M += d; + ix += strideX; + } + return (M2/n) - ((M/N)*(M/n)); +} + + +// EXPORTS // + +module.exports = variancech; diff --git a/lib/node_modules/@stdlib/stats/base/variancech/lib/index.js b/lib/node_modules/@stdlib/stats/base/variancech/lib/index.js index c0b519ac3b52..94ddc67f28c7 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/lib/index.js @@ -27,19 +27,16 @@ * var variancech = require( '@stdlib/stats/base/variancech' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = variancech( N, 1, x, 1 ); +* var v = variancech( x.length, 1, x, 1 ); * // returns ~4.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var variancech = require( '@stdlib/stats/base/variancech' ); * * 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 = variancech.ndarray( N, 1, x, 2, 1 ); +* var v = variancech.ndarray( 4, 1, x, 2, 1 ); * // returns 6.25 */ diff --git a/lib/node_modules/@stdlib/stats/base/variancech/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/variancech/lib/ndarray.js index 25b104edd458..1c04ab725cd3 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/lib/ndarray.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + // MAIN // /** @@ -37,40 +43,42 @@ * @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 = variancech( N, 1, x, 2, 1 ); +* var v = variancech( 4, 1, x, 2, 1 ); * // returns 6.25 */ -function variancech( N, correction, x, stride, offset ) { +function variancech( N, correction, x, strideX, offsetX ) { var mu; var ix; var M2; var M; var d; var n; + var o; var i; n = N - correction; if ( N <= 0 || n <= 0.0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { + if ( N === 1 || strideX === 0 ) { return 0.0; } - ix = offset; + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + ix = offsetX; // Use an estimate for the mean: mu = x[ ix ]; - ix += stride; + ix += strideX; // Compute the variance... M2 = 0.0; @@ -79,7 +87,7 @@ function variancech( N, correction, x, stride, offset ) { 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/variancech/lib/variancech.js b/lib/node_modules/@stdlib/stats/base/variancech/lib/variancech.js index 556f18f3b712..d6ef6233ab47 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/lib/variancech.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/lib/variancech.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + // MAIN // /** @@ -37,51 +43,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 {integer} strideX - stride length * @returns {number} variance * * @example * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = variancech( N, 1, x, 1 ); +* var v = variancech( x.length, 1, x, 1 ); * // returns ~4.3333 */ -function variancech( N, correction, x, stride ) { - var mu; - var ix; - var M2; - var M; - var d; - var n; - 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; - } - // Use an estimate for the mean: - mu = x[ ix ]; - ix += stride; - - // Compute the variance... - M2 = 0.0; - M = 0.0; - for ( i = 1; i < N; i++ ) { - d = x[ ix ] - mu; - M2 += d * d; - M += d; - ix += stride; - } - return (M2/n) - ((M/N)*(M/n)); +function variancech( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/variancech/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/variancech/test/test.ndarray.js index 17007830c611..c1327184ccfb 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/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 variancech = 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 (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancech( 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 (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancech( 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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancech( -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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( 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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancech( 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 = variancech( N, 1, x, 2, 0 ); + v = variancech( 4, 1, x, 2, 0 ); + + 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 = variancech( 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 = variancech( N, 1, x, -2, 6 ); + v = variancech( 4, 1, x, -2, 6 ); + + 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 = variancech( 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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( 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 = variancech( N, 1, x, 2, 1 ); + v = variancech( 4, 1, x, 2, 1 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = variancech( 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/variancech/test/test.variancech.js b/lib/node_modules/@stdlib/stats/base/variancech/test/test.variancech.js index 16f49df0ccc3..83fc264cbc23 100644 --- a/lib/node_modules/@stdlib/stats/base/variancech/test/test.variancech.js +++ b/lib/node_modules/@stdlib/stats/base/variancech/test/test.variancech.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 variancech = require( './../lib/variancech.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var variancech = 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 (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancech( 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 (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancech( 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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancech( -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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( 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` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( x.length, x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancech( 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 = variancech( N, 1, x, 2 ); + v = variancech( 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 = variancech( 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,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = variancech( N, 1, x, -2 ); + v = variancech( 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 = variancech( 4, 1, toAccessorArray( x ), -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -178,10 +295,21 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancech( 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 +325,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 = variancech( N, 1, x1, 2 ); + v = variancech( 4, 1, x1, 2 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessors)', function test( t ) { + var x0; + var x1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = variancech( 4, 1, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end();