Skip to content

Commit 6414789

Browse files
committed
[RFC]: add stats/incr/nanrss
1 parent 4748a5a commit 6414789

File tree

11 files changed

+832
-0
lines changed

11 files changed

+832
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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/nanrss/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 returns an updated [residual sum of squares][residual-sum-of-squares]. If not provided input values `x` and `y`, the accumulator function returns the current [residual sum of squares][residual-sum-of-squares].
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, NaN );
73+
// returns 1.0
74+
75+
r = accumulator( -1.0, -4.0 );
76+
// returns 10.0
77+
78+
r = accumulator( -3.0, 5.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 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+
if ( randu() < 0.2 ) {
120+
v1 = NaN;
121+
v2 = NaN;
122+
} else {
123+
v1 = ( randu()*100.0 ) - 50.0;
124+
v2 = ( randu()*100.0 ) - 50.0;
125+
}
126+
accumulator( v1, v2 );
127+
}
128+
console.log( accumulator() );
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
* * *
140+
141+
## See Also
142+
143+
- <span class="package-name">[`@stdlib/stats/incr/mrss`][@stdlib/stats/incr/mrss]</span><span class="delimiter">: </span><span class="description">compute a moving residual sum of squares (RSS) incrementally.</span>
144+
- <span class="package-name">[`@stdlib/stats/incr/mse`][@stdlib/stats/incr/mse]</span><span class="delimiter">: </span><span class="description">compute the mean squared error (MSE) incrementally.</span>
145+
- <span class="package-name">[`@stdlib/stats/incr/rmse`][@stdlib/stats/incr/rmse]</span><span class="delimiter">: </span><span class="description">compute the root mean squared error (RMSE) incrementally.</span>
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[residual-sum-of-squares]: https://en.wikipedia.org/wiki/Residual_sum_of_squares
156+
157+
<!-- <related-links> -->
158+
159+
[@stdlib/stats/incr/mrss]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mrss
160+
161+
[@stdlib/stats/incr/mse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mse
162+
163+
[@stdlib/stats/incr/rmse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/rmse
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
v = acc( randu()-0.5, randu()-0.5 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
Returns
11+
-------
12+
acc: Function
13+
Accumulator function.
14+
15+
Examples
16+
--------
17+
> var accumulator = {{alias}}();
18+
> var r = accumulator()
19+
null
20+
> r = accumulator( 2.0, 3.0 )
21+
1.0
22+
> r = accumulator( 1.0, NaN )
23+
1.0
24+
> r = accumulator( -5.0, 2.0 )
25+
50.0
26+
> r = accumulator()
27+
50.0
28+
29+
See Also
30+
--------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
* @param x - value
27+
* @param y - value
28+
* @returns residual sum of squares
29+
*/
30+
type accumulator = ( x?: number, y?: number ) => number | null;
31+
32+
/**
33+
* Returns an accumulator function which incrementally computes the residual sum of squares.
34+
*
35+
* @returns accumulator function
36+
*
37+
* @example
38+
* var accumulator = incrnanrss();
39+
*
40+
* var r = accumulator();
41+
* // returns null
42+
*
43+
* r = accumulator( 2.0, 3.0 );
44+
* // returns 1.0
45+
*
46+
* r = accumulator( 1.0, NaN );
47+
* // returns 1.0
48+
*
49+
* r = accumulator( -5.0, 2.0 );
50+
* // returns 50.0
51+
*
52+
* r = accumulator();
53+
* // returns 50.0
54+
*/
55+
declare function incrnanrss(): accumulator;
56+
57+
58+
// EXPORTS //
59+
60+
export = incrnanrss;

0 commit comments

Comments
 (0)