Skip to content

Commit 839547d

Browse files
feat: add nanrss for handling NaN
--- 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 40e6837 commit 839547d

File tree

11 files changed

+794
-0
lines changed

11 files changed

+794
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
# incrnanrss
22+
23+
> Compute the [residual sum of squares][residual-sum-of-squares] (RSS) incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [**residual sum of squares**][residual-sum-of-squares] (also referred to as the **sum of squared residuals** (SSR) and the **sum of squared errors** (SSE)) is defined as
28+
29+
<!-- <equation class="equation" label="eq:residual_sum_of_squares" align="center" raw="\operatorname{RSS} = \sum_{i=0}^{n-1} (y_i - x_i)^2" alt="Equation for the residual sum of squares."> -->
30+
31+
```math
32+
\mathop{\mathrm{RSS}} = \sum_{i=0}^{n-1} (y_i - x_i)^2
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{RSS} = \sum_{i=0}^{n-1} (y_i - x_i)^2" data-equation="eq:residual_sum_of_squares">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@78799028a150a44d463029bdb62ac870b1c1f9d4/lib/node_modules/@stdlib/stats/incr/rss/docs/img/equation_residual_sum_of_squares.svg" alt="Equation for the residual sum of squares.">
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 incrnanrss = require( '@stdlib/stats/incr/nanrss' );
52+
```
53+
54+
#### incrnanrss()
55+
56+
Returns an accumulator `function` which incrementally computes the [residual sum of squares][residual-sum-of-squares], ignoring NaN values.
57+
58+
```javascript
59+
var accumulator = incrnanrss();
60+
```
61+
62+
#### accumulator( \[x, y] )
63+
64+
If provided input values x and y, the accumulator function updates the RSS. If NaN is provided, it is ignored, and the previous RSS is returned. If no arguments are provided, the accumulator function returns the current RSS.
65+
66+
```javascript
67+
var accumulator = incrnanrss();
68+
69+
var r = accumulator( 2.0, 3.0 );
70+
// returns 1.0
71+
72+
r = accumulator( -1.0, -4.0 );
73+
// returns 10.0
74+
75+
r = accumulator( -3.0, 5.0 );
76+
// returns 74.0
77+
78+
r = accumulator( NaN, 3.0 );
79+
// returns 74.0
80+
81+
r = accumulator();
82+
// returns 74.0
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var randu = require( '@stdlib/random/base/randu' );
107+
var incrnanrss = require( '@stdlib/stats/incr/nanrss' );
108+
109+
var accumulator;
110+
var v1;
111+
var v2;
112+
var i;
113+
114+
// Initialize an accumulator:
115+
accumulator = incrnanrss();
116+
117+
// For each simulated datum, update the residual sum of squares...
118+
for ( i = 0; i < 100; i++ ) {
119+
v1 = ( randu()*100.0 ) - 50.0;
120+
v2 = ( randu()*100.0 ) - 50.0;
121+
accumulator( v1, v2 );
122+
123+
// NaN values are ignored
124+
accumulator( NaN, v2 );
125+
}
126+
console.log( accumulator() );
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
134+
135+
<section class="related">
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
[residual-sum-of-squares]: https://en.wikipedia.org/wiki/Residual_sum_of_squares
145+
146+
<!-- <related-links> -->
147+
148+
<!-- </related-links> -->
149+
150+
</section>
151+
152+
<!-- /.links -->
153+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 pkg = require( './../package.json' ).name;
26+
var incrnanrss = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanrss();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanrss();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
if ( i%10 === 0 ) {
59+
v = acc( NaN, randu()-0.5 );
60+
} else {
61+
v = acc( randu()-0.5, randu()-0.5 );
62+
}
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( v !== v ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});

lib/node_modules/@stdlib/stats/incr/nanrss/docs/img/equation_residual_sum_of_squares.svg

Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes the residual
4+
sum of squares (RSS), ignoring `NaN` values.
5+
6+
If provided input values, the accumulator function returns an updated
7+
residual sum of squares. If not provided input values, the accumulator
8+
function returns the current residual sum of squares.
9+
10+
NaN input values are ignored.
11+
12+
Returns
13+
-------
14+
acc: Function
15+
Accumulator function.
16+
17+
Examples
18+
--------
19+
> var accumulator = {{alias}}();
20+
> var r = accumulator()
21+
null
22+
> r = accumulator( 2.0, 3.0 )
23+
1.0
24+
> r = accumulator( NaN, 3.0 )
25+
1.0
26+
> r = accumulator( -5.0, 2.0 )
27+
50.0
28+
> r = accumulator()
29+
50.0
30+
31+
See Also
32+
--------
33+
34+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided arguments, returns an updated residual sum of squares; otherwise, returns the current residual sum of squares.
25+
*
26+
* ## Notes
27+
*
28+
* NaN input values are ignored.
29+
* @returns residual sum of squares
30+
*/
31+
type accumulator = ( x?: number, y?: number ) => number | null;
32+
33+
/**
34+
* Returns an accumulator function which incrementally computes the residual sum of squares, ignoring `NaN` values.
35+
*
36+
* @returns accumulator function
37+
*
38+
* @example
39+
* var accumulator = incrnanrss();
40+
*
41+
* var r = accumulator();
42+
* // returns null
43+
*
44+
* r = accumulator( 2.0, 3.0 );
45+
* // returns 1.0
46+
*
47+
* r = accumulator( -5.0, 2.0 );
48+
* // returns 50.0
49+
*
50+
* r = accumulator();
51+
* // returns 50.0
52+
* r = accumulator( NaN, 3.0 );
53+
* // 50.0
54+
* r = accumulator( 5.0, NaN );
55+
* // 50.0
56+
*/
57+
declare function incrnanrss(): accumulator;
58+
59+
60+
// EXPORTS //
61+
62+
export = incrnanrss;
63+

0 commit comments

Comments
 (0)