From a7631e2094178bb83bf5de55334ca6ae5047b9b4 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Mon, 14 Jul 2025 08:49:28 +0000 Subject: [PATCH 1/3] feat: add `stats/array/nanstdev` --- 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/nanstdev/README.md | 190 ++++++++++ .../array/nanstdev/benchmark/benchmark.js | 104 ++++++ ...on_corrected_sample_standard_deviation.svg | 73 ++++ .../docs/img/equation_population_mean.svg | 42 +++ ...equation_population_standard_deviation.svg | 66 ++++ .../docs/img/equation_sample_mean.svg | 43 +++ .../stats/array/nanstdev/docs/repl.txt | 38 ++ .../array/nanstdev/docs/types/index.d.ts | 52 +++ .../stats/array/nanstdev/docs/types/test.ts | 76 ++++ .../stats/array/nanstdev/examples/index.js | 37 ++ .../@stdlib/stats/array/nanstdev/lib/index.js | 42 +++ .../@stdlib/stats/array/nanstdev/lib/main.js | 81 ++++ .../@stdlib/stats/array/nanstdev/package.json | 71 ++++ .../@stdlib/stats/array/nanstdev/test/test.js | 351 ++++++++++++++++++ 14 files changed, 1266 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/README.md create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_population_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_population_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_sample_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/package.json create mode 100644 lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/README.md b/lib/node_modules/@stdlib/stats/array/nanstdev/README.md new file mode 100644 index 000000000000..e355ca91b31f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/README.md @@ -0,0 +1,190 @@ + + +# nanstdev + +> Calculate the [standard deviation][standard-deviation] of an array ignoring `NaN` values. + +
+ +The population [standard deviation][standard-deviation] of a finite size population of size `N` is given by + + + +```math +\sigma = \sqrt{\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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], the result is biased and yields an **uncorrected sample standard deviation**. To compute a **corrected sample standard deviation** for a sample of size `n`, + + + +```math +s = \sqrt{\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 standard deviation and population standard deviation. 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 nanstdev = require( '@stdlib/stats/array/nanstdev' ); +``` + +#### nanstdev( x\[, correction] ) + +Computes the [standard deviation][standard-deviation] of an array ignoring `NaN` values. + +```javascript +var x = [ 1.0, -2.0, NaN, 2.0 ]; + +var v = nanstdev( x ); +// returns ~2.0817 +``` + +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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], 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 [standard deviation][standard-deviation]. To adjust the degrees of freedom when computing the [standard deviation][standard-deviation], provide a `correction` argument. + +```javascript +var x = [ 1.0, -2.0, NaN, 2.0 ]; + +var v = nanstdev( x, 0.0 ); +// returns ~1.6997 +``` + +
+ + + +
+ +## Notes + +- If provided an empty array, the function returns `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`. +- 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 uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var nanstdev = require( '@stdlib/stats/array/nanstdev' ); + +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + +var x = filledarrayBy( 10, 'generic', rand ); +console.log( x ); + +var v = nanstdev( x ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/array/nanstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..fb09131b4a53 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/benchmark/benchmark.js @@ -0,0 +1,104 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var nanstdev = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +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 x = filledarrayBy( len, 'generic', rand ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = nanstdev( 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/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg new file mode 100644 index 000000000000..6af85c9d5732 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg @@ -0,0 +1,73 @@ + +s equals StartRoot 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 EndRoot + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_population_mean.svg b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_population_mean.svg new file mode 100644 index 000000000000..4bbdf0d2a56f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/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/nanstdev/docs/img/equation_population_standard_deviation.svg b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_population_standard_deviation.svg new file mode 100644 index 000000000000..ad431efeff2a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_population_standard_deviation.svg @@ -0,0 +1,66 @@ + +sigma equals StartRoot 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 EndRoot + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_sample_mean.svg b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/img/equation_sample_mean.svg new file mode 100644 index 000000000000..aea7a5f6687a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/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/nanstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt new file mode 100644 index 000000000000..5710467c0972 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( x[, correction] ) + Computes the standard deviation of an array ignoring `NaN` values. + + 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 standard deviation 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 standard deviation 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 standard deviation, + 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 + The standard deviation. + + Examples + -------- + > var x = [ 1.0, -2.0, NaN, 2.0 ]; + > {{alias}}( x, 1.0 ) + ~2.0817 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..a0efdb4bac9a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/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 standard deviation of an array ignoring `NaN` values. +* +* ## Notes +* +* - Setting the correction parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the standard deviation according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 standard deviation, 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 +* @returns standard deviation +* +* @example +* var x = [ 1.0, -2.0, NaN, 2.0 ]; +* +* var v = nanstdev( x, 1.0 ); +* // returns ~2.0817 +*/ +declare function nanstdev( x: InputArray, correction?: number ): number; + + +// EXPORTS // + +export = nanstdev; diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/test.ts new file mode 100644 index 000000000000..7b757174782c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/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 nanstdev = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + nanstdev( x ); // $ExpectType number + nanstdev( new AccessorArray( x ) ); // $ExpectType number + + nanstdev( x, 1.0 ); // $ExpectType number + nanstdev( 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... +{ + nanstdev( 10 ); // $ExpectError + nanstdev( '10' ); // $ExpectError + nanstdev( true ); // $ExpectError + nanstdev( false ); // $ExpectError + nanstdev( null ); // $ExpectError + nanstdev( undefined ); // $ExpectError + nanstdev( {} ); // $ExpectError + nanstdev( ( x: number ): number => x ); // $ExpectError + + nanstdev( 10, 1.0 ); // $ExpectError + nanstdev( '10', 1.0 ); // $ExpectError + nanstdev( true, 1.0 ); // $ExpectError + nanstdev( false, 1.0 ); // $ExpectError + nanstdev( null, 1.0 ); // $ExpectError + nanstdev( undefined, 1.0 ); // $ExpectError + nanstdev( {}, 1.0 ); // $ExpectError + nanstdev( ( 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 ); + + nanstdev( x, '10' ); // $ExpectError + nanstdev( x, true ); // $ExpectError + nanstdev( x, false ); // $ExpectError + nanstdev( x, null ); // $ExpectError + nanstdev( x, [] ); // $ExpectError + nanstdev( x, {} ); // $ExpectError + nanstdev( 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 ); + + nanstdev(); // $ExpectError + nanstdev( x, 1.0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/array/nanstdev/examples/index.js new file mode 100644 index 000000000000..11fc042dc6ba --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 nanstdev = require( './../lib' ); + +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + +var x = filledarrayBy( 10, 'generic', rand ); +console.log( x ); + +var v = nanstdev( x ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/array/nanstdev/lib/index.js new file mode 100644 index 000000000000..b0cadd04dc61 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/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 standard deviation of an array ignoring `NaN` values. +* +* @module @stdlib/stats/array/nanstdev +* +* @example +* var nanstdev = require( '@stdlib/stats/array/nanstdev' ); +* +* var x = [ 1.0, -2.0, NaN, 2.0 ]; +* +* var v = nanstdev( x, 1.0 ); +* // returns ~2.0817 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js new file mode 100644 index 000000000000..612f697820c5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js @@ -0,0 +1,81 @@ +/** +* @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/nanstdev' ).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 ignoring `NaN` values. +* +* @param {NumericArray} x - input array +* @param {number} [correction=1.0] - degrees of freedom adjustment +* @throws {TypeError} first argument must have a supported data type +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} second argument must be an number +* @returns {number} standard deviation +* +* @example +* var x = [ 1.0, -2.0, NaN, 2.0 ]; +* +* var v = nanstdev( x, 1.0 ); +* // returns ~2.0817 +*/ +function nanstdev( 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 = nanstdev; diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/package.json b/lib/node_modules/@stdlib/stats/array/nanstdev/package.json new file mode 100644 index 000000000000..55e36c12fa1f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/array/nanstdev", + "version": "0.0.0", + "description": "Calculate the standard deviation of an array 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", + "standard deviation", + "variance", + "var", + "deviation", + "dispersion", + "spread", + "sample standard deviation", + "unbiased", + "stdev", + "std", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js new file mode 100644 index 000000000000..03c11a423de1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js @@ -0,0 +1,351 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +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 nanstdev = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nanstdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( nanstdev.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() { + nanstdev( 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() { + nanstdev( 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() { + nanstdev( 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() { + nanstdev( 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() { + nanstdev( [ 1, 2, 3 ], value ); + }; + } +}); + +tape( 'the function calculates the population standard deviation of an array (ignoring `NaN` values)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + v = nanstdev( x, 0.0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = nanstdev( x, 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = nanstdev( x, 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population standard deviation of an array (ignoring `NaN` values) (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + v = nanstdev( toAccessorArray( x ), 0.0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = nanstdev( toAccessorArray( x ), 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = nanstdev( toAccessorArray( x ), 0.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population standard deviation of an array (ignoring `NaN` values) (array-like object)', function test( t ) { + var x; + var v; + + x = { + 'length': 7, + '0': 1.0, + '1': -2.0, + '2': -4.0, + '3': 5.0, + '4': NaN, + '5': 0.0, + '6': 3.0 + }; + v = nanstdev( x, 0.0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of an array (ignoring `NaN` values) (default)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + v = nanstdev( x ); + t.strictEqual( v, sqrt( 53.5/(x.length-2) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = nanstdev( x ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = nanstdev( x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of an array (ignoring `NaN` values) (default, accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + v = nanstdev( toAccessorArray( x ) ); + t.strictEqual( v, sqrt( 53.5/(x.length-2) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = nanstdev( toAccessorArray( x ) ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = nanstdev( toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of an array (ignoring `NaN` values)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + v = nanstdev( x, 1.0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-2) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = nanstdev( x, 1.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = nanstdev( x, 1.0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of an array (ignoring `NaN` values) (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ]; + v = nanstdev( toAccessorArray( x ), 1.0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-2) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = nanstdev( toAccessorArray( x ), 1.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = nanstdev( 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 = nanstdev( [] ); + 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 = nanstdev( 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 standard deviation', function test( t ) { + var v = nanstdev( [ 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 standard deviation (accessors)', function test( t ) { + var v = nanstdev( 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 = nanstdev( x, x.length ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanstdev( 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 = nanstdev( x, x.length ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanstdev( x, x.length+1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); From 632314d1109e3570f695706a64cba06a65005f27 Mon Sep 17 00:00:00 2001 From: Gururaj Gurram <143020143+gururaj1512@users.noreply.github.com> Date: Mon, 14 Jul 2025 15:41:43 +0530 Subject: [PATCH 2/3] chore: minor cleanup Signed-off-by: Gururaj Gurram <143020143+gururaj1512@users.noreply.github.com> --- lib/node_modules/@stdlib/stats/array/nanstdev/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/README.md b/lib/node_modules/@stdlib/stats/array/nanstdev/README.md index e355ca91b31f..1bd9ee35a0df 100644 --- a/lib/node_modules/@stdlib/stats/array/nanstdev/README.md +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/README.md @@ -132,7 +132,7 @@ var v = nanstdev( x, 0.0 ); ## Notes - If provided an empty array, the function returns `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`. +- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), 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]). From 4e604478217853dbff34c94fa1d89b0530e9c698 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 15 Jul 2025 22:57:41 -0700 Subject: [PATCH 3/3] chore: clean-up --- 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: na - task: lint_javascript_tests status: passed - 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 --- --- lib/node_modules/@stdlib/stats/array/nanstdev/README.md | 4 ++-- lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt | 4 ++-- .../@stdlib/stats/array/nanstdev/docs/types/index.d.ts | 4 ++-- lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js | 2 +- lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/README.md b/lib/node_modules/@stdlib/stats/array/nanstdev/README.md index 1bd9ee35a0df..3615e0f050ac 100644 --- a/lib/node_modules/@stdlib/stats/array/nanstdev/README.md +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/README.md @@ -112,7 +112,7 @@ var v = nanstdev( x ); 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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], 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`. +- **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 [standard deviation][standard-deviation] according to `N-c` where `N` corresponds to the number of non-`NaN` array elements and `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], 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 [standard deviation][standard-deviation]. To adjust the degrees of freedom when computing the [standard deviation][standard-deviation], provide a `correction` argument. @@ -132,7 +132,7 @@ var v = nanstdev( x, 0.0 ); ## Notes - If provided an empty array, the function returns `NaN`. -- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), the function returns `NaN`. +- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `N` corresponds to the number of non-`NaN` array elements), 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]). diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt index 5710467c0972..b276bb37614a 100644 --- a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/repl.txt @@ -13,14 +13,14 @@ 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 standard deviation according to `N-c` where `N` corresponds to - the number of array elements and `c` corresponds to the provided + the number of non-NaN array elements and `c` corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 standard deviation, 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`. + commonly referred to as Bessel's correction). Default: 1.0. Returns ------- diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts index a0efdb4bac9a..c4fffb6ce645 100644 --- a/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/docs/types/index.d.ts @@ -32,10 +32,10 @@ type InputArray = NumericArray | Collection | AccessorArrayLike; * * ## Notes * -* - Setting the correction parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the standard deviation according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 standard deviation, 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). +* - Setting the correction parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the standard deviation according to `N-c` where `N` corresponds to the number of non-`NaN` array elements and `c` corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 standard deviation, 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 +* @param correction - degrees of freedom adjustment (default: 1.0) * @returns standard deviation * * @example diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js index 612f697820c5..bf1ad9159f49 100644 --- a/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/lib/main.js @@ -45,7 +45,7 @@ var GENERIC_DTYPE = 'generic'; * @param {number} [correction=1.0] - degrees of freedom adjustment * @throws {TypeError} first argument must have a supported data type * @throws {TypeError} first argument must be an array-like object -* @throws {TypeError} second argument must be an number +* @throws {TypeError} second argument must be a number * @returns {number} standard deviation * * @example diff --git a/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js index 03c11a423de1..34c56dbd944e 100644 --- a/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/nanstdev/test/test.js @@ -320,7 +320,7 @@ tape( 'if provided an array containing a single element, the function returns `0 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 ) { +tape( 'if provided a `correction` parameter yielding a correction term less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -335,7 +335,7 @@ tape( 'if provided a `correction` parameter which is greater than or equal to th 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 ) { +tape( 'if provided a `correction` parameter yielding a correction term less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { var x; var v;