Skip to content
Merged
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
149 changes: 149 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nangmean/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<!--

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

-->

# incrnangmean

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

<section class="intro">

The [geometric mean][geometric-mean] is defined as the nth root of a product of _n_ numbers.

<!-- <equation class="equation" label="eq:geometric_mean" align="center" raw="\biggl( \prod_{i=0}^{n-1} x_i \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}}" alt="Equation for the geometric mean."> -->

```math
\biggl( \prod_{i=0}^{n-1} x_i \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}}
```


<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );
```

#### incrnangmean()

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

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

#### accumulator( \[x] )

If provided an input value `x`, the accumulator function returns an updated [geometric mean][geometric-mean]. If not provided an input value `x`, the accumulator function returns the current [geometric mean][geometric-mean].

```javascript
var accumulator = incrnangmean();

var prod = accumulator( 2.0 );
// returns 2.0

prod = accumulator( 1.0 );
// returns ~1.414

prod = accumulator( NaN );
// returns ~1.414

prod = accumulator( 3.0 );
// returns ~1.817

prod = accumulator();
// returns ~1.817
```

</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 bernoulli = require( '@stdlib/random/base/bernoulli' );
var uniform = require( '@stdlib/random/base/uniform' );
var incrnangmean = require( '@stdlib/stats/incr/nangmean' );

var accumulator;
var v;
var i;

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

// For each simulated value, update the geometric mean...
for ( i = 0; i < 100; i++ ) {
if ( bernoulli( 0.2 ) ) {
v = NaN;
} else {
v = uniform( 0.0, 100.0 );
}
accumulator( v );
}
console.log( accumulator() );
```

</section>

<!-- /.examples -->

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

<section class="related">

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

[geometric-mean]: https://en.wikipedia.org/wiki/Geometric_mean

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

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

</section>

<!-- /.links -->
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 incrnangmean = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrnangmean();
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 = incrnangmean();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu()*10.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();
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nangmean/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{{alias}}()
Returns an accumulator function which incrementally computes a geometric
mean, ignoring `NaN` values.

If provided a value, the accumulator function returns an updated geometric
mean. If not provided a value, the accumulator function returns the current
geometric mean.

If provided a negative value, the accumulated value is `NaN` for all future
invocations.

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

Examples
--------
> var accumulator = {{alias}}();
> var v = accumulator()
null
> v = accumulator( 2.0 )
2.0
> v = accumulator( NaN )
2.0
> v = accumulator( 5.0 )
~3.16
> v = accumulator()
~3.16

See Also
--------

59 changes: 59 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nangmean/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* @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 a value, returns an updated geometric mean; otherwise, returns the current geometric mean.
*
* @param x - value
* @returns geometric mean
*/
type accumulator = ( x?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a geometric mean, ignoring `NaN` values.
*
* @returns accumulator function
*
* @example
* var accumulator = incrnangmean();
*
* var v = accumulator();
* // returns null
*
* v = accumulator( 2.0 );
* // returns 2.0
*
* v = accumulator( NaN );
* // returns 2.0
*
* v = accumulator( 5.0 );
* // returns ~3.16
*
* v = accumulator();
* // returns ~3.16
*/
declare function incrnangmean(): accumulator;


// EXPORTS //

export = incrnangmean;
Loading
Loading