From 17ac621b6919994323c799b423e280d18856618c Mon Sep 17 00:00:00 2001 From: Aryan Kumar Date: Mon, 19 Jan 2026 19:52:28 +0000 Subject: [PATCH 1/4] feat: add stats/base/ndarray/snanvariance --- 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 --- --- .../stats/base/ndarray/snanvariance/README.md | 127 +++++++++++++++ .../snanvariance/benchmark/benchmark.js | 106 ++++++++++++ .../docs/img/equation_arithmetic_mean.svg | 42 +++++ .../base/ndarray/snanvariance/docs/repl.txt | 36 +++++ .../snanvariance/docs/types/index.d.ts | 50 ++++++ .../ndarray/snanvariance/docs/types/test.ts | 60 +++++++ .../ndarray/snanvariance/examples/index.js | 43 +++++ .../base/ndarray/snanvariance/lib/index.js | 45 ++++++ .../base/ndarray/snanvariance/lib/main.js | 97 +++++++++++ .../base/ndarray/snanvariance/package.json | 66 ++++++++ .../base/ndarray/snanvariance/test/test.js | 152 ++++++++++++++++++ 11 files changed, 824 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/img/equation_arithmetic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md new file mode 100644 index 000000000000..536c6a119301 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md @@ -0,0 +1,127 @@ + + +# snanvariance + +> Compute the sample variance of a one-dimensional ndarray, ignoring `NaN` values. + +
+ +The **sample variance** of a set of values is defined as + +```math +\sigma^2 = \frac{1}{n - c} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 +``` + +
+ + + +
+ +## Usage + +```javascript +var snanvariance = require( '@stdlib/stats/base/ndarray/snanvariance' ); +``` + +#### snanvariance( arrays, correction ) + +Computes the sample variance of a one-dimensional ndarray, ignoring NaN values. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float32Array( [ 1.0, 2.0, NaN, 3.0 ] ); +var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var v = snanvariance( [ x ], 1 ); +// returns 1.0 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. +- **correction**: degrees of freedom adjustment. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var snanvariance = require( '@stdlib/stats/base/ndarray/snanvariance' ); + +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + +var xbuf = filledarrayBy( 10, 'float32', rand ); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); + +console.log( ndarray2array( x ) ); + +var v = snanvariance( [ x ], 1 ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js new file mode 100644 index 000000000000..0f6e60403201 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js @@ -0,0 +1,106 @@ +/** +* @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/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var snanvariance = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a random number. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var xbuf; + var x; + + xbuf = filledarrayBy( len, 'float32', rand ); + x = new ndarray( 'float32', xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = snanvariance( [ x ], 1 ); + } + b.toc(); + + // NaN is allowed for variance: + if ( isnanf( v ) ) { + b.pass( 'returned NaN (allowed)' ); + } else { + b.pass( 'returned a number' ); + } + b.end(); + } +} + + +// MAIN // + +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( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..c31439606fb6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,42 @@ + +mu 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/base/ndarray/snanvariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/repl.txt new file mode 100644 index 000000000000..3197d8281e1a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( arrays, correction ) + Computes the sample variance of a one-dimensional ndarray, ignoring `NaN` + values. + + If the number of non-NaN elements minus the correction is less than or equal + to zero, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + correction: number + Degrees of freedom adjustment. + + Returns + ------- + out: number + Sample variance. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, NaN, 3.0 ] ); + > var dt = 'float32'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ], 1 ) + 1 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts new file mode 100644 index 000000000000..32ed626ee002 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @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 { float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the sample variance of a one-dimensional ndarray, ignoring `NaN` values. +* +* @param arrays - array-like object containing an input ndarray +* @param correction - degrees of freedom adjustment +* @returns sample variance +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float32Array( [ 1.0, 2.0, NaN, 3.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = snanvariance( [ x ], 1 ); +* // returns 1.0 +*/ +declare function snanvariance( + arrays: [ float32ndarray ], + correction: number +): number; + + +// EXPORTS // + +export = snanvariance; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts new file mode 100644 index 000000000000..8f8cbac71ba0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import snanvariance = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + + snanvariance( [ x ], 1 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + snanvariance( '10', 1 ); // $ExpectError + snanvariance( 10, 1 ); // $ExpectError + snanvariance( true, 1 ); // $ExpectError + snanvariance( false, 1 ); // $ExpectError + snanvariance( null, 1 ); // $ExpectError + snanvariance( undefined, 1 ); // $ExpectError + snanvariance( [], 1 ); // $ExpectError + snanvariance( {}, 1 ); // $ExpectError + snanvariance( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid correction argument... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + + snanvariance( [ x ] ); // $ExpectError + snanvariance( [ x ], '1' ); // $ExpectError + snanvariance( [ x ], {} ); // $ExpectError + snanvariance( [ x ], null ); // $ExpectError + snanvariance( [ x ], undefined ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js new file mode 100644 index 000000000000..eb89a9983ad9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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 uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var snanvariance = require( './../lib' ); + +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + +var xbuf = filledarrayBy( 10, 'float32', rand ); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); + +console.log( 'Input ndarray:' ); +console.log( ndarray2array( x ) ); + +// Compute sample variance (correction = 1): +var v = snanvariance( [ x ], 1 ); +console.log( 'Sample variance (ignoring NaNs):', v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js new file mode 100644 index 000000000000..518f8fa4703b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js @@ -0,0 +1,45 @@ +/** +* @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 sample variance of a one-dimensional ndarray, ignoring `NaN` values. +* +* @module @stdlib/stats/base/ndarray/snanvariance +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var snanvariance = require( '@stdlib/stats/base/ndarray/snanvariance' ); +* +* var xbuf = new Float32Array( [ 1.0, 2.0, NaN, 3.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = snanvariance( [ x ], 1 ); +* // returns 1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js new file mode 100644 index 000000000000..9191d0003b5e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js @@ -0,0 +1,97 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Computes the sample variance of a one-dimensional ndarray, ignoring `NaN` values. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @param {number} correction - degrees of freedom adjustment +* @returns {number} sample variance +*/ +function snanvariance( arrays, correction ) { + var stride; + var offset; + var sumsq; + var count; + var data; + var mean; + var ix; + var x; + var N; + var i; + var v; + + x = arrays[ 0 ]; + + N = numelDimension( x, 0 ); + data = getData( x ); + stride = getStride( x, 0 ); + offset = getOffset( x ); + + mean = 0.0; + sumsq = 0.0; + count = 0; + + // First pass: compute mean of non-NaN values: + ix = offset; + for ( i = 0; i < N; i++ ) { + v = data[ ix ]; + if ( !isnan( v ) ) { + mean += v; + count += 1; + } + ix += stride; + } + + if ( count - correction <= 0 ) { + return NaN; + } + + mean /= count; + + // Second pass: compute squared deviations: + ix = offset; + for ( i = 0; i < N; i++ ) { + v = data[ ix ]; + if ( !isnan( v ) ) { + v -= mean; + sumsq += v * v; + } + ix += stride; + } + + return sumsq / ( count - correction ); +} + + +// EXPORTS // + +module.exports = snanvariance; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json new file mode 100644 index 000000000000..1ac2e0f64e66 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/stats/base/ndarray/snanmean", + "version": "0.0.0", + "description": "Compute the arithmetic mean of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values.", + "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", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js new file mode 100644 index 000000000000..fc09060c2347 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js @@ -0,0 +1,152 @@ +/** +* @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 isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var snanvariance = require( './../lib' ); + + +// FUNCTIONS // + +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof snanvariance, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 2', function test( t ) { + t.strictEqual( snanvariance.length, 2, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the sample variance of a one-dimensional ndarray', function test( t ) { + var x; + var v; + + // [1, 2, 3] → variance = 1 + x = new Float32Array( [ 1.0, 2.0, 3.0 ] ); + v = snanvariance( [ vector( x, 3, 1, 0 ) ], 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + // [-4, -5] → variance = 0.5 + x = new Float32Array( [ -4.0, -5.0 ] ); + v = snanvariance( [ vector( x, 2, 1, 0 ) ], 1 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function ignores NaN values', function test( t ) { + var x; + var v; + + // [1, NaN, 2, 3] → [1,2,3] + x = new Float32Array( [ 1.0, NaN, 2.0, 3.0 ] ); + v = snanvariance( [ vector( x, 4, 1, 0 ) ], 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns NaN if insufficient non-NaN values are available', function test( t ) { + var x; + var v; + + x = new Float32Array( [ 2.0 ] ); + v = snanvariance( [ vector( x, 1, 1, 0 ) ], 1 ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + x = new Float32Array( [ NaN, NaN ] ); + v = snanvariance( [ vector( x, 2, 1, 0 ) ], 1 ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays with non-unit strides', function test( t ) { + var x; + var v; + + // Values accessed: [1, 2, 3] + x = new Float32Array([ + 1.0, // 0 + 0.0, + 2.0, // 1 + 0.0, + 3.0, // 2 + 0.0 + ]); + + v = snanvariance( [ vector( x, 3, 2, 0 ) ], 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays with negative strides', function test( t ) { + var x; + var v; + + // Values accessed (reverse): [1,2,3] + x = new Float32Array([ + 3.0, // 2 + 0.0, + 2.0, // 1 + 0.0, + 1.0, // 0 + 0.0 + ]); + + v = snanvariance( [ vector( x, 3, -2, 4 ) ], 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays with non-zero offsets', function test( t ) { + var x; + var v; + + // Values accessed: [1,2,3] + x = new Float32Array([ + 0.0, + 1.0, // 0 + 0.0, + 2.0, // 1 + 0.0, + 3.0 // 2 + ]); + + v = snanvariance( [ vector( x, 3, 2, 1 ) ], 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); From 353c6f734a008f1b6cf81fbdc8f11d3b0e311884 Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Tue, 20 Jan 2026 19:26:55 +0000 Subject: [PATCH 2/4] chore: update copyright years --- .../@stdlib/stats/base/ndarray/snanvariance/README.md | 2 +- .../stats/base/ndarray/snanvariance/benchmark/benchmark.js | 2 +- .../stats/base/ndarray/snanvariance/docs/types/index.d.ts | 2 +- .../@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts | 2 +- .../@stdlib/stats/base/ndarray/snanvariance/examples/index.js | 2 +- .../@stdlib/stats/base/ndarray/snanvariance/lib/index.js | 2 +- .../@stdlib/stats/base/ndarray/snanvariance/lib/main.js | 2 +- .../@stdlib/stats/base/ndarray/snanvariance/test/test.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md index 536c6a119301..86f9dc99268a 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2025 The Stdlib Authors. +Copyright (c) 2026 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/ndarray/snanvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js index 0f6e60403201..3f372c3a948b 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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/ndarray/snanvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts index 32ed626ee002..28576608364e 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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/ndarray/snanvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts index 8f8cbac71ba0..a83def5a9e1c 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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/ndarray/snanvariance/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js index eb89a9983ad9..f179e88b8aaf 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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/ndarray/snanvariance/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js index 518f8fa4703b..3fd781a98297 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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/ndarray/snanvariance/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js index 9191d0003b5e..7534b9745c45 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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/ndarray/snanvariance/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js index fc09060c2347..9480dda1b6c6 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 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. From c26a571ca87f30c229e7810125fa00d0f7856c81 Mon Sep 17 00:00:00 2001 From: Aryan Kumar Date: Tue, 20 Jan 2026 19:31:34 +0000 Subject: [PATCH 3/4] fix: correct package metadata for snanvariance --- 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: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed --- --- .../stats/base/ndarray/snanvariance/package.json | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json index 1ac2e0f64e66..05f6d75884a4 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/package.json @@ -1,7 +1,7 @@ { - "name": "@stdlib/stats/base/ndarray/snanmean", + "name": "@stdlib/stats/base/ndarray/snanvariance", "version": "0.0.0", - "description": "Compute the arithmetic mean of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values.", + "description": "Compute the sample variance of a one-dimensional ndarray, ignoring `NaN` values.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -55,11 +55,10 @@ "stats", "mathematics", "math", - "average", - "avg", - "mean", - "arithmetic mean", - "central tendency", + "variance", + "sample variance", + "dispersion", + "spread", "ndarray" ], "__stdlib__": {} From 9182dba2d78c76dd4d2bcfaf4d3b6d6d211131bf Mon Sep 17 00:00:00 2001 From: Aryan Kumar Date: Wed, 21 Jan 2026 18:32:17 +0000 Subject: [PATCH 4/4] docs: update snanvariance documentation --- 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: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - 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: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/ndarray/snanvariance/README.md | 4 ++-- .../base/ndarray/snanvariance/benchmark/benchmark.js | 11 +++++++++++ .../stats/base/ndarray/snanvariance/lib/index.js | 2 +- .../stats/base/ndarray/snanvariance/lib/main.js | 2 +- .../stats/base/ndarray/snanvariance/test/test.js | 10 ++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md index 86f9dc99268a..7e501b40895c 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/README.md @@ -20,7 +20,7 @@ limitations under the License. # snanvariance -> Compute the sample variance of a one-dimensional ndarray, ignoring `NaN` values. +> Compute the sample variance of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values.
@@ -44,7 +44,7 @@ var snanvariance = require( '@stdlib/stats/base/ndarray/snanvariance' ); #### snanvariance( arrays, correction ) -Computes the sample variance of a one-dimensional ndarray, ignoring NaN values. +Computes the sample variance of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values. ```javascript var Float32Array = require( '@stdlib/array/float32' ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js index 3f372c3a948b..3fe3c0aa8cdc 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/benchmark/benchmark.js @@ -63,6 +63,12 @@ function createBenchmark( len ) { return benchmark; + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ function benchmark( b ) { var v; var i; @@ -86,6 +92,11 @@ function createBenchmark( len ) { // MAIN // +/** +* Main execution sequence. +* +* @private +*/ function main() { var len; var min; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js index 3fd781a98297..2331d97db392 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Compute the sample variance of a one-dimensional ndarray, ignoring `NaN` values. +* Compute the sample variance of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values. * * @module @stdlib/stats/base/ndarray/snanvariance * diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js index 7534b9745c45..162163207d46 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/lib/main.js @@ -30,7 +30,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); // MAIN // /** -* Computes the sample variance of a one-dimensional ndarray, ignoring `NaN` values. +* Computes the sample variance of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values. * * @param {ArrayLikeObject} arrays - array-like object containing an input ndarray * @param {number} correction - degrees of freedom adjustment diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js index 9480dda1b6c6..a3a8f5b5f9cc 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/snanvariance/test/test.js @@ -29,6 +29,16 @@ var snanvariance = require( './../lib' ); // FUNCTIONS // +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ function vector( buffer, length, stride, offset ) { return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); }