Skip to content

Commit f3dfedc

Browse files
gururaj1512kgryte
andauthored
feat: add stats/base/ndarray/sztest2
PR-URL: #7703 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 79915fb commit f3dfedc

File tree

10 files changed

+1217
-0
lines changed

10 files changed

+1217
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
# sztest2
22+
23+
> Compute a two-sample Z-test for two one-dimensional single-precision floating-point ndarrays.
24+
25+
<section class="intro">
26+
27+
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`:
28+
29+
- `H0: μX - μY ≥ Δ` versus the alternative hypothesis `H1: μX - μY < Δ`.
30+
- `H0: μX - μY ≤ Δ` versus the alternative hypothesis `H1: μX - μY > Δ`.
31+
- `H0: μX - μY = Δ` versus the alternative hypothesis `H1: μX - μY ≠ Δ`.
32+
33+
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).
34+
35+
</section>
36+
37+
<!-- /.intro -->
38+
39+
<section class="usage">
40+
41+
## Usage
42+
43+
```javascript
44+
var sztest2 = require( '@stdlib/stats/base/ndarray/sztest2' );
45+
```
46+
47+
#### sztest2( arrays )
48+
49+
Computes a two-sample Z-test for two one-dimensional single-precision floating-point ndarrays.
50+
51+
```javascript
52+
var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' );
53+
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
54+
var structFactory = require( '@stdlib/array/struct-factory' );
55+
var Float32Array = require( '@stdlib/array/float32' );
56+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
57+
var ndarray = require( '@stdlib/ndarray/ctor' );
58+
59+
var opts = {
60+
'dtype': 'float32'
61+
};
62+
63+
var xbuf = new Float32Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] );
64+
var x = new ndarray( opts.dtype, xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
65+
66+
var ybuf = new Float32Array( [ 3.0, 3.0, 5.0, 7.0, 7.0 ] );
67+
var y = new ndarray( opts.dtype, ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
68+
69+
var alt = scalar2ndarray( resolveEnum( 'two-sided' ), {
70+
'dtype': 'int8'
71+
});
72+
var alpha = scalar2ndarray( 0.05, opts );
73+
var diff = scalar2ndarray( 0.0, opts );
74+
var sigmax = scalar2ndarray( 1.0, opts );
75+
var sigmay = scalar2ndarray( 2.0, opts );
76+
77+
var ResultsArray = structFactory( Float32Results );
78+
var out = new ndarray( Float32Results, new ResultsArray( 1 ), [], [ 0 ], 0, 'row-major' );
79+
80+
var v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] );
81+
82+
var bool = ( v === out );
83+
// returns true
84+
```
85+
86+
The function has the following parameters:
87+
88+
- **arrays**: array-like object containing the following ndarrays in order:
89+
90+
1. first one-dimensional input ndarray.
91+
2. second one-dimensional input ndarray.
92+
3. a zero-dimensional output ndarray containing a [results object][@stdlib/stats/base/ztest/two-sample/results/float32].
93+
4. a zero-dimensional ndarray specifying the alternative hypothesis.
94+
5. a zero-dimensional ndarray specifying the significance level.
95+
6. a zero-dimensional ndarray specifying the difference in means under the null hypothesis.
96+
7. a zero-dimensional ndarray specifying the known standard deviation of the first one-dimensional input ndarray.
97+
8. a zero-dimensional ndarray specifying the known standard deviation of the second one-dimensional input ndarray.
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- 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.
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<section class="examples">
114+
115+
## Examples
116+
117+
<!-- eslint no-undef: "error" -->
118+
119+
```javascript
120+
var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' );
121+
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
122+
var structFactory = require( '@stdlib/array/struct-factory' );
123+
var normal = require( '@stdlib/random/array/normal' );
124+
var ndarray = require( '@stdlib/ndarray/ctor' );
125+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
126+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
127+
var sztest2 = require( '@stdlib/stats/base/ndarray/sztest2' );
128+
129+
var opts = {
130+
'dtype': 'float32'
131+
};
132+
133+
// Create one-dimensional ndarrays containing pseudorandom numbers drawn from a normal distribution:
134+
var xbuf = normal( 100, 0.0, 1.0, opts );
135+
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
136+
console.log( ndarray2array( x ) );
137+
138+
var ybuf = normal( 100, 0.0, 1.0, opts );
139+
var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
140+
console.log( ndarray2array( y ) );
141+
142+
// Specify the alternative hypothesis:
143+
var alt = scalar2ndarray( resolveEnum( 'two-sided' ), {
144+
'dtype': 'int8'
145+
});
146+
147+
// Specify the significance level:
148+
var alpha = scalar2ndarray( 0.05, opts );
149+
150+
// Specify the difference in means under the null hypothesis:
151+
var diff = scalar2ndarray( 0.0, opts );
152+
153+
// Specify the known standard deviations:
154+
var sigmax = scalar2ndarray( 1.0, opts );
155+
var sigmay = scalar2ndarray( 1.0, opts );
156+
157+
// Create a zero-dimensional results ndarray:
158+
var ResultsArray = structFactory( Float32Results );
159+
var out = new ndarray( Float32Results, new ResultsArray( 1 ), [], [ 0 ], 0, 'row-major' );
160+
161+
// Perform a Z-test:
162+
var v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] );
163+
console.log( v.get().toString() );
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
171+
172+
<section class="related">
173+
174+
</section>
175+
176+
<!-- /.related -->
177+
178+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
179+
180+
<section class="links">
181+
182+
[@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
183+
184+
</section>
185+
186+
<!-- /.links -->
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 normal = require( '@stdlib/random/array/normal' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
29+
var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' );
30+
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
31+
var structFactory = require( '@stdlib/array/struct-factory' );
32+
var pkg = require( './../package.json' ).name;
33+
var sztest2 = require( './../lib' );
34+
35+
36+
// VARIABLES //
37+
38+
var options = {
39+
'dtype': 'float32'
40+
};
41+
var ResultsArray = structFactory( Float32Results );
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var sigmax;
55+
var sigmay;
56+
var alpha;
57+
var diff;
58+
var xbuf;
59+
var ybuf;
60+
var obuf;
61+
var out;
62+
var alt;
63+
var x;
64+
var y;
65+
66+
xbuf = normal( len, 0.0, 1.0, options );
67+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
68+
69+
ybuf = normal( len, 0.0, 1.0, options );
70+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
71+
72+
obuf = new ResultsArray( 1 );
73+
out = new ndarray( Float32Results, obuf, [], [ 0 ], 0, 'row-major' );
74+
75+
alt = scalar2ndarray( resolveEnum( 'two-sided' ), 'int8', 'row-major' );
76+
alpha = scalar2ndarray( 0.05, options.dtype, 'row-major' );
77+
diff = scalar2ndarray( 0.0, options.dtype, 'row-major' );
78+
sigmax = scalar2ndarray( 1.0, options.dtype, 'row-major' );
79+
sigmay = scalar2ndarray( 1.0, options.dtype, 'row-major' );
80+
81+
return benchmark;
82+
83+
function benchmark( b ) {
84+
var v;
85+
var i;
86+
87+
b.tic();
88+
for ( i = 0; i < b.iterations; i++ ) {
89+
v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] );
90+
if ( typeof v !== 'object' ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
}
94+
b.toc();
95+
if ( isnanf( v.get().statistic ) || isnanf( v.get().pValue ) ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
}
101+
}
102+
103+
104+
// MAIN //
105+
106+
/**
107+
* Main execution sequence.
108+
*
109+
* @private
110+
*/
111+
function main() {
112+
var len;
113+
var min;
114+
var max;
115+
var f;
116+
var i;
117+
118+
min = 1; // 10^min
119+
max = 6; // 10^max
120+
121+
for ( i = min; i <= max; i++ ) {
122+
len = pow( 10, i );
123+
f = createBenchmark( len );
124+
bench( pkg+':len='+len, f );
125+
}
126+
}
127+
128+
main();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
{{alias}}( arrays )
3+
Computes a two-sample Z-test for two one-dimensional single-precision
4+
floating-point ndarrays.
5+
6+
Parameters
7+
----------
8+
arrays: ArrayLikeObject<ndarray>
9+
Array-like object containing the following ndarrays in order:
10+
11+
- first one-dimensional input ndarray.
12+
- second one-dimensional input ndarray.
13+
- a zero-dimensional output ndarray containing a results object.
14+
- a zero-dimensional ndarray specifying the alternative hypothesis.
15+
- a zero-dimensional ndarray specifying the significance level.
16+
- a zero-dimensional ndarray specifying the difference in means under
17+
the null hypothesis.
18+
- a zero-dimensional ndarray specifying the known standard deviation of
19+
the first one-dimensional input ndarray.
20+
- a zero-dimensional ndarray specifying the known standard deviation of
21+
the second one-dimensional input ndarray.
22+
23+
Returns
24+
-------
25+
out: ndarray
26+
Output ndarray.
27+
28+
Examples
29+
--------
30+
// Create input ndarrays:
31+
> var xbuf = new {{alias:@stdlib/array/float32}}( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] );
32+
> var ybuf = new {{alias:@stdlib/array/float32}}( [ 3.0, 3.0, 5.0, 7.0, 7.0 ] );
33+
> var dt = 'float32';
34+
> var sh = [ xbuf.length ];
35+
> var st = [ 1 ];
36+
> var oo = 0;
37+
> var ord = 'row-major';
38+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
39+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
40+
41+
// Create the output ndarray:
42+
> var S = {{alias:@stdlib/stats/base/ztest/two-sample/results/float32}};
43+
> var Results = {{alias:@stdlib/array/struct-factory}}( S );
44+
> var obuf = new Results( 1 );
45+
> var out = new {{alias:@stdlib/ndarray/ctor}}( S, obuf, [], [ 0 ], 0, ord );
46+
47+
// Specify the alternative hypothesis:
48+
> var alt = {{alias:@stdlib/ndarray/from-scalar}}( 'two-sided' );
49+
50+
// Specify the significance level:
51+
> var opts = { 'dtype': dt };
52+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 0.05, opts );
53+
54+
// Specify the difference in means under the null hypothesis:
55+
> var diff = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, opts );
56+
57+
// Specify the known standard deviations:
58+
> var sigmax = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, opts );
59+
> var sigmay = {{alias:@stdlib/ndarray/from-scalar}}( 2.0, opts );
60+
61+
// Perform a Z-test:
62+
> {{alias}}( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] );
63+
64+
// Print the results:
65+
> out.get().toString()
66+
67+
See Also
68+
--------
69+

0 commit comments

Comments
 (0)