Skip to content

Commit 5724e73

Browse files
committed
feat: add stats/array/stdev
--- 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 6bc3cd0 commit 5724e73

File tree

10 files changed

+896
-0
lines changed

10 files changed

+896
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
# stdev
22+
23+
> Calculate the [standard deviation][standard-deviation] of an array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var stdev = require( '@stdlib/stats/array/stdev' );
37+
```
38+
39+
#### stdev( correction, x )
40+
41+
Computes the [standard deviation][standard-deviation] of an array.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, 2.0 ];
45+
46+
var v = stdev( 1, x );
47+
// returns ~2.0817
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **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 `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).
53+
- **x**: input array.
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<section class="notes">
60+
61+
## Notes
62+
63+
- If provided an empty array, the function returns `NaN`.
64+
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
65+
- 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]).
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
79+
var stdev = require( '@stdlib/stats/array/stdev' );
80+
81+
var x = discreteUniform( 10, -50, 50, {
82+
'dtype': 'float64'
83+
});
84+
console.log( x );
85+
86+
var v = stdev( 1, x );
87+
console.log( v );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
95+
96+
<section class="related">
97+
98+
</section>
99+
100+
<!-- /.related -->
101+
102+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="links">
105+
106+
[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
107+
108+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
109+
110+
</section>
111+
112+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var stdev = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = uniform( len, -10, 10, options );
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var v;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = stdev( 1, x );
58+
if ( isnan( v ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( v ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( correction, x )
3+
Computes the standard deviation of an array.
4+
5+
If provided an empty array, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
correction: number
10+
Degrees of freedom adjustment. Setting this parameter to a value other
11+
than `0` has the effect of adjusting the divisor during the calculation
12+
of the standard deviation according to `N - c` where `c` corresponds to
13+
the provided degrees of freedom adjustment. When computing the standard
14+
deviation of a population, setting this parameter to `0` is the standard
15+
choice (i.e., the provided array contains data constituting an entire
16+
population). When computing the corrected sample standard deviation,
17+
setting this parameter to `1` is the standard choice (i.e., the provided
18+
array contains data sampled from a larger population; this is commonly
19+
referred to as Bessel's correction).
20+
21+
x: Array<number>|TypedArray
22+
Input array.
23+
24+
Returns
25+
-------
26+
out: number
27+
The standard deviation.
28+
29+
Examples
30+
--------
31+
> var x = [ 1.0, -2.0, 2.0 ];
32+
> {{alias}}( 1, x )
33+
~2.0817
34+
35+
See Also
36+
--------
37+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Computes the standard deviation of an array.
32+
*
33+
* @param correction - degrees of freedom adjustment
34+
* @param x - input array
35+
* @returns standard deviation
36+
*
37+
* @example
38+
* var x = [ 1.0, -2.0, 2.0 ];
39+
*
40+
* var v = stdev( 1, x );
41+
* // returns ~2.0817
42+
*/
43+
declare function stdev( correction: number, x: InputArray ): number;
44+
45+
46+
// EXPORTS //
47+
48+
export = stdev;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
import AccessorArray = require( '@stdlib/array/base/accessor' );
20+
import stdev = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a number...
26+
{
27+
const x = new Float64Array( 10 );
28+
29+
stdev( 1, x ); // $ExpectType number
30+
stdev( 1, new AccessorArray( x ) ); // $ExpectType number
31+
}
32+
33+
// The compiler throws an error if the function is provided a first argument which is not a number...
34+
{
35+
const x = new Float64Array( 10 );
36+
37+
stdev( '10', x ); // $ExpectError
38+
stdev( true, x ); // $ExpectError
39+
stdev( false, x ); // $ExpectError
40+
stdev( null, x ); // $ExpectError
41+
stdev( undefined, x ); // $ExpectError
42+
stdev( [], x ); // $ExpectError
43+
stdev( {}, x ); // $ExpectError
44+
stdev( ( x: number ): number => x, x ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided a second argument which is not a numeric array...
48+
{
49+
stdev( 1, 10 ); // $ExpectError
50+
stdev( 1, '10' ); // $ExpectError
51+
stdev( 1, true ); // $ExpectError
52+
stdev( 1, false ); // $ExpectError
53+
stdev( 1, null ); // $ExpectError
54+
stdev( 1, undefined ); // $ExpectError
55+
stdev( 1, {} ); // $ExpectError
56+
stdev( 1, ( x: number ): number => x ); // $ExpectError
57+
}
58+
59+
// The compiler throws an error if the function is provided an unsupported number of arguments...
60+
{
61+
const x = new Float64Array( 10 );
62+
63+
stdev(); // $ExpectError
64+
stdev( 1 ); // $ExpectError
65+
stdev( 1, x, {} ); // $ExpectError
66+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var stdev = require( './../lib' );
23+
24+
var x = discreteUniform( 10, -50, 50, {
25+
'dtype': 'float64'
26+
});
27+
console.log( x );
28+
29+
var v = stdev( 1, x );
30+
console.log( v );

0 commit comments

Comments
 (0)