diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sztest/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/sztest/README.md index 8101cd54d391..88a2c7f940e4 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/sztest/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sztest/README.md @@ -152,6 +152,229 @@ console.log( v.get().toString() ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### 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 + +// 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[] ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### 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 +#include +#include + +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 ); +} +``` + +
+ + + +
+ + +