Skip to content

Commit 1ce2a18

Browse files
committed
feat: add stats/strided/dztest
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: skipped - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 19ae8b4 commit 1ce2a18

33 files changed

+3502
-0
lines changed
Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
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+
# dztest
24+
25+
> Compute a one-sample Z-test for a double-precision floating-point strided array.
26+
27+
<section class="intro">
28+
29+
TODO: add introduction
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var dztest = require( '@stdlib/stats/strided/dztest' );
41+
```
42+
43+
#### dztest( N, alternative, alpha, mu, sigma, x, strideX, out )
44+
45+
Computes a one-sample Z-test for a double-precision floating-point strided array.
46+
47+
```javascript
48+
var Float64Array = require( '@stdlib/array/float64' );
49+
var Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
50+
51+
var x = new Float64Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] );
52+
53+
var results = new Results();
54+
var out = dztest( x.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, results );
55+
// returns {...}
56+
57+
var bool = ( out === results );
58+
// returns true
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **N**: number of indexed elements.
64+
- **alternative**: [alternative hypothesis][@stdlib/stats/base/ztest/alternatives].
65+
- **alpha**: significance level.
66+
- **mu**: mean value under the null hypothesis.
67+
- **sigma**: known standard deviation.
68+
- **x**: input [`Float64Array`][@stdlib/array/float64].
69+
- **strideX**: stride length for `x`.
70+
- **out**: output [results object][@stdlib/stats/base/ztest/one-sample/results/float64].
71+
72+
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`,
73+
74+
```javascript
75+
var Float64Array = require( '@stdlib/array/float64' );
76+
var Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
77+
78+
var x = new Float64Array( [ 4.0, 0.0, 4.0, 0.0, 6.0, 0.0, 6.0, 0.0, 5.0, 0.0 ] );
79+
80+
var results = new Results();
81+
var out = dztest( x.length, 'two-sided', 0.05, 0.0, 1.0, x, 2, results );
82+
// returns {...}
83+
84+
var bool = ( out === results );
85+
// returns true
86+
```
87+
88+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
89+
90+
<!-- eslint-disable stdlib/capitalized-comments -->
91+
92+
```javascript
93+
var Float64Array = require( '@stdlib/array/float64' );
94+
var Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
95+
96+
var x0 = new Float64Array( [ 0.0, 4.0, 4.0, 6.0, 6.0, 5.0 ] );
97+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
98+
99+
var results = new Results();
100+
var out = dztest( x.length, 'two-sided', 0.05, 0.0, 1.0, x1, 1, results );
101+
// returns {...}
102+
103+
var bool = ( out === results );
104+
// returns true
105+
```
106+
107+
#### dztest.ndarray( N, alternative, alpha, mu, sigma, x, strideX, offsetX, out )
108+
109+
Computes a one-sample Z-test for a double-precision floating-point strided array using alternative indexing semantics.
110+
111+
```javascript
112+
var Float64Array = require( '@stdlib/array/float64' );
113+
var Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
114+
115+
var x = new Float64Array( [ 4.0, 4.0, 6.0, 6.0, 5.0 ] );
116+
117+
var results = new Results();
118+
var out = dztest.ndarray( x.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, 0, results );
119+
// returns {...}
120+
121+
var bool = ( out === results );
122+
// returns true
123+
```
124+
125+
The function has the following additional parameters:
126+
127+
- **offsetX**: starting index for `x`.
128+
129+
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
130+
131+
```javascript
132+
var Float64Array = require( '@stdlib/array/float64' );
133+
var Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
134+
135+
var x = new Float64Array( [ 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 = dztest.ndarray( x.length, '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+
</section>
152+
153+
<!-- /.notes -->
154+
155+
<section class="examples">
156+
157+
## Examples
158+
159+
<!-- eslint no-undef: "error" -->
160+
161+
```javascript
162+
var normal = require( '@stdlib/random/array/normal' );
163+
var Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
164+
var dztest = require( '@stdlib/stats/strided/dztest' );
165+
166+
var x = normal( 1000, 0.0, 1.0, {
167+
'dtype': 'float64'
168+
});
169+
170+
var results = new Results();
171+
var out = dztest( x.length, 'two-sided', 0.05, 0.0, 1.0, x, 1, results );
172+
// returns {...}
173+
174+
console.log( out.toString() );
175+
```
176+
177+
</section>
178+
179+
<!-- /.examples -->
180+
181+
<!-- C interface documentation. -->
182+
183+
* * *
184+
185+
<section class="c">
186+
187+
## C APIs
188+
189+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
190+
191+
<section class="intro">
192+
193+
</section>
194+
195+
<!-- /.intro -->
196+
197+
<!-- C usage documentation. -->
198+
199+
<section class="usage">
200+
201+
### Usage
202+
203+
```c
204+
#include "stdlib/stats/strided/dztest.h"
205+
```
206+
207+
#### stdlib_strided_dztest( N, alternative, alpha, mu, sigma, \*X, strideX, \*results )
208+
209+
Computes a one-sample Z-test for a double-precision floating-point strided array.
210+
211+
```c
212+
#include "stdlib/stats/base/ztest/one-sample/results/float64.h"
213+
#include "stdlib/stats/base/ztest/alternatives.h"
214+
215+
struct stdlib_stats_ztest_one_sample_float64_results results = {
216+
.rejected = false,
217+
.alpha = 0.0,
218+
.alternative = STDLIB_STATS_ZTEST_TWO_SIDED,
219+
.pValue = 0.0,
220+
.statistic = 0.0,
221+
.ci = { 0.0, 0.0 },
222+
.nullValue = 0.0,
223+
.sd = 0.0
224+
};
225+
226+
const double x[] = { 4.0, 4.0, 6.0, 6.0, 5.0 };
227+
228+
stdlib_strided_dztest( 5, STDLIB_STATS_ZTEST_TWO_SIDED, 0.05, 0.0, 1.0, x, 1, &results );
229+
```
230+
231+
The function accepts the following arguments:
232+
233+
- **N**: `[in] CBLAS_INT` number of indexed elements.
234+
- **alternative**: `[in] enum STDLIB_STATS_ZTEST_ALTERNATIVE` [alternative hypothesis][@stdlib/stats/base/ztest/alternatives].
235+
- **alpha**: significance level.
236+
- **mu**: value of the mean under the null hypothesis.
237+
- **sigma** known standard deviation.
238+
- **X**: `[in] double*` input array.
239+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
240+
- **results**: `[out] struct stdlib_stats_ztest_one_sample_results_float64*` output [results object][@stdlib/stats/base/ztest/one-sample/results/float64].
241+
242+
```c
243+
void stdlib_strided_dztest( const CBLAS_INT N, const enum STDLIB_STATS_ZTEST_ALTERNATIVE alternative, const double alpha, const double mu, const double sigma, const double *X, const CBLAS_INT strideX, struct stdlib_stats_ztest_one_sample_float64_results *results );
244+
```
245+
246+
#### stdlib_strided_dztest_ndarray( N, alternative, alpha, mu, sigma, \*X, strideX, offsetX, \*results )
247+
248+
Computes a one-sample Z-test for a double-precision floating-point strided array using alternative indexing semantics.
249+
250+
```c
251+
#include "stdlib/stats/base/ztest/one-sample/results/float64.h"
252+
#include "stdlib/stats/base/ztest/alternatives.h"
253+
254+
struct stdlib_stats_ztest_one_sample_float64_results results = {
255+
.rejected = false,
256+
.alpha = 0.0,
257+
.alternative = STDLIB_STATS_ZTEST_TWO_SIDED,
258+
.pValue = 0.0,
259+
.statistic = 0.0,
260+
.ci = { 0.0, 0.0 },
261+
.nullValue = 0.0,
262+
.sd = 0.0
263+
};
264+
265+
const double x[] = { 4.0, 4.0, 6.0, 6.0, 5.0 };
266+
267+
stdlib_strided_dztest_ndarray( 5, STDLIB_STATS_ZTEST_TWO_SIDED, 0.05, 0.0, 1.0, x, 1, 0, &results );
268+
```
269+
270+
The function accepts the following arguments:
271+
272+
- **N**: `[in] CBLAS_INT` number of indexed elements.
273+
- **alternative**: `[in] enum STDLIB_STATS_ZTEST_ALTERNATIVE` [alternative hypothesis][@stdlib/stats/base/ztest/alternatives].
274+
- **alpha**: significance level.
275+
- **mu**: value of the mean under the null hypothesis.
276+
- **sigma** known standard deviation.
277+
- **X**: `[in] double*` input array.
278+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
279+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
280+
- **results**: `[out] struct stdlib_stats_ztest_one_sample_results_float64*` output [results object][@stdlib/stats/base/ztest/one-sample/results/float64].
281+
282+
```c
283+
void stdlib_strided_dztest_ndarray( const CBLAS_INT N, const enum STDLIB_STATS_ZTEST_ALTERNATIVE alternative, const double alpha, const double mu, const double sigma, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, struct stdlib_stats_ztest_one_sample_float64_results *results );
284+
```
285+
286+
</section>
287+
288+
<!-- /.usage -->
289+
290+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
291+
292+
<section class="notes">
293+
294+
</section>
295+
296+
<!-- /.notes -->
297+
298+
<!-- C API usage examples. -->
299+
300+
<section class="examples">
301+
302+
### Examples
303+
304+
```c
305+
#include "stdlib/stats/strided/dztest.h"
306+
#include "stdlib/stats/base/ztest/one-sample/results/float64.h"
307+
#include "stdlib/stats/base/ztest/alternatives.h"
308+
#include <stdbool.h>
309+
#include <stdio.h>
310+
311+
int main( void ) {
312+
// Create a strided array:
313+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
314+
315+
// Specify the number of elements:
316+
const int N = 4;
317+
318+
// Specify the stride length:
319+
const int strideX = 2;
320+
321+
// Initialize a results object:
322+
struct stdlib_stats_ztest_one_sample_float64_results results = {
323+
.rejected = false,
324+
.alpha = 0.0,
325+
.alternative = STDLIB_STATS_ZTEST_TWO_SIDED,
326+
.pValue = 0.0,
327+
.statistic = 0.0,
328+
.ci = { 0.0, 0.0 },
329+
.nullValue = 0.0,
330+
.sd = 0.0
331+
};
332+
333+
// Compute a z-test:
334+
stdlib_strided_dztest( N, STDLIB_STATS_ZTEST_TWO_SIDED, 0.05, 5.0, 3.0, x, strideX, &results );
335+
336+
// Print the result:
337+
printf( "Statistic: %lf\n", results.statistic );
338+
printf( "Null hypothesis was %s\n", ( results.rejected ) ? "rejected" : "not rejected" );
339+
}
340+
```
341+
342+
</section>
343+
344+
<!-- /.examples -->
345+
346+
</section>
347+
348+
<!-- /.c -->
349+
350+
<section class="references">
351+
352+
</section>
353+
354+
<!-- /.references -->
355+
356+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
357+
358+
<section class="related">
359+
360+
</section>
361+
362+
<!-- /.related -->
363+
364+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
365+
366+
<section class="links">
367+
368+
[variance]: https://en.wikipedia.org/wiki/Variance
369+
370+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
371+
372+
[@stdlib/stats/base/ztest/alternatives]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/ztest/alternatives
373+
374+
[@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
375+
376+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
377+
378+
</section>
379+
380+
<!-- /.links -->

0 commit comments

Comments
 (0)