Skip to content

Commit 1c7d46a

Browse files
committed
done
1 parent e4b5c9d commit 1c7d46a

File tree

10 files changed

+889
-0
lines changed

10 files changed

+889
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
# incrnanmrss
22+
23+
> Compute a moving [residual sum of squares][residual-sum-of-squares] (RSS) incrementally, ignoring `NaN` calues.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, 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}^{W-1} (y_i - x_i)^2" alt="Equation for the residual sum of squares."> -->
30+
31+
```math
32+
\mathop{\mathrm{RSS}} = \sum_{i=0}^{W-1} (y_i - x_i)^2
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{RSS} = \sum_{i=0}^{W-1} (y_i - x_i)^2" data-equation="eq:residual_sum_of_squares">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@df44d1456cc422c38a368a47b586b7eaffb19cc8/lib/node_modules/@stdlib/stats/incr/mrss/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 incrnanmrss = require( '@stdlib/stats/incr/nanmrss' );
52+
```
53+
54+
#### incrnanmrss( window )
55+
56+
Returns an accumulator `function` which incrementally computes a moving [residual sum of squares][residual-sum-of-squares]. The `window` parameter defines the number of values over which to compute the moving [residual sum of squares][residual-sum-of-squares].
57+
58+
```javascript
59+
var accumulator = incrnanmrss( 3 );
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 = incrnanmrss( 3 );
68+
69+
var r = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
r = accumulator( 2.0, 3.0 ); // [(2.0,3.0)]
74+
// returns 1.0
75+
76+
// Fill the window...
77+
r = accumulator( NaN, 3.0 ); // [(2.0,3.0), ( NaN, 3.0)]
78+
// returns 1.0
79+
80+
// Fill the window...
81+
r = accumulator( 2.0, NaN ); // [(2.0,3.0), ( NaN, 3.0), ( 2.0, NaN )]
82+
// returns 1.0
83+
84+
// Fill the window...
85+
r = accumulator( NaN, NaN ); // [(2.0,3.0)]
86+
// returns 1.0
87+
88+
r = accumulator( -1.0, 4.0 ); // [(2.0,3.0), (-1.0,4.0)]
89+
// returns 26.0
90+
91+
r = accumulator( 3.0, 9.0 ); // [(2.0,3.0), (-1.0,4.0), (3.0,9.0)]
92+
// returns 62.0
93+
94+
// Window begins sliding...
95+
r = accumulator( -7.0, 3.0 ); // [(-1.0,4.0), (3.0,9.0), (-7.0,3.0)]
96+
// returns 161.0
97+
98+
r = accumulator( -5.0, -3.0 ); // [(3.0,9.0), (-7.0,3.0), (-5.0,-3.0)]
99+
// returns 140.0
100+
101+
r = accumulator();
102+
// returns 140.0
103+
```
104+
105+
</section>
106+
107+
<!-- /.usage -->
108+
109+
<section class="notes">
110+
111+
## Notes
112+
113+
- 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.
114+
- As `W` (x,y) pairs 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.
115+
116+
</section>
117+
118+
<!-- /.notes -->
119+
120+
<section class="examples">
121+
122+
## Examples
123+
124+
<!-- eslint no-undef: "error" -->
125+
126+
```javascript
127+
var uniform = require( '@stdlib/random/base/uniform' );
128+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
129+
var incrnanmrss = require( '@stdlib/stats/incr/nanmrss' );
130+
131+
var accumulator;
132+
var v1;
133+
var v2;
134+
var i;
135+
136+
// Initialize an accumulator:
137+
accumulator = incrnanmrss( 5 );
138+
139+
// For each simulated datum, update the moving residual sum of squares...
140+
for ( i = 0; i < 100; i++ ) {
141+
v1 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 );
142+
v2 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 );
143+
accumulator( v1, v2 );
144+
}
145+
console.log( accumulator() );
146+
```
147+
148+
</section>
149+
150+
<!-- /.examples -->
151+
152+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
153+
154+
<section class="related">
155+
156+
* * *
157+
158+
## See Also
159+
160+
- <span class="package-name">[`@stdlib/stats/incr/rss`][@stdlib/stats/incr/rss]</span><span class="delimiter">: </span><span class="description">compute the residual sum of squares (RSS) incrementally.</span>
161+
- <span class="package-name">[`@stdlib/stats/incr/mmse`][@stdlib/stats/incr/mmse]</span><span class="delimiter">: </span><span class="description">compute a moving mean squared error (MSE) incrementally.</span>
162+
- <span class="package-name">[`@stdlib/stats/incr/mrmse`][@stdlib/stats/incr/mrmse]</span><span class="delimiter">: </span><span class="description">compute a moving root mean squared error (RMSE) incrementally.</span>
163+
164+
</section>
165+
166+
<!-- /.related -->
167+
168+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
169+
170+
<section class="links">
171+
172+
[residual-sum-of-squares]: https://en.wikipedia.org/wiki/Residual_sum_of_squares
173+
174+
<!-- <related-links> -->
175+
176+
[@stdlib/stats/incr/rss]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/rss
177+
178+
[@stdlib/stats/incr/mmse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmse
179+
180+
[@stdlib/stats/incr/mrmse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mrmse
181+
182+
<!-- </related-links> -->
183+
184+
</section>
185+
186+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
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 incrnanmrss = 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 = incrnanmrss( (i%5)+1 );
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 = incrnanmrss( 5 );
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+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
residual sum of squares (RSS), ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving residual sum of squares.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
residual sum of squares. If not provided a value, the accumulator function
11+
returns the current moving residual sum of squares.
12+
13+
As `W` values are needed to fill the window buffer, the first `W-1` returned
14+
values are calculated from smaller sample sizes. Until the window is full,
15+
each returned value is calculated from all provided values.
16+
17+
Parameters
18+
----------
19+
W: integer
20+
Window size.
21+
22+
Returns
23+
-------
24+
acc: Function
25+
Accumulator function.
26+
27+
Examples
28+
--------
29+
> var accumulator = {{alias}}( 3 );
30+
> var r = accumulator()
31+
null
32+
> r = accumulator( 2.0, 3.0 )
33+
1.0
34+
> r = accumulator( NaN, 3.0 )
35+
1.0
36+
> r = accumulator( 2.0, NaN )
37+
1.0
38+
> r = accumulator( NaN, NaN )
39+
1.0
40+
> r = accumulator( -5.0, 2.0 )
41+
50.0
42+
> r = accumulator( 3.0, 2.0 )
43+
51.0
44+
> r = accumulator( 5.0, -2.0 )
45+
99.0
46+
> r = accumulator()
47+
99.0
48+
49+
See Also
50+
--------
51+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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, ignoring `NaN` values.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
29+
*
30+
* @param x - value
31+
* @param y - value
32+
* @returns residual sum of squares
33+
*/
34+
type accumulator = ( x?: number, y?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes a moving residual sum of squares, ignoring `NaN` values.
38+
*
39+
* ## Notes
40+
*
41+
* - The `W` parameter defines the number of values over which to compute the moving residual sum of squares.
42+
* - 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.
43+
*
44+
* @param W - window size
45+
* @throws must provide a positive integer
46+
* @returns accumulator function
47+
*
48+
* @example
49+
* var accumulator = incrmrss( 3 );
50+
*
51+
* var r = accumulator();
52+
* // returns null
53+
*
54+
* r = accumulator( 2.0, 3.0 );
55+
* // returns 1.0
56+
*
57+
* r = accumulator( NaN, NaN );
58+
* // returns 1.0
59+
*
60+
* r = accumulator( 2.0, NaN );
61+
* // returns 1.0
62+
*
63+
* r = accumulator( NaN, 3.0 );
64+
* // returns 1.0
65+
*
66+
* r = accumulator( -5.0, 2.0 );
67+
* // returns 50.0
68+
*
69+
* r = accumulator( 3.0, 2.0 );
70+
* // returns 51.0
71+
*
72+
* r = accumulator( 5.0, -2.0 );
73+
* // returns 99.0
74+
*
75+
* r = accumulator();
76+
* // returns 99.0
77+
*/
78+
declare function incrnanmrss( W: number ): accumulator;
79+
80+
81+
// EXPORTS //
82+
83+
export = incrnanmrss;

0 commit comments

Comments
 (0)