Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanwmean/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# incrnanwmean

> Compute a [weighted arithmetic mean][weighted-arithmetic-mean] incrementally, ignoring `NaN` values.

<section class="intro">

The [weighted arithmetic mean][weighted-arithmetic-mean] is defined as

<!-- <equation class="equation" label="eq:weighted_arithmetic_mean" align="center" raw="\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}" alt="Equation for the weighted arithmetic mean."> -->

```math
\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}
```

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrnanwmean = require( '@stdlib/stats/incr/nanwmean' );
```

#### incrnanwmean()

Returns an accumulator `function` which incrementally computes a [weighted arithmetic mean][weighted-arithmetic-mean], ignoring `NaN` values.

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

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

If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted mean. If not provided any input values or provided `NaN` values, the accumulator function returns the current mean.

```javascript
var accumulator = incrnanwmean();

var mu = accumulator( 2.0, 1.0 );
// returns 2.0

mu = accumulator( 2.0, 0.5 );
// returns 2.0

mu = accumulator( NaN, 1.0 );
// returns 2.0

mu = accumulator( 3.0, 1.5 );
// returns 2.5

mu = accumulator();
// returns 2.5
```

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

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

var accumulator;
var v;
var w;
var i;

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

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

</section>

<!-- /.examples -->

<section class="related">

* * *

## See Also

</section>

<!-- /.related -->

<section class="links">

[weighted-arithmetic-mean]: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var incrnanwmean = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrnanwmean();
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( format( '%s::accumulator', pkg ), function benchmark( b ) {
var acc;
var v;
var i;

acc = incrnanwmean();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu(), 1.0 );
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();
});
37 changes: 37 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanwmean/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{alias}}()
Returns an accumulator function which incrementally computes a weighted
arithmetic mean, ignoring `NaN` values.

If provided arguments, the accumulator function returns an updated weighted
mean. If not provided arguments, the accumulator function returns the
current weighted mean.

If a value is `NaN`, the accumulator function ignores the value and
maintains the current state.

The accumulator function accepts two arguments:

- x: value.
- w: weight.

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

Examples
--------
> var accumulator = {{alias}}();
> var mu = accumulator()
null
> mu = accumulator( 2.0, 1.0 )
2.0
> mu = accumulator( NaN, 1.0 )
2.0
> mu = accumulator( 3.0, 1.0 )
2.5
> mu = accumulator()
2.5

See Also
--------
64 changes: 64 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 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 both arguments, returns an updated weighted arithmetic mean; otherwise, returns the current weighted arithmetic mean.
*
* ## Notes
*
* - If provided `NaN` values, accumulator returns current weighted arithmetic mean ignoring `NaN` value.
*
* @param x - value
* @param w - weight
* @returns weighted arithmetic mean
*/
type accumulator = ( x?: number, w?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a weighted arithmetic mean.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should read "... weighted arithmetic mean, ignoring NaN values." to match nanewmean and the module description.

*
* @returns accumulator function
*
* @example
* var accumulator = incrnanwmean();
*
* var mu = accumulator();
* // returns null
*
* mu = accumulator( 2.0, 1.0 );
* // returns 2.0
*
* mu = accumulator( 2.0, 0.5 );
* // returns 2.0
*
* mu = accumulator( 3.0, 1.5 );
* // returns 2.5
*
* mu = accumulator();
* // returns 2.5
*/
declare function incrnanwmean(): accumulator;


// EXPORTS //

export = incrnanwmean;
70 changes: 70 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/

import incrwmean = require( './index' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect package name. Copy paste error from incrwmean

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be updated below as well!!!



// TESTS //

// The function returns an accumulator function...
{
incrwmean(); // $ExpectType accumulator
}

// The compiler throws an error if the function is provided arguments...
{
incrwmean( '5' ); // $ExpectError
incrwmean( 5 ); // $ExpectError
incrwmean( true ); // $ExpectError
incrwmean( false ); // $ExpectError
incrwmean( null ); // $ExpectError
incrwmean( undefined ); // $ExpectError
incrwmean( [] ); // $ExpectError
incrwmean( {} ); // $ExpectError
incrwmean( ( x: number ): number => x ); // $ExpectError
}

// The function returns an accumulator function which returns an accumulated result...
{
const acc = incrwmean();

acc(); // $ExpectType number | null
acc( 3.14, 1.0 ); // $ExpectType number | null
}

// The compiler throws an error if the returned accumulator function is provided invalid arguments...
{
const acc = incrwmean();

acc( '5', 1.0 ); // $ExpectError
acc( true, 1.0 ); // $ExpectError
acc( false, 1.0 ); // $ExpectError
acc( null, 1.0 ); // $ExpectError
acc( [], 1.0 ); // $ExpectError
acc( {}, 1.0 ); // $ExpectError
acc( ( x: number ): number => x, 1.0 ); // $ExpectError

acc( 3.14, '5' ); // $ExpectError
acc( 3.14, true ); // $ExpectError
acc( 3.14, false ); // $ExpectError
acc( 3.14, null ); // $ExpectError
acc( 3.14, [] ); // $ExpectError
acc( 3.14, {} ); // $ExpectError
acc( 3.14, ( x: number ): number => x ); // $ExpectError
}

Loading