|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 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 | +# snanmeanwd |
| 22 | + |
| 23 | +> Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's 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@b11f5e9b032ce5e4ebf0c99656a580d995c532b0/lib/node_modules/@stdlib/stats/strided/snanmeanwd/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 snanmeanwd = require( '@stdlib/stats/strided/snanmeanwd' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### snanmeanwd( N, x, strideX ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 60 | + |
| 61 | +var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] ); |
| 62 | + |
| 63 | +var v = snanmeanwd( x.length, x, 1 ); |
| 64 | +// returns ~0.3333 |
| 65 | +``` |
| 66 | + |
| 67 | +The function has the following parameters: |
| 68 | + |
| 69 | +- **N**: number of indexed elements. |
| 70 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 71 | +- **strideX**: stride length for `x`. |
| 72 | + |
| 73 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`, |
| 74 | + |
| 75 | +<!-- eslint-disable max-len --> |
| 76 | + |
| 77 | +```javascript |
| 78 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 79 | + |
| 80 | +var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] ); |
| 81 | + |
| 82 | +var v = snanmeanwd( 5, x, 2 ); |
| 83 | +// returns 1.25 |
| 84 | +``` |
| 85 | + |
| 86 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 87 | + |
| 88 | +<!-- eslint-disable stdlib/capitalized-comments, max-len --> |
| 89 | + |
| 90 | +```javascript |
| 91 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 92 | + |
| 93 | +var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); |
| 94 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 95 | + |
| 96 | +var v = snanmeanwd( 5, x1, 2 ); |
| 97 | +// returns 1.25 |
| 98 | +``` |
| 99 | + |
| 100 | +#### snanmeanwd.ndarray( N, x, strideX, offsetX ) |
| 101 | + |
| 102 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. |
| 103 | + |
| 104 | +```javascript |
| 105 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 106 | + |
| 107 | +var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] ); |
| 108 | + |
| 109 | +var v = snanmeanwd.ndarray( x.length, x, 1, 0 ); |
| 110 | +// returns ~0.33333 |
| 111 | +``` |
| 112 | + |
| 113 | +The function has the following additional parameters: |
| 114 | + |
| 115 | +- **offsetX**: starting index for `x`. |
| 116 | + |
| 117 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other element in `x` starting from the second element |
| 118 | + |
| 119 | +<!-- eslint-disable max-len --> |
| 120 | + |
| 121 | +```javascript |
| 122 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 123 | + |
| 124 | +var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); |
| 125 | + |
| 126 | +var v = snanmeanwd.ndarray( 5, x, 2, 1 ); |
| 127 | +// returns 1.25 |
| 128 | +``` |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.usage --> |
| 133 | + |
| 134 | +<section class="notes"> |
| 135 | + |
| 136 | +## Notes |
| 137 | + |
| 138 | +- If `N <= 0`, both functions return `NaN`. |
| 139 | +- If every indexed element is `NaN`, both functions return `NaN`. |
| 140 | + |
| 141 | +</section> |
| 142 | + |
| 143 | +<!-- /.notes --> |
| 144 | + |
| 145 | +<section class="examples"> |
| 146 | + |
| 147 | +## Examples |
| 148 | + |
| 149 | +<!-- eslint no-undef: "error" --> |
| 150 | + |
| 151 | +```javascript |
| 152 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 153 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 154 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 155 | +var snanmeanwd = require( '@stdlib/stats/strided/snanmeanwd' ); |
| 156 | + |
| 157 | +function rand() { |
| 158 | + if ( bernoulli( 0.8 ) < 1 ) { |
| 159 | + return NaN; |
| 160 | + } |
| 161 | + return uniform( -50.0, 50.0 ); |
| 162 | +} |
| 163 | + |
| 164 | +var x = filledarrayBy( 10, 'float32', rand ); |
| 165 | +console.log( x ); |
| 166 | + |
| 167 | +var v = snanmeanwd( x.length, x, 1 ); |
| 168 | +console.log( v ); |
| 169 | +``` |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.examples --> |
| 174 | + |
| 175 | +<!-- C interface documentation. --> |
| 176 | + |
| 177 | +* * * |
| 178 | + |
| 179 | +<section class="c"> |
| 180 | + |
| 181 | +## C APIs |
| 182 | + |
| 183 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 184 | + |
| 185 | +<section class="intro"> |
| 186 | + |
| 187 | +</section> |
| 188 | + |
| 189 | +<!-- /.intro --> |
| 190 | + |
| 191 | +<!-- C usage documentation. --> |
| 192 | + |
| 193 | +<section class="usage"> |
| 194 | + |
| 195 | +### Usage |
| 196 | + |
| 197 | +```c |
| 198 | +#include "stdlib/stats/strided/snanmeanwd.h" |
| 199 | +``` |
| 200 | + |
| 201 | +#### stdlib_strided_snanmeanwd( N, \*X, strideX ) |
| 202 | + |
| 203 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm. |
| 204 | + |
| 205 | +```c |
| 206 | +const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f }; |
| 207 | + |
| 208 | +float v = stdlib_strided_snanmeanwd( 4, x, 1 ); |
| 209 | +// returns ~0.33333f |
| 210 | +``` |
| 211 | +
|
| 212 | +The function accepts the following arguments: |
| 213 | +
|
| 214 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 215 | +- **X**: `[in] float*` input array. |
| 216 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 217 | +
|
| 218 | +```c |
| 219 | +float stdlib_strided_snanmeanwd( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ); |
| 220 | +``` |
| 221 | + |
| 222 | +#### stdlib_strided_snanmeanwd_ndarray( N, \*X, strideX, offsetX ) |
| 223 | + |
| 224 | +Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. |
| 225 | + |
| 226 | +```c |
| 227 | +const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f }; |
| 228 | + |
| 229 | +float v = stdlib_strided_snanmeanwd_ndarray( 4, x, 1, 0 ); |
| 230 | +// returns ~0.33333f |
| 231 | +``` |
| 232 | +
|
| 233 | +The function accepts the following arguments: |
| 234 | +
|
| 235 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 236 | +- **X**: `[in] float*` input array. |
| 237 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 238 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 239 | +
|
| 240 | +```c |
| 241 | +float stdlib_strided_snanmeanwd_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 242 | +``` |
| 243 | + |
| 244 | +</section> |
| 245 | + |
| 246 | +<!-- /.usage --> |
| 247 | + |
| 248 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 249 | + |
| 250 | +<section class="notes"> |
| 251 | + |
| 252 | +</section> |
| 253 | + |
| 254 | +<!-- /.notes --> |
| 255 | + |
| 256 | +<!-- C API usage examples. --> |
| 257 | + |
| 258 | +<section class="examples"> |
| 259 | + |
| 260 | +### Examples |
| 261 | + |
| 262 | +```c |
| 263 | +#include "stdlib/stats/strided/snanmeanwd.h" |
| 264 | +#include <stdio.h> |
| 265 | + |
| 266 | +int main( void ) { |
| 267 | + // Create a strided array: |
| 268 | + const float x[] = { 1.0f, 2.0f, 0.0f/0.0f, 3.0f, 0.0f/0.0f, 4.0f, 5.0f, 6.0f, 0.0f/0.0f, 7.0f, 8.0f, 0.0f/0.0f }; |
| 269 | + |
| 270 | + // Specify the number of elements: |
| 271 | + const int N = 6; |
| 272 | + |
| 273 | + // Specify the stride length: |
| 274 | + const int strideX = 2; |
| 275 | + |
| 276 | + // Compute the arithmetic mean: |
| 277 | + float v = stdlib_strided_snanmeanwd( N, x, strideX ); |
| 278 | + |
| 279 | + // Print the result: |
| 280 | + printf( "mean: %f\n", v ); |
| 281 | +} |
| 282 | +``` |
| 283 | +
|
| 284 | +</section> |
| 285 | +
|
| 286 | +<!-- /.examples --> |
| 287 | +
|
| 288 | +</section> |
| 289 | +
|
| 290 | +<!-- /.c --> |
| 291 | +
|
| 292 | +* * * |
| 293 | +
|
| 294 | +<section class="references"> |
| 295 | +
|
| 296 | +## References |
| 297 | +
|
| 298 | +- Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022][@welford:1962a]. |
| 299 | +- van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961][@vanreeken:1968a]. |
| 300 | +
|
| 301 | +</section> |
| 302 | +
|
| 303 | +<!-- /.references --> |
| 304 | +
|
| 305 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 306 | +
|
| 307 | +<section class="related"> |
| 308 | +
|
| 309 | +* * * |
| 310 | +
|
| 311 | +## See Also |
| 312 | +
|
| 313 | +- <span class="package-name">[`@stdlib/stats/strided/dnanmeanwd`][@stdlib/stats/strided/dnanmeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array, using Welford's algorithm and ignoring NaN values.</span> |
| 314 | +- <span class="package-name">[`@stdlib/stats/base/nanmeanwd`][@stdlib/stats/base/nanmeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array, ignoring NaN values and using Welford's algorithm.</span> |
| 315 | +- <span class="package-name">[`@stdlib/stats/strided/smeanwd`][@stdlib/stats/strided/smeanwd]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using Welford's algorithm.</span> |
| 316 | +- <span class="package-name">[`@stdlib/stats/base/snanmean`][@stdlib/stats/base/snanmean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values.</span> |
| 317 | +
|
| 318 | +</section> |
| 319 | +
|
| 320 | +<!-- /.related --> |
| 321 | +
|
| 322 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 323 | +
|
| 324 | +<section class="links"> |
| 325 | +
|
| 326 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 327 | +
|
| 328 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 329 | +
|
| 330 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 331 | +
|
| 332 | +[@welford:1962a]: https://doi.org/10.1080/00401706.1962.10490022 |
| 333 | +
|
| 334 | +[@vanreeken:1968a]: https://doi.org/10.1145/362929.362961 |
| 335 | +
|
| 336 | +<!-- <related-links> --> |
| 337 | +
|
| 338 | +[@stdlib/stats/strided/dnanmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmeanwd |
| 339 | +
|
| 340 | +[@stdlib/stats/base/nanmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmeanwd |
| 341 | +
|
| 342 | +[@stdlib/stats/strided/smeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeanwd |
| 343 | +
|
| 344 | +[@stdlib/stats/base/snanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanmean |
| 345 | +
|
| 346 | +<!-- </related-links> --> |
| 347 | +
|
| 348 | +</section> |
| 349 | +
|
| 350 | +<!-- /.links --> |
0 commit comments