Skip to content

Commit 644a461

Browse files
committed
feat: add stats/base/ndarray/ztest2
--- 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: skipped - 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 099dca8 commit 644a461

File tree

10 files changed

+1212
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)