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