Skip to content

Commit f5750de

Browse files
committed
Add pkg to incrementally compute the mean error
1 parent 69bae07 commit f5750de

File tree

8 files changed

+582
-0
lines changed

8 files changed

+582
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# incrme
22+
23+
> Compute the [mean error][mean-absolute-error] incrementally.
24+
25+
<section class="intro">
26+
27+
The [mean error][mean-absolute-error] is defined as
28+
29+
<!-- <equation class="equation" label="eq:mean_error" align="center" raw="\operatorname{ME} = \frac{\sum_{i=0}^{n-1} (y_i - x_i)}{n}" alt="Equation for the mean error."> -->
30+
31+
<div class="equation" align="center" data-raw-text="\operatorname{ME} = \frac{\sum_{i=0}^{n-1} (y_i - x_i)}{n}" data-equation="eq:mean_error">
32+
<img src="" alt="Equation for the mean error.">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var incrme = require( '@stdlib/stats/incr/me' );
48+
```
49+
50+
#### incrme()
51+
52+
Returns an accumulator `function` which incrementally computes the [mean error][mean-absolute-error].
53+
54+
```javascript
55+
var accumulator = incrme();
56+
```
57+
58+
#### accumulator( \[x, y] )
59+
60+
If provided input values `x` and `y`, the accumulator function returns an updated [mean error][mean-absolute-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean error][mean-absolute-error].
61+
62+
```javascript
63+
var accumulator = incrme();
64+
65+
var m = accumulator( 2.0, 3.0 );
66+
// returns 1.0
67+
68+
m = accumulator( -1.0, -4.0 );
69+
// returns -1.0
70+
71+
m = accumulator( -3.0, 5.0 );
72+
// returns 2.0
73+
74+
m = accumulator();
75+
// returns 2.0
76+
```
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<section class="notes">
83+
84+
## Notes
85+
86+
- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
87+
- Be careful when interpreting the [mean error][mean-absolute-error] as errors can cancel. This stated, that errors can cancel makes the [mean error][mean-absolute-error] suitable for measuring the bias in forecasts.
88+
- **Warning**: the [mean error][mean-absolute-error] is scale-dependent and, thus, the measure should **not** be used to make comparisons between datasets having different scales.
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<section class="examples">
95+
96+
## Examples
97+
98+
<!-- eslint no-undef: "error" -->
99+
100+
```javascript
101+
var randu = require( '@stdlib/random/base/randu' );
102+
var incrme = require( '@stdlib/stats/incr/me' );
103+
104+
var accumulator;
105+
var v1;
106+
var v2;
107+
var i;
108+
109+
// Initialize an accumulator:
110+
accumulator = incrme();
111+
112+
// For each simulated datum, update the mean error...
113+
for ( i = 0; i < 100; i++ ) {
114+
v1 = ( randu()*100.0 ) - 50.0;
115+
v2 = ( randu()*100.0 ) - 50.0;
116+
accumulator( v1, v2 );
117+
}
118+
console.log( accumulator() );
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<section class="links">
126+
127+
[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error
128+
129+
</section>
130+
131+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrme = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrme();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrme();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()-0.5, randu()-0.5 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes the mean error.
4+
5+
If provided input values, the accumulator function returns an updated mean
6+
error. If not provided input values, the accumulator function returns the current mean error.
7+
8+
If provided `NaN` or a value which, when used in computations, results in
9+
`NaN`, the accumulated value is `NaN` for all future invocations.
10+
11+
Returns
12+
-------
13+
acc: Function
14+
Accumulator function.
15+
16+
Examples
17+
--------
18+
> var accumulator = {{alias}}();
19+
> var m = accumulator()
20+
null
21+
> m = accumulator( 2.0, 3.0 )
22+
1.0
23+
> m = accumulator( -5.0, 2.0 )
24+
4.0
25+
> m = accumulator()
26+
4.0
27+
28+
See Also
29+
--------
30+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var randu = require( '@stdlib/random/base/randu' );
22+
var incrme = require( './../lib' );
23+
24+
var accumulator;
25+
var err;
26+
var v1;
27+
var v2;
28+
var i;
29+
30+
// Initialize an accumulator:
31+
accumulator = incrme();
32+
33+
// For each simulated datum, update the mean error...
34+
console.log( '\nValue\tValue\tMean\n' );
35+
for ( i = 0; i < 100; i++ ) {
36+
v1 = ( randu()*100.0 ) - 50.0;
37+
v2 = ( randu()*100.0 ) - 50.0;
38+
err = accumulator( v1, v2 );
39+
console.log( '%d\t%d\t%d', v1.toFixed( 3 ), v2.toFixed( 3 ), err.toFixed( 3 ) );
40+
}
41+
console.log( '\nFinal ME: %d\n', accumulator() );
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Compute the mean error incrementally.
23+
*
24+
* @module @stdlib/stats/incr/me
25+
*
26+
* @example
27+
* var incrme = require( '@stdlib/stats/incr/me' );
28+
*
29+
* var accumulator = incrme();
30+
*
31+
* var m = accumulator();
32+
* // returns null
33+
*
34+
* m = accumulator( 2.0, 3.0 );
35+
* // returns 1.0
36+
*
37+
* m = accumulator( -5.0, 2.0 );
38+
* // returns 4.0
39+
*
40+
* m = accumulator();
41+
* // returns 4.0
42+
*/
43+
44+
// MODULES //
45+
46+
var incrme = require( './main.js' );
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = incrme;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var incrmean = require( '@stdlib/stats/incr/mean' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns an accumulator function which incrementally computes the mean error.
30+
*
31+
* @returns {Function} accumulator function
32+
*
33+
* @example
34+
* var accumulator = incrme();
35+
*
36+
* var m = accumulator();
37+
* // returns null
38+
*
39+
* m = accumulator( 2.0, 3.0 );
40+
* // returns 1.0
41+
*
42+
* m = accumulator( -5.0, 2.0 );
43+
* // returns 4.0
44+
*
45+
* m = accumulator();
46+
* // returns 4.0
47+
*/
48+
function incrme() {
49+
var mean = incrmean();
50+
return accumulator;
51+
52+
/**
53+
* If provided input values, the accumulator function returns an updated mean error. If not provided input values, the accumulator function returns the current mean error.
54+
*
55+
* @private
56+
* @param {number} [x] - input value
57+
* @param {number} [y] - input value
58+
* @returns {(number|null)} mean error or null
59+
*/
60+
function accumulator( x, y ) {
61+
if ( arguments.length === 0 ) {
62+
return mean();
63+
}
64+
return mean( y-x );
65+
}
66+
}
67+
68+
69+
// EXPORTS //
70+
71+
module.exports = incrme;

0 commit comments

Comments
 (0)