|
| 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 | +# dnanmeanpn |
| 22 | + |
| 23 | +> Calculate the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction 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@939b3065109682bbaf70403aba5b13b054107b3e/lib/node_modules/@stdlib/stats/strided/dnanmeanpn/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 dnanmeanpn = require( '@stdlib/stats/strided/dnanmeanpn' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### dnanmeanpn( N, x, strideX ) |
| 55 | + |
| 56 | +Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x`, ignoring `NaN` values and using a two-pass error correction algorithm. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 60 | + |
| 61 | +var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); |
| 62 | + |
| 63 | +var v = dnanmeanpn( 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 [`Float64Array`][@stdlib/array/float64]. |
| 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 Float64Array = require( '@stdlib/array/float64' ); |
| 79 | + |
| 80 | +var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] ); |
| 81 | + |
| 82 | +var v = dnanmeanpn( 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 Float64Array = require( '@stdlib/array/float64' ); |
| 92 | + |
| 93 | +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); |
| 94 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 95 | + |
| 96 | +var v = dnanmeanpn( 5, x1, 2 ); |
| 97 | +// returns 1.25 |
| 98 | +``` |
| 99 | + |
| 100 | +#### dnanmeanpn.ndarray( N, x, strideX, offsetX ) |
| 101 | + |
| 102 | +Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm and alternative indexing semantics. |
| 103 | + |
| 104 | +```javascript |
| 105 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 106 | + |
| 107 | +var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); |
| 108 | + |
| 109 | +var v = dnanmeanpn.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 Float64Array = require( '@stdlib/array/float64' ); |
| 123 | + |
| 124 | +var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); |
| 125 | + |
| 126 | +var v = dnanmeanpn.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 randu = require( '@stdlib/random/base/randu' ); |
| 153 | +var round = require( '@stdlib/math/base/special/round' ); |
| 154 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 155 | +var dnanmeanpn = require( '@stdlib/stats/strided/dnanmeanpn' ); |
| 156 | + |
| 157 | +var x; |
| 158 | +var i; |
| 159 | + |
| 160 | +x = new Float64Array( 10 ); |
| 161 | +for ( i = 0; i < x.length; i++ ) { |
| 162 | + if ( randu() < 0.2 ) { |
| 163 | + x[ i ] = NaN; |
| 164 | + } else { |
| 165 | + x[ i ] = round( randu() * 10.0 ); |
| 166 | + } |
| 167 | +} |
| 168 | +console.log( x ); |
| 169 | + |
| 170 | +var v = dnanmeanpn( x.length, x, 1 ); |
| 171 | +console.log( v ); |
| 172 | +``` |
| 173 | + |
| 174 | +</section> |
| 175 | + |
| 176 | +<!-- /.examples --> |
| 177 | + |
| 178 | +<!-- C interface documentation. --> |
| 179 | + |
| 180 | +* * * |
| 181 | + |
| 182 | +<section class="c"> |
| 183 | + |
| 184 | +## C APIs |
| 185 | + |
| 186 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 187 | + |
| 188 | +<section class="intro"> |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.intro --> |
| 193 | + |
| 194 | +<!-- C usage documentation. --> |
| 195 | + |
| 196 | +<section class="usage"> |
| 197 | + |
| 198 | +### Usage |
| 199 | + |
| 200 | +```c |
| 201 | +#include "stdlib/stats/strided/dnanmeanpn.h" |
| 202 | +``` |
| 203 | + |
| 204 | +#### stdlib_strided_dnanmeanpn( N, \*X, strideX ) |
| 205 | + |
| 206 | +Computes arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm. |
| 207 | + |
| 208 | +```c |
| 209 | +const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 }; |
| 210 | + |
| 211 | +double v = stdlib_strided_dnanmeanpn( 6, x, 2 ); |
| 212 | +// returns ~4.6667 |
| 213 | +``` |
| 214 | +
|
| 215 | +The function accepts the following arguments: |
| 216 | +
|
| 217 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 218 | +- **X**: `[in] double*` input array. |
| 219 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 220 | +
|
| 221 | +```c |
| 222 | +double stdlib_strided_dnanmeanpn( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); |
| 223 | +``` |
| 224 | + |
| 225 | +#### stdlib_strided_dnanmeanpn_ndarray( N, \*X, strideX, offsetX ) |
| 226 | + |
| 227 | +Computes the aithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm and alternative indexing semantics. |
| 228 | + |
| 229 | +```c |
| 230 | +const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 }; |
| 231 | + |
| 232 | +double v = stdlib_strided_dnanmeanpn( 6, x, 2, 0 ); |
| 233 | +// returns ~4.6667 |
| 234 | +``` |
| 235 | +
|
| 236 | +The function accepts the following arguments: |
| 237 | +
|
| 238 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 239 | +- **X**: `[in] double*` input array. |
| 240 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 241 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 242 | +
|
| 243 | +```c |
| 244 | +double stdlib_strided_dnanmeanpn_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 245 | +``` |
| 246 | + |
| 247 | +</section> |
| 248 | + |
| 249 | +<!-- /.usage --> |
| 250 | + |
| 251 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 252 | + |
| 253 | +<section class="notes"> |
| 254 | + |
| 255 | +</section> |
| 256 | + |
| 257 | +<!-- /.notes --> |
| 258 | + |
| 259 | +<!-- C API usage examples. --> |
| 260 | + |
| 261 | +<section class="examples"> |
| 262 | + |
| 263 | +### Examples |
| 264 | + |
| 265 | +```c |
| 266 | +#include "stdlib/stats/strided/dnanmeanpn.h" |
| 267 | +#include <stdio.h> |
| 268 | + |
| 269 | +int main( void ) { |
| 270 | + // Create a strided array: |
| 271 | + const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 }; |
| 272 | + |
| 273 | + // Specify the number of elements: |
| 274 | + const int N = 6; |
| 275 | + |
| 276 | + // Specify the stride length: |
| 277 | + const int strideX = 2; |
| 278 | + |
| 279 | + // Compute the arithmetic mean: |
| 280 | + double v = stdlib_strided_dnanmeanpn( N, x, strideX ); |
| 281 | + |
| 282 | + // Print the result: |
| 283 | + printf( "mean: %lf\n", v ); |
| 284 | +} |
| 285 | +``` |
| 286 | +
|
| 287 | +</section> |
| 288 | +
|
| 289 | +<!-- /.examples --> |
| 290 | +
|
| 291 | +</section> |
| 292 | +
|
| 293 | +<!-- /.c --> |
| 294 | +
|
| 295 | +<section class="references"> |
| 296 | +
|
| 297 | +## References |
| 298 | +
|
| 299 | +- Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a]. |
| 300 | +- Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036][@schubert:2018a]. |
| 301 | +
|
| 302 | +</section> |
| 303 | +
|
| 304 | +<!-- /.references --> |
| 305 | +
|
| 306 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 307 | +
|
| 308 | +<section class="related"> |
| 309 | +
|
| 310 | +* * * |
| 311 | +
|
| 312 | +## See Also |
| 313 | +
|
| 314 | +- <span class="package-name">[`@stdlib/stats/base/dmeanpn`][@stdlib/stats/base/dmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using a two-pass error correction algorithm.</span> |
| 315 | +- <span class="package-name">[`@stdlib/stats/strided/dnanmean`][@stdlib/stats/strided/dnanmean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array, ignoring NaN values.</span> |
| 316 | +- <span class="package-name">[`@stdlib/stats/base/nanmeanpn`][@stdlib/stats/base/nanmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array, ignoring NaN values and using a two-pass error correction algorithm.</span> |
| 317 | +- <span class="package-name">[`@stdlib/stats/base/snanmeanpn`][@stdlib/stats/base/snanmeanpn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values and using a two-pass error correction algorithm.</span> |
| 318 | +
|
| 319 | +</section> |
| 320 | +
|
| 321 | +<!-- /.related --> |
| 322 | +
|
| 323 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 324 | +
|
| 325 | +<section class="links"> |
| 326 | +
|
| 327 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 328 | +
|
| 329 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 330 | +
|
| 331 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 332 | +
|
| 333 | +[@neely:1966a]: https://doi.org/10.1145/365719.365958 |
| 334 | +
|
| 335 | +[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 |
| 336 | +
|
| 337 | +<!-- <related-links> --> |
| 338 | +
|
| 339 | +[@stdlib/stats/base/dmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dmeanpn |
| 340 | +
|
| 341 | +[@stdlib/stats/strided/dnanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmean |
| 342 | +
|
| 343 | +[@stdlib/stats/base/nanmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmeanpn |
| 344 | +
|
| 345 | +[@stdlib/stats/base/snanmeanpn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanmeanpn |
| 346 | +
|
| 347 | +<!-- </related-links> --> |
| 348 | +
|
| 349 | +</section> |
| 350 | +
|
| 351 | +<!-- /.links --> |
0 commit comments