-
-
Notifications
You must be signed in to change notification settings - Fork 846
feat: add stats/array/stdevpn
#7556
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 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. | ||
|
||
--> | ||
|
||
# stdevpn | ||
|
||
> Calculate the [standard deviation][standard-deviation] of an array using a two-pass algorithm. | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var stdevpn = require( '@stdlib/stats/array/stdevpn' ); | ||
``` | ||
|
||
#### stdevpn( x\[, correction] ) | ||
|
||
Computes the [standard deviation][standard-deviation] of an array using a two-pass algorithm. | ||
|
||
```javascript | ||
var x = [ 1.0, -2.0, 2.0 ]; | ||
|
||
var v = stdevpn( x ); | ||
// returns ~2.0817 | ||
``` | ||
|
||
The function has the following parameters: | ||
|
||
- **x**: input array. | ||
- **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 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`. | ||
|
||
By default, the function computes the sample [standard deviation][standard-deviation]. To use adjust the degrees of freedom when computing the [standard deviation][standard-deviation], provide a `correction` argument. | ||
|
||
```javascript | ||
var x = [ 1.0, -2.0, 2.0 ]; | ||
|
||
var v = stdevpn( x, 0.0 ); | ||
// returns ~1.6997 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- If provided an empty array, the function returns `NaN`. | ||
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. | ||
- 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]). | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
var stdevpn = require( '@stdlib/stats/array/stdevpn' ); | ||
|
||
var x = discreteUniform( 10, -50, 50, { | ||
'dtype': 'float64' | ||
}); | ||
console.log( x ); | ||
|
||
var v = stdevpn( x ); | ||
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 | ||
|
||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
96 changes: 96 additions & 0 deletions
96
lib/node_modules/@stdlib/stats/array/stdevpn/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 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 pow = require( '@stdlib/math/base/special/pow' ); | ||
var pkg = require( './../package.json' ).name; | ||
var stdevpn = require( './../lib' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var options = { | ||
'dtype': 'generic' | ||
}; | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = uniform( len, -10, 10, options ); | ||
return benchmark; | ||
|
||
function benchmark( b ) { | ||
var v; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = stdevpn( x, 1.0 ); | ||
if ( isnan( v ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( v ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
f = createBenchmark( len ); | ||
bench( pkg+':len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
38 changes: 38 additions & 0 deletions
38
lib/node_modules/@stdlib/stats/array/stdevpn/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
{{alias}}( x[, correction] ) | ||
Computes the standard deviation of an array using a two-pass algorithm. | ||
|
||
If provided an empty array, the function returns `NaN`. | ||
|
||
Parameters | ||
---------- | ||
x: Array<number>|TypedArray | ||
Input array. | ||
|
||
correction: number (optional) | ||
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 according to `N-c` where `N` corresponds to | ||
the number of array elements and `c` corresponds to the provided | ||
degrees of freedom adjustment. When computing the 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, | ||
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`. | ||
|
||
Returns | ||
------- | ||
out: number | ||
The standard deviation. | ||
|
||
Examples | ||
-------- | ||
> var x = [ 1.0, -2.0, 2.0 ]; | ||
> {{alias}}( x, 1.0 ) | ||
~2.0817 | ||
|
||
See Also | ||
-------- | ||
|
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/stats/array/stdevpn/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 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. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/// <reference types="@stdlib/types"/> | ||
|
||
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; | ||
|
||
/** | ||
* Input array. | ||
*/ | ||
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>; | ||
|
||
/** | ||
* Computes the standard deviation of an array using a two-pass algorithm. | ||
* | ||
* @param x - input array | ||
* @param correction - degrees of freedom adjustment | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @returns standard deviation | ||
* | ||
* @example | ||
* var x = [ 1.0, -2.0, 2.0 ]; | ||
* | ||
* var v = stdevpn( x, 1.0 ); | ||
* // returns ~2.0817 | ||
*/ | ||
declare function stdevpn( x: InputArray, correction?: number ): number; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = stdevpn; |
76 changes: 76 additions & 0 deletions
76
lib/node_modules/@stdlib/stats/array/stdevpn/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 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. | ||
*/ | ||
|
||
import AccessorArray = require( '@stdlib/array/base/accessor' ); | ||
import stdevpn = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a number... | ||
{ | ||
const x = new Float64Array( 10 ); | ||
|
||
stdevpn( x ); // $ExpectType number | ||
stdevpn( new AccessorArray( x ) ); // $ExpectType number | ||
|
||
stdevpn( x, 1.0 ); // $ExpectType number | ||
stdevpn( new AccessorArray( x ), 1.0 ); // $ExpectType number | ||
} | ||
|
||
// The compiler throws an error if the function is provided a first argument which is not a numeric array... | ||
{ | ||
stdevpn( 10 ); // $ExpectError | ||
stdevpn( '10' ); // $ExpectError | ||
stdevpn( true ); // $ExpectError | ||
stdevpn( false ); // $ExpectError | ||
stdevpn( null ); // $ExpectError | ||
stdevpn( undefined ); // $ExpectError | ||
stdevpn( {} ); // $ExpectError | ||
stdevpn( ( x: number ): number => x ); // $ExpectError | ||
|
||
stdevpn( 10, 1.0 ); // $ExpectError | ||
stdevpn( '10', 1.0 ); // $ExpectError | ||
stdevpn( true, 1.0 ); // $ExpectError | ||
stdevpn( false, 1.0 ); // $ExpectError | ||
stdevpn( null, 1.0 ); // $ExpectError | ||
stdevpn( undefined, 1.0 ); // $ExpectError | ||
stdevpn( {}, 1.0 ); // $ExpectError | ||
stdevpn( ( x: number ): number => x, 1.0 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a second argument which is not a number... | ||
{ | ||
const x = new Float64Array( 10 ); | ||
|
||
stdevpn( x, '10' ); // $ExpectError | ||
stdevpn( x, true ); // $ExpectError | ||
stdevpn( x, false ); // $ExpectError | ||
stdevpn( x, null ); // $ExpectError | ||
stdevpn( x, [] ); // $ExpectError | ||
stdevpn( x, {} ); // $ExpectError | ||
stdevpn( x, ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
const x = new Float64Array( 10 ); | ||
|
||
stdevpn(); // $ExpectError | ||
stdevpn( x, 1.0, {} ); // $ExpectError | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.