Skip to content

Commit 56e7962

Browse files
committed
feat: stats/base/ndarray/dnanstdevyc
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent e53e179 commit 56e7962

File tree

11 files changed

+1035
-0
lines changed

11 files changed

+1035
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# dnanstdevyc
22+
23+
> Calculate the [standard deviation][standard-deviation] of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a one-pass algorithm proposed by Youngs and Cramer.
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/stdev/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/stdev/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/stdev/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/stdev/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 dnanstdevyc = require( '@stdlib/stats/base/ndarray/dnanstdevyc' );
99+
```
100+
101+
#### dnanstdevyc( arrays )
102+
103+
Computes the [standard deviation][standard-deviation] of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values and using a one-pass algorithm proposed by Youngs and Cramer.
104+
105+
```javascript
106+
var Float64Array = require( '@stdlib/array/float64' );
107+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
108+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
109+
110+
var opts = {
111+
'dtype': 'float64'
112+
};
113+
114+
var xbuf = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
115+
var x = new ndarray( opts.dtype, xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
116+
var correction = scalar2ndarray( 1.0, opts );
117+
118+
var v = dnanstdevyc( [ 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 non-NaN 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 non-NaN elements in the input ndarray and `c` corresponds to the provided degrees of freedom adjustment), the function returns `NaN`.
136+
- The function ignores `NaN` values when computing the standard deviation.
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<section class="examples">
143+
144+
## Examples
145+
146+
<!-- eslint no-undef: "error" -->
147+
148+
```javascript
149+
var uniform = require( '@stdlib/random/base/uniform' );
150+
var filledarrayBy = require( '@stdlib/array/filled-by' );
151+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
152+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
153+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
154+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
155+
var dnanstdevyc = require( '@stdlib/stats/base/ndarray/dnanstdevyc' );
156+
157+
function rand() {
158+
if ( bernoulli( 0.8 ) < 1 ) {
159+
return NaN;
160+
}
161+
return uniform( -50.0, 50.0 );
162+
}
163+
164+
var opts = {
165+
'dtype': 'float64'
166+
};
167+
168+
var xbuf = filledarrayBy( 10, 'float64', rand );
169+
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
170+
console.log( ndarray2array( x ) );
171+
172+
var correction = scalar2ndarray( 1.0, opts );
173+
var v = dnanstdevyc( [ x, correction ] );
174+
console.log( v );
175+
```
176+
177+
</section>
178+
179+
<!-- /.examples -->
180+
181+
* * *
182+
183+
<section class="references">
184+
185+
## References
186+
187+
- Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826][@youngs:1971a].
188+
189+
</section>
190+
191+
<!-- /.references -->
192+
193+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
194+
195+
<section class="related">
196+
197+
</section>
198+
199+
<!-- /.related -->
200+
201+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
202+
203+
<section class="links">
204+
205+
[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
206+
207+
[@youngs:1971a]: https://doi.org/10.1080/00401706.1971.10488826
208+
209+
</section>
210+
211+
<!-- /.links -->
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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/base/uniform' );
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
31+
var format = require( '@stdlib/string/format' );
32+
var pkg = require( './../package.json' ).name;
33+
var dnanstdevyc = require( './../lib' );
34+
35+
36+
// VARIABLES //
37+
38+
var options = {
39+
'dtype': 'float64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Returns a random value, possibly NaN.
47+
*
48+
* @private
49+
* @returns {number} random value or `NaN`
50+
*/
51+
function rand() {
52+
if ( bernoulli( 0.8 ) < 1 ) {
53+
return NaN;
54+
}
55+
return uniform( -10.0, 10.0 );
56+
}
57+
58+
/**
59+
* Creates a benchmark function.
60+
*
61+
* @private
62+
* @param {PositiveInteger} len - array length
63+
* @returns {Function} benchmark function
64+
*/
65+
function createBenchmark( len ) {
66+
var correction;
67+
var xbuf;
68+
var x;
69+
70+
xbuf = filledarrayBy( len, 'float64', rand );
71+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
72+
correction = scalar2ndarray( 1.0, options );
73+
74+
return benchmark;
75+
76+
/**
77+
* Benchmark function.
78+
*
79+
* @private
80+
* @param {Benchmark} b - benchmark instance
81+
*/
82+
function benchmark( b ) {
83+
var v;
84+
var i;
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
v = dnanstdevyc( [ x, correction ] );
89+
if ( isnan( v ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
}
93+
b.toc();
94+
if ( isnan( v ) ) {
95+
b.fail( 'should not return NaN' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
}
100+
}
101+
102+
103+
// MAIN //
104+
105+
/**
106+
* Main execution sequence.
107+
*
108+
* @private
109+
*/
110+
function main() {
111+
var len;
112+
var min;
113+
var max;
114+
var f;
115+
var i;
116+
117+
min = 1; // 10^min
118+
max = 6; // 10^max
119+
120+
for ( i = min; i <= max; i++ ) {
121+
len = pow( 10, i );
122+
f = createBenchmark( len );
123+
bench( format( '%s:len=%d', pkg, len ), f );
124+
}
125+
}
126+
127+
main();
Lines changed: 43 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)