From 653ff803b2ad9d14f600830d80f45641402882e6 Mon Sep 17 00:00:00 2001 From: orthodox-64 Date: Mon, 24 Nov 2025 01:31:38 +0530 Subject: [PATCH 1/9] feat: add stats/base/ndarray/dnanmeanpn --- .../stats/base/ndarray/dnanmeanpn/README.md | 140 +++++++++++++ .../ndarray/dnanmeanpn/benchmark/benchmark.js | 110 ++++++++++ .../docs/img/equation_arithmetic_mean.svg | 42 ++++ .../base/ndarray/dnanmeanpn/docs/repl.txt | 32 +++ .../ndarray/dnanmeanpn/docs/types/index.d.ts | 46 +++++ .../ndarray/dnanmeanpn/docs/types/test.ts | 57 ++++++ .../base/ndarray/dnanmeanpn/examples/index.js | 40 ++++ .../base/ndarray/dnanmeanpn/lib/index.js | 45 +++++ .../stats/base/ndarray/dnanmeanpn/lib/main.js | 64 ++++++ .../base/ndarray/dnanmeanpn/package.json | 69 +++++++ .../base/ndarray/dnanmeanpn/test/test.js | 191 ++++++++++++++++++ 11 files changed, 836 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/img/equation_arithmetic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/package.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md new file mode 100644 index 000000000000..324b2e497add --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md @@ -0,0 +1,140 @@ + + +# dnanmeanpn + +> Compute the [arithmetic mean][arithmetic-mean] of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a two-pass error correction algorithm. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +A two-pass error correction algorithm can reduce numerical error when summing floating-point values (see Neely, 1966). + +
+ + + +
+ +## Usage + +```javascript +var dnanmeanpn = require( '@stdlib/stats/base/ndarray/dnanmeanpn' ); +``` + +#### dnanmeanpn( arrays ) + +Computes the [arithmetic mean][arithmetic-mean] of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a two-pass error correction algorithm. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); +var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var v = dnanmeanpn( [ x ] ); +// returns ~0.3333 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `NaN`. +- `NaN` values are ignored (i.e., they do not contribute to the mean nor the count of values). +- Uses a two-pass error correction algorithm which first computes an initial estimate of the mean and then an error term to refine the estimate. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dnanmeanpn = require( '@stdlib/stats/base/ndarray/dnanmeanpn' ); + +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + +var xbuf = filledarrayBy( 10, 'float64', rand ); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = dnanmeanpn( [ x ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/benchmark/benchmark.js new file mode 100644 index 000000000000..b42f9796f9fc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/benchmark/benchmark.js @@ -0,0 +1,110 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var dnanmeanpn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a random number. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var xbuf; + var x; + + xbuf = filledarrayBy( len, 'float64', rand ); + x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dnanmeanpn( [ 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/base/ndarray/dnanmeanpn/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..c31439606fb6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,42 @@ + +mu equals StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/repl.txt new file mode 100644 index 000000000000..da0dcb175800 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( arrays ) + Computes the arithmetic mean of a one-dimensional double-precision floating- + point ndarray, ignoring `NaN` values and using a two-pass error correction + algorithm. + + If provided an empty ndarray, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + Returns + ------- + out: number + Arithmetic mean. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, NaN, 2.0 ] ); + > var dt = 'float64'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ] ) + ~0.3333 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/index.d.ts new file mode 100644 index 000000000000..3369d832792a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @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 { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the arithmetic mean of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a two-pass error correction algorithm. +* +* @param arrays - array-like object containing an input ndarray +* @returns arithmetic mean +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = dnanmeanpn( [ x ] ); +* // returns ~0.3333 +*/ +declare function dnanmeanpn( arrays: [ float64ndarray ] ): number; + + +// EXPORTS // + +export = dnanmeanpn; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/test.ts new file mode 100644 index 000000000000..e5e04dda8b9f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dnanmeanpn = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dnanmeanpn( [ x ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dnanmeanpn( '10' ); // $ExpectError + dnanmeanpn( 10 ); // $ExpectError + dnanmeanpn( true ); // $ExpectError + dnanmeanpn( false ); // $ExpectError + dnanmeanpn( null ); // $ExpectError + dnanmeanpn( undefined ); // $ExpectError + dnanmeanpn( [] ); // $ExpectError + dnanmeanpn( {} ); // $ExpectError + dnanmeanpn( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dnanmeanpn(); // $ExpectError + dnanmeanpn( [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/examples/index.js new file mode 100644 index 000000000000..c722845541ff --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dnanmeanpn = require( './../lib' ); + +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + +var xbuf = filledarrayBy( 10, 'float64', rand ); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = dnanmeanpn( [ x ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/index.js new file mode 100644 index 000000000000..eed81c889d9f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute the arithmetic mean of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a two-pass error correction algorithm. +* +* @module @stdlib/stats/base/ndarray/dnanmeanpn +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var dnanmeanpn = require( '@stdlib/stats/base/ndarray/dnanmeanpn' ); +* +* var xbuf = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = dnanmeanpn( [ x ] ); +* // returns ~0.3333 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js new file mode 100644 index 000000000000..a6b14f0029c0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/stats/strided/dnanmeanpn' ).ndarray; + + +// MAIN // + +/** +* Computes the arithmetic mean of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a two-pass error correction algorithm. +* +* ## Method +* +* - This implementation uses a two-pass approach, as suggested by Neely (1966), and applies an error correction term. +* +* ## References +* +* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." *Communications of the ACM* 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958). +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @returns {number} arithmetic mean +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, NaN, 2.0, 3.0, 4.0, NaN, NaN ] ); +* var x = new ndarray( 'float64', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); +* +* var v = dnanmeanpn( [ x ] ); +* // returns ~1.75 +*/ +function dnanmeanpn( arrays ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = dnanmeanpn; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/package.json new file mode 100644 index 000000000000..dd62a6518be1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/base/ndarray/dnanmeanpn", + "version": "0.0.0", + "description": "Compute the arithmetic mean of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a two-pass error correction algorithm.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "nan", + "ignore nan", + "two-pass", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js new file mode 100644 index 000000000000..7669a4f74136 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js @@ -0,0 +1,191 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var dnanmeanpn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dnanmeanpn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dnanmeanpn.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the arithmetic mean (ignoring NaNs)', function test( t ) { + var x; + var v; + + x = new Float64Array( [ 2.0, 1.0, NaN, 2.0, -2.0, NaN ] ); + + v = dnanmeanpn( [ vector( x, 6, 1, 0 ) ] ); + t.strictEqual( v, 0.75, 'returns expected mean' ); + t.end(); +}); + +tape( 'supports empty input', function test( t ) { + var x; + var v; + + x = new Float64Array( [] ); + v = dnanmeanpn( [ vector( x, 0, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'returns NaN when all values are NaN', function test( t ) { + var x; + var v; + + x = new Float64Array( [ NaN, NaN, NaN ] ); + v = dnanmeanpn( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function computes the arithmetic mean with various array configurations', function test( t ) { + var x; + var v; + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ] ); + v = dnanmeanpn( [ vector( x, 7, 1, 0 ) ] ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = new Float64Array( [ -4.0, NaN ] ); + v = dnanmeanpn( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the first element when length is 1', function test( t ) { + var x; + var v; + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + v = dnanmeanpn( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports positive strides', function test( t ) { + var x; + var v; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0, + NaN // 4 + ]); + + v = dnanmeanpn( [ vector( x, 5, 2, 0 ) ] ); + t.strictEqual( v, 1.25, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var x; + var v; + + x = new Float64Array([ + 1.0, // 4 + 2.0, + 2.0, // 3 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 1 + 2.0, + NaN // 0 + ]); + + v = dnanmeanpn( [ vector( x, 5, -2, 8 ) ] ); + t.strictEqual( v, 1.25, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a stride equal to 0, the function returns the first element', function test( t ) { + var x; + var v; + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + v = dnanmeanpn( [ vector( x, 5, 0, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var x; + var v; + + x = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + NaN, + NaN // 4 + ]); + + v = dnanmeanpn( [ vector( x, 5, 2, 1 ) ] ); + t.strictEqual( v, 1.25, 'returns expected value' ); + + t.end(); +}); From 20d8b02aac4e262ec9881f4cdf70e7076f6b8137 Mon Sep 17 00:00:00 2001 From: orthodox-64 Date: Mon, 24 Nov 2025 01:38:02 +0530 Subject: [PATCH 2/9] fix: lint --- .../@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js index a6b14f0029c0..91b7382ce631 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js @@ -38,7 +38,7 @@ var strided = require( '@stdlib/stats/strided/dnanmeanpn' ).ndarray; * * ## References * -* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." *Communications of the ACM* 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958). +* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958). * * @param {ArrayLikeObject} arrays - array-like object containing an input ndarray * @returns {number} arithmetic mean @@ -51,7 +51,7 @@ var strided = require( '@stdlib/stats/strided/dnanmeanpn' ).ndarray; * var x = new ndarray( 'float64', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); * * var v = dnanmeanpn( [ x ] ); -* // returns ~1.75 +* // returns 1.7142857142857142 */ function dnanmeanpn( arrays ) { var x = arrays[ 0 ]; From fda63f55ad8b8ccad0d4fab48a02f9bed22d8a0a Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 23 Nov 2025 23:57:08 -0800 Subject: [PATCH 3/9] docs: update copy Signed-off-by: Athan --- .../@stdlib/stats/base/ndarray/dnanmeanpn/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md index 324b2e497add..5106364397cb 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md @@ -39,8 +39,6 @@ The [arithmetic mean][arithmetic-mean] is defined as -A two-pass error correction algorithm can reduce numerical error when summing floating-point values (see Neely, 1966). - From e46d7a3c6ca08484ad736e51f79cdab23a2bd557 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 23 Nov 2025 23:57:55 -0800 Subject: [PATCH 4/9] docs: update notes Signed-off-by: Athan --- .../@stdlib/stats/base/ndarray/dnanmeanpn/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md index 5106364397cb..90e5e77c61e3 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md @@ -79,8 +79,6 @@ The function has the following parameters: ## Notes - If provided an empty one-dimensional ndarray, the function returns `NaN`. -- `NaN` values are ignored (i.e., they do not contribute to the mean nor the count of values). -- Uses a two-pass error correction algorithm which first computes an initial estimate of the mean and then an error term to refine the estimate. From df5bcdf11503db24ca4e9335668fad16753338c9 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 23 Nov 2025 23:58:39 -0800 Subject: [PATCH 5/9] docs: add missing references Signed-off-by: Athan --- .../stats/base/ndarray/dnanmeanpn/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md index 90e5e77c61e3..da02a3ddc430 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/README.md @@ -117,6 +117,19 @@ console.log( v ); +* * * + +
+ +## References + +- Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a]. +- Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036][@schubert:2018a]. + +
+ + + From b36740663421707f632b7f5fb7df51128dc4b812 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 24 Nov 2025 00:01:27 -0800 Subject: [PATCH 6/9] docs: fix references Signed-off-by: Athan --- .../@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js index 91b7382ce631..37f315c45fb7 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/lib/main.js @@ -34,11 +34,12 @@ var strided = require( '@stdlib/stats/strided/dnanmeanpn' ).ndarray; * * ## Method * -* - This implementation uses a two-pass approach, as suggested by Neely (1966), and applies an error correction term. +* - This implementation uses a two-pass approach, as suggested by Neely (1966). * * ## References * * - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958). +* - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036). * * @param {ArrayLikeObject} arrays - array-like object containing an input ndarray * @returns {number} arithmetic mean From 5ee71471246d0d6b37459117c7d8319a6b0b0e41 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 24 Nov 2025 00:04:02 -0800 Subject: [PATCH 7/9] test: update descriptions Signed-off-by: Athan --- .../base/ndarray/dnanmeanpn/test/test.js | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js index 7669a4f74136..05c349762f7a 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js @@ -65,10 +65,19 @@ tape( 'the function computes the arithmetic mean (ignoring NaNs)', function test v = dnanmeanpn( [ vector( x, 6, 1, 0 ) ] ); t.strictEqual( v, 0.75, 'returns expected mean' ); + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ] ); + v = dnanmeanpn( [ vector( x, 7, 1, 0 ) ] ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = new Float64Array( [ -4.0, NaN ] ); + v = dnanmeanpn( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, -4.0, 'returns expected value' ); + t.end(); }); -tape( 'supports empty input', function test( t ) { +tape( 'when provided an empty input ndarray, the function returns NaN', function test( t ) { var x; var v; @@ -78,7 +87,7 @@ tape( 'supports empty input', function test( t ) { t.end(); }); -tape( 'returns NaN when all values are NaN', function test( t ) { +tape( 'when provided an input ndarray containing only NaN values, the function returns NaN', function test( t ) { var x; var v; @@ -88,22 +97,7 @@ tape( 'returns NaN when all values are NaN', function test( t ) { t.end(); }); -tape( 'the function computes the arithmetic mean with various array configurations', function test( t ) { - var x; - var v; - - x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, NaN, 0.0, 3.0 ] ); - v = dnanmeanpn( [ vector( x, 7, 1, 0 ) ] ); - t.strictEqual( v, 0.5, 'returns expected value' ); - - x = new Float64Array( [ -4.0, NaN ] ); - v = dnanmeanpn( [ vector( x, 2, 1, 0 ) ] ); - t.strictEqual( v, -4.0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns the first element when length is 1', function test( t ) { +tape( 'when provided an input ndarray containing a single element, the function returns that element', function test( t ) { var x; var v; From e2c223d716426cf7f85257bdeb2805ba647b00ce Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 24 Nov 2025 00:05:20 -0800 Subject: [PATCH 8/9] test: update description Signed-off-by: Athan --- .../@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js index 05c349762f7a..024ff69643d7 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js @@ -150,7 +150,7 @@ tape( 'the function supports negative strides', function test( t ) { t.end(); }); -tape( 'if provided a stride equal to 0, the function returns the first element', function test( t ) { +tape( 'when provided an input ndarray having a stride equal to 0, the function returns the first indexed element', function test( t ) { var x; var v; From d89174707d875d6fd586849dedc8451e2888f7b5 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 24 Nov 2025 00:07:53 -0800 Subject: [PATCH 9/9] style: remove whitespace Signed-off-by: Athan --- .../@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js index 024ff69643d7..b8fbf72e3948 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dnanmeanpn/test/test.js @@ -73,7 +73,6 @@ tape( 'the function computes the arithmetic mean (ignoring NaNs)', function test x = new Float64Array( [ -4.0, NaN ] ); v = dnanmeanpn( [ vector( x, 2, 1, 0 ) ] ); t.strictEqual( v, -4.0, 'returns expected value' ); - t.end(); });