Skip to content

Commit 6526e28

Browse files
committed
Auto-generated commit
1 parent 42d31e9 commit 6526e28

File tree

12 files changed

+998
-0
lines changed

12 files changed

+998
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`d687dfb`](https://github.com/stdlib-js/stdlib/commit/d687dfb2c5fa089b942f3a56ad668ae15ea4eec3) - add `stats/base/ndarray/sstdevch` [(#9383)](https://github.com/stdlib-js/stdlib/pull/9383)
1314
- [`9e89946`](https://github.com/stdlib-js/stdlib/commit/9e89946832d57bb62985856154032f79be7ec695) - add `stats/base/ndarray/stdevwd` [(#9384)](https://github.com/stdlib-js/stdlib/pull/9384)
1415
- [`e88f80d`](https://github.com/stdlib-js/stdlib/commit/e88f80dbfd6514e9949f7b523e703a962c780273) - add `stats/strided/dnanmidrange` [(#9374)](https://github.com/stdlib-js/stdlib/pull/9374)
1516
- [`65784f7`](https://github.com/stdlib-js/stdlib/commit/65784f785748107e1fc5d84ad75c9778422e8442) - add `stats/strided/midrange-by` [(#9328)](https://github.com/stdlib-js/stdlib/pull/9328)
@@ -3535,6 +3536,7 @@ A total of 560 issues were closed in this release:
35353536

35363537
<details>
35373538

3539+
- [`d687dfb`](https://github.com/stdlib-js/stdlib/commit/d687dfb2c5fa089b942f3a56ad668ae15ea4eec3) - **feat:** add `stats/base/ndarray/sstdevch` [(#9383)](https://github.com/stdlib-js/stdlib/pull/9383) _(by Kaustubh Patange, Athan Reines)_
35383540
- [`9e89946`](https://github.com/stdlib-js/stdlib/commit/9e89946832d57bb62985856154032f79be7ec695) - **feat:** add `stats/base/ndarray/stdevwd` [(#9384)](https://github.com/stdlib-js/stdlib/pull/9384) _(by Pratik)_
35393541
- [`7f51132`](https://github.com/stdlib-js/stdlib/commit/7f511325dacfffc581b49bd6d3d4c5a035c167d8) - **docs:** improve doctests for ndarray instances in `stats/maxsorted` [(#9385)](https://github.com/stdlib-js/stdlib/pull/9385) _(by Shivam Mittal, Athan Reines)_
35403542
- [`e88f80d`](https://github.com/stdlib-js/stdlib/commit/e88f80dbfd6514e9949f7b523e703a962c780273) - **feat:** add `stats/strided/dnanmidrange` [(#9374)](https://github.com/stdlib-js/stdlib/pull/9374) _(by Sachin Pangal, Athan Reines)_

base/ndarray/sstdevch/README.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
# sstdevch
22+
23+
> Calculate the [standard deviation][standard-deviation] of a one-dimensional single-precision floating-point ndarray 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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/sstdevch/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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/sstdevch/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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/sstdevch/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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/sstdevch/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 sstdevch = require( '@stdlib/stats/base/ndarray/sstdevch' );
99+
```
100+
101+
#### sstdevch( arrays )
102+
103+
Computes the [standard deviation][standard-deviation] of a one-dimensional single-precision floating-point ndarray using a one-pass trial mean algorithm.
104+
105+
```javascript
106+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
107+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
108+
var Float32Array = require( '@stdlib/array/float32' );
109+
110+
var opts = {
111+
'dtype': 'float32'
112+
};
113+
114+
var xbuf = new Float32Array( [ 1.0, -2.0, 2.0 ] );
115+
var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
116+
var correction = scalar2ndarray( 1.0, opts );
117+
118+
var v = sstdevch( [ x, correction ] );
119+
// returns ~2.0817
120+
```
121+
122+
The function has the following parameters:
123+
124+
- **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).
125+
126+
</section>
127+
128+
<!-- /.usage -->
129+
130+
<section class="notes">
131+
132+
## Notes
133+
134+
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
135+
- 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`.
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<section class="examples">
142+
143+
## Examples
144+
145+
<!-- eslint no-undef: "error" -->
146+
147+
```javascript
148+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
149+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
150+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
151+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
152+
var sstdevch = require( '@stdlib/stats/base/ndarray/sstdevch' );
153+
154+
var opts = {
155+
'dtype': 'float32'
156+
};
157+
158+
var xbuf = discreteUniform( 10, -50, 50, opts );
159+
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
160+
console.log( ndarray2array( x ) );
161+
162+
var correction = scalar2ndarray( 1.0, opts );
163+
var v = sstdevch( [ x, correction ] );
164+
console.log( v );
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
* * *
172+
173+
<section class="references">
174+
175+
## References
176+
177+
- Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a].
178+
- Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154][@ling:1974a].
179+
- Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. 1983. "Algorithms for Computing the Sample Variance: Analysis and Recommendations." _The American Statistician_ 37 (3). American Statistical Association, Taylor & Francis, Ltd.: 242–47. doi:[10.1080/00031305.1983.10483115][@chan:1983a].
180+
- Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036][@schubert:2018a].
181+
182+
</section>
183+
184+
<!-- /.references -->
185+
186+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
187+
188+
<section class="related">
189+
190+
</section>
191+
192+
<!-- /.related -->
193+
194+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="links">
197+
198+
[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
199+
200+
[@neely:1966a]: https://doi.org/10.1145/365719.365958
201+
202+
[@ling:1974a]: https://doi.org/10.2307/2286154
203+
204+
[@chan:1983a]: https://doi.org/10.1080/00031305.1983.10483115
205+
206+
[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036
207+
208+
</section>
209+
210+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var sstdevch = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float32'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var correction;
52+
var xbuf;
53+
var x;
54+
55+
xbuf = uniform( len, -10.0, 10.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
correction = scalar2ndarray( 1.0, options );
58+
59+
return benchmark;
60+
61+
function benchmark( b ) {
62+
var v;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = sstdevch( [ x, correction ] );
68+
if ( isnanf( v ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnanf( v ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s::len=%d', pkg, len ), f );
103+
}
104+
}
105+
106+
main();
Lines changed: 43 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)