|
| 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 | +# sztest2 |
| 22 | + |
| 23 | +> Compute a two-sample Z-test for two one-dimensional single-precision floating-point ndarrays. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +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`: |
| 28 | + |
| 29 | +- `H0: μX - μY ≥ Δ` versus the alternative hypothesis `H1: μX - μY < Δ`. |
| 30 | +- `H0: μX - μY ≤ Δ` versus the alternative hypothesis `H1: μX - μY > Δ`. |
| 31 | +- `H0: μX - μY = Δ` versus the alternative hypothesis `H1: μX - μY ≠ Δ`. |
| 32 | + |
| 33 | +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). |
| 34 | + |
| 35 | +</section> |
| 36 | + |
| 37 | +<!-- /.intro --> |
| 38 | + |
| 39 | +<section class="usage"> |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +```javascript |
| 44 | +var sztest2 = require( '@stdlib/stats/base/ndarray/sztest2' ); |
| 45 | +``` |
| 46 | + |
| 47 | +#### sztest2( arrays ) |
| 48 | + |
| 49 | +Computes a two-sample Z-test for two one-dimensional single-precision floating-point ndarrays. |
| 50 | + |
| 51 | +```javascript |
| 52 | +var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' ); |
| 53 | +var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' ); |
| 54 | +var structFactory = require( '@stdlib/array/struct-factory' ); |
| 55 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 56 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 57 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 58 | + |
| 59 | +var opts = { |
| 60 | + 'dtype': 'float32' |
| 61 | +}; |
| 62 | + |
| 63 | +var xbuf = new Float32Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] ); |
| 64 | +var x = new ndarray( opts.dtype, xbuf, [ 5 ], [ 1 ], 0, 'row-major' ); |
| 65 | + |
| 66 | +var ybuf = new Float32Array( [ 3.0, 3.0, 5.0, 7.0, 7.0 ] ); |
| 67 | +var y = new ndarray( opts.dtype, ybuf, [ 5 ], [ 1 ], 0, 'row-major' ); |
| 68 | + |
| 69 | +var alt = scalar2ndarray( resolveEnum( 'two-sided' ), { |
| 70 | + 'dtype': 'int8' |
| 71 | +}); |
| 72 | +var alpha = scalar2ndarray( 0.05, opts ); |
| 73 | +var diff = scalar2ndarray( 0.0, opts ); |
| 74 | +var sigmax = scalar2ndarray( 1.0, opts ); |
| 75 | +var sigmay = scalar2ndarray( 2.0, opts ); |
| 76 | + |
| 77 | +var ResultsArray = structFactory( Float32Results ); |
| 78 | +var out = new ndarray( Float32Results, new ResultsArray( 1 ), [], [ 0 ], 0, 'row-major' ); |
| 79 | + |
| 80 | +var v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] ); |
| 81 | + |
| 82 | +var bool = ( v === out ); |
| 83 | +// returns true |
| 84 | +``` |
| 85 | + |
| 86 | +The function has the following parameters: |
| 87 | + |
| 88 | +- **arrays**: array-like object containing the following ndarrays in order: |
| 89 | + |
| 90 | + 1. first one-dimensional input ndarray. |
| 91 | + 2. second one-dimensional input ndarray. |
| 92 | + 3. a zero-dimensional output ndarray containing a [results object][@stdlib/stats/base/ztest/two-sample/results/float32]. |
| 93 | + 4. a zero-dimensional ndarray specifying the alternative hypothesis. |
| 94 | + 5. a zero-dimensional ndarray specifying the significance level. |
| 95 | + 6. a zero-dimensional ndarray specifying the difference in means under the null hypothesis. |
| 96 | + 7. a zero-dimensional ndarray specifying the known standard deviation of the first one-dimensional input ndarray. |
| 97 | + 8. a zero-dimensional ndarray specifying the known standard deviation of the second one-dimensional input ndarray. |
| 98 | + |
| 99 | +</section> |
| 100 | + |
| 101 | +<!-- /.usage --> |
| 102 | + |
| 103 | +<section class="notes"> |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +- 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. |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.notes --> |
| 112 | + |
| 113 | +<section class="examples"> |
| 114 | + |
| 115 | +## Examples |
| 116 | + |
| 117 | +<!-- eslint no-undef: "error" --> |
| 118 | + |
| 119 | +```javascript |
| 120 | +var Float32Results = require( '@stdlib/stats/base/ztest/two-sample/results/float32' ); |
| 121 | +var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' ); |
| 122 | +var structFactory = require( '@stdlib/array/struct-factory' ); |
| 123 | +var normal = require( '@stdlib/random/array/normal' ); |
| 124 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 125 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 126 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 127 | +var sztest2 = require( '@stdlib/stats/base/ndarray/sztest2' ); |
| 128 | + |
| 129 | +var opts = { |
| 130 | + 'dtype': 'float32' |
| 131 | +}; |
| 132 | + |
| 133 | +// Create one-dimensional ndarrays containing pseudorandom numbers drawn from a normal distribution: |
| 134 | +var xbuf = normal( 100, 0.0, 1.0, opts ); |
| 135 | +var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 136 | +console.log( ndarray2array( x ) ); |
| 137 | + |
| 138 | +var ybuf = normal( 100, 0.0, 1.0, opts ); |
| 139 | +var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' ); |
| 140 | +console.log( ndarray2array( y ) ); |
| 141 | + |
| 142 | +// Specify the alternative hypothesis: |
| 143 | +var alt = scalar2ndarray( resolveEnum( 'two-sided' ), { |
| 144 | + 'dtype': 'int8' |
| 145 | +}); |
| 146 | + |
| 147 | +// Specify the significance level: |
| 148 | +var alpha = scalar2ndarray( 0.05, opts ); |
| 149 | + |
| 150 | +// Specify the difference in means under the null hypothesis: |
| 151 | +var diff = scalar2ndarray( 0.0, opts ); |
| 152 | + |
| 153 | +// Specify the known standard deviations: |
| 154 | +var sigmax = scalar2ndarray( 1.0, opts ); |
| 155 | +var sigmay = scalar2ndarray( 1.0, opts ); |
| 156 | + |
| 157 | +// Create a zero-dimensional results ndarray: |
| 158 | +var ResultsArray = structFactory( Float32Results ); |
| 159 | +var out = new ndarray( Float32Results, new ResultsArray( 1 ), [], [ 0 ], 0, 'row-major' ); |
| 160 | + |
| 161 | +// Perform a Z-test: |
| 162 | +var v = sztest2( [ x, y, out, alt, alpha, diff, sigmax, sigmay ] ); |
| 163 | +console.log( v.get().toString() ); |
| 164 | +``` |
| 165 | + |
| 166 | +</section> |
| 167 | + |
| 168 | +<!-- /.examples --> |
| 169 | + |
| 170 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 171 | + |
| 172 | +<section class="related"> |
| 173 | + |
| 174 | +</section> |
| 175 | + |
| 176 | +<!-- /.related --> |
| 177 | + |
| 178 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 179 | + |
| 180 | +<section class="links"> |
| 181 | + |
| 182 | +[@stdlib/stats/base/ztest/two-sample/results/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/ztest/two-sample/results/float32 |
| 183 | + |
| 184 | +</section> |
| 185 | + |
| 186 | +<!-- /.links --> |
0 commit comments