|
| 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 --> |
0 commit comments