|
| 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 | +# incrnanmeanabs |
| 22 | + |
| 23 | +> Compute an [arithmetic mean][arithmetic-mean] of absolute values incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [arithmetic mean][arithmetic-mean] of absolute values is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:arithmetic_mean_absolute_values" align="center" raw="\mu = \frac{1}{n} \sum_{i=0}^{n-1} |x_i|" alt="Equation for the arithmetic mean of absolute values."> --> |
| 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_absolute_values"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg" alt="Equation for the arithmetic mean of absolute values."> |
| 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 incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanmeanabs() |
| 55 | + |
| 56 | +Returns an accumulator function which incrementally computes an [arithmetic mean][arithmetic-mean] of absolute values, ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanmeanabs(); |
| 60 | +``` |
| 61 | + |
| 62 | +#### accumulator( \[x] ) |
| 63 | + |
| 64 | +If provided an input value `x`, the accumulator function returns an updated mean. If not provided an input value `x`, the accumulator function returns the current mean. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanmeanabs(); |
| 68 | + |
| 69 | +var mu = accumulator( 2.0 ); |
| 70 | +// returns 2.0 |
| 71 | + |
| 72 | +mu = accumulator( NaN ); |
| 73 | +// returns 2.0 |
| 74 | + |
| 75 | +mu = accumulator( -1.0 ); |
| 76 | +// returns 1.5 |
| 77 | + |
| 78 | +mu = accumulator( -3.0 ); |
| 79 | +// returns 2.0 |
| 80 | + |
| 81 | +mu = accumulator(); |
| 82 | +// returns 2.0 |
| 83 | +``` |
| 84 | + |
| 85 | +</section> |
| 86 | + |
| 87 | +<!-- /.usage --> |
| 88 | + |
| 89 | +<section class="notes"> |
| 90 | + |
| 91 | +## Notes |
| 92 | + |
| 93 | +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.notes --> |
| 98 | + |
| 99 | +<section class="examples"> |
| 100 | + |
| 101 | +## Examples |
| 102 | + |
| 103 | +<!-- eslint no-undef: "error" --> |
| 104 | + |
| 105 | +```javascript |
| 106 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 107 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 108 | +var incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' ); |
| 109 | + |
| 110 | +// Initialize an accumulator: |
| 111 | +var accumulator = incrnanmeanabs(); |
| 112 | + |
| 113 | +// For each simulated datum, update the mean... |
| 114 | +var i; |
| 115 | +for ( i = 0; i < 100; i++ ) { |
| 116 | + accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -100.0, 100.0 ) ); |
| 117 | +} |
| 118 | +console.log( accumulator() ); |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.examples --> |
| 124 | + |
| 125 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 126 | + |
| 127 | +<section class="related"> |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.related --> |
| 132 | + |
| 133 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 134 | + |
| 135 | +<section class="links"> |
| 136 | + |
| 137 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.links --> |
0 commit comments