Skip to content

Commit c070642

Browse files
committed
feat: add stats/strided/ztest
--- 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 265464f commit c070642

File tree

14 files changed

+2090
-0
lines changed

14 files changed

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

0 commit comments

Comments
 (0)