Skip to content

Commit b245422

Browse files
committed
Simplify implementation
1 parent 95ca471 commit b245422

1 file changed

Lines changed: 4 additions & 30 deletions

File tree

  • lib/node_modules/@stdlib/stats/incr/mae/lib

lib/node_modules/@stdlib/stats/incr/mae/lib/main.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,14 @@
2121
// MODULES //
2222

2323
var abs = require( '@stdlib/math/base/special/abs' );
24+
var incrmean = require( '@stdlib/stats/incr/mean' );
2425

2526

2627
// MAIN //
2728

2829
/**
2930
* Returns an accumulator function which incrementally computes the mean absolute error.
3031
*
31-
* ## Method
32-
*
33-
* - This implementation uses [Welford's method][algorithms-variance] for efficient computation, which can be derived as follows
34-
*
35-
* ```tex
36-
* \begin{align*}
37-
* \operatorname{MAE}_n &= \frac{1}{n} \sum_{i=0}^{n-1} |y_i - x_i| \\
38-
* &= \frac{1}{n} \biggl(|y_{n-1} - x_{n-1}| + \sum_{i=0}^{n-2} |y_i - x_i|\biggr) \\
39-
* &= \frac{1}{n} \biggl(|y_{n-1} - x_{n-1}| + (n-1)\operatorname{MAE}_{n-1}\biggr) \\
40-
* &= \operatorname{MAE}_{n-1} + \frac{1}{n} \biggl(|y_{n-1} - x_{n-1}| - \operatorname{MAE}_{n-1}\biggr)
41-
* \end{align*}
42-
* ```
43-
*
44-
* [algorithms-variance]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
45-
*
4632
* @returns {Function} accumulator function
4733
*
4834
* @example
@@ -61,13 +47,7 @@ var abs = require( '@stdlib/math/base/special/abs' );
6147
* // returns 4.0
6248
*/
6349
function incrmae() {
64-
var delta;
65-
var mu;
66-
var N;
67-
68-
mu = 0.0;
69-
N = 0;
70-
50+
var mean = incrmean();
7151
return accumulator;
7252

7353
/**
@@ -80,15 +60,9 @@ function incrmae() {
8060
*/
8161
function accumulator( x, y ) {
8262
if ( arguments.length === 0 ) {
83-
if ( N === 0 ) {
84-
return null;
85-
}
86-
return mu;
63+
return mean();
8764
}
88-
N += 1;
89-
delta = abs( y-x ) - mu;
90-
mu += delta / N;
91-
return mu;
65+
return mean( abs( y-x ) );
9266
}
9367
}
9468

0 commit comments

Comments
 (0)