Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions lib/node_modules/@stdlib/stats/base/ndarray/sztest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,229 @@ console.log( v.get().toString() );

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/stats/base/ndarray/sztest.h"
```

#### stdlib_stats_sztest( arrays )

Computes a one-sample Z-test for a one-dimensional single-precision floating-point ndarray.

```c
#include "stdlib/stats/base/ztest/one-sample/results/float32.h"
#include "stdlib/stats/base/ztest/alternatives.h"
#include "stdlib/ndarray/ctor.h"
#include "stdlib/ndarray/dtypes.h"
#include "stdlib/ndarray/index_modes.h"
#include "stdlib/ndarray/orders.h"
#include "stdlib/ndarray/base/bytes_per_element.h"
#include <stdint.h>

// Create a one-dimensional input ndarray:
const float xbuf[] = { 1.0f, 3.0f, 4.0f, 2.0f };
int64_t xshape[] = { 4 };
int64_t xstrides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)xbuf, 1, xshape, xstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );

// Create a zero-dimensional output ndarray to hold the results (its buffer is written directly as a `stdlib_stats_ztest_one_sample_float32_results` struct):
uint8_t ybuf[ sizeof( struct stdlib_stats_ztest_one_sample_float32_results ) ];
int64_t *zshape = NULL;
int64_t zstrides[] = { 0 };
struct ndarray *y = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, ybuf, 0, zshape, zstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 0, submodes );

// Create a zero-dimensional ndarray specifying the alternative hypothesis:
const int8_t altbuf[] = { STDLIB_STATS_ZTEST_TWO_SIDED };
struct ndarray *alt = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT8, (uint8_t *)altbuf, 0, zshape, zstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 0, submodes );

// Create a zero-dimensional ndarray specifying the significance level:
const float alphabuf[] = { 0.05f };
struct ndarray *alpha = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)alphabuf, 0, zshape, zstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 0, submodes );

// Create a zero-dimensional ndarray specifying the mean under the null hypothesis:
const float mubuf[] = { 0.0f };
struct ndarray *mu = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)mubuf, 0, zshape, zstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 0, submodes );

// Create a zero-dimensional ndarray specifying the known standard deviation:
const float sigmabuf[] = { 1.0f };
struct ndarray *sigma = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)sigmabuf, 0, zshape, zstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 0, submodes );

// Perform a Z-test:
const struct ndarray *arrays[] = { x, y, alt, alpha, mu, sigma };
stdlib_stats_sztest( arrays );

// Interpret the output buffer as a results struct:
struct stdlib_stats_ztest_one_sample_float32_results *results = (struct stdlib_stats_ztest_one_sample_float32_results *)ybuf;
// results->rejected, results->statistic, results->pValue, etc.

// Free allocated memory:
stdlib_ndarray_free( x );
stdlib_ndarray_free( y );
stdlib_ndarray_free( alt );
stdlib_ndarray_free( alpha );
stdlib_ndarray_free( mu );
stdlib_ndarray_free( sigma );
```

The function accepts the following arguments:

- **arrays**: `[in] struct ndarray**` list containing, in order: a one-dimensional input ndarray; a zero-dimensional output ndarray whose underlying buffer is written with the test results as a `stdlib_stats_ztest_one_sample_float32_results` struct; a zero-dimensional ndarray (`int8`) specifying the alternative hypothesis; a zero-dimensional ndarray specifying the significance level; a zero-dimensional ndarray specifying the mean under the null hypothesis; and a zero-dimensional ndarray specifying the known standard deviation.

```c
void stdlib_stats_sztest( const struct ndarray *arrays[] );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/stats/base/ndarray/sztest.h"
#include "stdlib/stats/base/ztest/one-sample/results/float32.h"
#include "stdlib/stats/base/ztest/alternatives.h"
#include "stdlib/ndarray/ctor.h"
#include "stdlib/ndarray/dtypes.h"
#include "stdlib/ndarray/index_modes.h"
#include "stdlib/ndarray/orders.h"
#include "stdlib/ndarray/base/bytes_per_element.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

int main( void ) {
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;

// Create a one-dimensional input ndarray:
const float xbuf[] = { 1.0f, 3.0f, 4.0f, 2.0f, 5.0f, 3.0f, 6.0f, 2.0f };
int64_t xshape[] = { 8 };
int64_t xstrides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)xbuf, 1, xshape, xstrides, 0, order, imode, 1, submodes );
if ( x == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Create a zero-dimensional output ndarray to hold the results:
uint8_t ybuf[ sizeof( struct stdlib_stats_ztest_one_sample_float32_results ) ];
int64_t *yshape = NULL;
int64_t ystrides[] = { 0 };
struct ndarray *y = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, ybuf, 0, yshape, ystrides, 0, order, imode, 0, submodes );
if ( y == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Create a zero-dimensional ndarray specifying the alternative hypothesis:
const int8_t altbuf[] = { STDLIB_STATS_ZTEST_TWO_SIDED };
int64_t *altshape = NULL;
int64_t altstrides[] = { 0 };
struct ndarray *alt = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT8, (uint8_t *)altbuf, 0, altshape, altstrides, 0, order, imode, 0, submodes );
if ( alt == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Create a zero-dimensional ndarray specifying the significance level:
const float alphabuf[] = { 0.05f };
int64_t *alphashape = NULL;
int64_t alphastrides[] = { 0 };
struct ndarray *alpha = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)alphabuf, 0, alphashape, alphastrides, 0, order, imode, 0, submodes );
if ( alpha == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Create a zero-dimensional ndarray specifying the mean under the null hypothesis:
const float mubuf[] = { 0.0f };
int64_t *mushape = NULL;
int64_t mustrides[] = { 0 };
struct ndarray *mu = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)mubuf, 0, mushape, mustrides, 0, order, imode, 0, submodes );
if ( mu == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Create a zero-dimensional ndarray specifying the known standard deviation:
const float sigmabuf[] = { 1.0f };
int64_t *sigmashape = NULL;
int64_t sigmastrides[] = { 0 };
struct ndarray *sigma = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)sigmabuf, 0, sigmashape, sigmastrides, 0, order, imode, 0, submodes );
if ( sigma == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Define a list of ndarrays:
const struct ndarray *arrays[] = { x, y, alt, alpha, mu, sigma };

// Perform a Z-test:
stdlib_stats_sztest( arrays );

// Interpret the output buffer as a results struct:
struct stdlib_stats_ztest_one_sample_float32_results *results = (struct stdlib_stats_ztest_one_sample_float32_results *)ybuf;

// Print the results:
printf( "rejected: %s\n", ( results->rejected ) ? "true" : "false" );
printf( "statistic: %f\n", results->statistic );
printf( "p-value: %f\n", results->pValue );
printf( "ci: [%f, %f]\n", results->ci[ 0 ], results->ci[ 1 ] );

// Free allocated memory:
stdlib_ndarray_free( x );
stdlib_ndarray_free( y );
stdlib_ndarray_free( alt );
stdlib_ndarray_free( alpha );
stdlib_ndarray_free( mu );
stdlib_ndarray_free( sigma );
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var normal = require( '@stdlib/random/normal' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var Float32Results = require( '@stdlib/stats/base/ztest/one-sample/results/float32' );
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
var structFactory = require( '@stdlib/array/struct-factory' );
var format = require( '@stdlib/string/format' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var sztest = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( sztest instanceof Error )
};
var options = {
'dtype': 'float32'
};
var ResultsArray = structFactory( Float32Results );

var alt = scalar2ndarray( resolveEnum( 'two-sided' ), {
'dtype': 'int8'
});
var alpha = scalar2ndarray( 0.05, options );
var mu = scalar2ndarray( 0.0, options );
var sigma = scalar2ndarray( 1.0, options );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var obuf;
var out;
var x;

x = normal( [ len ], 0.0, 1.0, options );

obuf = new ResultsArray( 1 );
out = new ndarray( Float32Results, obuf, [], [ 0 ], 0, 'row-major' );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = sztest( [ x, out, alt, alpha, mu, sigma ] );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( isnanf( v.get().statistic ) || isnanf( v.get().pValue ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
}
}

main();
Loading
Loading