Skip to content

Commit c81a64c

Browse files
committed
feat: add stats/base/ndarray/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 7e1b98b commit c81a64c

File tree

10 files changed

+1104
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)