|
| 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 | +# incrnanstdev |
| 22 | + |
| 23 | +> Compute a [corrected sample standard deviation][sample-stdev] incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [corrected sample standard deviation][sample-stdev] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}" alt="Equation for the corrected sample standard deviation."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}" data-equation="eq:corrected_sample_standard_deviation"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for the corrected sample standard deviation."> |
| 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 incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### incrnanstdev( \[mean] ) |
| 55 | + |
| 56 | +Returns an accumulator function which incrementally computes a [corrected sample standard deviation][sample-stdev], ignoring `NaN` values. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var accumulator = incrnanstdev(); |
| 60 | +``` |
| 61 | + |
| 62 | +If the mean is already known, provide a `mean` argument. |
| 63 | + |
| 64 | +```javascript |
| 65 | +var accumulator = incrnanstdev( 3.0 ); |
| 66 | +``` |
| 67 | + |
| 68 | +#### accumulator( \[x] ) |
| 69 | + |
| 70 | +If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][sample-stdev]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][sample-stdev]. |
| 71 | + |
| 72 | +```javascript |
| 73 | +var accumulator = incrnanstdev(); |
| 74 | + |
| 75 | +var s = accumulator( 2.0 ); |
| 76 | +// returns 0.0 |
| 77 | + |
| 78 | +s = accumulator( 1.0 ); // => sqrt(((2-1.5)^2+(1-1.5)^2) / (2-1)) |
| 79 | +// returns ~0.7071 |
| 80 | + |
| 81 | +s = accumulator( 3.0 ); // => sqrt(((2-2)^2+(1-2)^2+(3-2)^2) / (3-1)) |
| 82 | +// returns 1.0 |
| 83 | + |
| 84 | +s = accumulator( NaN ); |
| 85 | +// returns 1.0 |
| 86 | + |
| 87 | +s = accumulator(); |
| 88 | +// returns 1.0 |
| 89 | +``` |
| 90 | + |
| 91 | +</section> |
| 92 | + |
| 93 | +<!-- /.usage --> |
| 94 | + |
| 95 | +<section class="notes"> |
| 96 | + |
| 97 | +## Notes |
| 98 | + |
| 99 | +- 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. |
| 100 | + |
| 101 | +</section> |
| 102 | + |
| 103 | +<!-- /.notes --> |
| 104 | + |
| 105 | +<section class="examples"> |
| 106 | + |
| 107 | +## Examples |
| 108 | + |
| 109 | +<!-- eslint no-undef: "error" --> |
| 110 | + |
| 111 | +```javascript |
| 112 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 113 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 114 | +var incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); |
| 115 | + |
| 116 | +// Initialize an accumulator: |
| 117 | +var accumulator = incrnanstdev(); |
| 118 | + |
| 119 | +// For each simulated datum, update the sample standard deviation... |
| 120 | +var i; |
| 121 | +for ( i = 0; i < 100; i++ ) { |
| 122 | + accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ) ); |
| 123 | +} |
| 124 | +console.log( accumulator() ); |
| 125 | +``` |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.examples --> |
| 130 | + |
| 131 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 132 | + |
| 133 | +<section class="related"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.related --> |
| 138 | + |
| 139 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 140 | + |
| 141 | +<section class="links"> |
| 142 | + |
| 143 | +[sample-stdev]: https://en.wikipedia.org/wiki/Standard_deviation |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.links --> |
0 commit comments