-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add stats/base/ndarray/snanstdev
#9656
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
Open
aryan7071
wants to merge
15
commits into
stdlib-js:develop
Choose a base branch
from
aryan7071:feat/stats-base-ndarray-snanstdev
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+819
−0
Open
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9c80e53
feat: add stats/base/ndarray/snanstdev
aryan7071 c29e391
chore: update copyright years
stdlib-bot 5b74335
test: fix snanstdev benchmarks and tests
aryan7071 bb8a42e
test: fix snanstdev benchmarks and tests
aryan7071 7b8ef96
test: align snanstdev tests with snanstdevpn semantics
aryan7071 05ec5ed
fix: add correction parameter to snanstdev ndarray API
aryan7071 e303e8b
fix: add correction parameter to snanstdev ndarray API
aryan7071 0caca5c
fix: add correction parameter to snanstdev ndarray API
aryan7071 421ea02
fix: add correction parameter to snanstdev ndarray API
aryan7071 26867a7
fix: add correction parameter to snanstdev ndarray API
aryan7071 384bf5b
docs: update snanstdev example to include correction ndarray
aryan7071 1dd7e90
fix: align snanstdev docs test and typings with correction semantics
aryan7071 ff870e1
test: align snanstdev test comments with ndarray semantics
aryan7071 c8099ce
docs: add missing JSDoc in benchmark files
aryan7071 b9ccdd3
feat: update snanstdev implementation, docs, and types
aryan7071 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
122 changes: 122 additions & 0 deletions
122
lib/node_modules/@stdlib/stats/base/ndarray/snanstdev/README.md
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,122 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # snanstdev | ||
|
|
||
| > Compute the standard deviation of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| This package provides an ndarray interface for computing the standard deviation of a one-dimensional | ||
| single-precision floating-point ndarray while ignoring `NaN` values. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var snanstdev = require( '@stdlib/stats/base/ndarray/snanstdev' ); | ||
| ``` | ||
|
|
||
| #### snanstdev( arrays ) | ||
|
|
||
| Computes the standard deviation of a one-dimensional single-precision floating-point ndarray, ignoring NaN values. | ||
|
|
||
| ```javascript | ||
| var Float32Array = require( '@stdlib/array/float32' ); | ||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
|
|
||
| var xbuf = new Float32Array( [ 1.0, 3.0, NaN, 2.0 ] ); | ||
| var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); | ||
|
|
||
| var v = snanstdev( [ x ] ); | ||
| // returns <number> | ||
| ``` | ||
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **arrays**: array-like object containing a one-dimensional input ndarray. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - If provided an empty one-dimensional ndarray, the function returns `NaN`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var uniform = require( '@stdlib/random/base/uniform' ); | ||
| var filledarrayBy = require( '@stdlib/array/filled-by' ); | ||
| var bernoulli = require( '@stdlib/random/base/bernoulli' ); | ||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
| var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
| var snanstdev = require( '@stdlib/stats/base/ndarray/snanstdev' ); | ||
|
|
||
| function rand() { | ||
| if ( bernoulli( 0.8 ) < 1 ) { | ||
| return NaN; | ||
| } | ||
| return uniform( -50.0, 50.0 ); | ||
| } | ||
|
|
||
| var xbuf = filledarrayBy( 10, 'float32', rand ); | ||
| var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); | ||
| console.log( ndarray2array( x ) ); | ||
|
|
||
| var v = snanstdev( [ 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"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
104 changes: 104 additions & 0 deletions
104
lib/node_modules/@stdlib/stats/base/ndarray/snanstdev/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,104 @@ | ||
| /** | ||
| * @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/base/uniform' ); | ||
| var bernoulli = require( '@stdlib/random/base/bernoulli' ); | ||
| var filledarrayBy = require( '@stdlib/array/filled-by' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var snanstdev = require( './../lib' ); | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Returns a random number. | ||
| * | ||
| * @private | ||
| * @returns {number} random number or `NaN` | ||
| */ | ||
| function rand() { | ||
| if ( bernoulli( 0.8 ) < 1 ) { | ||
| return NaN; | ||
| } | ||
| return uniform( -10.0, 10.0 ); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @param {PositiveInteger} len - array length | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( len ) { | ||
| var xbuf; | ||
| var x; | ||
|
|
||
| xbuf = filledarrayBy( len, 'float32', rand ); | ||
| x = new ndarray( 'float32', xbuf, [ len ], [ 1 ], 0, 'row-major' ); | ||
|
|
||
| return benchmark; | ||
|
|
||
| function benchmark( b ) { | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| snanstdev( [ x ] ); | ||
| } | ||
| b.toc(); | ||
|
|
||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
aryan7071 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
| // 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( format( '%s:len=%d', pkg, len ), f ); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
42 changes: 42 additions & 0 deletions
42
...ules/@stdlib/stats/base/ndarray/snanstdev/docs/img/equation_arithmetic_mean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/stats/base/ndarray/snanstdev/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,32 @@ | ||
|
|
||
| {{alias}}( arrays ) | ||
| Computes the standard deviation of a one-dimensional single-precision | ||
| floating-point ndarray, ignoring `NaN` values. | ||
|
|
||
| If provided an empty ndarray, the function returns `NaN`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| arrays: ArrayLikeObject<ndarray> | ||
| Array-like object containing a one-dimensional input ndarray. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: number | ||
| Computed standard deviation. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 2.0 ] ); | ||
| > var dt = 'float32'; | ||
| > var sh = [ xbuf.length ]; | ||
| > var sx = [ 1 ]; | ||
| > var ox = 0; | ||
| > var ord = 'row-major'; | ||
| > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); | ||
| > {{alias}}( [ x ] ) | ||
| NaN | ||
aryan7071 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| See Also | ||
| -------- | ||
|
|
||
46 changes: 46 additions & 0 deletions
46
lib/node_modules/@stdlib/stats/base/ndarray/snanstdev/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,46 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /// <reference types="@stdlib/types"/> | ||
|
|
||
| import { float32ndarray } from '@stdlib/types/ndarray'; | ||
|
|
||
| /** | ||
| * Computes the standard deviation of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values. | ||
| * | ||
| * @param arrays - array-like object containing an input ndarray | ||
| * @returns computed standard deviation | ||
| * | ||
| * @example | ||
| * var Float32Array = require( '@stdlib/array/float32' ); | ||
| * var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
| * | ||
| * var xbuf = new Float32Array( [ 1.0, 3.0, NaN, 2.0 ] ); | ||
| * var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); | ||
| * | ||
| * var v = snanstdev( [ x ] ); | ||
| * // returns <number> | ||
| */ | ||
| declare function snanstdev( arrays: [ float32ndarray ] ): number; | ||
aryan7071 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = snanstdev; | ||
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.
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 is not the intro comment that we are looking for. See other packages.