Skip to content

Commit 715168d

Browse files
committed
feat: add implementation of stats/incr/nanmprod
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 9687965 commit 715168d

11 files changed

Lines changed: 861 additions & 0 deletions

File tree

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# incrnanmprod
22+
23+
> Compute a moving product incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the moving product is defined as
28+
29+
<!-- <equation class="equation" label="eq:moving_product" align="center" raw="\prod_{i=0}^{W-1} x_i" alt="Equation for the moving product."> -->
30+
31+
```math
32+
\prod_{i=0}^{W-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\prod_{i=0}^{W-1} x_i" data-equation="eq:moving_product">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmprod/docs/img/equation_moving_product.svg" alt="Equation for the moving product.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var incrnanmprod = require( '@stdlib/stats/incr/nanmprod' );
52+
```
53+
54+
#### incrnanmprod( window )
55+
56+
Returns an accumulator `function` which incrementally computes a moving product, ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving product.
57+
58+
```javascript
59+
var accumulator = incrnanmprod( 3 );
60+
```
61+
62+
#### accumulator( \[x] )
63+
64+
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.
65+
66+
```javascript
67+
var accumulator = incrnanmprod( 3 );
68+
69+
var p = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
p = accumulator( 2.0 ); // [2.0]
74+
// returns 2.0
75+
76+
p = accumulator( NaN ); // [2.0] (NaN ignored)
77+
// returns 2.0
78+
79+
p = accumulator( 3.0 ); // [2.0, 3.0]
80+
// returns 6.0
81+
82+
// Window begins sliding...
83+
p = accumulator( 5.0 ); // [2.0, 3.0, 5.0]
84+
// returns 30.0
85+
86+
p = accumulator( NaN ); // [2.0, 3.0, 5.0] (NaN ignored)
87+
// returns 30.0
88+
89+
p = accumulator( 7.0 ); // [3.0, 5.0, 7.0]
90+
// returns 105.0
91+
92+
p = accumulator();
93+
// returns 105.0
94+
```
95+
96+
Under certain conditions, overflow may be transient.
97+
98+
```javascript
99+
// Large values:
100+
var x = 5.0e+300;
101+
var y = 1.0e+300;
102+
103+
// Tiny value:
104+
var z = 2.0e-302;
105+
106+
// Initialize an accumulator:
107+
var accumulator = incrnanmprod( 3 );
108+
109+
var p = accumulator( x );
110+
// returns 5.0e+300
111+
112+
// Transient overflow:
113+
p = accumulator( y );
114+
// returns Infinity
115+
116+
// Recover a finite result:
117+
p = accumulator( z );
118+
// returns 1.0e+299
119+
```
120+
121+
Similarly, under certain conditions, underflow may be transient.
122+
123+
```javascript
124+
// Tiny values:
125+
var x = 4.0e-302;
126+
var y = 9.0e-303;
127+
128+
// Large value:
129+
var z = 2.0e+300;
130+
131+
// Initialize an accumulator:
132+
var accumulator = incrnanmprod( 3 );
133+
134+
var p = accumulator( x );
135+
// returns 4.0e-302
136+
137+
// Transient underflow:
138+
p = accumulator( y );
139+
// returns 0.0
140+
141+
// Recover a non-zero result:
142+
p = accumulator( z );
143+
// returns 7.2e-304
144+
```
145+
146+
</section>
147+
148+
<!-- /.usage -->
149+
150+
<section class="notes">
151+
152+
## Notes
153+
154+
- 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.
155+
- `NaN` values are ignored and do not affect the computed moving product.
156+
- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
157+
- For large accumulation windows 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.
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
163+
<section class="examples">
164+
165+
## Examples
166+
167+
<!-- eslint no-undef: "error" -->
168+
169+
```javascript
170+
var randu = require( '@stdlib/random/base/randu' );
171+
var incrnanmprod = require( '@stdlib/stats/incr/nanmprod' );
172+
173+
// Initialize an accumulator:
174+
var accumulator = incrnanmprod( 5 );
175+
176+
// For each simulated datum, update the moving product...
177+
var i;
178+
for ( i = 0; i < 100; i++ ) {
179+
accumulator( ( randu()*10.0 ) - 5.0 );
180+
}
181+
console.log( accumulator() );
182+
```
183+
184+
</section>
185+
186+
<!-- /.examples -->
187+
188+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
189+
190+
<!-- /.related -->
191+
192+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
193+
194+
<section class="links">
195+
196+
<!-- <related-links> -->
197+
198+
<!-- </related-links> -->
199+
200+
</section>
201+
202+
<!-- /.links -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var incrnanmprod = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var f;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
f = incrnanmprod( (i%5)+1 );
38+
if ( typeof f !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
if ( typeof f !== 'function' ) {
44+
b.fail( 'should return a function' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( format( '%s::accumulator', pkg ), function benchmark( b ) {
51+
var acc;
52+
var v;
53+
var i;
54+
55+
acc = incrnanmprod( 5 );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = acc( randu() );
60+
if ( v !== v ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( v !== v ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
Lines changed: 30 additions & 0 deletions
Loading
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
product, ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving product.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
product. If not provided a value, the accumulator function returns the
11+
current moving product.
12+
13+
`NaN` values are ignored and do not affect the computed moving product.
14+
15+
As `W` values are needed to fill the window buffer, the first `W-1` returned
16+
values are calculated from smaller sample sizes. Until the window is full,
17+
each returned value is calculated from all provided values.
18+
19+
For accumulations over large windows or accumulations of large numbers, care
20+
should be taken to prevent overflow. Note, however, that overflow/underflow
21+
may be transient, as the accumulator does not use a double-precision
22+
floating-point number to store an accumulated product. Instead, the
23+
accumulator splits an accumulated product into a normalized fraction and
24+
exponent and updates each component separately. Doing so guards against a
25+
loss in precision.
26+
27+
Parameters
28+
----------
29+
W: integer
30+
Window size.
31+
32+
Returns
33+
-------
34+
acc: Function
35+
Accumulator function.
36+
37+
Examples
38+
--------
39+
> var accumulator = {{alias}}( 3 );
40+
> var p = accumulator()
41+
null
42+
> p = accumulator( 2.0 )
43+
2.0
44+
> p = accumulator( NaN )
45+
2.0
46+
> p = accumulator( 3.0 )
47+
6.0
48+
> p = accumulator( 5.0 )
49+
30.0
50+
> p = accumulator()
51+
30.0
52+
53+
See Also
54+
--------
55+

0 commit comments

Comments
 (0)