Skip to content

Commit 1624fe5

Browse files
gururaj1512kgrytestdlib-bot
authored
feat: add stats/strided/ztest2
PR-URL: #7464 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 9cc5aa7 commit 1624fe5

File tree

14 files changed

+2489
-0
lines changed

14 files changed

+2489
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
<!-- lint disable max-heading-length -->
22+
23+
# ztest2
24+
25+
> Compute a two-sample Z-test.
26+
27+
<section class="intro">
28+
29+
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`:
30+
31+
- `H0: μX - μY ≥ Δ` versus the alternative hypothesis `H1: μX - μY < Δ`.
32+
- `H0: μX - μY ≤ Δ` versus the alternative hypothesis `H1: μX - μY > Δ`.
33+
- `H0: μX - μY = Δ` versus the alternative hypothesis `H1: μX - μY ≠ Δ`.
34+
35+
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).
36+
37+
</section>
38+
39+
<!-- /.intro -->
40+
41+
<section class="usage">
42+
43+
## Usage
44+
45+
```javascript
46+
var ztest2 = require( '@stdlib/stats/strided/ztest2' );
47+
```
48+
49+
#### ztest2( NX, NY, alternative, alpha, diff, sigmax, x, strideX, sigmay, y, strideY, out )
50+
51+
Computes a two-sample Z-test.
52+
53+
```javascript
54+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
55+
56+
var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
57+
var y = [ 3.0, 3.0, 5.0, 7.0, 7.0 ];
58+
59+
var results = new Results();
60+
var out = ztest2( x.length, y.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, 2.0, y, 1, results );
61+
// returns {...}
62+
63+
var bool = ( out === results );
64+
// returns true
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **NX**: number of indexed elements in `x`.
70+
- **NY**: number of indexed elements in `y`.
71+
- **alternative**: [alternative hypothesis][@stdlib/stats/base/ztest/alternatives].
72+
- **alpha**: significance level.
73+
- **diff**: difference in means under the null hypothesis.
74+
- **sigmax**: known standard deviation of `x`.
75+
- **x**: first input array.
76+
- **strideX**: stride length for `x`.
77+
- **sigmay**: known standard deviation of `y`.
78+
- **y**: second input array.
79+
- **strideY**: stride length for `y`.
80+
- **out**: output [results object][@stdlib/stats/base/ztest/two-sample/results/float64].
81+
82+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to perform a two-sample Z-test over every other element in `x` and `y`,
83+
84+
```javascript
85+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
86+
87+
var x = [ 4.0, 0.0, 4.0, 0.0, 6.0, 0.0, 6.0, 0.0, 5.0, 0.0 ];
88+
var y = [ 3.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 7.0, 0.0 ];
89+
90+
var results = new Results();
91+
var out = ztest2( 5, 5, 'two-sided', 0.05, 0.0, 1.0, x, 2, 2.0, y, 2, results );
92+
// returns {...}
93+
94+
var bool = ( out === results );
95+
// returns true
96+
```
97+
98+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
99+
100+
<!-- eslint-disable stdlib/capitalized-comments -->
101+
102+
```javascript
103+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
104+
var Float64Array = require( '@stdlib/array/float64' );
105+
106+
var x0 = new Float64Array( [ 0.0, 4.0, 4.0, 6.0, 6.0, 5.0 ] );
107+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
108+
109+
var y0 = new Float64Array( [ 0.0, 3.0, 3.0, 5.0, 7.0, 7.0 ] );
110+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
111+
112+
var results = new Results();
113+
var out = ztest2( 5, 5, 'two-sided', 0.05, 0.0, 1.0, x1, 1, 2.0, y1, 1, results );
114+
// returns {...}
115+
116+
var bool = ( out === results );
117+
// returns true
118+
```
119+
120+
#### ztest2.ndarray( NX, NY, alternative, alpha, diff, sigmax, x, strideX, offsetX, sigmay, y, strideY, offsetY, out )
121+
122+
Computes a two-sample Z-test using alternative indexing semantics.
123+
124+
```javascript
125+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
126+
127+
var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
128+
var y = [ 3.0, 3.0, 5.0, 7.0, 7.0 ];
129+
130+
var results = new Results();
131+
var out = ztest2.ndarray( x.length, y.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, 0, 2.0, y, 1, 0, results );
132+
// returns {...}
133+
134+
var bool = ( out === results );
135+
// returns true
136+
```
137+
138+
The function has the following additional parameters:
139+
140+
- **offsetX**: starting index for `x`.
141+
- **offsetY**: starting index for `y`.
142+
143+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to perform a two-sample Z-test over every other element in `x` and `y` starting from the second element
144+
145+
```javascript
146+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
147+
148+
var x = [ 0.0, 4.0, 0.0, 4.0, 0.0, 6.0, 0.0, 6.0, 0.0, 5.0 ];
149+
var y = [ 0.0, 3.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 7.0 ];
150+
151+
var results = new Results();
152+
var out = ztest2.ndarray( 5, 5, 'two-sided', 0.05, 0.0, 1.0, x, 2, 1, 2.0, y, 2, 1, results );
153+
// returns {...}
154+
155+
var bool = ( out === results );
156+
// returns true
157+
```
158+
159+
</section>
160+
161+
<!-- /.usage -->
162+
163+
<section class="notes">
164+
165+
## Notes
166+
167+
- As a general rule of thumb, a Z-test is most reliable when `N >= 50`. For smaller sample sizes or when the standard deviations are unknown, prefer a t-test.
168+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
169+
- Depending on the environment, the typed versions ([`dztest2`][@stdlib/stats/strided/dztest2], [`sztest2`][@stdlib/stats/strided/sztest2], etc.) are likely to be significantly more performant.
170+
171+
</section>
172+
173+
<!-- /.notes -->
174+
175+
<section class="examples">
176+
177+
## Examples
178+
179+
<!-- eslint no-undef: "error" -->
180+
181+
```javascript
182+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
183+
var normal = require( '@stdlib/random/array/normal' );
184+
var ztest2 = require( '@stdlib/stats/strided/ztest2' );
185+
186+
var x = normal( 1000, 4.0, 2.0, {
187+
'dtype': 'generic'
188+
});
189+
var y = normal( 800, 3.0, 2.0, {
190+
'dtype': 'generic'
191+
});
192+
193+
var results = new Results();
194+
var out = ztest2( x.length, y.length, 'two-sided', 0.05, 1.0, 2.0, x, 1, 2.0, y, 1, results );
195+
// returns {...}
196+
197+
console.log( out.toString() );
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
<section class="references">
205+
206+
</section>
207+
208+
<!-- /.references -->
209+
210+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
211+
212+
<section class="related">
213+
214+
</section>
215+
216+
<!-- /.related -->
217+
218+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
219+
220+
<section class="links">
221+
222+
[variance]: https://en.wikipedia.org/wiki/Variance
223+
224+
[@stdlib/stats/base/ztest/alternatives]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/ztest/alternatives
225+
226+
[@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
227+
228+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
229+
230+
[@stdlib/stats/strided/dztest2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dztest
231+
232+
[@stdlib/stats/strided/sztest2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/sztest
233+
234+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
235+
236+
</section>
237+
238+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
28+
var pkg = require( './../package.json' ).name;
29+
var ztest2 = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var results;
50+
var x;
51+
var y;
52+
53+
results = new Results();
54+
x = normal( len, 0.0, 1.0, options );
55+
y = normal( len, 0.0, 1.0, options );
56+
57+
return benchmark;
58+
59+
function benchmark( b ) {
60+
var out;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = ztest2( x.length, y.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, 1.0, y, 1, results );
66+
if ( isnan( out.statistic ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( out.statistic ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)