From cc0f7d717f1ed65a29649036fb0ad82a8edda526 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Wed, 25 Jun 2025 12:29:23 +0000 Subject: [PATCH 1/2] feat: add support for accessor arrays and refactor `stats/base/variancewd` --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/variancewd/README.md | 44 ++--- .../base/variancewd/benchmark/benchmark.js | 19 +- .../variancewd/benchmark/benchmark.ndarray.js | 17 +- .../stats/base/variancewd/docs/repl.txt | 34 ++-- .../base/variancewd/docs/types/index.d.ts | 19 +- .../stats/base/variancewd/docs/types/test.ts | 3 + .../stats/base/variancewd/examples/index.js | 14 +- .../lib/{variancewd.js => accessors.js} | 45 +++-- .../stats/base/variancewd/lib/index.js | 14 +- .../@stdlib/stats/base/variancewd/lib/main.js | 61 +++++- .../stats/base/variancewd/lib/ndarray.js | 28 ++- .../base/variancewd/test/test.ndarray.js | 169 ++++++++++++++++- .../base/variancewd/test/test.variancewd.js | 175 ++++++++++++++++-- 13 files changed, 502 insertions(+), 140 deletions(-) rename lib/node_modules/@stdlib/stats/base/variancewd/lib/{variancewd.js => accessors.js} (76%) diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/README.md b/lib/node_modules/@stdlib/stats/base/variancewd/README.md index 4491cdec3950..a4270e9061f9 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/README.md +++ b/lib/node_modules/@stdlib/stats/base/variancewd/README.md @@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var variancewd = require( '@stdlib/stats/base/variancewd' ); ``` -#### variancewd( N, correction, x, stride ) +#### variancewd( N, correction, x, strideX ) -Computes the [variance][variance] of a strided array `x` using Welford's algorithm. +Computes the [variance][variance] of a strided array using Welford's 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 = variancewd( N, 1, x, 2 ); +var v = variancewd( 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 = variancewd( N, 1, x1, 2 ); +var v = variancewd( 4, 1, x1, 2 ); // returns 6.25 ``` -#### variancewd.ndarray( N, correction, x, stride, offset ) +#### variancewd.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a strided array using Welford's algorithm and alternative indexing semantics. @@ -160,15 +154,12 @@ The function has the following additional parameters: - **offset**: 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 = variancewd.ndarray( N, 1, x, 2, 1 ); +var v = variancewd.ndarray( 4, 1, x, 2, 1 ); // returns 6.25 ``` @@ -182,6 +173,7 @@ var v = variancewd.ndarray( N, 1, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). - Depending on the environment, the typed versions ([`dvariancewd`][@stdlib/stats/strided/dvariancewd], [`svariancewd`][@stdlib/stats/strided/svariancewd], etc.) are likely to be significantly more performant. @@ -195,18 +187,12 @@ var v = variancewd.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 variancewd = require( '@stdlib/stats/base/variancewd' ); -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 = variancewd( x.length, 1, x, 1 ); @@ -263,6 +249,8 @@ console.log( v ); [@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [@stdlib/stats/strided/dvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dvariancewd diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/variancewd/benchmark/benchmark.js index f2bce8fa9b4b..653a13db7e47 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/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 variancewd = require( './../lib/variancewd.js' ); +var variancewd = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var variancewd = require( './../lib/variancewd.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/variancewd/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/variancewd/benchmark/benchmark.ndarray.js index 7a1b4cf5d487..bf5fd4fa45d7 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/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 variancewd = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var variancewd = 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/variancewd/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/variancewd/docs/repl.txt index 2752beda92df..6ccd6f6054f8 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/variancewd/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the variance of a strided array using Welford's algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -30,8 +30,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -45,22 +45,19 @@ > {{alias}}( x.length, 1, x, 1 ) ~4.3333 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > var stride = 2; - > {{alias}}( N, 1, x, stride ) + > {{alias}}( 3, 1, x, 2 ) ~4.3333 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, 1, x1, stride ) + > {{alias}}( 3, 1, x1, 2 ) ~4.3333 -{{alias}}.ndarray( N, correction, x, stride, offset ) + +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the variance of a strided array using Welford's algorithm and alternative indexing semantics. @@ -88,10 +85,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -107,9 +104,8 @@ ~4.3333 // Using offset parameter: - > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, 1, x, 2, 1 ) + > x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; + > {{alias}}.ndarray( 3, 1, x, 2, 1 ) ~4.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/variancewd/docs/types/index.d.ts index a445a515c887..3671308bf4c5 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/variancewd/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 `variancewd`. @@ -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 = variancewd( 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 Welford's 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 = variancewd.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/variancewd/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/variancewd/docs/types/test.ts index b634d979e86a..0a4d061c50ec 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/variancewd/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import variancewd = require( './index' ); @@ -26,6 +27,7 @@ import variancewd = require( './index' ); const x = new Float64Array( 10 ); variancewd( x.length, 1, x, 1 ); // $ExpectType number + variancewd( 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 variancewd = require( './index' ); const x = new Float64Array( 10 ); variancewd.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType number + variancewd.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/variancewd/examples/index.js b/lib/node_modules/@stdlib/stats/base/variancewd/examples/index.js index 1e82073b3724..91ef47630e9c 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/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 variancewd = 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 = variancewd( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/lib/variancewd.js b/lib/node_modules/@stdlib/stats/base/variancewd/lib/accessors.js similarity index 76% rename from lib/node_modules/@stdlib/stats/base/variancewd/lib/variancewd.js rename to lib/node_modules/@stdlib/stats/base/variancewd/lib/accessors.js index 9ec5e92e2073..f09fecbe3ce4 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/lib/variancewd.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/lib/accessors.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,20 +62,29 @@ * - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). * - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). * +* @private * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment -* @param {NumericArray} x - input array -* @param {integer} stride - stride length +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} variance * * @example -* var x = [ 1.0, -2.0, 2.0 ]; +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); * -* var v = variancewd( x.length, 1, x, 1 ); -* // returns ~4.3333 +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = variancewd( 4, 1, arraylike2object( x ), 2, 1 ); +* // returns 6.25 */ -function variancewd( N, correction, x, stride ) { +function variancewd( N, correction, x, strideX, offsetX ) { var delta; + var xbuf; + var get; var mu; var M2; var ix; @@ -83,26 +92,22 @@ function variancewd( N, correction, x, stride ) { 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; - 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; - } + ix = offsetX; M2 = 0.0; mu = 0.0; for ( i = 0; i < N; i++ ) { - v = x[ ix ]; + v = get( xbuf, ix ); delta = v - mu; mu += delta / (i+1); M2 += delta * ( v - mu ); - ix += stride; + ix += strideX; } return M2 / n; } diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/lib/index.js b/lib/node_modules/@stdlib/stats/base/variancewd/lib/index.js index 84abb1ec883e..bc70d9596cdd 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/lib/index.js @@ -27,25 +27,29 @@ * var variancewd = require( '@stdlib/stats/base/variancewd' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = variancewd( N, 1, x, 1 ); +* var v = variancewd( x.length, 1, x, 1 ); * // returns ~4.3333 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var variancewd = require( '@stdlib/stats/base/variancewd' ); * * 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 = variancewd.ndarray( N, 1, x, 2, 1 ); +* var v = variancewd.ndarray( 4, 1, x, 2, 1 ); * // returns 6.25 */ // MODULES // +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/lib/main.js b/lib/node_modules/@stdlib/stats/base/variancewd/lib/main.js index 1aab3de2e8ac..ff478a018620 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/lib/main.js @@ -20,14 +20,69 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var variancewd = require( './variancewd.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( variancewd, 'ndarray', ndarray ); +/** +* Computes the variance of a strided array using Welford's algorithm. +* +* ## Method +* +* - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let +* +* ```tex +* \begin{align*} +* S_n &= n \sigma_n^2 \\ +* &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ +* &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 +* \end{align*} +* ``` +* +* Accordingly, +* +* ```tex +* \begin{align*} +* S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\ +* &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ +* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ +* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ +* &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ +* &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ +* &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ +* &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ +* &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) +* \end{align*} +* ``` +* +* where we use the identity +* +* ```tex +* x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) +* ``` +* +* ## References +* +* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). +* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {number} variance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = variancewd( x.length, 1, x, 1 ); +* // returns ~4.3333 +*/ +function variancewd( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/variancewd/lib/ndarray.js index c5c3ed400c37..33ef0bc18b05 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/lib/ndarray.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + // MAIN // /** @@ -65,24 +71,22 @@ * @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 = variancewd( N, 1, x, 2, 1 ); +* var v = variancewd( 4, 1, x, 2, 1 ); * // returns 6.25 */ -function variancewd( N, correction, x, stride, offset ) { +function variancewd( N, correction, x, strideX, offsetX ) { var delta; var mu; var M2; var ix; + var o; var v; var n; var i; @@ -91,10 +95,14 @@ function variancewd( 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; } - ix = offset; + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, correction, o, strideX, offsetX ); + } + ix = offsetX; M2 = 0.0; mu = 0.0; for ( i = 0; i < N; i++ ) { @@ -102,7 +110,7 @@ function variancewd( N, correction, x, stride, offset ) { delta = v - mu; mu += delta / (i+1); M2 += delta * ( v - mu ); - ix += stride; + ix += strideX; } return M2 / n; } diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/variancewd/test/test.ndarray.js index 36cab0111266..7c4723cdc28b 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/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 variancewd = 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 = variancewd( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancewd( x.length, 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancewd( 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 = variancewd( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancewd( x.length, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancewd( 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 = variancewd( 0, 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancewd( -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 = variancewd( 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 = variancewd( x.length, x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancewd( 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 = variancewd( N, 1, x, 2, 0 ); + v = variancewd( 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 = variancewd( 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 = variancewd( N, 1, x, -2, 6 ); + v = variancewd( 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 = variancewd( 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 = variancewd( 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 = variancewd( N, 1, x, 2, 1 ); + v = variancewd( 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 = variancewd( 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/variancewd/test/test.variancewd.js b/lib/node_modules/@stdlib/stats/base/variancewd/test/test.variancewd.js index 2a18e88b0122..7c8765a5717e 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/test/test.variancewd.js +++ b/lib/node_modules/@stdlib/stats/base/variancewd/test/test.variancewd.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 variancewd = require( './../lib/variancewd.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var variancewd = 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 = variancewd( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancewd( x.length, 0, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancewd( 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 = variancewd( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancewd( x.length, 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancewd( 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 = variancewd( 0, 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancewd( -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 = variancewd( 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 = variancewd( x.length, x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancewd( 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 = variancewd( N, 1, x, 2 ); + v = variancewd( 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 = variancewd( 4, 1, toAccessorArray( x ), 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -159,8 +257,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = variancewd( N, 1, x, -2 ); + v = variancewd( 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 = variancewd( 4, 1, toAccessorArray( x ), -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); @@ -178,10 +296,21 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancewd( x.length, 1, toAccessorArray( x ), 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -197,9 +326,33 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = variancewd( N, 1, x1, 2 ); + v = variancewd( 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 = variancewd( 4, 1, toAccessorArray( x1 ), 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); From ff24bcfe5eae81670c0e0e783452b4d8d586d964 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 25 Jun 2025 20:26:01 -0700 Subject: [PATCH 2/2] docs: fix missing comma Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/base/variancewd/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/variancewd/README.md b/lib/node_modules/@stdlib/stats/base/variancewd/README.md index a4270e9061f9..5d449dddd103 100644 --- a/lib/node_modules/@stdlib/stats/base/variancewd/README.md +++ b/lib/node_modules/@stdlib/stats/base/variancewd/README.md @@ -154,7 +154,7 @@ The function has the following additional parameters: - **offset**: 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 element in `x` starting from the second element +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 x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];