|
| 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 | +# incrnanmmda |
| 22 | + |
| 23 | +> Compute a moving [mean directional accuracy][mean-directional-accuracy] (MDA) incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For a window of size `W`, the [mean directional accuracy][mean-directional-accuracy] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:mean_directional_accuracy" align="center" raw="\operatorname{MDA} = \begin{cases} 1 & \textrm{if}\ W = 1 \\ \frac{1}{W} \sum_{i=1}^{W} \delta_{\operatorname{sgn}(\Delta f_{i,i-1}),\ \operatorname{sgn}(\Delta a_{i,i-1})} & \textrm{if}\ W > 1 \end{cases}" alt="Equation for the mean directional accuracy."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mathop{\mathrm{MDA}} = \begin{cases} 1 & \textrm{if}\ W = 1 \\ \frac{1}{W} \sum_{i=1}^{W} \delta_{\mathop{\mathrm{sgn}}(\Delta f_{i,i-1}),\ \mathop{\mathrm{sgn}}(\Delta a_{i,i-1})} & \textrm{if}\ W > 1 \end{cases} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\operatorname{MDA} = \begin{cases} 1 & \textrm{if}\ W = 1 \\\frac{1}{W} \sum_{i=1}^{W} \delta_{\operatorname{sgn}(\Delta f_{i,i-1}),\ \operatorname{sgn}(\Delta a_{i,i-1})} & \textrm{if}\ W > 1 \end{cases}" data-equation="eq:mean_directional_accuracy"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@99730afbace8256ce53cfbc0714c7f3cac92466a/lib/node_modules/@stdlib/stats/incr/mmda/docs/img/equation_mean_directional_accuracy.svg" alt="Equation for the mean directional accuracy."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where `f_i` is the forecast value, `a_i` is the actual value, `sgn(x)` is the [signum][@stdlib/math/base/special/signum] function, and `δ` is the [Kronecker delta][@stdlib/math/base/special/kronecker-delta]. |
| 43 | + |
| 44 | +</section> |
| 45 | + |
| 46 | +<!-- /.intro --> |
| 47 | + |
| 48 | +<section class="usage"> |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```javascript |
| 53 | +var incrnanmmda = require( '@stdlib/stats/incr/nanmmda' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### incrnanmmda( window ) |
| 57 | + |
| 58 | +Returns an accumulator `function` which incrementally computes a moving [mean directional accuracy][mean-directional-accuracy], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [mean directional accuracy][mean-directional-accuracy]. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var accumulator = incrnanmmda( 3 ); |
| 62 | +``` |
| 63 | + |
| 64 | +#### accumulator( \[f, a] ) |
| 65 | + |
| 66 | +If provided input values `f` and `a`, the accumulator function returns an updated [mean directional accuracy][mean-directional-accuracy]. If not provided input values `f` and `a`, the accumulator function returns the current [mean directional accuracy][mean-directional-accuracy]. |
| 67 | + |
| 68 | +```javascript |
| 69 | +var accumulator = incrnanmmda( 3 ); |
| 70 | + |
| 71 | +var m = accumulator(); |
| 72 | +// returns null |
| 73 | + |
| 74 | +m = accumulator( NaN, 4.0 ); |
| 75 | +// returns null |
| 76 | + |
| 77 | +// Fill the window... |
| 78 | +m = accumulator( 2.0, 3.0 ); // [(+,+)] |
| 79 | +// returns 1.0 |
| 80 | + |
| 81 | +m = accumulator( 1.0, 4.0 ); // [(+,+), (-,+)] |
| 82 | +// returns 0.5 |
| 83 | + |
| 84 | +m = accumulator( 3.0, NaN ); // [(+,+), (-,+)] |
| 85 | +// returns 0.5 |
| 86 | + |
| 87 | +m = accumulator( 3.0, 9.0 ); // [(+,+), (-,+), (+,+)] |
| 88 | +// returns ~0.67 |
| 89 | + |
| 90 | +// Window begins sliding... |
| 91 | +m = accumulator( 7.0, 3.0 ); // [(-,+), (+,+), (+,-)] |
| 92 | +// returns ~0.33 |
| 93 | + |
| 94 | +m = accumulator( NaN, NaN ); // [(-,+), (+,+), (+,-)] |
| 95 | +// returns -0.33 |
| 96 | + |
| 97 | +m = accumulator( 5.0, 3.0 ); // [(+,+), (+,-), (-,0)] |
| 98 | +// returns ~0.33 |
| 99 | + |
| 100 | +m = accumulator(); |
| 101 | +// returns ~0.33 |
| 102 | +``` |
| 103 | + |
| 104 | +</section> |
| 105 | + |
| 106 | +<!-- /.usage --> |
| 107 | + |
| 108 | +<section class="notes"> |
| 109 | + |
| 110 | +## Notes |
| 111 | + |
| 112 | +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is **not** changed and the current input values get **ignored**. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 113 | +- As `W` (f,a) pairs 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. |
| 114 | + |
| 115 | +</section> |
| 116 | + |
| 117 | +<!-- /.notes --> |
| 118 | + |
| 119 | +<section class="examples"> |
| 120 | + |
| 121 | +## Examples |
| 122 | + |
| 123 | +<!-- eslint no-undef: "error" --> |
| 124 | + |
| 125 | +```javascript |
| 126 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 127 | +var randu = require( '@stdlib/random/base/randu' ); |
| 128 | +var incrnanmmda = require( '@stdlib/stats/incr/nanmmda' ); |
| 129 | + |
| 130 | +var accumulator; |
| 131 | +var v1; |
| 132 | +var v2; |
| 133 | +var i; |
| 134 | + |
| 135 | +// Initialize an accumulator: |
| 136 | +accumulator = incrnanmmda( 5 ); |
| 137 | + |
| 138 | +// For each simulated datum, update the moving mean directional accuracy... |
| 139 | +for ( i = 0; i < 100; i++ ) { |
| 140 | + v1 = ( bernoulli( 0.1 ) ) ? NaN : ( randu()*100.0 ) - 50.0; |
| 141 | + v2 = ( bernoulli( 0.1 ) ) ? NaN : ( randu()*100.0 ) - 50.0; |
| 142 | + accumulator( v1, v2 ); |
| 143 | +} |
| 144 | +console.log( accumulator() ); |
| 145 | +``` |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.examples --> |
| 150 | + |
| 151 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 152 | + |
| 153 | +<section class="related"> |
| 154 | + |
| 155 | +* * * |
| 156 | + |
| 157 | +## See Also |
| 158 | + |
| 159 | +- <span class="package-name">[`@stdlib/stats/incr/mda`][@stdlib/stats/incr/mda]</span><span class="delimiter">: </span><span class="description">compute the mean directional accuracy (MDA) incrementally.</span> |
| 160 | +- <span class="package-name">[`@stdlib/stats/incr/mmape`][@stdlib/stats/incr/mmape]</span><span class="delimiter">: </span><span class="description">compute a moving mean absolute percentage error (MAPE) incrementally.</span> |
| 161 | + |
| 162 | +</section> |
| 163 | + |
| 164 | +<!-- /.related --> |
| 165 | + |
| 166 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 167 | + |
| 168 | +<section class="links"> |
| 169 | + |
| 170 | +[mean-directional-accuracy]: https://en.wikipedia.org/wiki/Mean_Directional_Accuracy_%28MDA%29 |
| 171 | + |
| 172 | +[@stdlib/math/base/special/signum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/signum |
| 173 | + |
| 174 | +[@stdlib/math/base/special/kronecker-delta]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/kronecker-delta |
| 175 | + |
| 176 | +<!-- <related-links> --> |
| 177 | + |
| 178 | +[@stdlib/stats/incr/mda]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mda |
| 179 | + |
| 180 | +[@stdlib/stats/incr/mmape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmape |
| 181 | + |
| 182 | +<!-- </related-links> --> |
| 183 | + |
| 184 | +</section> |
| 185 | + |
| 186 | +<!-- /.links --> |
0 commit comments