File tree Expand file tree Collapse file tree
lib/node_modules/@stdlib/stats/incr/mae/lib Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2121// MODULES //
2222
2323var 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*/
6349function 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
You can’t perform that action at this time.
0 commit comments