Skip to content

Commit 2503633

Browse files
committed
feat: add stats/meanors
1 parent 97deb1c commit 2503633

File tree

13 files changed

+2748
-0
lines changed

13 files changed

+2748
-0
lines changed
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
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+
# meanors
22+
23+
> Compute the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using ordinary recursive summation.
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/strided/meanors/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
This implementation uses ordinary recursive summation, which is a simple approach that accumulates results without specialized numerical techniques. It is useful for general-purpose mean calculations where the input values are well-behaved.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var meanors = require( '@stdlib/stats/meanors' );
54+
```
55+
56+
#### meanors( x\[, options] )
57+
58+
Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using ordinary recursive summation.
59+
60+
```javascript
61+
var array = require( '@stdlib/ndarray/array' );
62+
63+
var x = array( [ 1.0, 2.0, -2.0, 4.0 ] );
64+
65+
var y = meanors( x );
66+
// returns <ndarray>
67+
68+
var v = y.get();
69+
// returns 1.25
70+
```
71+
72+
The function has the following parameters:
73+
74+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
75+
- **options**: function options (_optional_).
76+
77+
The function accepts the following options:
78+
79+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
80+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
81+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
82+
83+
By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option.
84+
85+
```javascript
86+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
87+
var array = require( '@stdlib/ndarray/array' );
88+
89+
var x = array( [ 1.0, 2.0, -2.0, 4.0 ], {
90+
'shape': [ 2, 2 ],
91+
'order': 'row-major'
92+
});
93+
var v = ndarray2array( x );
94+
// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]
95+
96+
var y = meanors( x, {
97+
'dims': [ 0 ]
98+
});
99+
// returns <ndarray>
100+
101+
v = ndarray2array( y );
102+
// returns [ -0.5, 3.0 ]
103+
104+
y = meanors( x, {
105+
'dims': [ 1 ]
106+
});
107+
// returns <ndarray>
108+
109+
v = ndarray2array( y );
110+
// returns [ 1.5, 1.0 ]
111+
112+
y = meanors( x, {
113+
'dims': [ 0, 1 ]
114+
});
115+
// returns <ndarray>
116+
117+
v = y.get();
118+
// returns 1.25
119+
```
120+
121+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`.
122+
123+
```javascript
124+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
125+
var array = require( '@stdlib/ndarray/array' );
126+
127+
var x = array( [ 1.0, 2.0, -2.0, 4.0 ], {
128+
'shape': [ 2, 2 ],
129+
'order': 'row-major'
130+
});
131+
132+
var v = ndarray2array( x );
133+
// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]
134+
135+
var y = meanors( x, {
136+
'dims': [ 0 ],
137+
'keepdims': true
138+
});
139+
// returns <ndarray>
140+
141+
v = ndarray2array( y );
142+
// returns [ [ -0.5, 3.0 ] ]
143+
144+
y = meanors( x, {
145+
'dims': [ 1 ],
146+
'keepdims': true
147+
});
148+
// returns <ndarray>
149+
150+
v = ndarray2array( y );
151+
// returns [ [ 1.5 ], [ 1.0 ] ]
152+
153+
y = meanors( x, {
154+
'dims': [ 0, 1 ],
155+
'keepdims': true
156+
});
157+
// returns <ndarray>
158+
159+
v = ndarray2array( y );
160+
// returns [ [ 1.25 ] ]
161+
```
162+
163+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
164+
165+
```javascript
166+
var getDType = require( '@stdlib/ndarray/dtype' );
167+
var array = require( '@stdlib/ndarray/array' );
168+
169+
var x = array( [ 1.0, 2.0, -2.0, 4.0 ], {
170+
'dtype': 'generic'
171+
});
172+
173+
var y = meanors( x, {
174+
'dtype': 'float64'
175+
});
176+
// returns <ndarray>
177+
178+
var dt = String( getDType( y ) );
179+
// returns 'float64'
180+
```
181+
182+
#### meanors.assign( x, out\[, options] )
183+
184+
Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using ordinary recursive summation and assigns the results to a provided output [ndarray][@stdlib/ndarray/ctor].
185+
186+
```javascript
187+
var array = require( '@stdlib/ndarray/array' );
188+
var zeros = require( '@stdlib/ndarray/zeros' );
189+
190+
var x = array( [ 1.0, 2.0, -2.0, 4.0 ] );
191+
var y = zeros( [] );
192+
193+
var out = meanors.assign( x, y );
194+
// returns <ndarray>
195+
196+
var v = out.get();
197+
// returns 1.25
198+
199+
var bool = ( out === y );
200+
// returns true
201+
```
202+
203+
The function has the following parameters:
204+
205+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
206+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
207+
- **options**: function options (_optional_).
208+
209+
The function accepts the following options:
210+
211+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
212+
213+
</section>
214+
215+
<!-- /.usage -->
216+
217+
<section class="notes">
218+
219+
## Notes
220+
221+
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor].
222+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
223+
224+
</section>
225+
226+
<!-- /.notes -->
227+
228+
<section class="examples">
229+
230+
## Examples
231+
232+
<!-- eslint no-undef: "error" -->
233+
234+
```javascript
235+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
236+
var getDType = require( '@stdlib/ndarray/dtype' );
237+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
238+
var ndarray = require( '@stdlib/ndarray/ctor' );
239+
var meanors = require( '@stdlib/stats/meanors' );
240+
241+
// Generate an array of random numbers:
242+
var xbuf = discreteUniform( 25, 0, 20, {
243+
'dtype': 'generic'
244+
});
245+
246+
// Wrap in an ndarray:
247+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
248+
console.log( ndarray2array( x ) );
249+
250+
// Perform a reduction:
251+
var y = meanors( x, {
252+
'dims': [ 0 ]
253+
});
254+
255+
// Resolve the output array data type:
256+
var dt = getDType( y );
257+
console.log( dt );
258+
259+
// Print the results:
260+
console.log( ndarray2array( y ) );
261+
```
262+
263+
</section>
264+
265+
<!-- /.examples -->
266+
267+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
268+
269+
<section class="related">
270+
271+
</section>
272+
273+
<!-- /.related -->
274+
275+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
276+
277+
<section class="links">
278+
279+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
280+
281+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
282+
283+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
284+
285+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
286+
287+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
288+
289+
</section>
290+
291+
<!-- /.links -->

0 commit comments

Comments
 (0)