Skip to content

Commit 28544ef

Browse files
committed
feat: add stats/base/ndarray/sdsnanmeanors
1 parent e964c37 commit 28544ef

File tree

11 files changed

+841
-0
lines changed

11 files changed

+841
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
# sdsnanmeanors
22+
23+
> Compute the [arithmetic mean][arithmetic-mean] of a one-dimensional single-precision floating-point ndarray, ignoring NaN values and using ordinary recursive summation with extended accumulation.
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+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var sdsnanmeanors = require( '@stdlib/stats/base/ndarray/sdsnanmeanors' );
52+
```
53+
54+
#### sdsnanmeanors( arrays )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a one-dimensional single-precision floating-point ndarray, ignoring NaN values and using ordinary recursive summation with extended accumulation.
57+
58+
```javascript
59+
var Float32Array = require( '@stdlib/array/float32' );
60+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
61+
62+
var xbuf = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
63+
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
64+
65+
var v = sdsnanmeanors( [ x ] );
66+
// returns ~0.3333
67+
```
68+
69+
The function has the following parameters:
70+
71+
- **arrays**: array-like object containing a one-dimensional input ndarray.
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<section class="notes">
78+
79+
## Notes
80+
81+
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
82+
- If every indexed element is `NaN`, the function returns `NaN`.
83+
- Accumulated intermediate values are stored as double-precision floating-point numbers.
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<section class="examples">
90+
91+
## Examples
92+
93+
<!-- eslint no-undef: "error" -->
94+
95+
```javascript
96+
var uniform = require( '@stdlib/random/base/uniform' );
97+
var filledarrayBy = require( '@stdlib/array/filled-by' );
98+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
99+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
100+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
101+
var sdsnanmeanors = require( '@stdlib/stats/base/ndarray/sdsnanmeanors' );
102+
103+
function rand() {
104+
if ( bernoulli( 0.8 ) < 1 ) {
105+
return NaN;
106+
}
107+
return uniform( -50.0, 50.0 );
108+
}
109+
110+
var xbuf = filledarrayBy( 10, 'float32', rand );
111+
var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
112+
console.log( ndarray2array( x ) );
113+
114+
var v = sdsnanmeanors( [ x ] );
115+
console.log( v );
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
135+
136+
</section>
137+
138+
<!-- /.links -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var sdsnanmeanors = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var xbuf;
51+
var x;
52+
var i;
53+
54+
xbuf = uniform( len, -10.0, 10.0, options );
55+
for ( i = 0; i < len; i++ ) {
56+
if ( bernoulli( 0.8 ) < 1 ) {
57+
xbuf[ i ] = NaN;
58+
}
59+
}
60+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
61+
62+
return benchmark;
63+
64+
function benchmark( b ) {
65+
var v;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
v = sdsnanmeanors( [ x ] );
71+
if ( isnanf( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnanf( v ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( pkg+':len='+len, f );
106+
}
107+
}
108+
109+
main();
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( arrays )
3+
Computes the arithmetic mean of a one-dimensional single-precision
4+
floating-point ndarray, ignoring NaN values and using ordinary recursive
5+
summation with extended accumulation.
6+
7+
Accumulated intermediate values are stored as double-precision floating-
8+
point numbers.
9+
10+
If provided an empty ndarray, the function returns `NaN`.
11+
12+
If every indexed element is `NaN`, the function returns `NaN`.
13+
14+
Parameters
15+
----------
16+
arrays: ArrayLikeObject<ndarray>
17+
Array-like object containing a one-dimensional input ndarray.
18+
19+
Returns
20+
-------
21+
out: number
22+
Arithmetic mean.
23+
24+
Examples
25+
--------
26+
> var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, NaN, 2.0 ] );
27+
> var dt = 'float32';
28+
> var sh = [ xbuf.length ];
29+
> var sx = [ 1 ];
30+
> var ox = 0;
31+
> var ord = 'row-major';
32+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
33+
> {{alias}}( [ x ] )
34+
~0.3333
35+
36+
See Also
37+
--------
38+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the arithmetic mean of a one-dimensional single-precision floating-point ndarray, ignoring NaN values and using ordinary recursive summation with extended accumulation.
27+
*
28+
* @param arrays - array-like object containing an input ndarray
29+
* @returns arithmetic mean
30+
*
31+
* @example
32+
* var Float32Array = require( '@stdlib/array/float32' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
36+
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var v = sdsnanmeanors( [ x ] );
39+
* // returns ~0.3333
40+
*/
41+
declare function sdsnanmeanors( arrays: [ float32ndarray ] ): number;
42+
43+
44+
// EXPORTS //
45+
46+
export = sdsnanmeanors;

0 commit comments

Comments
 (0)