|
| 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 | +# nanmeanwd |
| 22 | + |
| 23 | +> Compute the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using Welford's algorithm, ignoring `NaN` values. |
| 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/nanmean/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 nanmeanwd = require( '@stdlib/stats/nanmeanwd' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### nanmeanwd( x\[, options] ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using Welford's algorithm, ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var array = require( '@stdlib/ndarray/array' ); |
| 60 | + |
| 61 | +var x = array( [ 1.0, NaN, -2.0, 4.0 ] ); |
| 62 | + |
| 63 | +var y = nanmeanwd( x ); |
| 64 | +// returns <ndarray> |
| 65 | + |
| 66 | +var v = y.get(); |
| 67 | +// returns 1.0 |
| 68 | +``` |
| 69 | + |
| 70 | +The function has the following parameters: |
| 71 | + |
| 72 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 73 | +- **options**: function options (_optional_). |
| 74 | + |
| 75 | +The function accepts the following options: |
| 76 | + |
| 77 | +- **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]. |
| 78 | +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 79 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 85 | +var array = require( '@stdlib/ndarray/array' ); |
| 86 | + |
| 87 | +var x = array( [ 1.0, NaN, -2.0, 4.0 ], { |
| 88 | + 'shape': [ 2, 2 ], |
| 89 | + 'order': 'row-major' |
| 90 | +}); |
| 91 | +var v = ndarray2array( x ); |
| 92 | +// returns [ [ 1.0, NaN ], [ -2.0, 4.0 ] ] |
| 93 | + |
| 94 | +var y = nanmeanwd( x, { |
| 95 | + 'dims': [ 0 ] |
| 96 | +}); |
| 97 | +// returns <ndarray> |
| 98 | + |
| 99 | +v = ndarray2array( y ); |
| 100 | +// returns [ -0.5, 4.0 ] |
| 101 | + |
| 102 | +y = nanmeanwd( x, { |
| 103 | + 'dims': [ 1 ] |
| 104 | +}); |
| 105 | +// returns <ndarray> |
| 106 | + |
| 107 | +v = ndarray2array( y ); |
| 108 | +// returns [ 1.0, 1.0 ] |
| 109 | + |
| 110 | +y = nanmeanwd( x, { |
| 111 | + 'dims': [ 0, 1 ] |
| 112 | +}); |
| 113 | +// returns <ndarray> |
| 114 | + |
| 115 | +v = y.get(); |
| 116 | +// returns 1.0 |
| 117 | +``` |
| 118 | + |
| 119 | +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`. |
| 120 | + |
| 121 | +```javascript |
| 122 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 123 | +var array = require( '@stdlib/ndarray/array' ); |
| 124 | + |
| 125 | +var x = array( [ 1.0, NaN, -2.0, 4.0 ], { |
| 126 | + 'shape': [ 2, 2 ], |
| 127 | + 'order': 'row-major' |
| 128 | +}); |
| 129 | + |
| 130 | +var v = ndarray2array( x ); |
| 131 | +// returns [ [ 1.0, NaN ], [ -2.0, 4.0 ] ] |
| 132 | + |
| 133 | +var y = nanmeanwd( x, { |
| 134 | + 'dims': [ 0 ], |
| 135 | + 'keepdims': true |
| 136 | +}); |
| 137 | +// returns <ndarray> |
| 138 | + |
| 139 | +v = ndarray2array( y ); |
| 140 | +// returns [ [ -0.5, 4.0 ] ] |
| 141 | + |
| 142 | +y = nanmeanwd( x, { |
| 143 | + 'dims': [ 1 ], |
| 144 | + 'keepdims': true |
| 145 | +}); |
| 146 | +// returns <ndarray> |
| 147 | + |
| 148 | +v = ndarray2array( y ); |
| 149 | +// returns [ [ 1.0 ], [ 1.0 ] ] |
| 150 | + |
| 151 | +y = nanmeanwd( x, { |
| 152 | + 'dims': [ 0, 1 ], |
| 153 | + 'keepdims': true |
| 154 | +}); |
| 155 | +// returns <ndarray> |
| 156 | + |
| 157 | +v = ndarray2array( y ); |
| 158 | +// returns [ [ 1.0 ] ] |
| 159 | +``` |
| 160 | + |
| 161 | +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. |
| 162 | + |
| 163 | +```javascript |
| 164 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 165 | +var array = require( '@stdlib/ndarray/array' ); |
| 166 | + |
| 167 | +var x = array( [ 1.0, NaN, -2.0, 4.0 ], { |
| 168 | + 'dtype': 'generic' |
| 169 | +}); |
| 170 | + |
| 171 | +var y = nanmeanwd( x, { |
| 172 | + 'dtype': 'float64' |
| 173 | +}); |
| 174 | +// returns <ndarray> |
| 175 | + |
| 176 | +var dt = String( getDType( y ) ); |
| 177 | +// returns 'float64' |
| 178 | +``` |
| 179 | + |
| 180 | +#### nanmeanwd.assign( x, out\[, options] ) |
| 181 | + |
| 182 | +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using Welford's algorithm, ignoring `NaN` values, and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 183 | + |
| 184 | +```javascript |
| 185 | +var array = require( '@stdlib/ndarray/array' ); |
| 186 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 187 | + |
| 188 | +var x = array( [ 1.0, NaN, -2.0, 4.0 ] ); |
| 189 | +var y = zeros( [] ); |
| 190 | + |
| 191 | +var out = nanmeanwd.assign( x, y ); |
| 192 | +// returns <ndarray> |
| 193 | + |
| 194 | +var v = out.get(); |
| 195 | +// returns 1.0 |
| 196 | + |
| 197 | +var bool = ( out === y ); |
| 198 | +// returns true |
| 199 | +``` |
| 200 | + |
| 201 | +The method has the following parameters: |
| 202 | + |
| 203 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. |
| 204 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 205 | +- **options**: function options (_optional_). |
| 206 | + |
| 207 | +The method accepts the following options: |
| 208 | + |
| 209 | +- **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]. |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.usage --> |
| 214 | + |
| 215 | +<section class="notes"> |
| 216 | + |
| 217 | +## Notes |
| 218 | + |
| 219 | +- This function uses Welford's algorithm for computing the mean, which provides improved numerical stability compared to naive summation approaches, particularly beneficial when dealing with values of different magnitudes or accumulating floating-point errors. |
| 220 | +- 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]. |
| 221 | +- 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]. |
| 222 | + |
| 223 | +</section> |
| 224 | + |
| 225 | +<!-- /.notes --> |
| 226 | + |
| 227 | +<section class="examples"> |
| 228 | + |
| 229 | +## Examples |
| 230 | + |
| 231 | +<!-- eslint no-undef: "error" --> |
| 232 | + |
| 233 | +```javascript |
| 234 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 235 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 236 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 237 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 238 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 239 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 240 | +var nanmeanwd = require( '@stdlib/stats/nanmeanwd' ); |
| 241 | + |
| 242 | +function rand() { |
| 243 | + if ( bernoulli( 0.8 ) < 1 ) { |
| 244 | + return NaN; |
| 245 | + } |
| 246 | + return uniform( 0.0, 20.0 ); |
| 247 | +} |
| 248 | + |
| 249 | +// Generate an array of random numbers: |
| 250 | +var xbuf = filledarrayBy( 25, 'generic', rand ); |
| 251 | + |
| 252 | +// Wrap in an ndarray: |
| 253 | +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 254 | +console.log( ndarray2array( x ) ); |
| 255 | + |
| 256 | +// Perform a reduction: |
| 257 | +var y = nanmeanwd( x, { |
| 258 | + 'dims': [ 0 ] |
| 259 | +}); |
| 260 | + |
| 261 | +// Resolve the output array data type: |
| 262 | +var dt = getDType( y ); |
| 263 | +console.log( dt ); |
| 264 | + |
| 265 | +// Print the results: |
| 266 | +console.log( ndarray2array( y ) ); |
| 267 | +``` |
| 268 | + |
| 269 | +</section> |
| 270 | + |
| 271 | +<!-- /.examples --> |
| 272 | + |
| 273 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 274 | + |
| 275 | +<section class="related"> |
| 276 | + |
| 277 | +</section> |
| 278 | + |
| 279 | +<!-- /.related --> |
| 280 | + |
| 281 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 282 | + |
| 283 | +<section class="links"> |
| 284 | + |
| 285 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 286 | + |
| 287 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 288 | + |
| 289 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 290 | + |
| 291 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 292 | + |
| 293 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 294 | + |
| 295 | +</section> |
| 296 | + |
| 297 | +<!-- /.links --> |
0 commit comments