|
| 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 | +# dttest |
| 24 | + |
| 25 | +> Compute a one-sample Student's t-test for a double-precision floating-point strided array. |
| 26 | +
|
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +A one-sample t-test compares the mean of a set of measurements `X` to a given constant `μ0` when the population standard deviation is **unknown** and must be estimated from the data. Under the null hypothesis, the test statistic |
| 30 | + |
| 31 | +``` |
| 32 | +t = (x̄ - μ0) / (s / √N) |
| 33 | +``` |
| 34 | + |
| 35 | +follows a Student's t-distribution with `N - 1` degrees of freedom, where `x̄` is the sample mean and `s` is the sample standard deviation. |
| 36 | + |
| 37 | +The test supports three null hypotheses `H0`: |
| 38 | + |
| 39 | +- `H0: μ ≥ μ0` versus the alternative hypothesis `H1: μ < μ0`. |
| 40 | +- `H0: μ ≤ μ0` versus the alternative hypothesis `H1: μ > μ0`. |
| 41 | +- `H0: μ = μ0` versus the alternative hypothesis `H1: μ ≠ μ0`. |
| 42 | + |
| 43 | +</section> |
| 44 | + |
| 45 | +<!-- /.intro --> |
| 46 | + |
| 47 | +<section class="usage"> |
| 48 | + |
| 49 | +## Usage |
| 50 | + |
| 51 | +```javascript |
| 52 | +var dttest = require( '@stdlib/stats/strided/dttest' ); |
| 53 | +``` |
| 54 | + |
| 55 | +#### dttest( N, alternative, alpha, mu, x, strideX, out ) |
| 56 | + |
| 57 | +Computes a one-sample Student's t-test for a double-precision floating-point strided array. |
| 58 | + |
| 59 | +```javascript |
| 60 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 61 | + |
| 62 | +var x = new Float64Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] ); |
| 63 | + |
| 64 | +var out = {}; |
| 65 | +var results = dttest( x.length, 'two-sided', 0.05, 5.0, x, 1, out ); |
| 66 | +// returns {...} |
| 67 | + |
| 68 | +var bool = ( results === out ); |
| 69 | +// returns true |
| 70 | +``` |
| 71 | + |
| 72 | +The function has the following parameters: |
| 73 | + |
| 74 | +- **N**: number of indexed elements. Must be greater than `1`. |
| 75 | +- **alternative**: alternative hypothesis. Must be one of `'two-sided'`, `'greater'`, or `'less'`. |
| 76 | +- **alpha**: significance level in `[0,1]`. |
| 77 | +- **mu**: mean value under the null hypothesis. |
| 78 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 79 | +- **strideX**: stride length for `x`. |
| 80 | +- **out**: output results object. On return, the object will have the following properties: |
| 81 | + - **rejected**: boolean indicating whether the null hypothesis was rejected. |
| 82 | + - **alternative**: the alternative hypothesis string. |
| 83 | + - **alpha**: significance level. |
| 84 | + - **pValue**: p-value of the test. |
| 85 | + - **statistic**: value of the t-statistic. |
| 86 | + - **ci**: two-element array containing the confidence interval bounds. |
| 87 | + - **df**: degrees of freedom (`N - 1`). |
| 88 | + - **nullValue**: mean under the null hypothesis. |
| 89 | + - **mean**: sample mean. |
| 90 | + - **sd**: standard error of the mean. |
| 91 | + |
| 92 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to perform a one-sample t-test over every other element in `x`, |
| 93 | + |
| 94 | +```javascript |
| 95 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 96 | + |
| 97 | +var x = new Float64Array( [ 4.0, 0.0, 4.0, 0.0, 6.0, 0.0, 6.0, 0.0, 5.0, 0.0 ] ); |
| 98 | + |
| 99 | +var out = {}; |
| 100 | +var results = dttest( 5, 'two-sided', 0.05, 5.0, x, 2, out ); |
| 101 | +// returns {...} |
| 102 | +``` |
| 103 | + |
| 104 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 105 | + |
| 106 | +```javascript |
| 107 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 108 | + |
| 109 | +// Initial two elements are excluded from computation: |
| 110 | +var x0 = new Float64Array( [ 0.0, 0.0, 4.0, 4.0, 6.0, 6.0, 5.0 ] ); |
| 111 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT * 2 ); |
| 112 | + |
| 113 | +var out = {}; |
| 114 | +var results = dttest( 5, 'two-sided', 0.05, 5.0, x1, 1, out ); |
| 115 | +// returns {...} |
| 116 | +``` |
| 117 | + |
| 118 | +#### dttest.ndarray( N, alternative, alpha, mu, x, strideX, offsetX, out ) |
| 119 | + |
| 120 | +Computes a one-sample Student's t-test for a double-precision floating-point strided array using alternative indexing semantics. |
| 121 | + |
| 122 | +```javascript |
| 123 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 124 | + |
| 125 | +var x = new Float64Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] ); |
| 126 | + |
| 127 | +var out = {}; |
| 128 | +var results = dttest.ndarray( x.length, 'two-sided', 0.05, 5.0, x, 1, 0, out ); |
| 129 | +// returns {...} |
| 130 | + |
| 131 | +var bool = ( results === out ); |
| 132 | +// returns true |
| 133 | +``` |
| 134 | + |
| 135 | +The function has the following additional parameters: |
| 136 | + |
| 137 | +- **offsetX**: starting index for `x`. |
| 138 | + |
| 139 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the `offsetX` parameter supports indexing semantics based on a starting index. For example, to perform a one-sample t-test starting from the third element, |
| 140 | + |
| 141 | +```javascript |
| 142 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 143 | + |
| 144 | +var x = new Float64Array( [ 0.0, 0.0, 4.0, 4.0, 6.0, 6.0, 5.0 ] ); |
| 145 | + |
| 146 | +var out = {}; |
| 147 | +var results = dttest.ndarray( 5, 'two-sided', 0.05, 5.0, x, 1, 2, out ); |
| 148 | +// returns {...} |
| 149 | +``` |
| 150 | + |
| 151 | +</section> |
| 152 | + |
| 153 | +<!-- /.usage --> |
| 154 | + |
| 155 | +<section class="notes"> |
| 156 | + |
| 157 | +## Notes |
| 158 | + |
| 159 | +- If `N <= 1`, the function returns an object with all numeric fields set to `NaN` and `rejected` set to `false`. |
| 160 | +- If `alpha` is not in the interval `[0,1]` or is `NaN`, the function returns an object with all numeric fields set to `NaN`. |
| 161 | +- Unlike the z-test, the t-test does **not** require a known population standard deviation. The standard deviation is estimated from the data, and the degrees of freedom are `N - 1`. |
| 162 | +- The confidence interval is computed using the t-distribution quantile corresponding to the chosen `alpha` and `alternative`. |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.notes --> |
| 167 | + |
| 168 | +<section class="examples"> |
| 169 | + |
| 170 | +## Examples |
| 171 | + |
| 172 | +```javascript |
| 173 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 174 | +var dttest = require( '@stdlib/stats/strided/dttest' ); |
| 175 | + |
| 176 | +// Simulate data drawn from N(5, 1): |
| 177 | +var x = new Float64Array( [ 4.2, 5.1, 4.8, 5.5, 4.9, 5.3, 4.7, 5.0, 5.2, 4.6 ] ); |
| 178 | + |
| 179 | +// Two-sided test: is the mean equal to 5? |
| 180 | +var out = {}; |
| 181 | +dttest( x.length, 'two-sided', 0.05, 5.0, x, 1, out ); |
| 182 | +console.log( 'statistic: %d', out.statistic ); |
| 183 | +console.log( 'p-value: %d', out.pValue ); |
| 184 | +console.log( 'ci: [%d, %d]', out.ci[ 0 ], out.ci[ 1 ] ); |
| 185 | +console.log( 'rejected: %s', out.rejected ); |
| 186 | + |
| 187 | +// One-sided test: is the mean greater than 4? |
| 188 | +dttest( x.length, 'greater', 0.05, 4.0, x, 1, out ); |
| 189 | +console.log( '\ngreater-than test (mu=4):' ); |
| 190 | +console.log( 'p-value: %d', out.pValue ); |
| 191 | +console.log( 'rejected: %s', out.rejected ); |
| 192 | +``` |
| 193 | + |
| 194 | +</section> |
| 195 | + |
| 196 | +<!-- /.examples --> |
| 197 | + |
| 198 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 199 | + |
| 200 | +<section class="related"> |
| 201 | + |
| 202 | +## See Also |
| 203 | + |
| 204 | +- <span class="package-name">[`@stdlib/stats/strided/dztest`][@stdlib/stats/strided/dztest]</span><span class="delimiter">: </span><span class="description">compute a one-sample Z-test for a double-precision floating-point strided array.</span> |
| 205 | +- <span class="package-name">[`@stdlib/stats/strided/sttest`][@stdlib/stats/strided/sttest]</span><span class="delimiter">: </span><span class="description">compute a one-sample Student's t-test for a single-precision floating-point strided array.</span> |
| 206 | +- <span class="package-name">[`@stdlib/stats/ttest`][@stdlib/stats/ttest]</span><span class="delimiter">: </span><span class="description">one-sample and paired Student's t-test.</span> |
| 207 | + |
| 208 | +</section> |
| 209 | + |
| 210 | +<!-- /.related --> |
| 211 | + |
| 212 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 213 | + |
| 214 | +[@stdlib/stats/strided/dztest]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dztest |
| 215 | + |
| 216 | +[@stdlib/stats/strided/sttest]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/sttest |
| 217 | + |
| 218 | +[@stdlib/stats/ttest]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/ttest |
| 219 | + |
| 220 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
0 commit comments