-
-
Notifications
You must be signed in to change notification settings - Fork 860
feat: add stats/base/ndarray/sztest2
#7703
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
kgryte
merged 8 commits into
stdlib-js:develop
from
gururaj1512:stats-base-ndarray-sztest2
Jul 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd59cf1
feat: add `stats/base/ndarray/sztest2`
gururaj1512 49617b2
docs: fix enumeration
kgryte 00084a3
docs: fix comment
kgryte 5271183
docs: update descriptions
kgryte 582fb5a
docs: update descriptions
kgryte bac39dc
docs: update descriptions
kgryte 8314a46
docs: update descriptions
kgryte 8b346ae
docs: update comments
kgryte 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
186 changes: 186 additions & 0 deletions
186
lib/node_modules/@stdlib/stats/base/ndarray/sztest2/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,186 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# sztest2 | ||
|
||
> Compute a two-sample Z-test for two one-dimensional single-precision floating-point ndarrays. | ||
|
||
<section class="intro"> | ||
|
||
A Z-test commonly refers to a two-sample location test which compares the means of two independent sets of measurements `X` and `Y` when the population standard deviations are known. A Z-test supports testing three different null hypotheses `H0`: | ||
|
||
- `H0: μX - μY ≥ Δ` versus the alternative hypothesis `H1: μX - μY < Δ`. | ||
- `H0: μX - μY ≤ Δ` versus the alternative hypothesis `H1: μX - μY > Δ`. | ||
- `H0: μX - μY = Δ` versus the alternative hypothesis `H1: μX - μY ≠ Δ`. | ||
|
||
Here, `μX` and `μY` are the true population means of samples `X` and `Y`, respectively, and `Δ` is the hypothesized difference in means (typically `0` by default). | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var sztest2 = require( '@stdlib/stats/base/ndarray/sztest2' ); | ||
``` | ||
|
||
#### sztest2( arrays ) | ||
|
||
Computes a two-sample Z-test for two one-dimensional single-precision floating-point ndarrays. | ||
|
||
```javascript | ||
var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' ); | ||
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' ); | ||
var structFactory = require( '@stdlib/array/struct-factory' ); | ||
var Float32Array = require( '@stdlib/array/float32' ); | ||
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
var ndarray = require( '@stdlib/ndarray/ctor' ); | ||
|
||
var opts = { | ||
'dtype': 'float32' | ||
}; | ||
|
||
var xbuf = new Float32Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] ); | ||
var x = new ndarray( opts.dtype, xbuf, [ 5 ], [ 1 ], 0, 'row-major' ); | ||
|
||
var ybuf = new Float32Array( [ 3.0, 3.0, 5.0, 7.0, 7.0 ] ); | ||
var y = new ndarray( opts.dtype, ybuf, [ 5 ], [ 1 ], 0, 'row-major' ); | ||
|
||
var alt = scalar2ndarray( resolveEnum( 'two-sided' ), { | ||
'dtype': 'int8' | ||
}); | ||
var alpha = scalar2ndarray( 0.05, opts ); | ||
var diff = scalar2ndarray( 0.0, opts ); | ||
var sigmax = scalar2ndarray( 1.0, opts ); | ||
var sigmay = scalar2ndarray( 2.0, opts ); | ||
|
||
var ResultsArray = structFactory( Float32Results ); | ||
var out = new ndarray( Float32Results, new ResultsArray( 1 ), [], [ 0 ], 0, 'row-major' ); | ||
|
||
var v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] ); | ||
|
||
var bool = ( v === out ); | ||
// returns true | ||
``` | ||
|
||
The function has the following parameters: | ||
|
||
- **arrays**: array-like object containing the following ndarrays in order: | ||
|
||
1. first one-dimensional input ndarray. | ||
1. second one-dimensional input ndarray. | ||
2. a zero-dimensional output ndarray containing a [results object][@stdlib/stats/base/ztest/two-sample/results/float32]. | ||
3. a zero-dimensional ndarray specifying the alternative hypothesis. | ||
4. a zero-dimensional ndarray specifying the significance level. | ||
5. a zero-dimensional ndarray specifying the difference in means under the null hypothesis. | ||
6. a zero-dimensional ndarray specifying the known standard deviation of first one-dimensional input ndarray. | ||
7. a zero-dimensional ndarray specifying the known standard deviation of second one-dimensional input ndarray. | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- As a general rule of thumb, a Z-test is most reliable for sample sizes greater than `50`. For smaller sample sizes or when the standard deviation is unknown, prefer a t-test. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' ); | ||
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' ); | ||
var structFactory = require( '@stdlib/array/struct-factory' ); | ||
var normal = require( '@stdlib/random/array/normal' ); | ||
var ndarray = require( '@stdlib/ndarray/ctor' ); | ||
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
var sztest2 = require( '@stdlib/stats/base/ndarray/sztest2' ); | ||
|
||
var opts = { | ||
'dtype': 'float32' | ||
}; | ||
|
||
// Create a one-dimensional ndarrays containing pseudorandom numbers drawn from a normal distribution: | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var xbuf = normal( 100, 0.0, 1.0, opts ); | ||
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); | ||
console.log( ndarray2array( x ) ); | ||
|
||
var ybuf = normal( 100, 0.0, 1.0, opts ); | ||
var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' ); | ||
console.log( ndarray2array( y ) ); | ||
|
||
// Specify the alternative hypothesis: | ||
var alt = scalar2ndarray( resolveEnum( 'two-sided' ), { | ||
'dtype': 'int8' | ||
}); | ||
|
||
// Specify the significance level: | ||
var alpha = scalar2ndarray( 0.05, opts ); | ||
|
||
// Specify the difference in means under the null hypothesis: | ||
var diff = scalar2ndarray( 0.0, opts ); | ||
|
||
// Specify the known standard deviations: | ||
var sigmax = scalar2ndarray( 1.0, opts ); | ||
var sigmay = scalar2ndarray( 1.0, opts ); | ||
|
||
// Create a zero-dimensional results ndarray: | ||
var ResultsArray = structFactory( Float32Results ); | ||
var out = new ndarray( Float32Results, new ResultsArray( 1 ), [], [ 0 ], 0, 'row-major' ); | ||
|
||
// Perform a Z-test: | ||
var v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] ); | ||
console.log( v.get().toString() ); | ||
``` | ||
|
||
</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"> | ||
|
||
[@stdlib/stats/base/ztest/two-sample/results/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/ztest/two-sample/results/float32 | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
128 changes: 128 additions & 0 deletions
128
lib/node_modules/@stdlib/stats/base/ndarray/sztest2/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,128 @@ | ||
/** | ||
* @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 normal = require( '@stdlib/random/array/normal' ); | ||
var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var ndarray = require( '@stdlib/ndarray/base/ctor' ); | ||
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); | ||
var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' ); | ||
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' ); | ||
var structFactory = require( '@stdlib/array/struct-factory' ); | ||
var pkg = require( './../package.json' ).name; | ||
var sztest2 = require( './../lib' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var options = { | ||
'dtype': 'float32' | ||
}; | ||
var ResultsArray = structFactory( Float32Results ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var sigmax; | ||
var sigmay; | ||
var alpha; | ||
var diff; | ||
var xbuf; | ||
var ybuf; | ||
var obuf; | ||
var out; | ||
var alt; | ||
var x; | ||
var y; | ||
|
||
xbuf = normal( len, 0.0, 1.0, options ); | ||
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); | ||
|
||
ybuf = normal( len, 0.0, 1.0, options ); | ||
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); | ||
|
||
obuf = new ResultsArray( 1 ); | ||
out = new ndarray( Float32Results, obuf, [], [ 0 ], 0, 'row-major' ); | ||
|
||
alt = scalar2ndarray( resolveEnum( 'two-sided' ), 'int8', 'row-major' ); | ||
alpha = scalar2ndarray( 0.05, options.dtype, 'row-major' ); | ||
diff = scalar2ndarray( 0.0, options.dtype, 'row-major' ); | ||
sigmax = scalar2ndarray( 1.0, options.dtype, 'row-major' ); | ||
sigmay = scalar2ndarray( 1.0, options.dtype, 'row-major' ); | ||
|
||
return benchmark; | ||
|
||
function benchmark( b ) { | ||
var v; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] ); | ||
if ( typeof v !== 'object' ) { | ||
b.fail( 'should return an ndarray' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnanf( v.get().statistic ) || isnanf( v.get().pValue ) ) { | ||
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(); |
69 changes: 69 additions & 0 deletions
69
lib/node_modules/@stdlib/stats/base/ndarray/sztest2/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,69 @@ | ||
|
||
{{alias}}( arrays ) | ||
Computes a two-sample Z-test for two one-dimensional single-precision | ||
floating-point ndarrays. | ||
|
||
Parameters | ||
---------- | ||
arrays: ArrayLikeObject<ndarray> | ||
Array-like object containing the following ndarrays in order: | ||
|
||
- first one-dimensional input ndarray. | ||
- second one-dimensional input ndarray. | ||
- a zero-dimensional output ndarray containing a results object. | ||
- a zero-dimensional ndarray specifying the alternative hypothesis. | ||
- a zero-dimensional ndarray specifying the significance level. | ||
- a zero-dimensional ndarray specifying the difference in means under | ||
the null hypothesis. | ||
- a zero-dimensional ndarray specifying the known standard deviation of | ||
first one-dimensional input ndarray. | ||
- a zero-dimensional ndarray specifying the known standard deviation of | ||
second one-dimensional input ndarray. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Returns | ||
------- | ||
out: ndarray | ||
Output ndarray. | ||
|
||
Examples | ||
-------- | ||
// Create input ndarrays: | ||
> var xbuf = new {{alias:@stdlib/array/float32}}( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] ); | ||
> var ybuf = new {{alias:@stdlib/array/float32}}( [ 3.0, 3.0, 5.0, 7.0, 7.0 ] ); | ||
> var dt = 'float32'; | ||
> var sh = [ xbuf.length ]; | ||
> var st = [ 1 ]; | ||
> var oo = 0; | ||
> var ord = 'row-major'; | ||
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord ); | ||
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord ); | ||
|
||
// Create the output ndarray: | ||
> var S = {{alias:@stdlib/stats/base/ztest/two-sample/results/float32}}; | ||
> var Results = {{alias:@stdlib/array/struct-factory}}( S ); | ||
> var obuf = new Results( 1 ); | ||
> var out = new {{alias:@stdlib/ndarray/ctor}}( S, obuf, [], [ 0 ], 0, ord ); | ||
|
||
// Specify the alternative hypothesis: | ||
> var alt = {{alias:@stdlib/ndarray/from-scalar}}( 'two-sided' ); | ||
|
||
// Specify the significance level: | ||
> var opts = { 'dtype': dt }; | ||
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 0.05, opts ); | ||
|
||
// Specify the difference in means under the null hypothesis: | ||
> var diff = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, opts ); | ||
|
||
// Specify the known standard deviations: | ||
> var sigmax = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, opts ); | ||
> var sigmay = {{alias:@stdlib/ndarray/from-scalar}}( 2.0, opts ); | ||
|
||
// Perform a Z-test: | ||
> {{alias}}( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] ); | ||
|
||
// Print the results: | ||
> out.get().toString() | ||
|
||
See Also | ||
-------- | ||
|
Oops, something went wrong.
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.