|
| 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 | +# nanstdevch |
| 22 | + |
| 23 | +> Calculate the [standard deviation][standard-deviation] of an array ignoring `NaN` values and using a one-pass trial mean algorithm. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The population [standard deviation][standard-deviation] of a finite size population of size `N` is given by |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:population_standard_deviation" align="center" raw="\sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2}" alt="Equation for the population standard deviation."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2}" data-equation="eq:population_standard_deviation"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e592576989e92b8def74a5fbac9109f3a81f16f9/lib/node_modules/@stdlib/stats/base/nanstdevch/docs/img/equation_population_standard_deviation.svg" alt="Equation for the population standard deviation."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where the population mean is given by |
| 43 | + |
| 44 | +<!-- <equation class="equation" label="eq:population_mean" align="center" raw="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" alt="Equation for the population mean."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i |
| 48 | +``` |
| 49 | + |
| 50 | +<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e592576989e92b8def74a5fbac9109f3a81f16f9/lib/node_modules/@stdlib/stats/base/nanstdevch/docs/img/equation_population_mean.svg" alt="Equation for the population mean."> |
| 52 | + <br> |
| 53 | +</div> --> |
| 54 | + |
| 55 | +<!-- </equation> --> |
| 56 | + |
| 57 | +Often in the analysis of data, the true population [standard deviation][standard-deviation] is not known _a priori_ and must be estimated from a sample drawn from the population distribution. If one attempts to use the formula for the population [standard deviation][standard-deviation], the result is biased and yields an **uncorrected sample standard deviation**. To compute a **corrected sample standard deviation** for a sample of size `n`, |
| 58 | + |
| 59 | +<!-- <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 computing a corrected sample standard deviation."> --> |
| 60 | + |
| 61 | +```math |
| 62 | +s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2} |
| 63 | +``` |
| 64 | + |
| 65 | +<!-- <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"> |
| 66 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e592576989e92b8def74a5fbac9109f3a81f16f9/lib/node_modules/@stdlib/stats/base/nanstdevch/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for computing a corrected sample standard deviation."> |
| 67 | + <br> |
| 68 | +</div> --> |
| 69 | + |
| 70 | +<!-- </equation> --> |
| 71 | + |
| 72 | +where the sample mean is given by |
| 73 | + |
| 74 | +<!-- <equation class="equation" label="eq:sample_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the sample mean."> --> |
| 75 | + |
| 76 | +```math |
| 77 | +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i |
| 78 | +``` |
| 79 | + |
| 80 | +<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:sample_mean"> |
| 81 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e592576989e92b8def74a5fbac9109f3a81f16f9/lib/node_modules/@stdlib/stats/base/nanstdevch/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean."> |
| 82 | + <br> |
| 83 | +</div> --> |
| 84 | + |
| 85 | +<!-- </equation> --> |
| 86 | + |
| 87 | +The use of the term `n-1` is commonly referred to as Bessel's correction. Note, however, that applying Bessel's correction can increase the mean squared error between the sample standard deviation and population standard deviation. Depending on the characteristics of the population distribution, other correction factors (e.g., `n-1.5`, `n+1`, etc) can yield better estimators. |
| 88 | + |
| 89 | +</section> |
| 90 | + |
| 91 | +<!-- /.intro --> |
| 92 | + |
| 93 | +<section class="usage"> |
| 94 | + |
| 95 | +## Usage |
| 96 | + |
| 97 | +```javascript |
| 98 | +var nanstdevch = require( '@stdlib/stats/array/nanstdevch' ); |
| 99 | +``` |
| 100 | + |
| 101 | +#### nanstdevch( x\[, correction] ) |
| 102 | + |
| 103 | +Computes the [standard deviation][standard-deviation] of an array ignoring `NaN` values and using a one-pass trial mean algorithm. |
| 104 | + |
| 105 | +```javascript |
| 106 | +var x = [ 1.0, -2.0, NaN, 2.0 ]; |
| 107 | + |
| 108 | +var v = nanstdevch( x ); |
| 109 | +// returns ~2.0817 |
| 110 | +``` |
| 111 | + |
| 112 | +The function has the following parameters: |
| 113 | + |
| 114 | +- **x**: input array. |
| 115 | +- **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `N` corresponds to the number of non-`NaN` array elements and `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). Default: `1.0`. |
| 116 | + |
| 117 | +By default, the function computes the sample [standard deviation][standard-deviation]. To adjust the degrees of freedom when computing the [standard deviation][standard-deviation], provide a `correction` argument. |
| 118 | + |
| 119 | +```javascript |
| 120 | +var x = [ 1.0, -2.0, NaN, 2.0 ]; |
| 121 | + |
| 122 | +var v = nanstdevch( x, 0.0 ); |
| 123 | +// returns ~1.6997 |
| 124 | +``` |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.usage --> |
| 129 | + |
| 130 | +<section class="notes"> |
| 131 | + |
| 132 | +## Notes |
| 133 | + |
| 134 | +- If provided an empty array, the function returns `NaN`. |
| 135 | +- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `N` corresponds to the number of non-`NaN` array elements), the function returns `NaN`. |
| 136 | +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 137 | +- The underlying algorithm is a specialized case of Neely's two-pass algorithm. As the standard deviation is invariant with respect to changes in the location parameter, the underlying algorithm uses the first array element as a trial mean to shift subsequent data values and thus mitigate catastrophic cancellation. Accordingly, the algorithm's accuracy is best when data is **unordered** (i.e., the data is **not** sorted in either ascending or descending order such that the first value is an "extreme" value). |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.notes --> |
| 142 | + |
| 143 | +<section class="examples"> |
| 144 | + |
| 145 | +## Examples |
| 146 | + |
| 147 | +<!-- eslint no-undef: "error" --> |
| 148 | + |
| 149 | +```javascript |
| 150 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 151 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 152 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 153 | +var nanstdevch = require( '@stdlib/stats/array/nanstdevch' ); |
| 154 | + |
| 155 | +function rand() { |
| 156 | + if ( bernoulli( 0.8 ) < 1 ) { |
| 157 | + return NaN; |
| 158 | + } |
| 159 | + return uniform( -50.0, 50.0 ); |
| 160 | +} |
| 161 | + |
| 162 | +var x = filledarrayBy( 10, 'generic', rand ); |
| 163 | +console.log( x ); |
| 164 | + |
| 165 | +var v = nanstdevch( x ); |
| 166 | +console.log( v ); |
| 167 | +``` |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.examples --> |
| 172 | + |
| 173 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 174 | + |
| 175 | +<section class="related"> |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.related --> |
| 180 | + |
| 181 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 182 | + |
| 183 | +<section class="links"> |
| 184 | + |
| 185 | +[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation |
| 186 | + |
| 187 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 188 | + |
| 189 | +</section> |
| 190 | + |
| 191 | +<!-- /.links --> |
0 commit comments