Skip to content

Commit 6d25872

Browse files
Harsh MathurHarsh Mathur
authored andcommitted
feat: add stats/base/ndarray/meankbn
1 parent 289e58a commit 6d25872

File tree

16 files changed

+938
-0
lines changed

16 files changed

+938
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# meankbn-ndarray
22+
23+
> Compute the [arithmetic mean][arithmetic-mean] of a one-dimensional ndarray using an improved Kahan–Babuška algorithm.
24+
25+
<section class="intro">
26+
27+
The [arithmetic mean][arithmetic-mean] is defined as
28+
29+
<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
30+
31+
```math
32+
\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@42d8f64d805113ab899c79c7c39d6c6bac7fe25c/lib/node_modules/@stdlib/stats/base/ndarray/mean/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
37+
<br>
38+
</div> -->
39+
<!-- </equation> -->
40+
41+
</section>
42+
43+
<!-- /.intro -->
44+
45+
<section class="usage">
46+
47+
## Usage
48+
49+
```javascript
50+
var meankbn = require( '@stdlib/stats/base/ndarray/meankbn' );
51+
```
52+
53+
#### meanbkn( arrays )
54+
55+
Computes the [arithmetic mean][arithmetic-mean] of a one-dimensional ndarray.
56+
57+
```javascript
58+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
59+
60+
var xbuf = [ 1.0, -2.0, 2.0 ];
61+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
62+
63+
var v = meankbn( [ x ] );
64+
// returns ~0.3333
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **arrays**: array-like object containing a one-dimensional input ndarray.
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
93+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
94+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
95+
var meankbn = require( '@stdlib/stats/base/ndarray/meankbn' );
96+
97+
var xbuf = discreteUniform( 10, -50, 50, {
98+
'dtype': 'generic'
99+
});
100+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
101+
console.log( ndarray2array( x ) );
102+
103+
var v = meankbn( [ x ] );
104+
console.log( v );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
124+
125+
</section>
126+
127+
<!-- /.links -->
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 pkg = require( './../package.json' ).name;
28+
var meanbkn = require( './../lib/main.js' );
29+
30+
// VARIABLES //
31+
32+
var options = {
33+
'dtype': 'generic'
34+
};
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
44+
function createBenchmark( len ) {
45+
var x = uniform( len, -10, 10, options );
46+
return benchmark;
47+
48+
function benchmark( b ) {
49+
var v;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
v = meanbkn( x.length, x, 1 );
55+
if ( isnan( v ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnan( v ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
77+
function main() {
78+
var len;
79+
var min;
80+
var max;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 6; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
len = pow( 10, i );
89+
f = createBenchmark( len );
90+
bench( pkg+':len='+len, f );
91+
}
92+
}
93+
94+
main();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 pkg = require( './../package.json' ).name;
28+
var meanbkn = require( './../lib/ndarray.js' );
29+
30+
// VARIABLES //
31+
32+
var options = {
33+
'dtype': 'generic'
34+
};
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveInteger} len - array length
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( len ) {
46+
var x = uniform( len, -10, 10, options );
47+
return benchmark;
48+
49+
function benchmark( b ) {
50+
var v;
51+
var i;
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = meanbkn( x.length, x, 1, 0 );
56+
if ( isnan( v ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( isnan( v ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
}
67+
}
68+
69+
70+
// MAIN //
71+
72+
/**
73+
* Main execution sequence.
74+
*
75+
* @private
76+
*/
77+
function main() {
78+
var len;
79+
var min;
80+
var max;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 6; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
len = pow( 10, i );
89+
f = createBenchmark( len );
90+
bench( pkg+':ndarray:len='+len, f );
91+
}
92+
}
93+
94+
main();
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{alias}}( arrays )
2+
Computes the arithmetic mean of a one-dimensional ndarray using an
3+
improved Kahan–Babuška algorithm.
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+
--------

0 commit comments

Comments
 (0)