Skip to content
Open
170 changes: 170 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# incrnanme

> Compute the [mean error][mean-absolute-error] (ME) incrementally, ignoring `NaN` values.

<section class="intro">

The [mean error][mean-absolute-error] is defined as

<!-- <equation class="equation" label="eq:mean_error" align="center" raw="\operatorname{ME} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)" alt="Equation for the mean error."> -->

```math
\mathop{\mathrm{ME}} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)
```

<!-- <div class="equation" align="center" data-raw-text="\operatorname{ME} = \frac{1}{n} \sum_{i=0}^{n-1} (y_i - x_i)" data-equation="eq:mean_error">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7d6e6319f451be0997d35a6cf491b08e1f2cb5cf/lib/node_modules/@stdlib/stats/incr/nanme/docs/img/equation_mean_error.svg" alt="Equation for the mean error.">
<br>
</div> -->

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrnanme = require( '@stdlib/stats/incr/nanme' );
```

#### incrnanme()

Returns an accumulator `function` which incrementally computes the [mean error][mean-absolute-error], ignoring `NaN` values.

```javascript
var accumulator = incrnanme();
```

#### accumulator( \[x, y] )

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].

```javascript
var accumulator = incrnanme();

var m = accumulator( 2.0, 3.0 );
// returns 1.0

m = accumulator( -1.0, -4.0 );
// returns -1.0

m = accumulator( -3.0, 5.0 );
// returns 2.0

m = accumulator();
// returns 2.0
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- 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.
- 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.
- **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.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var incrnanme = require( '@stdlib/stats/incr/nanme' );

var accumulator;
var v1;
var v2;
var i;

// Initialize an accumulator:
accumulator = incrnanme();

// For each simulated datum, update the mean error...
for ( i = 0; i < 100; i++ ) {
if( randu() < 0.2 ){
v1 = NaN;
} else {
v1 = ( randu()*100.0 ) - 50.0;
}
if( randu() < 0.2 ){
v2 = NaN;
} else {
v2 = ( randu()*100.0 ) - 50.0;
}
}
console.log( accumulator() );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/mae`][@stdlib/stats/incr/mae]</span><span class="delimiter">: </span><span class="description">compute the mean absolute error (MAE) incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mme`][@stdlib/stats/incr/mme]</span><span class="delimiter">: </span><span class="description">compute a moving mean error (ME) incrementally.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[mean-absolute-error]: https://en.wikipedia.org/wiki/Mean_absolute_error

<!-- <related-links> -->

[@stdlib/stats/incr/mae]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mae

[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean

[@stdlib/stats/incr/mme]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mme

<!-- </related-links> -->

</section>

<!-- /.links -->
69 changes: 69 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanme/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var incrnanme = require( '@stdlib/stats/incr/nanme/lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrnanme();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
}
b.toc();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::accumulator', function benchmark( b ) {
var acc;
var v;
var i;

acc = incrnanme();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu()-0.5, randu()-0.5 );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanme/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

{{alias}}()
Returns an accumulator function which incrementally computes the mean error
(ME), ignoring NaN values.

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.

Returns
-------
acc: Function
Accumulator function.

Examples
--------
> var accumulator = {{alias}}();
> var m = accumulator()
null
> m = accumulator( 2.0, 3.0 )
1.0
> m = accumulator( -5.0, 2.0 )
4.0
> m = accumulator( -3.0, NaN )
4.0
> m = accumulator( NaN, 2.0 )
4.0
> m = accumulator()
4.0

See Also
--------

63 changes: 63 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanme/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

/**
* 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.
*
* @param x - input value
* @param y - input value
* @returns mean error or null
*/
type accumulator = ( x?: number, y?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes the mean error, ignoring `NaN` values.
*
* @returns accumulator function
*
* @example
* var accumulator = incrnanme();
*
* var m = accumulator();
* // returns null
*
* m = accumulator( 2.0, 3.0 );
* // returns 1.0
*
* m = accumulator( -5.0, 2.0 );
* // returns 4.0
*
* m = accumulator( -3.0, NaN );
* // returns 4.0
*
* m = accumulator( NaN, 2.0 );
* // returns 4.0
*
* m = accumulator();
* // returns 4.0
*/
declare function incrnanme(): accumulator;


// EXPORTS //

export = incrnanme;
Loading