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