diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/README.md b/lib/node_modules/@stdlib/stats/base/variancepn/README.md index 5775585ee772..d5cdc5b2e515 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/README.md +++ b/lib/node_modules/@stdlib/stats/base/variancepn/README.md @@ -2,7 +2,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. @@ -88,8 +88,10 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, + +
## Usage @@ -98,7 +100,7 @@ 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. @@ -115,17 +117,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 arrays 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,18 +134,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 = 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. @@ -160,24 +156,23 @@ var v = variancepn.ndarray( N, 1, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [variance][variance] for every other value in `x` starting from the second value +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer , the offset parameters support indexing semantics based on starting indices. For example, to calculate the [variance][variance] for every other value in `x` starting from the second value ```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 ```
+ +
## Notes @@ -185,11 +180,14 @@ 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`. - Depending on the environment, the typed versions ([`dvariancepn`][@stdlib/stats/base/dvariancepn], [`svariancepn`][@stdlib/stats/base/svariancepn], etc.) are likely to be significantly more performant. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
+ +
## Examples @@ -197,18 +195,13 @@ 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 uniform = require( '@stdlib/random/array/uniform' ); var Float64Array = require( '@stdlib/array/float64' ); 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 = uniform( 10, -50.0, 50.0, { + 'dtype': 'float64' +}); console.log( x ); var v = variancepn( x.length, 1, x, 1 ); @@ -217,6 +210,7 @@ console.log( v );
+ * * * @@ -230,6 +224,7 @@ console.log( v ); + @@ -247,16 +242,21 @@ console.log( v ); + + + 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..9e8968de7cbf 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/benchmark/benchmark.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. @@ -21,13 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); var pkg = require( './../package.json' ).name; var variancepn = require( './../lib/variancepn.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.0, 10.0, 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..682f726db404 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 @@ -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. @@ -21,13 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var 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.0, 10.0, 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..0f56f035aa3d 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/variancepn/docs/repl.txt @@ -1,12 +1,12 @@ -{{alias}}( N, correction, x, stride ) - Computes the variance of a strided array using a two-pass algorithm. +{{alias}}( N, correction, x, strideX ) + Computes the variance of a strided array (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 arrays are accessed at runtime. - Indexing is relative to the first index. To introduce an offset, use a typed - array view. + Indexing is relative to the first index. To introduce + an offset, use a typed array view. If `N <= 0`, the function returns `NaN`. @@ -30,8 +30,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -43,24 +43,23 @@ // Standard Usage: > var x = [ 1.0, -2.0, 2.0 ]; > {{alias}}( x.length, 1, x, 1 ) - ~4.3333 + 4.333333333333334 - // Using `N` and `stride` parameters: + // Using `N` and `strideX` 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 ) - ~4.3333 + > var strideX = 2; + > {{alias}}( 4, 1, x, strideX ) + NaN // 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 ) - ~4.3333 + > strideX = 2; + > {{alias}}( 4, 1, x1, strideX ) + NaN + -{{alias}}.ndarray( N, correction, x, stride, offset ) +{{alias}}.ndarray( N, correction, x, strideX, offset ) Computes the variance of a strided array using a two-pass algorithm and alternative indexing semantics. @@ -88,8 +87,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. offset: integer Starting index. @@ -108,10 +107,9 @@ // 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 ) - ~4.3333 + > {{alias}}.ndarray( 4, 1, x, 2, 1 ) + NaN + 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..48eed198e106 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 @@ -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,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..035da9739b20 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/examples/index.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. @@ -18,18 +18,14 @@ '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 ); -} +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/accessors.js b/lib/node_modules/@stdlib/stats/base/variancepn/lib/accessors.js new file mode 100644 index 000000000000..702a046db8f0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/accessors.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).ndarray; + + +// MAIN // + +/** +* 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 {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length for 'x' +* @param {NonNegativeInteger} offsetX - starting index for 'x' +* @returns {Object} variance +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* +* var v = variancepn( 4, 1, arraylike2object( toAccessorArray( x ) ), 2, 1 ); +* // returns 6.25 +*/ +function variancepn( N, correction, x, strideX, offsetX ) { + var xbuf; + var get; + var mu; + var ix; + var M2; + var M; + var d; + var n; + var i; + + // Cache references to array data: + xbuf = x.data; + + // Cache references to element accessors: + get = x.accessors[ 0 ]; + + n = N - correction; + if ( N <= 0 || n <= 0.0 ) { + return NaN; + } + if ( N === 1 || strideX === 0 ) { + return 0.0; + } + // Compute an estimate for the mean: + /* eslint-disable-next-line no-underscore-dangle */ + mu = gsumpw( N, xbuf._buffer, strideX, offsetX ) / N; + + // Compute the variance... + ix = offsetX; + M2 = 0.0; + M = 0.0; + for ( i = 0; 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 = variancepn; 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..77ab878855c8 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/index.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. @@ -27,22 +27,20 @@ * 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 main = require( './main.js' ); 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..b19dbe83af19 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/main.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. 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..853ad02da4ce 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,20 +42,18 @@ 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 for 'x' +* @param {NonNegativeInteger} offsetX - starting index for 'x' * @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 ox; var mu; var ix; var M2; @@ -66,21 +66,27 @@ 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; } + + ox = arraylike2object( x ); + if ( ox.accessorProtocol) { + return accessors( N, correction, ox, 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/lib/variancepn.js b/lib/node_modules/@stdlib/stats/base/variancepn/lib/variancepn.js index e6be432823a1..a4d2616f6516 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/lib/variancepn.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/lib/variancepn.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,8 @@ // MODULES // -var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -40,7 +41,7 @@ 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 {integer} strideX - stride length for 'x' * @returns {number} variance * * @example @@ -50,40 +51,8 @@ var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ); * var v = variancepn( N, 1, x, 1 ); * // returns ~4.3333 */ -function variancepn( 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; - } - // Compute an estimate for the mean: - mu = gsumpw( N, x, stride ) / N; - - 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; - M2 += d * d; - M += d; - ix += stride; - } - return (M2/n) - ((M/N)*(M/n)); +function variancepn( N, correction, x, strideX ) { + return ndarray(N, correction, x, strideX, stride2offset(N, strideX)); } diff --git a/lib/node_modules/@stdlib/stats/base/variancepn/test/test.js b/lib/node_modules/@stdlib/stats/base/variancepn/test/test.js index 952358607ce1..06df5112b985 100644 --- a/lib/node_modules/@stdlib/stats/base/variancepn/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/variancepn/test/test.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. 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..35341536fb08 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 @@ -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. @@ -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 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; @@ -120,7 +158,6 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -135,15 +172,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 +214,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(); @@ -178,7 +254,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', }); tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -192,9 +267,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..97ed107019f2 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 @@ -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. @@ -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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var Float64Array = require( '@stdlib/array/float64' ); -var variancepn = require( './../lib/variancepn.js' ); +var variancepn = require( './../lib' ); // 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; @@ -121,7 +159,6 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -136,15 +173,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 +215,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, x, -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -181,7 +257,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -197,9 +272,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();