From 5724e731b595d6cb559064cf72738d7ec923bea0 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Tue, 24 Jun 2025 11:36:27 +0000 Subject: [PATCH 1/7] feat: add stats/array/stdev --- 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/stdev/README.md | 112 ++++++ .../stats/array/stdev/benchmark/benchmark.js | 96 ++++++ .../@stdlib/stats/array/stdev/docs/repl.txt | 37 ++ .../stats/array/stdev/docs/types/index.d.ts | 48 +++ .../stats/array/stdev/docs/types/test.ts | 66 ++++ .../stats/array/stdev/examples/index.js | 30 ++ .../@stdlib/stats/array/stdev/lib/index.js | 42 +++ .../@stdlib/stats/array/stdev/lib/main.js | 75 ++++ .../@stdlib/stats/array/stdev/package.json | 71 ++++ .../@stdlib/stats/array/stdev/test/test.js | 319 ++++++++++++++++++ 10 files changed, 896 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/README.md create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/package.json create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/test/test.js diff --git a/lib/node_modules/@stdlib/stats/array/stdev/README.md b/lib/node_modules/@stdlib/stats/array/stdev/README.md new file mode 100644 index 000000000000..fb563df98098 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/README.md @@ -0,0 +1,112 @@ + + +# stdev + +> Calculate the [standard deviation][standard-deviation] of an array. + +
+ +
+ + + +
+ +## Usage + +```javascript +var stdev = require( '@stdlib/stats/array/stdev' ); +``` + +#### stdev( correction, x ) + +Computes the [standard deviation][standard-deviation] of an array. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = stdev( 1, x ); +// returns ~2.0817 +``` + +The function has the following parameters: + +- **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 `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 corrected 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). +- **x**: input array. + +
+ + + +
+ +## 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var stdev = require( '@stdlib/stats/array/stdev' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = stdev( 1, x ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js new file mode 100644 index 000000000000..8be114f38ce3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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 stdev = 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 = stdev( 1, x ); + 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/stdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt new file mode 100644 index 000000000000..315e182497ca --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( correction, x ) + Computes the standard deviation of an array. + + If provided an empty array, the function returns `NaN`. + + Parameters + ---------- + correction: number + 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 `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 corrected 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). + + x: Array|TypedArray + Input array. + + Returns + ------- + out: number + The standard deviation. + + Examples + -------- + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}( 1, x ) + ~2.0817 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts new file mode 100644 index 000000000000..60332fe889f3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @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. +* +* @param correction - degrees of freedom adjustment +* @param x - input array +* @returns standard deviation +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = stdev( 1, x ); +* // returns ~2.0817 +*/ +declare function stdev( correction: number, x: InputArray ): number; + + +// EXPORTS // + +export = stdev; diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts new file mode 100644 index 000000000000..8a514567a31f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts @@ -0,0 +1,66 @@ +/* +* @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 stdev = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + stdev( 1, x ); // $ExpectType number + stdev( 1, new AccessorArray( x ) ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + stdev( '10', x ); // $ExpectError + stdev( true, x ); // $ExpectError + stdev( false, x ); // $ExpectError + stdev( null, x ); // $ExpectError + stdev( undefined, x ); // $ExpectError + stdev( [], x ); // $ExpectError + stdev( {}, x ); // $ExpectError + stdev( ( x: number ): number => x, x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a numeric array... +{ + stdev( 1, 10 ); // $ExpectError + stdev( 1, '10' ); // $ExpectError + stdev( 1, true ); // $ExpectError + stdev( 1, false ); // $ExpectError + stdev( 1, null ); // $ExpectError + stdev( 1, undefined ); // $ExpectError + stdev( 1, {} ); // $ExpectError + stdev( 1, ( 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 ); + + stdev(); // $ExpectError + stdev( 1 ); // $ExpectError + stdev( 1, x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/array/stdev/examples/index.js b/lib/node_modules/@stdlib/stats/array/stdev/examples/index.js new file mode 100644 index 000000000000..0b26c6195f09 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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 stdev = require( './../lib' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = stdev( 1, x ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/array/stdev/lib/index.js b/lib/node_modules/@stdlib/stats/array/stdev/lib/index.js new file mode 100644 index 000000000000..c15d345501bd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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. +* +* @module @stdlib/stats/array/stdev +* +* @example +* var stdev = require( '@stdlib/stats/array/stdev' ); +* +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = stdev( 1, x ); +* // returns ~2.0817 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js b/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js new file mode 100644 index 000000000000..e93b7e9d6769 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js @@ -0,0 +1,75 @@ +/** +* @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 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 isNumber = require( '@stdlib/assert/is-number' ); +var strided = require( '@stdlib/stats/base/stdev' ).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. +* +* @param {number} correction - degrees of freedom adjustment +* @param {NumericArray} x - input array +* @throws {TypeError} first argument must be an number +* @throws {TypeError} second argument must be an array-like object +* @throws {TypeError} second argument must have a supported data type +* @returns {number} standard deviation +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = stdev( 1, x ); +* // returns ~2.0817 +*/ +function stdev( correction, x ) { + var dt; + if ( !isNumber( correction ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an number. Value: `%s`.', correction ) ); + } + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. Second 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. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( IDTYPES, '", "' ), dt ) ); + } + return strided( x.length, correction, x, 1, 0 ); +} + + +// EXPORTS // + +module.exports = stdev; diff --git a/lib/node_modules/@stdlib/stats/array/stdev/package.json b/lib/node_modules/@stdlib/stats/array/stdev/package.json new file mode 100644 index 000000000000..a57af736463b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/array/stdev", + "version": "0.0.0", + "description": "Calculate the standard deviation of an array.", + "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/stdev/test/test.js b/lib/node_modules/@stdlib/stats/array/stdev/test/test.js new file mode 100644 index 000000000000..a7e501f8c9fe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/test/test.js @@ -0,0 +1,319 @@ +/** +* @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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var stdev = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof stdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 2', function test( t ) { + t.strictEqual( stdev.length, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a number', function test( t ) { + var values; + var i; + var x; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + 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() { + stdev( value, x ); + }; + } +}); + +tape( 'the function throws an error if provided a second 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() { + stdev( 1, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second 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() { + stdev( 1, value ); + }; + } +}); + +tape( 'the function calculates the population standard deviation of an array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdev( 0, x ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdev( 0, x ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = stdev( 0, x ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = stdev( 0, x ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = stdev( 0, x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population standard deviation 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 = stdev( 0, toAccessorArray( x ) ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdev( 0, toAccessorArray( x ) ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = stdev( 0, toAccessorArray( x ) ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = stdev( 0, toAccessorArray( x ) ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = stdev( 0, toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population standard deviation 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 = stdev( 0, x ); + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation of an array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdev( 1, x ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdev( 1, x ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = stdev( 1, x ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = stdev( 1, x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = stdev( 1, x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation 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 = stdev( 1, toAccessorArray( x ) ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = stdev( 1, toAccessorArray( x ) ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = stdev( 1, toAccessorArray( x ) ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = stdev( 1, toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = stdev( 1, toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample standard deviation 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 = stdev( 1, x ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdev( x.length, x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdev( x.length+1, x ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = stdev( x.length, toAccessorArray( x ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdev( x.length+1, toAccessorArray( x ) ); + 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 = stdev( 1, [] ); + 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 = stdev( 1, toAccessorArray( [] ) ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns a population standard deviation of `0`', function test( t ) { + var v = stdev( 0, [ 1.0 ] ); + t.strictEqual( v, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns a population standard deviation of `0` (accessors)', function test( t ) { + var v = stdev( 0, toAccessorArray( [ 1.0 ] ) ); + t.strictEqual( v, 0.0, 'returns expected value' ); + t.end(); +}); From afb6e810c8b89ba21499b7bda8c2beb2438c09e5 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 26 Jun 2025 17:01:59 +0000 Subject: [PATCH 2/7] refactor: implementation for stdev --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/array/stdev/README.md | 17 +- .../stats/array/stdev/benchmark/benchmark.js | 2 +- .../@stdlib/stats/array/stdev/docs/repl.txt | 29 +-- .../stats/array/stdev/docs/types/index.d.ts | 6 +- .../stats/array/stdev/docs/types/test.ts | 56 +++-- .../stats/array/stdev/examples/index.js | 2 +- .../@stdlib/stats/array/stdev/lib/index.js | 2 +- .../@stdlib/stats/array/stdev/lib/main.js | 30 ++- .../@stdlib/stats/array/stdev/test/test.js | 223 ++++++++++-------- 9 files changed, 212 insertions(+), 155 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/stdev/README.md b/lib/node_modules/@stdlib/stats/array/stdev/README.md index fb563df98098..0206a6615595 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/README.md +++ b/lib/node_modules/@stdlib/stats/array/stdev/README.md @@ -36,21 +36,30 @@ limitations under the License. var stdev = require( '@stdlib/stats/array/stdev' ); ``` -#### stdev( correction, x ) +#### stdev( x\[, correction] ) Computes the [standard deviation][standard-deviation] of an array. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var v = stdev( 1, x ); +var v = stdev( x ); // returns ~2.0817 ``` The function has the following parameters: -- **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 `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 corrected 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). - **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 use adjust the degrees of freedom when computing the [standard deviation][standard-deviation], provide a `correction` argument. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = stdev( x, 0.0 ); +// returns ~1.6997 +``` @@ -83,7 +92,7 @@ var x = discreteUniform( 10, -50, 50, { }); console.log( x ); -var v = stdev( 1, x ); +var v = stdev( x ); console.log( v ); ``` diff --git a/lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js index 8be114f38ce3..07cce86cb17b 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/array/stdev/benchmark/benchmark.js @@ -54,7 +54,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = stdev( 1, x ); + v = stdev( x, 1.0 ); if ( isnan( v ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt index 315e182497ca..d5bda912e88d 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/repl.txt @@ -1,26 +1,27 @@ -{{alias}}( correction, x ) +{{alias}}( x[, correction] ) Computes the standard deviation of an array. If provided an empty array, the function returns `NaN`. Parameters ---------- - correction: number - 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 `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 corrected 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). - 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 @@ -29,7 +30,7 @@ Examples -------- > var x = [ 1.0, -2.0, 2.0 ]; - > {{alias}}( 1, x ) + > {{alias}}( x, 1.0 ) ~2.0817 See Also diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts index 60332fe889f3..469f13cca576 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts @@ -30,17 +30,17 @@ type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Computes the standard deviation of an array. * -* @param correction - degrees of freedom adjustment * @param x - input array +* @param correction - degrees of freedom adjustment * @returns standard deviation * * @example * var x = [ 1.0, -2.0, 2.0 ]; * -* var v = stdev( 1, x ); +* var v = stdev( x, 1.0 ); * // returns ~2.0817 */ -declare function stdev( correction: number, x: InputArray ): number; +declare function stdev( x: InputArray, correction?: number ): number; // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts index 8a514567a31f..1ea6c22c2ca3 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/test.ts @@ -26,34 +26,45 @@ import stdev = require( './index' ); { const x = new Float64Array( 10 ); - stdev( 1, x ); // $ExpectType number - stdev( 1, new AccessorArray( x ) ); // $ExpectType number + stdev( x ); // $ExpectType number + stdev( new AccessorArray( x ) ); // $ExpectType number + + stdev( x, 1.0 ); // $ExpectType number + stdev( new AccessorArray( x ), 1.0 ); // $ExpectType number } -// The compiler throws an error if the function is provided a first argument which is not a number... +// The compiler throws an error if the function is provided a first argument which is not a numeric array... { - const x = new Float64Array( 10 ); + stdev( 10 ); // $ExpectError + stdev( '10' ); // $ExpectError + stdev( true ); // $ExpectError + stdev( false ); // $ExpectError + stdev( null ); // $ExpectError + stdev( undefined ); // $ExpectError + stdev( {} ); // $ExpectError + stdev( ( x: number ): number => x ); // $ExpectError - stdev( '10', x ); // $ExpectError - stdev( true, x ); // $ExpectError - stdev( false, x ); // $ExpectError - stdev( null, x ); // $ExpectError - stdev( undefined, x ); // $ExpectError - stdev( [], x ); // $ExpectError - stdev( {}, x ); // $ExpectError - stdev( ( x: number ): number => x, x ); // $ExpectError + stdev( 10, 1.0 ); // $ExpectError + stdev( '10', 1.0 ); // $ExpectError + stdev( true, 1.0 ); // $ExpectError + stdev( false, 1.0 ); // $ExpectError + stdev( null, 1.0 ); // $ExpectError + stdev( undefined, 1.0 ); // $ExpectError + stdev( {}, 1.0 ); // $ExpectError + stdev( ( x: number ): number => x, 1.0 ); // $ExpectError } -// The compiler throws an error if the function is provided a second argument which is not a numeric array... +// The compiler throws an error if the function is provided a second argument which is not a number... { - stdev( 1, 10 ); // $ExpectError - stdev( 1, '10' ); // $ExpectError - stdev( 1, true ); // $ExpectError - stdev( 1, false ); // $ExpectError - stdev( 1, null ); // $ExpectError - stdev( 1, undefined ); // $ExpectError - stdev( 1, {} ); // $ExpectError - stdev( 1, ( x: number ): number => x ); // $ExpectError + const x = new Float64Array( 10 ); + + stdev( x, '10' ); // $ExpectError + stdev( x, true ); // $ExpectError + stdev( x, false ); // $ExpectError + stdev( x, null ); // $ExpectError + stdev( x, [] ); // $ExpectError + stdev( x, {} ); // $ExpectError + stdev( x, ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... @@ -61,6 +72,5 @@ import stdev = require( './index' ); const x = new Float64Array( 10 ); stdev(); // $ExpectError - stdev( 1 ); // $ExpectError - stdev( 1, x, {} ); // $ExpectError + stdev( x, 1.0, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/stats/array/stdev/examples/index.js b/lib/node_modules/@stdlib/stats/array/stdev/examples/index.js index 0b26c6195f09..9b0063f92404 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/examples/index.js +++ b/lib/node_modules/@stdlib/stats/array/stdev/examples/index.js @@ -26,5 +26,5 @@ var x = discreteUniform( 10, -50, 50, { }); console.log( x ); -var v = stdev( 1, x ); +var v = stdev( x ); console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/array/stdev/lib/index.js b/lib/node_modules/@stdlib/stats/array/stdev/lib/index.js index c15d345501bd..c0474f555f00 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/lib/index.js +++ b/lib/node_modules/@stdlib/stats/array/stdev/lib/index.js @@ -28,7 +28,7 @@ * * var x = [ 1.0, -2.0, 2.0 ]; * -* var v = stdev( 1, x ); +* var v = stdev( x, 1.0 ); * // returns ~2.0817 */ diff --git a/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js b/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js index e93b7e9d6769..f4740d4812d6 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js @@ -21,11 +21,11 @@ // 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 isNumber = require( '@stdlib/assert/is-number' ); var strided = require( '@stdlib/stats/base/stdev' ).ndarray; var format = require( '@stdlib/string/format' ); @@ -41,30 +41,36 @@ var GENERIC_DTYPE = 'generic'; /** * Computes the standard deviation of an array. * -* @param {number} correction - degrees of freedom adjustment * @param {NumericArray} x - input array -* @throws {TypeError} first argument must be an number -* @throws {TypeError} second argument must be an array-like object -* @throws {TypeError} second argument must have a supported data type +* @param {number} correction - 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, 2.0 ]; * -* var v = stdev( 1, x ); +* var v = stdev( x, 1.0 ); * // returns ~2.0817 */ -function stdev( correction, x ) { +function stdev( x ) { + var correction; var dt; - if ( !isNumber( correction ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an number. Value: `%s`.', correction ) ); - } if ( !isCollection( x ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', 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. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( 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 ); } diff --git a/lib/node_modules/@stdlib/stats/array/stdev/test/test.js b/lib/node_modules/@stdlib/stats/array/stdev/test/test.js index a7e501f8c9fe..ced3c57844ab 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/stdev/test/test.js @@ -24,7 +24,6 @@ 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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var BooleanArray = require( '@stdlib/array/bool' ); var Complex128Array = require( '@stdlib/array/complex128' ); var stdev = require( './../lib' ); @@ -38,27 +37,26 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function has an arity of 2', function test( t ) { - t.strictEqual( stdev.length, 2, 'returns expected value' ); +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( stdev.length, 1, 'returns expected value' ); t.end(); }); -tape( 'the function throws an error if provided a first argument which is not a number', function test( t ) { +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; - var x; values = [ '5', + 5, + NaN, true, false, null, void 0, - [], {}, function noop() {} ]; - x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); } @@ -66,12 +64,12 @@ tape( 'the function throws an error if provided a first argument which is not a function badValue( value ) { return function badValue() { - stdev( value, x ); + stdev( value ); }; } }); -tape( 'the function throws an error if provided a second argument which is not an array-like object', function test( t ) { +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; @@ -93,12 +91,32 @@ tape( 'the function throws an error if provided a second argument which is not a function badValue( value ) { return function badValue() { - stdev( 1, value ); + stdev( 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() { + stdev( value ); }; } }); -tape( 'the function throws an error if provided a second argument which has an unsupported data type', function test( t ) { +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; @@ -113,7 +131,33 @@ tape( 'the function throws an error if provided a second argument which has an u function badValue( value ) { return function badValue() { - stdev( 1, value ); + stdev( 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() { + stdev( [ 1, 2, 3 ], value ); }; } }); @@ -123,23 +167,15 @@ tape( 'the function calculates the population standard deviation of an array', f var v; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = stdev( 0, x ); + v = stdev( x, 0.0 ); t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); x = [ -4.0, -4.0 ]; - v = stdev( 0, x ); + v = stdev( x, 0.0 ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ -0.0, 0.0, -0.0 ]; - v = stdev( 0, x ); - t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); - - x = [ NaN ]; - v = stdev( 0, x ); - t.strictEqual( v, 0.0, 'returns expected value' ); - - x = [ NaN, NaN ]; - v = stdev( 0, x ); + x = [ NaN, 4.0 ]; + v = stdev( x, 0.0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); @@ -150,23 +186,15 @@ tape( 'the function calculates the population standard deviation of an array (ac var v; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - v = stdev( 0, toAccessorArray( x ) ); + v = stdev( toAccessorArray( x ), 0.0 ); t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); x = [ -4.0, -4.0 ]; - v = stdev( 0, toAccessorArray( x ) ); - t.strictEqual( v, 0.0, 'returns expected value' ); - - x = [ -0.0, 0.0, -0.0 ]; - v = stdev( 0, toAccessorArray( x ) ); - t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); - - x = [ NaN ]; - v = stdev( 0, toAccessorArray( x ) ); + v = stdev( toAccessorArray( x ), 0.0 ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ NaN, NaN ]; - v = stdev( 0, toAccessorArray( x ) ); + x = [ NaN, 4.0 ]; + v = stdev( toAccessorArray( x ), 0.0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); @@ -185,135 +213,138 @@ tape( 'the function calculates the population standard deviation of an array (ar '4': 0.0, '5': 3.0 }; - v = stdev( 0, x ); + v = stdev( x, 0.0 ); t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); t.end(); }); -tape( 'the function calculates the sample standard deviation of an array', function test( t ) { +tape( 'the function calculates the sample standard deviation 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 = stdev( 1, x ); + v = stdev( x ); t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); x = [ -4.0, -4.0 ]; - v = stdev( 1, x ); + v = stdev( x ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ -0.0, 0.0, -0.0 ]; - v = stdev( 1, x ); - t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); - - x = [ NaN ]; - v = stdev( 1, x ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - x = [ NaN, NaN ]; - v = stdev( 1, x ); + x = [ NaN, 4.0 ]; + v = stdev( x ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); }); -tape( 'the function calculates the sample standard deviation of an array (accessors)', function test( t ) { +tape( 'the function calculates the sample standard deviation 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 = stdev( 1, toAccessorArray( x ) ); + v = stdev( toAccessorArray( x ) ); t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); x = [ -4.0, -4.0 ]; - v = stdev( 1, toAccessorArray( x ) ); + v = stdev( toAccessorArray( x ) ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = [ -0.0, 0.0, -0.0 ]; - v = stdev( 1, toAccessorArray( x ) ); - t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); - - x = [ NaN ]; - v = stdev( 1, toAccessorArray( x ) ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - x = [ NaN, NaN ]; - v = stdev( 1, toAccessorArray( x ) ); + v = stdev( toAccessorArray( x ) ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); }); -tape( 'the function calculates the sample standard deviation of an array (array-like object)', function test( t ) { +tape( 'the function calculates the sample standard deviation of an array', 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 = stdev( 1, x ); + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdev( x, 1.0 ); t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); - t.end(); -}); - -tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { - var x; - var v; - - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - - v = stdev( x.length, x ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + x = [ -4.0, -4.0 ]; + v = stdev( x, 1.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - v = stdev( x.length+1, x ); + x = [ NaN, 4.0 ]; + v = stdev( x, 1.0 ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); }); -tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { +tape( 'the function calculates the sample standard deviation of an array (accessors)', function test( t ) { var x; var v; - x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = stdev( toAccessorArray( x ), 1.0 ); + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); - v = stdev( x.length, toAccessorArray( x ) ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); + x = [ -4.0, -4.0 ]; + v = stdev( toAccessorArray( x ), 1.0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); - v = stdev( x.length+1, toAccessorArray( x ) ); + x = [ NaN, NaN ]; + v = stdev( 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 = stdev( 1, [] ); + var v = stdev( [] ); 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 = stdev( 1, toAccessorArray( [] ) ); + var v = stdev( toAccessorArray( [] ) ); t.strictEqual( isnan( v ), true, 'returns expected value' ); t.end(); }); -tape( 'if provided an array containing a single element, the function returns a population standard deviation of `0`', function test( t ) { - var v = stdev( 0, [ 1.0 ] ); +tape( 'if provided an array containing a single element, the function returns `0` when computing the population standard deviation', function test( t ) { + var v = stdev( [ 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 a population standard deviation of `0` (accessors)', function test( t ) { - var v = stdev( 0, toAccessorArray( [ 1.0 ] ) ); +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 = stdev( 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 = stdev( x, x.length ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdev( 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 = stdev( x, x.length ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = stdev( x, x.length+1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); From 951e761fa0d32b9a6cd88f120e50fa7bb7660b31 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Mon, 7 Jul 2025 18:21:24 +0000 Subject: [PATCH 3/7] docs: add intro in README --- 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: 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/array/stdev/README.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/array/stdev/README.md b/lib/node_modules/@stdlib/stats/array/stdev/README.md index 0206a6615595..f6282cc4f1da 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/README.md +++ b/lib/node_modules/@stdlib/stats/array/stdev/README.md @@ -24,6 +24,68 @@ limitations under the License.
+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. +
From 88d257329dffc58cdbed3fd9e26f48eab776be3d Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Mon, 7 Jul 2025 18:22:32 +0000 Subject: [PATCH 4/7] feat: add svgs --- 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- ...on_corrected_sample_standard_deviation.svg | 73 +++++++++++++++++++ .../docs/img/equation_population_mean.svg | 42 +++++++++++ ...equation_population_standard_deviation.svg | 66 +++++++++++++++++ .../stdev/docs/img/equation_sample_mean.svg | 43 +++++++++++ 4 files changed, 224 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_corrected_sample_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_population_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_population_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_sample_mean.svg diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_corrected_sample_standard_deviation.svg b/lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_corrected_sample_standard_deviation.svg new file mode 100644 index 000000000000..6af85c9d5732 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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/stdev/docs/img/equation_population_mean.svg b/lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_population_mean.svg new file mode 100644 index 000000000000..4bbdf0d2a56f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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/stdev/docs/img/equation_population_standard_deviation.svg b/lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_population_standard_deviation.svg new file mode 100644 index 000000000000..ad431efeff2a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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/stdev/docs/img/equation_sample_mean.svg b/lib/node_modules/@stdlib/stats/array/stdev/docs/img/equation_sample_mean.svg new file mode 100644 index 000000000000..aea7a5f6687a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/stdev/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 From 91e4f0ff771a0db2031b129beb2c79bf9f747702 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 7 Jul 2025 17:56:40 -0700 Subject: [PATCH 5/7] docs: fix grammar Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/stdev/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/stdev/README.md b/lib/node_modules/@stdlib/stats/array/stdev/README.md index f6282cc4f1da..1d620e128cfb 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/README.md +++ b/lib/node_modules/@stdlib/stats/array/stdev/README.md @@ -114,7 +114,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 [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 use adjust the degrees of freedom when computing the [standard deviation][standard-deviation], provide a `correction` argument. +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, 2.0 ]; From 89e90901b30307992c697d72be5e8e09401d3695 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 7 Jul 2025 18:07:23 -0700 Subject: [PATCH 6/7] docs: document that parameter is optional with a default value Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/stdev/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js b/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js index f4740d4812d6..0d39cc87325e 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/stdev/lib/main.js @@ -42,7 +42,7 @@ var GENERIC_DTYPE = 'generic'; * Computes the standard deviation of an array. * * @param {NumericArray} x - input array -* @param {number} correction - degrees of freedom adjustment +* @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 From f180367014525f179cd28a3dbbd86749247078df Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 7 Jul 2025 20:03:33 -0700 Subject: [PATCH 7/7] docs: add missing note Signed-off-by: Athan --- .../@stdlib/stats/array/stdev/docs/types/index.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts index 469f13cca576..50161ba60906 100644 --- a/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/array/stdev/docs/types/index.d.ts @@ -30,6 +30,10 @@ type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Computes the standard deviation of an array. * +* ## 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