From d8597f74e8bdbc3e3355c323e11d60213fb798af Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Wed, 25 Jun 2025 13:49:15 +0000 Subject: [PATCH 1/5] feat: add `stats/array/variancetk` --- 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: passed - 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/array/variancetk/README.md | 177 +++++++++ .../array/variancetk/benchmark/benchmark.js | 96 +++++ .../docs/img/equation_population_mean.svg | 42 +++ .../docs/img/equation_population_variance.svg | 54 +++ .../docs/img/equation_sample_mean.svg | 43 +++ .../img/equation_unbiased_sample_variance.svg | 61 +++ .../stats/array/variancetk/docs/repl.txt | 38 ++ .../array/variancetk/docs/types/index.d.ts | 52 +++ .../stats/array/variancetk/docs/types/test.ts | 76 ++++ .../stats/array/variancetk/examples/index.js | 30 ++ .../stats/array/variancetk/lib/index.js | 42 +++ .../stats/array/variancetk/lib/main.js | 89 +++++ .../stats/array/variancetk/package.json | 68 ++++ .../stats/array/variancetk/test/test.js | 349 ++++++++++++++++++ 14 files changed, 1217 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/README.md create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_variance.svg create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_sample_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_unbiased_sample_variance.svg create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/package.json create mode 100644 lib/node_modules/@stdlib/stats/array/variancetk/test/test.js diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/README.md b/lib/node_modules/@stdlib/stats/array/variancetk/README.md new file mode 100644 index 000000000000..7b6b97b4d15e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/README.md @@ -0,0 +1,177 @@ + + +# variancetk + +> Calculate the [variance][variance] of an array using a one-pass textbook algorithm. + +
+ +The population [variance][variance] of a finite size population of size `N` is given by + + + +```math +\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2 +``` + + + +where the population mean is given by + + + +```math +\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i +``` + + + +Often in the analysis of data, the true population [variance][variance] is not known _a priori_ and must be estimated from a sample drawn from the population distribution. If one attempts to use the formula for the population [variance][variance], the result is biased and yields a **biased sample variance**. To compute an **unbiased sample variance** for a sample of size `n`, + + + +```math +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 +``` + + + +where the sample mean is given by + + + +```math +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + +The use of the term `n-1` is commonly referred to as Bessel's correction. Note, however, that applying Bessel's correction can increase the mean squared error between the sample variance and population variance. Depending on the characteristics of the population distribution, other correction factors (e.g., `n-1.5`, `n+1`, etc) can yield better estimators. + +
+ + + +
+ +## Usage + +```javascript +var variancetk = require( '@stdlib/stats/array/variancetk' ); +``` + +#### variancetk( x\[, correction] ) + +Computes the variance of an array. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = variancetk( x ); +// returns ~4.3333 +``` + +The function has the following parameters: + +- **x**: input array. +- **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 `N` corresponds to the number of array elements and `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). Default: `1.0`. + +By default, the function computes the sample [variance][variance]. To use adjust the degrees of freedom when computing the [variance][variance], provide a `correction` argument. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = variancetk( x, 0.0 ); +// returns ~2.8889 +``` + +
+ + + +
+ +## Notes + +- If provided an empty array, the function returns `NaN`. +- If provided a `correction` argument which is greater than or equal to the number of elements in a provided input array, the function returns `NaN`. +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var variancetk = require( '@stdlib/stats/array/variancetk' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = variancetk( x ); +console.log( v ); +``` + +
+ + + +* * * + +
+ +## References + +- 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][@ling:1974a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/array/variancetk/benchmark/benchmark.js new file mode 100644 index 000000000000..02e11453d5d9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/benchmark/benchmark.js @@ -0,0 +1,96 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +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 variancetk = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = variancetk( x, 1.0 ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_mean.svg b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_mean.svg new file mode 100644 index 000000000000..4bbdf0d2a56f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_mean.svg @@ -0,0 +1,42 @@ + +mu equals StartFraction 1 Over upper N EndFraction sigma-summation Underscript i equals 0 Overscript upper N minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_variance.svg b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_variance.svg new file mode 100644 index 000000000000..4130ba0750d2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_population_variance.svg @@ -0,0 +1,54 @@ + +sigma squared equals StartFraction 1 Over upper N EndFraction sigma-summation Underscript i equals 0 Overscript upper N minus 1 Endscripts left-parenthesis x Subscript i Baseline minus mu right-parenthesis squared + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_sample_mean.svg b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_sample_mean.svg new file mode 100644 index 000000000000..aea7a5f6687a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_sample_mean.svg @@ -0,0 +1,43 @@ + +x overbar equals StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_unbiased_sample_variance.svg b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_unbiased_sample_variance.svg new file mode 100644 index 000000000000..1ae1283e7fb1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/img/equation_unbiased_sample_variance.svg @@ -0,0 +1,61 @@ + +s squared equals StartFraction 1 Over n minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/variancetk/docs/repl.txt new file mode 100644 index 000000000000..eec08d95aa5b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( x[, correction] ) + Computes the variance of an array using a one-pass textbook algorithm. + + If provided an empty array, the function returns `NaN`. + + Parameters + ---------- + x: Array|TypedArray + Input array. + + correction: number (optional) + 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 according to `N - c` where `N` corresponds to the number + of array elements and `c` corresponds to the provided degrees of freedom + adjustment. When computing the 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, 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). + Default: 1.0. + + Returns + ------- + out: number + Variance. + + Examples + -------- + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}( x, 1.0 ) + ~4.3333 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/variancetk/docs/types/index.d.ts new file mode 100644 index 000000000000..d4399c66edf4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Computes the variance of an array using a one-pass textbook algorithm. +* +* ## Notes +* +* - Setting the correction parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the variance according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the variance of a population, setting the correction parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample variance, setting the correction 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). +* +* @param x - input array +* @param correction - degrees of freedom adjustment (default: 1.0) +* @returns variance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = variancetk( x, 1.0 ); +* // returns ~4.3333 +*/ +declare function variancetk( x: InputArray, correction?: number ): number; + + +// EXPORTS // + +export = variancetk; diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/array/variancetk/docs/types/test.ts new file mode 100644 index 000000000000..01ad8168b2af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/docs/types/test.ts @@ -0,0 +1,76 @@ +/* +* @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. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import variancetk = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + variancetk( x ); // $ExpectType number + variancetk( new AccessorArray( x ) ); // $ExpectType number + + variancetk( x, 1.0 ); // $ExpectType number + variancetk( new AccessorArray( x ), 1.0 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a numeric array... +{ + variancetk( 10 ); // $ExpectError + variancetk( '10' ); // $ExpectError + variancetk( true ); // $ExpectError + variancetk( false ); // $ExpectError + variancetk( null ); // $ExpectError + variancetk( undefined ); // $ExpectError + variancetk( {} ); // $ExpectError + variancetk( ( x: number ): number => x ); // $ExpectError + + variancetk( 10, 1.0 ); // $ExpectError + variancetk( '10', 1.0 ); // $ExpectError + variancetk( true, 1.0 ); // $ExpectError + variancetk( false, 1.0 ); // $ExpectError + variancetk( null, 1.0 ); // $ExpectError + variancetk( undefined, 1.0 ); // $ExpectError + variancetk( {}, 1.0 ); // $ExpectError + variancetk( ( x: number ): number => x, 1.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + variancetk( x, '10' ); // $ExpectError + variancetk( x, true ); // $ExpectError + variancetk( x, false ); // $ExpectError + variancetk( x, null ); // $ExpectError + variancetk( x, [] ); // $ExpectError + variancetk( x, {} ); // $ExpectError + variancetk( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + variancetk(); // $ExpectError + variancetk( x, 1.0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/examples/index.js b/lib/node_modules/@stdlib/stats/array/variancetk/examples/index.js new file mode 100644 index 000000000000..b5613b803be2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var variancetk = require( './../lib' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = variancetk( x ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/lib/index.js b/lib/node_modules/@stdlib/stats/array/variancetk/lib/index.js new file mode 100644 index 000000000000..96f5e8e5f05c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/lib/index.js @@ -0,0 +1,42 @@ +/** +* @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'; + +/** +* Compute the variance of an array using a one-pass textbook algorithm. +* +* @module @stdlib/stats/array/variancetk +* +* @example +* var variance = require( '@stdlib/stats/array/variancetk' ); +* +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = variance( x, 1.0 ); +* // returns ~4.3333 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js b/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js new file mode 100644 index 000000000000..e64b5ff426cf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js @@ -0,0 +1,89 @@ +/** +* @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 isCollection = require( '@stdlib/assert/is-collection' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var dtypes = require( '@stdlib/array/dtypes' ); +var dtype = require( '@stdlib/array/dtype' ); +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var strided = require( '@stdlib/stats/base/variancetk' ).ndarray; +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var IDTYPES = dtypes( 'real_and_generic' ); +var GENERIC_DTYPE = 'generic'; + + +// MAIN // + +/** +* Computes the standard deviation of an array using a one-pass textbook algorithm. +* +* ## Method +* +* - This implementation uses a one-pass algorithm, as proposed by Youngs and Cramer (1971). +* +* ## References +* +* - Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826](https://doi.org/10.1080/00401706.1971.10488826). +* +* @param {NumericArray} x - input array +* @param {number} [correction=1.0] - degrees of freedom adjustment +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} first argument must have a supported data type +* @throws {TypeError} second argument must be a number +* @returns {number} variance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = variancetk( x, 1.0 ); +* // returns ~4.3333 +*/ +function variancetk( x ) { + var correction; + var dt; + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) ); + } + dt = dtype( x ) || GENERIC_DTYPE; + if ( !contains( IDTYPES, dt ) ) { + throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( IDTYPES, '", "' ), dt ) ); + } + if ( arguments.length > 1 ) { + correction = arguments[ 1 ]; + if ( !isNumber( correction ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a number. Value: `%s`.', correction ) ); + } + } else { + correction = 1.0; + } + return strided( x.length, correction, x, 1, 0 ); +} + + +// EXPORTS // + +module.exports = variancetk; diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/package.json b/lib/node_modules/@stdlib/stats/array/variancetk/package.json new file mode 100644 index 000000000000..1725f0533f19 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/stats/array/variancetk", + "version": "0.0.0", + "description": "Calculate the variance of an array using a one-pass textbook algorithm.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "deviation", + "dispersion", + "sample variance", + "unbiased", + "stdev", + "std", + "standard deviation", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js b/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js new file mode 100644 index 000000000000..ca01d827caf4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js @@ -0,0 +1,349 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var variancetk = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof variancetk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( variancetk.length, 1, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an array-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + variancetk( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an array-like object (correction)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + variancetk( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which has an unsupported data type', function test( t ) { + var values; + var i; + + values = [ + new BooleanArray( 4 ), + new Complex128Array( 4 ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + variancetk( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which has an unsupported data type (correction)', function test( t ) { + var values; + var i; + + values = [ + new BooleanArray( 4 ), + new Complex128Array( 4 ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + variancetk( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a number', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + variancetk( [ 1, 2, 3 ], value ); + }; + } +}); + +tape( 'the function calculates the population variance of a strided array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancetk( x, 0.0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancetk( x, 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancetk( x, 0.0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + 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 = variancetk( toAccessorArray( x ), 0.0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancetk( toAccessorArray( x ), 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancetk( toAccessorArray( x ), 0.0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population variance of an array (array-like object)', function test( t ) { + var x; + var v; + + x = { + 'length': 6, + '0': 1.0, + '1': -2.0, + '2': -4.0, + '3': 5.0, + '4': 0.0, + '5': 3.0 + }; + v = variancetk( x, 0.0 ); + t.strictEqual( v, 53.5/x.length, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance of an array (default)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancetk( x ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancetk( x ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancetk( x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance of an array (default, accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancetk( toAccessorArray( x ) ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancetk( toAccessorArray( x ) ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancetk( toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance of an array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancetk( x, 1.0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancetk( x, 1.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancetk( x, 1.0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample variance of an array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = variancetk( toAccessorArray( x ), 1.0 ); + t.strictEqual( v, 53.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = variancetk( toAccessorArray( x ), 1.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = variancetk( toAccessorArray( x ), 1.0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty array, the function returns `NaN`', function test( t ) { + var v = variancetk( [] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an empty array, the function returns `NaN` (accessors)', function test( t ) { + var v = variancetk( toAccessorArray( [] ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns `0` when computing the population variance', function test( t ) { + var v = variancetk( [ 1.0 ], 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns `0` when computing the population variance (accessors)', function test( t ) { + var v = variancetk( toAccessorArray( [ 1.0 ] ), 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a `correction` parameter which is greater than or equal to the input array length, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = variancetk( x, x.length ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancetk( x, x.length+1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter which is greater than or equal to the input array length, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = variancetk( x, x.length ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = variancetk( x, x.length+1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); From 03196f7ebf57586d826da2d9c58eeb10ad0b2398 Mon Sep 17 00:00:00 2001 From: Gururaj Gurram <143020143+gururaj1512@users.noreply.github.com> Date: Wed, 25 Jun 2025 21:30:06 +0530 Subject: [PATCH 2/5] docs: fix description Signed-off-by: Gururaj Gurram <143020143+gururaj1512@users.noreply.github.com> --- .../@stdlib/stats/array/variancetk/lib/main.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js b/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js index e64b5ff426cf..574be10cce37 100644 --- a/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js @@ -41,14 +41,6 @@ var GENERIC_DTYPE = 'generic'; /** * Computes the standard deviation of an array using a one-pass textbook algorithm. * -* ## Method -* -* - This implementation uses a one-pass algorithm, as proposed by Youngs and Cramer (1971). -* -* ## References -* -* - Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826](https://doi.org/10.1080/00401706.1971.10488826). -* * @param {NumericArray} x - input array * @param {number} [correction=1.0] - degrees of freedom adjustment * @throws {TypeError} first argument must be an array-like object From 9db0e189b312e5266b27f5ee273c40f3a0c6447a Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 30 Jun 2025 04:10:35 -0700 Subject: [PATCH 3/5] docs: fix typo Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/variancetk/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/README.md b/lib/node_modules/@stdlib/stats/array/variancetk/README.md index 7b6b97b4d15e..1b5739c6c807 100644 --- a/lib/node_modules/@stdlib/stats/array/variancetk/README.md +++ b/lib/node_modules/@stdlib/stats/array/variancetk/README.md @@ -94,7 +94,7 @@ The function has the following parameters: - **x**: input array. - **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 `N` corresponds to the number of array elements and `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). Default: `1.0`. -By default, the function computes the sample [variance][variance]. To use adjust the degrees of freedom when computing the [variance][variance], provide a `correction` argument. +By default, the function computes the sample [variance][variance]. To adjust the degrees of freedom when computing the [variance][variance], provide a `correction` argument. ```javascript var x = [ 1.0, -2.0, 2.0 ]; From dc303df5091d5badf10afa04d2630cbf609d2503 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 30 Jun 2025 04:10:57 -0700 Subject: [PATCH 4/5] test: fix import path Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/variancetk/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js b/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js index ca01d827caf4..1cdd44d343f9 100644 --- a/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/variancetk/test/test.js @@ -25,7 +25,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var BooleanArray = require( '@stdlib/array/bool' ); var Complex128Array = require( '@stdlib/array/complex128' ); -var variancetk = require( './../lib/main.js' ); +var variancetk = require( './../lib' ); // TESTS // From dcd7521c82de95681e5b431104e7e7b6524ded8c Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 30 Jun 2025 04:17:45 -0700 Subject: [PATCH 5/5] docs: fix description Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js b/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js index 574be10cce37..0ca545604e0f 100644 --- a/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/variancetk/lib/main.js @@ -39,7 +39,7 @@ var GENERIC_DTYPE = 'generic'; // MAIN // /** -* Computes the standard deviation of an array using a one-pass textbook algorithm. +* Computes the variance of an array using a one-pass textbook algorithm. * * @param {NumericArray} x - input array * @param {number} [correction=1.0] - degrees of freedom adjustment