Skip to content

Commit 3b1803f

Browse files
committed
feat(stats/base/ndarray): add meankbn wrapper (improved Kahan–Babuska) with docs, examples, tests, benchmark
1 parent 39c8178 commit 3b1803f

File tree

13 files changed

+652
-0
lines changed

13 files changed

+652
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# @stdlib/stats/base/ndarray/meankbn
2+
3+
Compute the arithmetic mean of a one dimensional ndarray using improved Kahan-Babuska compensation.
4+
5+
## Installation
6+
7+
This package is part of the stdlib monorepo and is not published independently.
8+
9+
## Usage
10+
11+
```javascript
12+
var ndarray = require('@stdlib/ndarray/base/ctor');
13+
var Float64Array = require('@stdlib/array/float64');
14+
var meankbn = require('@stdlib/stats/base/ndarray/meankbn');
15+
16+
var buf = new Float64Array([ 1.0, 3.0, 4.0, 2.0 ]);
17+
var x = new ndarray('float64', buf, [buf.length], [1], 0, 'row-major');
18+
19+
// NOTE: ndarray variants accept an array-like of ndarrays:
20+
var v = meankbn([ x ]);
21+
// returns 2.5
22+
23+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var mean = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var xbuf;
50+
var x;
51+
52+
xbuf = uniform( len, -10.0, 10.0, options );
53+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
54+
55+
return benchmark;
56+
57+
function benchmark( b ) {
58+
var v;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
v = mean( [ x ] );
64+
if ( isnan( v ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( v ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':len='+len, f );
99+
}
100+
}
101+
102+
main();
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( arrays )
3+
Computes the arithmetic mean of a one-dimensional ndarray.
4+
5+
If provided an empty ndarray, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
arrays: ArrayLikeObject<ndarray>
10+
Array-like object containing a one-dimensional input ndarray.
11+
12+
Returns
13+
-------
14+
out: number
15+
Arithmetic mean.
16+
17+
Examples
18+
--------
19+
> var xbuf = [ 1.0, -2.0, 2.0 ];
20+
> var dt = 'generic';
21+
> var sh = [ xbuf.length ];
22+
> var sx = [ 1 ];
23+
> var ox = 0;
24+
> var ord = 'row-major';
25+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
26+
> {{alias}}( [ x ] )
27+
~0.3333
28+
29+
See Also
30+
--------
31+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the arithmetic mean of a one-dimensional ndarray.
27+
*
28+
* @param arrays - array-like object containing an input ndarray
29+
* @returns arithmetic mean
30+
*
31+
* @example
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
*
34+
* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
35+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
36+
*
37+
* var v = mean( [ x ] );
38+
* // returns 2.5
39+
*/
40+
declare function mean<T extends ndarray = ndarray>( arrays: [ T ] ): number;
41+
42+
43+
// EXPORTS //
44+
45+
export = mean;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import mean = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
33+
mean( [ x ] ); // $ExpectType number
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
37+
{
38+
mean( '10' ); // $ExpectError
39+
mean( 10 ); // $ExpectError
40+
mean( true ); // $ExpectError
41+
mean( false ); // $ExpectError
42+
mean( null ); // $ExpectError
43+
mean( undefined ); // $ExpectError
44+
mean( [] ); // $ExpectError
45+
mean( {} ); // $ExpectError
46+
mean( ( x: number ): number => x ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
const x = zeros( [ 10 ], {
52+
'dtype': 'generic'
53+
});
54+
55+
mean(); // $ExpectError
56+
mean( [ x ], {} ); // $ExpectError
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
var ndarray = require('@stdlib/ndarray/base/ctor');
3+
var Float64Array = require('@stdlib/array/float64');
4+
var meankbn = require('@stdlib/stats/base/ndarray/meankbn');
5+
6+
var buf = new Float64Array([1.0, 3.0, 4.0, 2.0]);
7+
var x = new ndarray('float64', buf, [buf.length], [1], 0, 'row-major');
8+
9+
console.log('mean:', meankbn([x]));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Compute the arithmetic mean of a one dimensional ndarray using improved Kahan-Babuska compensation.
5+
*
6+
* @module @stdlib/stats/base/ndarray/meankbn
7+
*
8+
* @example
9+
* var ndarray = require('@stdlib/ndarray/base/ctor');
10+
* var Float64Array = require('@stdlib/array/float64');
11+
* var meankbn = require('@stdlib/stats/base/ndarray/meankbn');
12+
*
13+
* var buf = new Float64Array([ 1.0, 3.0, 4.0, 2.0 ]);
14+
* var x = new ndarray('float64', buf, [buf.length], [1], 0, 'row-major');
15+
*
16+
* var v = meankbn([ x ]);
17+
* // returns 2.5
18+
*/
19+
var main = require('./lib/main.js');
20+
21+
module.exports = main;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Compute the arithmetic mean of a one-dimensional ndarray.
23+
*
24+
* @module @stdlib/stats/base/ndarray/meankbn
25+
*
26+
* @example
27+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
* var mean = require( '@stdlib/stats/base/ndarray/meankbn' );
29+
*
30+
* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
31+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
32+
*
33+
* var v = mean( [ x ] );
34+
* // returns 2.5
35+
*/
36+
37+
// MODULES //
38+
39+
var main = require( './main.js' );
40+
41+
42+
// EXPORTS //
43+
44+
module.exports = main;

0 commit comments

Comments
 (0)