Skip to content

Commit 2a2efad

Browse files
committed
feat: add stats/strided/ztest2
1 parent f344e9d commit 2a2efad

File tree

14 files changed

+2410
-0
lines changed

14 files changed

+2410
-0
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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 for a strided array.
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( N, alternative, alpha, mu, sigmax, x, strideX, sigmay, y, strideY, out )
50+
51+
Computes a two-sample Z-test for a strided array.
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( 5, '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+
- **N**: number of indexed elements.
70+
- **alternative**: [alternative hypothesis][@stdlib/stats/base/ztest/alternatives].
71+
- **alpha**: significance level.
72+
- **mu**: mean value under the null hypothesis.
73+
- **sigmax**: known standard deviation of `x`.
74+
- **x**: input array.
75+
- **strideX**: stride length for `x`.
76+
- **sigmay**: known standard deviation of `y`.
77+
- **y**: input array.
78+
- **strideY**: stride length for `y`.
79+
- **out**: output [results object][@stdlib/stats/base/ztest/two-sample/results/float64].
80+
81+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to perform a two-sample Z-test over every other element in `x` and `y`,
82+
83+
```javascript
84+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
85+
86+
var x = [ 4.0, 0.0, 4.0, 0.0, 6.0, 0.0, 6.0, 0.0, 5.0, 0.0 ];
87+
var y = [ 3.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 7.0, 0.0 ];
88+
89+
var results = new Results();
90+
var out = ztest2( 5, 'two-sided', 0.05, 0.0, 1.0, x, 2, 2.0, y, 2, results );
91+
// returns {...}
92+
93+
var bool = ( out === results );
94+
// returns true
95+
```
96+
97+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
98+
99+
<!-- eslint-disable stdlib/capitalized-comments -->
100+
101+
```javascript
102+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
103+
var Float64Array = require( '@stdlib/array/float64' );
104+
105+
var x0 = new Float64Array( [ 0.0, 4.0, 4.0, 6.0, 6.0, 5.0 ] );
106+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
107+
108+
var y0 = new Float64Array( [ 0.0, 3.0, 3.0, 5.0, 7.0, 7.0 ] );
109+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
110+
111+
var results = new Results();
112+
var out = ztest2( 5, 'two-sided', 0.05, 0.0, 1.0, x1, 1, 2.0, y1, 1, results );
113+
// returns {...}
114+
115+
var bool = ( out === results );
116+
// returns true
117+
```
118+
119+
#### ztest2.ndarray( N, alternative, alpha, mu, sigmax, x, strideX, offsetX, sigmay, y, strideY, offsetY, out )
120+
121+
Computes a two-sample Z-test for a strided array using alternative indexing semantics.
122+
123+
```javascript
124+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
125+
126+
var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
127+
var y = [ 3.0, 3.0, 5.0, 7.0, 7.0 ];
128+
129+
var results = new Results();
130+
var out = ztest2.ndarray( x.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, 0, 2.0, y, 1, 0, results );
131+
// returns {...}
132+
133+
var bool = ( out === results );
134+
// returns true
135+
```
136+
137+
The function has the following additional parameters:
138+
139+
- **offsetX**: starting index for `x`.
140+
- **offsetY**: starting index for `y`.
141+
142+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to perform a two-sample Z-test over every other element in `x` and `y` starting from the second element
143+
144+
```javascript
145+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
146+
147+
var x = [ 0.0, 4.0, 0.0, 4.0, 0.0, 6.0, 0.0, 6.0, 0.0, 5.0 ];
148+
var y = [ 0.0, 3.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 7.0 ];
149+
150+
var results = new Results();
151+
var out = ztest2.ndarray( 5, 'two-sided', 0.05, 0.0, 1.0, x, 2, 1, 2.0, y, 2, 1, results );
152+
// returns {...}
153+
154+
var bool = ( out === results );
155+
// returns true
156+
```
157+
158+
</section>
159+
160+
<!-- /.usage -->
161+
162+
<section class="notes">
163+
164+
## Notes
165+
166+
- As a general rule of thumb, a Z-test is most reliable when `N >= 50`. For smaller sample sizes or when the standard deviation is unknown, prefer a t-test.
167+
- 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]).
168+
- 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.
169+
170+
</section>
171+
172+
<!-- /.notes -->
173+
174+
<section class="examples">
175+
176+
## Examples
177+
178+
<!-- eslint no-undef: "error" -->
179+
180+
```javascript
181+
var Results = require( '@stdlib/stats/base/ztest/two-sample/results/float64' );
182+
var normal = require( '@stdlib/random/array/normal' );
183+
var ztest2 = require( '@stdlib/stats/strided/ztest2' );
184+
185+
var x = normal( 1000, 0.0, 1.0, {
186+
'dtype': 'generic'
187+
});
188+
var y = normal( 1000, 0.0, 1.0, {
189+
'dtype': 'generic'
190+
});
191+
192+
var results = new Results();
193+
var out = ztest2( x.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, 1.0, y, 1, results );
194+
// returns {...}
195+
196+
console.log( out.toString() );
197+
```
198+
199+
</section>
200+
201+
<!-- /.examples -->
202+
203+
<section class="references">
204+
205+
</section>
206+
207+
<!-- /.references -->
208+
209+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
210+
211+
<section class="related">
212+
213+
</section>
214+
215+
<!-- /.related -->
216+
217+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
218+
219+
<section class="links">
220+
221+
[variance]: https://en.wikipedia.org/wiki/Variance
222+
223+
[@stdlib/stats/base/ztest/alternatives]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/ztest/alternatives
224+
225+
[@stdlib/stats/base/ztest/one-sample/results/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/ztest/one-sample/results/float64
226+
227+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
228+
229+
[@stdlib/stats/strided/dztest2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dztest
230+
231+
[@stdlib/stats/strided/sztest2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/sztest
232+
233+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
234+
235+
</section>
236+
237+
<!-- /.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, '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)