|
| 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 --> |
0 commit comments