Skip to content

feat: add stats/incr/nanprod #6026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
212 changes: 212 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanprod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<!--

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

-->

# incrnanprod

> Compute a product incrementally, ignoring the `NaN` values.

<section class="intro">

The product is defined as

<!-- <equation class="equation" label="eq:product" align="center" raw="p = \prod_{i=0}^{n-1} x_i" alt="Equation for the product."> -->

```math
p = \prod_{i=0}^{n-1} x_i
```

<!-- <div class="equation" align="center" data-raw-text="p = \prod_{i=0}^{n-1} x_i" data-equation="eq:product">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@ae1331d17d505898a27e695fc60144d786627384/lib/node_modules/@stdlib/stats/incr/prod/docs/img/equation_product.svg" alt="Equation for the product.">
<br>
</div> -->

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrnanprod = require( '@stdlib/stats/incr/nanprod' );
```

#### incrnanprod()

Returns an accumulator `function` which incrementally computes a product, ignoring `NaN` values.

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

#### accumulator( \[x] )

If provided an input value `x`, the accumulator function returns an updated product. If not provided an input value `x`, the accumulator function returns the current product.

```javascript
var accumulator = incrnanprod();

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

prod = accumulator( NaN );
// returns 2.0

prod = accumulator( 1.0 );
// returns 2.0

prod = accumulator( 3.0 );
// returns 6.0

prod = accumulator();
// returns 6.0
```

Under certain conditions, overflow may be transient.

```javascript
// Large values:
var x = 5.0e+300;
var y = 1.0e+300;

// Tiny value:
var z = 2.0e-302;

// Initialize an accumulator:
var accumulator = incrnanprod();

var prod = accumulator( x );
// returns 5.0e+300

// Transient overflow:
prod = accumulator( y );
// returns Infinity

// Recover a finite result:
prod = accumulator( z );
// returns 1.0e+299
```

Similarly, under certain conditions, underflow may be transient.

```javascript
// Tiny values:
var x = 4.0e-302;
var y = 9.0e-303;

// Large value:
var z = 2.0e+300;

// Initialize an accumulator:
var accumulator = incrnanprod();

var prod = accumulator( x );
// returns 4.0e-302

// Transient underflow:
prod = accumulator( y );
// returns 0.0

// Recover a non-zero result:
prod = accumulator( z );
// returns 7.2e-304
```

</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.
- For long running accumulations or accumulations of either large or small numbers, care should be taken to prevent overflow and underflow. Note, however, that overflow/underflow may be transient, as the accumulator does not use a double-precision floating-point number to store an accumulated product. Instead, the accumulator splits an accumulated product into a normalized **fraction** and **exponent** and updates each component separately. Doing so guards against a loss in precision.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var incrnanprod = require( '@stdlib/stats/incr/nanprod' );

var accumulator;
var v;
var i;

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

// For each simulated value, update the product...
for ( i = 0; i < 100; i++ ) {
v = ( bernoulli( 0.8 ) < 1 ) ? NaN : 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">

* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/mprod`][@stdlib/stats/incr/mprod]</span><span class="delimiter">: </span><span class="description">compute a moving product incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/sum`][@stdlib/stats/incr/sum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary 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">

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

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

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

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

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

</section>

<!-- /.links -->
69 changes: 69 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanprod/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 incrnanprod = require( './../lib' );


// MAIN //

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

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();
});
38 changes: 38 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanprod/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

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

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

For long running accumulations or accumulations of large numbers, care
should be taken to prevent overflow. Note, however, that overflow/underflow
may be transient, as the accumulator does not use a double-precision
floating-point number to store an accumulated product. Instead, the
accumulator splits an accumulated product into a normalized fraction and
exponent and updates each component separately. Doing so guards against a
loss in precision.

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 )
-10.0
> v = accumulator()
-10.0

See Also
--------

67 changes: 67 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanprod/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* @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 product; otherwise, returns the current product.
*
* ## Notes
*
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
* - For long running accumulations or accumulations of large numbers, care should be taken to prevent overflow. Note, however, that overflow/underflow may be transient, as the accumulator does not use a double-precision floating-point number to store an accumulated product. Instead, the accumulator splits an accumulated product into a normalized fraction and exponent and updates each component separately. Doing so guards against a loss in precision.
*
* @param x - value
* @returns product
*/
type accumulator = ( x?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a product, ignoring `NaN` values.
*
* @returns accumulator function
*
* @example
* var accumulator = incrnanprod();
*
* var v = accumulator();
* // returns null
*
* v = accumulator( 2.0 );
* // returns 2.0
*
* v = accumulator( NaN );
* // returns 2.0
*
* v = accumulator( 3.0 );
* // returns 6.0
*
* v = accumulator( 4.0 );
* // returns 24.0
*
* v = accumulator();
* // returns 24.0
*/
declare function incrnanprod(): accumulator;


// EXPORTS //

export = incrnanprod;
Loading