|
| 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 | +# incrnanmsumabs |
| 22 | + |
| 23 | +> Compute a moving sum of absolute values incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For a window of size `W`, the moving sum of absolute values is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:moving_sum_absolute_values" align="center" raw="s = \sum_{i=0}^{W-1} |x_i|" alt="Equation for the moving sum of absolute values."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +s = \sum_{i=0}^{W-1} |x_i| |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="s = \sum_{i=0}^{W-1} |x_i|" data-equation="eq:moving_sum_absolute_values"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmsumabs/docs/img/equation_moving_sum_absolute_values.svg" alt="Equation for the moving sum 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 incrnanmsumabs = require( '@stdlib/stats/incr/nanmsumabs' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanmsumabs( window ) |
| 55 | + |
| 56 | +Returns an accumulator `function` which incrementally computes a moving sum of absolute values. The `window` parameter defines the number of values over which to compute the moving sum, ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanmsumabs( 3 ); |
| 60 | +``` |
| 61 | + |
| 62 | +#### accumulator( \[x] ) |
| 63 | + |
| 64 | +If provided an input value `x`, the accumulator function returns an updated sum. If not provided an input value `x`, the accumulator function returns the current sum. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var accumulator = incrnanmsumabs( 3 ); |
| 68 | + |
| 69 | +var sum = accumulator(); |
| 70 | +// returns null |
| 71 | + |
| 72 | +// Fill the window... |
| 73 | +sum = accumulator( 2.0 ); // [2.0] |
| 74 | +// returns 2.0 |
| 75 | + |
| 76 | +sum = accumulator( NaN ); // [2.0] |
| 77 | +// returns 2.0 |
| 78 | + |
| 79 | +sum = accumulator( -1.0 ); // [2.0, -1.0] |
| 80 | +// returns 3.0 |
| 81 | + |
| 82 | +sum = accumulator( 3.0 ); // [2.0, -1.0, 3.0] |
| 83 | +// returns 6.0 |
| 84 | + |
| 85 | +// Window begins sliding... |
| 86 | +sum = accumulator( -7.0 ); // [-1.0, 3.0, -7.0] |
| 87 | +// returns 11.0 |
| 88 | + |
| 89 | +sum = accumulator( -5.0 ); // [3.0, -7.0, -5.0] |
| 90 | +// returns 15.0 |
| 91 | + |
| 92 | +sum = accumulator(); |
| 93 | +// returns 15.0 |
| 94 | +``` |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.usage --> |
| 99 | + |
| 100 | +<section class="notes"> |
| 101 | + |
| 102 | +## Notes |
| 103 | + |
| 104 | +- 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. |
| 105 | +- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. |
| 106 | + |
| 107 | +</section> |
| 108 | + |
| 109 | +<!-- /.notes --> |
| 110 | + |
| 111 | +<section class="examples"> |
| 112 | + |
| 113 | +## Examples |
| 114 | + |
| 115 | +<!-- eslint no-undef: "error" --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var randu = require( '@stdlib/random/base/randu' ); |
| 119 | +var incrnanmsumabs = require( '@stdlib/stats/incr/nanmsumabs' ); |
| 120 | + |
| 121 | +var accumulator; |
| 122 | +var v; |
| 123 | +var i; |
| 124 | + |
| 125 | +// Initialize an accumulator: |
| 126 | +accumulator = incrnanmsumabs( 5 ); |
| 127 | + |
| 128 | +// For each simulated datum, update the moving sum... |
| 129 | +for ( i = 0; i < 100; i++ ) { |
| 130 | + if ( randu() < 0.2 ) { |
| 131 | + v = NaN; |
| 132 | + } else { |
| 133 | + v = ( randu()*100.0 ) - 50.0; |
| 134 | + } |
| 135 | + accumulator( v ); |
| 136 | +} |
| 137 | +console.log( accumulator() ); |
| 138 | +``` |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.examples --> |
| 143 | + |
| 144 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 145 | + |
| 146 | +<section class="related"> |
| 147 | + |
| 148 | +* * * |
| 149 | + |
| 150 | +## See Also |
| 151 | + |
| 152 | +- <span class="package-name">[`@stdlib/stats/incr/mmeanabs`][@stdlib/stats/incr/mmeanabs]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean of absolute values incrementally.</span> |
| 153 | +- <span class="package-name">[`@stdlib/stats/incr/msum`][@stdlib/stats/incr/msum]</span><span class="delimiter">: </span><span class="description">compute a moving sum incrementally.</span> |
| 154 | +- <span class="package-name">[`@stdlib/stats/incr/sum`][@stdlib/stats/incr/sum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally.</span> |
| 155 | +- <span class="package-name">[`@stdlib/stats/incr/sumabs`][@stdlib/stats/incr/sumabs]</span><span class="delimiter">: </span><span class="description">compute a sum of absolute values incrementally.</span> |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.related --> |
| 160 | + |
| 161 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 162 | + |
| 163 | +<section class="links"> |
| 164 | + |
| 165 | +<!-- <related-links> --> |
| 166 | + |
| 167 | +[@stdlib/stats/incr/mmeanabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmeanabs |
| 168 | + |
| 169 | +[@stdlib/stats/incr/msum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msum |
| 170 | + |
| 171 | +[@stdlib/stats/incr/sum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/sum |
| 172 | + |
| 173 | +[@stdlib/stats/incr/sumabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/sumabs |
| 174 | + |
| 175 | +<!-- </related-links> --> |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.links --> |
0 commit comments