diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/README.md b/lib/node_modules/@stdlib/stats/base/nanvariancech/README.md index 479cf68094bd..4711fecb45a5 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/README.md +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/README.md @@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var nanvariancech = require( '@stdlib/stats/base/nanvariancech' ); ``` -#### nanvariancech( N, correction, x, stride ) +#### nanvariancech( N, correction, x, strideX ) Computes the [variance][variance] of a strided array `x` ignoring `NaN` values and using a one-pass trial mean algorithm. @@ -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 and `n` corresponds to the number of non-`NaN` indexed elements. 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 stided 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, NaN ]; -var N = floor( x.length / 2 ); -var v = nanvariancech( N, 1, x, 2 ); +var v = nanvariancech( 4, 1, x, 2 ); // returns 6.25 ``` @@ -134,41 +131,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = nanvariancech( N, 1, x1, 2 ); +var v = nanvariancech( 4, 1, x1, 2 ); // returns 6.25 ``` -#### nanvariancech.ndarray( N, correction, x, stride, offset ) +#### nanvariancech.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, NaN, 2.0 ]; -var v = nanvariancech.ndarray( x.length, 1, x, 1, 0 ); +var v = nanvariancech.ndarray( 4, 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 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, NaN, NaN ]; -var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -var N = floor( x.length / 2 ); - -var v = nanvariancech.ndarray( N, 1, x, 2, 1 ); +var v = nanvariancech.ndarray( 5, 1, x, 2, 1 ); // returns 6.25 ``` @@ -183,6 +174,7 @@ var v = nanvariancech.ndarray( N, 1, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - If `n - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements), both functions return `NaN`. - 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 non-`NaN` 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 ([`dnanvariancech`][@stdlib/stats/base/dnanvariancech], [`snanvariancech`][@stdlib/stats/base/snanvariancech], etc.) are likely to be significantly more performant. @@ -196,18 +188,19 @@ var v = nanvariancech.ndarray( N, 1, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var nanvariancech = require( '@stdlib/stats/base/nanvariancech' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'generic', rand ); console.log( x ); var v = nanvariancech( x.length, 1, x, 1 ); @@ -281,6 +274,8 @@ console.log( v ); [@stdlib/stats/base/variancech]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/variancech +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.js index 90544257d2e3..fd0a1451d9d2 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.js @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -30,6 +32,19 @@ var nanvariancech = require( './../lib/nanvariancech.js' ); // FUNCTIONS // +/** +* Returns a random value or `NaN`. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -38,17 +53,7 @@ var nanvariancech = require( './../lib/nanvariancech.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*20.0 ) - 10.0 ); - } - } + var x = filledarrayBy( len, 'generic', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.ndarray.js index 070399adcdfa..d4183a1c2cd7 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/benchmark/benchmark.ndarray.js @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -30,6 +32,19 @@ var nanvariancech = require( './../lib/ndarray.js' ); // FUNCTIONS // +/** +* Returns a random value or `NaN`. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -38,17 +53,7 @@ var nanvariancech = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*20.0 ) - 10.0 ); - } - } + var x = filledarrayBy( len, 'generic', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/repl.txt index 2b19376b9f9d..f65c7c0b0c64 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the variance of a strided array ignoring `NaN` values and using a one-pass trial mean algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed + 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 @@ -34,8 +34,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + stride length. Returns ------- @@ -46,25 +46,23 @@ -------- // Standard Usage: > var x = [ 1.0, -2.0, NaN, 2.0 ]; - > {{alias}}( x.length, 1, x, 1 ) + > {{alias}}( 4, 1, x, 1 ) ~4.3333 // 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, stride ) ~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, stride ) ~4.3333 -{{alias}}.ndarray( N, correction, x, stride, offset ) +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the variance of a strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. @@ -93,10 +91,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -108,13 +106,12 @@ -------- // Standard Usage: > var x = [ 1.0, -2.0, NaN, 2.0 ]; - > {{alias}}.ndarray( x.length, 1, x, 1, 0 ) + > {{alias}}.ndarray( 4, 1, x, 1, 0 ) ~4.3333 // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, 1, x, 2, 1 ) + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~4.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/types/index.d.ts index 81f681661e59..735228752d94 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/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 `nanvariancech`. @@ -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 = nanvariancech( 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 ignoring `NaN` values and 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 = nanvariancech.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, offset: 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/nanvariancech/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/types/test.ts index 4722f6fe9412..8c5a2fcd87f2 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/docs/types/test.ts @@ -16,16 +16,16 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import nanvariancech = require( './index' ); - // TESTS // // The function returns a number... { const x = new Float64Array( 10 ); - nanvariancech( x.length, 1, x, 1 ); // $ExpectType number + nanvariancech( 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... @@ -100,7 +100,7 @@ import nanvariancech = require( './index' ); { const x = new Float64Array( 10 ); - nanvariancech.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + nanvariancech.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/nanvariancech/examples/index.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/examples/index.js index d49eb9c49d89..e2c1345ccc80 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/examples/index.js @@ -18,22 +18,19 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var nanvariancech = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) > 1 ) { + return NaN; } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'generic', rand ); console.log( x ); var v = nanvariancech( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/accessors.js new file mode 100644 index 000000000000..18b36a833e0e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/accessors.js @@ -0,0 +1,122 @@ +/** +* @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 ignoring `NaN` values and 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). +* +* @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 arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); +* +* var v = nanvariancech( 5, 1, arraylike2object( x ), 2, 1 ); +* // returns 6.25 +*/ +function nanvariancech( N, correction, x, strideX, offsetX ) { + var xget; + var xbuf; + var mu; + var ix; + var M2; + var nc; + var M; + var d; + var v; + var n; + var i; + + // Cache references to array data: + xbuf = x.data; + + // Cache references to element accessors: + xget = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + v = xget( xbuf, offsetX ); + if ( v === v && N-correction > 0.0 ) { + return 0.0; + } + return NaN; + } + ix = offsetX; + + // Find an estimate for the mean... + for ( i = 0; i < N; i++ ) { + v = xget( xbuf, ix ); + if ( v === v ) { + mu = v; + break; + } + ix += strideX; + } + if ( i === N ) { + return NaN; + } + ix += strideX; + i += 1; + + // Compute the variance... + M2 = 0.0; + M = 0.0; + n = 1; + for ( i; i < N; i++ ) { + v = xget( xbuf, ix ); + if ( v === v ) { + d = v - mu; + M2 += d * d; + M += d; + n += 1; + } + ix += strideX; + } + nc = n - correction; + if ( nc <= 0.0 ) { + return NaN; + } + return (M2/nc) - ((M/n)*(M/nc)); +} + + +// EXPORTS // + +module.exports = nanvariancech; diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/nanvariancech.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/nanvariancech.js index a69d93ed3990..04c366a9775c 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/nanvariancech.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/nanvariancech.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + // MAIN // /** @@ -37,7 +43,7 @@ * @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 @@ -46,66 +52,8 @@ * var v = nanvariancech( x.length, 1, x, 1 ); * // returns ~4.3333 */ -function nanvariancech( N, correction, x, stride ) { - var mu; - var ix; - var M2; - var nc; - var M; - var d; - var v; - var n; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - v = x[ 0 ]; - if ( v === v && N-correction > 0.0 ) { - return 0.0; - } - return NaN; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - // Find an estimate for the mean... - for ( i = 0; i < N; i++ ) { - v = x[ ix ]; - if ( v === v ) { - mu = v; - break; - } - ix += stride; - } - if ( i === N ) { - return NaN; - } - ix += stride; - i += 1; - - // Compute the variance... - M2 = 0.0; - M = 0.0; - n = 1; - for ( i; i < N; i++ ) { - v = x[ ix ]; - if ( v === v ) { - d = v - mu; - M2 += d * d; - M += d; - n += 1; - } - ix += stride; - } - nc = n - correction; - if ( nc <= 0.0 ) { - return NaN; - } - return (M2/nc) - ((M/n)*(M/nc)); +function nanvariancech( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX) ); } diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/ndarray.js index c7a63df90567..58ee81d6bba4 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/lib/ndarray.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + // MAIN // /** @@ -37,20 +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 {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, NaN, NaN ]; -* var N = floor( x.length / 2 ); * -* var v = nanvariancech( N, 1, x, 2, 1 ); +* var v = nanvariancech( 5, 1, x, 2, 1 ); * // returns 6.25 */ -function nanvariancech( N, correction, x, stride, offset ) { +function nanvariancech( N, correction, x, strideX, offsetX ) { var mu; var ix; var M2; @@ -60,18 +63,23 @@ function nanvariancech( N, correction, x, stride, offset ) { var v; var n; var i; + var o; if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - v = x[ offset ]; + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + if ( N === 1 || strideX === 0 ) { + v = x[ offsetX ]; if ( v === v && N-correction > 0.0 ) { return 0.0; } return NaN; } - ix = offset; + ix = offsetX; // Find an estimate for the mean... for ( i = 0; i < N; i++ ) { @@ -80,12 +88,12 @@ function nanvariancech( N, correction, x, stride, offset ) { mu = v; break; } - ix += stride; + ix += strideX; } if ( i === N ) { return NaN; } - ix += stride; + ix += strideX; i += 1; // Compute the variance... @@ -100,7 +108,7 @@ function nanvariancech( N, correction, x, stride, offset ) { M += d; n += 1; } - ix += stride; + ix += strideX; } nc = n - correction; if ( nc <= 0.0 ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.nanvariancech.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.nanvariancech.js index ec54970e47f7..ed1dd9a617f6 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.nanvariancech.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.nanvariancech.js @@ -21,9 +21,9 @@ // 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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var nanvariancech = require( './../lib/nanvariancech.js' ); @@ -148,6 +148,114 @@ tape( 'the function calculates the sample variance of a strided array (ignoring t.end(); }); +tape( 'the function calculates the population variance of a strided array (accessors) (ignoring `NaN` values)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-6), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanvariancech( 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 (accessors) (ignoring `NaN` values)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-2), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-7), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanvariancech( 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; @@ -163,6 +271,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanvariancech( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanvariancech( -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` provided the first element is not `NaN`', function test( t ) { var x; var v; @@ -213,7 +336,6 @@ tape( 'if provided a `correction` parameter yielding a correction term less than }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -230,15 +352,36 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]; - N = floor( x.length / 2 ); - v = nanvariancech( N, 1, x, 2 ); + v = nanvariancech( 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, + NaN, // 4 + NaN + ]; + + v = nanvariancech( 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; var i; @@ -255,9 +398,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]; - N = floor( x.length / 2 ); - v = nanvariancech( N, 1, x, -2 ); + v = nanvariancech( 5, 1, x, -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); x = []; @@ -270,6 +412,37 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ + NaN, // 4 + NaN, + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = nanvariancech( 5, 1, toAccessorArray( x ), -2 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanvariancech( x.length, 1, toAccessorArray( x ), -1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` provided the correction term is not less than `0` and the first element is not `NaN`', function test( t ) { var x; var v; @@ -295,7 +468,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -313,9 +485,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 = nanvariancech( N, 1, x1, 2 ); + v = nanvariancech( 4, 1, x1, 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.ndarray.js index 8a66200f7f9f..8cb11920554b 100644 --- a/lib/node_modules/@stdlib/stats/base/nanvariancech/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanvariancech/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 nanvariancech = require( './../lib/ndarray.js' ); @@ -147,6 +147,114 @@ tape( 'the function calculates the sample variance of a strided array (ignoring t.end(); }); +tape( 'the function calculates the population variance of a strided array (accessors) (ignoring `NaN` values)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-6), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanvariancech( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanvariancech( 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 (accessors) (ignoring `NaN` values)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-2), 'returns expected value' ); + + x = [ 1.0, NaN, NaN, -2.0, NaN, -4.0, NaN, 5.0, NaN, 0.0, 3.0, NaN ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-7), 'returns expected value' ); + + x = [ -4.0, NaN ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ 4.0 ]; + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ]; + v = nanvariancech( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( NaN ); + } + v = nanvariancech( 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; @@ -162,6 +270,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 = nanvariancech( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanvariancech( -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` provided the first element is not `NaN`', function test( t ) { var x; var v; @@ -212,7 +335,6 @@ tape( 'if provided a `correction` parameter yielding a correction term less than }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -229,15 +351,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]; - N = floor( x.length / 2 ); - v = nanvariancech( N, 1, x, 2, 0 ); + v = nanvariancech( 4, 1, 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; var i; @@ -254,9 +374,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]; - N = floor( x.length / 2 ); - v = nanvariancech( N, 1, x, -2, 8 ); + v = nanvariancech( 4, 1, x, -2, 8 ); t.strictEqual( v, 6.25, 'returns expected value' ); x = []; @@ -269,6 +388,60 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0, + NaN, // 4 + NaN + ]; + + v = nanvariancech( 4, 1, toAccessorArray( x ), 2, 0 ); + + 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; + var i; + + x = [ + NaN, // 4 + NaN, + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = nanvariancech( 4, 1, toAccessorArray( x ), -2, 8 ); + t.strictEqual( v, 6.25, 'returns expected value' ); + + x = []; + for ( i = 0; i < 1e3; i++ ) { + x.push( 100.0 ); + } + v = nanvariancech( x.length, 1, toAccessorArray( x ), -1, x.length-1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` provided the correction term is not less than `0` and the first element is not `NaN`', function test( t ) { var x; var v; @@ -292,7 +465,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p }); tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -308,9 +480,31 @@ tape( 'the function supports an `offset` parameter', function test( t ) { NaN, NaN // 4 ]; - N = floor( x.length / 2 ); - v = nanvariancech( N, 1, x, 2, 1 ); + v = nanvariancech( 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 + NaN, + NaN // 4 + ]; + + v = nanvariancech( 4, 1, toAccessorArray( x ), 2, 1 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end();