Skip to content

Commit 9e93136

Browse files
committed
feat: add stats/base/ndarray/sztest
--- 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 a056348 commit 9e93136

File tree

10 files changed

+1108
-0
lines changed

10 files changed

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