-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add stats/base/ndarray/sstdevwd
#9565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
c6b806f
1338cc6
25e44f6
5f4c4ba
5c231c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # sstdevwd | ||
|
|
||
| > Calculate the [standard deviation][standard-deviation] of a one-dimensional single-precision floating-point ndarray using Welford's algorithm. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The population [standard deviation][standard-deviation] of a finite size population of size `N` is given by | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \sigma = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2} | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/base/ndarray/sstdevwd/docs/img/equation_population_standard_deviation.svg" alt="Equation for the population standard deviation."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where the population mean is given by | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/base/ndarray/sstdevwd/docs/img/equation_population_mean.svg" alt="Equation for the population mean."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| 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`, | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2} | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/base/ndarray/sstdevwd/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for computing a corrected sample standard deviation."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where the sample mean is given by | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/base/ndarray/sstdevwd/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var sstdevwd = require( '@stdlib/stats/base/ndarray/sstdevwd' ); | ||
| ``` | ||
|
|
||
| #### sstdevwd( arrays ) | ||
|
|
||
| Computes the [standard deviation][standard-deviation] of a one-dimensional single-precision floating-point ndarray. | ||
|
|
||
| ```javascript | ||
| var Float32Array = require( '@stdlib/array/float32' ); | ||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
| var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
|
|
||
| var opts = { | ||
| 'dtype': 'float32' | ||
| }; | ||
|
|
||
| var xbuf = new Float32Array( [ 1.0, -2.0, 2.0 ] ); | ||
| var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); | ||
| var correction = scalar2ndarray( 1.0, opts ); | ||
|
|
||
| var v = sstdevwd( [ x, correction ] ); | ||
| // returns ~2.0817 | ||
| ``` | ||
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **arrays**: array-like object containing two elements: a one-dimensional input ndarray and a zero-dimensional ndarray specifying the degrees of freedom adjustment. Providing a non-zero degrees of freedom adjustment has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `N` is the number of elements in the input ndarray 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 corrected 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). | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - If provided an empty one-dimensional ndarray, the function returns `NaN`. | ||
| - If `N - c` is less than or equal to `0` (where `N` corresponds to the number of elements in the input ndarray and `c` corresponds to the provided degrees of freedom adjustment), the function returns `NaN`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
| var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
| var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
| var sstdevwd = require( '@stdlib/stats/base/ndarray/sstdevwd' ); | ||
|
|
||
| var opts = { | ||
| 'dtype': 'float32' | ||
| }; | ||
|
|
||
| var xbuf = discreteUniform( 10, -50, 50, opts ); | ||
| var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); | ||
| console.log( ndarray2array( x ) ); | ||
|
|
||
| var correction = scalar2ndarray( 1.0, opts ); | ||
| var v = sstdevwd( [ x, correction ] ); | ||
| console.log( v ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,106 @@ | ||||||
| /** | ||||||
| * @license Apache-2.0 | ||||||
| * | ||||||
| * Copyright (c) 2026 The Stdlib Authors. | ||||||
| * | ||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| * you may not use this file except in compliance with the License. | ||||||
| * You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| * See the License for the specific language governing permissions and | ||||||
| * limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| 'use strict'; | ||||||
|
|
||||||
| // MODULES // | ||||||
|
|
||||||
| var bench = require( '@stdlib/bench' ); | ||||||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||||||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||||||
|
||||||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | |
| var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be isnanf to match the single-precision import above.
| if ( isnan( v ) ) { | |
| if ( isnanf( v ) ) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here - use isnanf for consistency.
| if ( isnan( v ) ) { | |
| if ( isnanf( v ) ) { |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||||||
|
|
||||||||||
| {{alias}}( arrays ) | ||||||||||
| Computes the standard deviation of a one-dimensional single-precision | ||||||||||
| floating-point ndarray. | ||||||||||
|
||||||||||
| Computes the standard deviation of a one-dimensional single-precision | |
| floating-point ndarray. | |
| Computes the standard deviation of a one-dimensional single-precision | |
| floating-point ndarray using Welford's algorithm. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function description should include "using Welford's algorithm" to be consistent with the H1 title (line 23) and the JSDoc in
lib/main.js(line 34).