diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/README.md b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/README.md new file mode 100644 index 000000000000..14ce4879906d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/README.md @@ -0,0 +1,259 @@ + + +# Probability Density Function + +> [Half-normal][halfnormal-distribution] distribution probability density function (PDF). + +
+ +The [probability density function][pdf] (PDF) for a [half-normal][halfnormal-distribution] random variable is + + + +```math +f(x;\sigma)=\frac{\sqrt{2}}{\sigma\sqrt{\pi}}\, e^{-\frac{x^2}{2 \sigma^2}} +``` + + + + + +where `σ` is the scale parameter. + +
+ + + +
+ +## Usage + +```javascript +var pdf = require( '@stdlib/stats/base/dists/halfnormal/pdf' ); +``` + +#### pdf( x, sigma ) + +Evaluates the [probability density function][pdf] (PDF) for a [half-normal][halfnormal-distribution] distribution with parameter `sigma` (scale parameter). + +```javascript +var y = pdf( 2.0, 1.0 ); +// returns ~0.108 + +y = pdf( 1.0, 4.0 ); +// returns ~0.193 +``` + +If provided `NaN` as any argument, the function returns `NaN`. + +```javascript +var y = pdf( NaN, 1.0 ); +// returns NaN + +y = pdf( 0.0, NaN ); +// returns NaN +``` + +If provided `sigma <= 0`, the function returns `NaN`. + +```javascript +var y = pdf( 2.0, -1.0 ); +// returns NaN + +y = pdf( 2.0, 0.0 ); +// returns NaN +``` + +If provided a negative value for `x`, the function returns `0.0`. + +```javascript +var y = pdf( -1.0, 1.0 ); +// returns 0.0 +``` + +#### pdf.factory( sigma ) + +Returns a function for evaluating the [probability density function][pdf] (PDF) of a [half-normal][halfnormal-distribution] distribution with parameter `sigma` (scale parameter). + +```javascript +var mypdf = pdf.factory( 2.0 ); + +var y = mypdf( 3.0 ); +// returns ~0.130 + +y = mypdf( 1.0 ); +// returns ~0.352 +``` + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var pdf = require( '@stdlib/stats/base/dists/halfnormal/pdf' ); + +var opts = { + 'dtype': 'float64' +}; +var sigma = uniform( 10, 0.0, 20.0, opts ); +var x = uniform( 10, 0.0, 10.0, opts ); + +logEachMap( 'x: %0.4f, σ: %0.4f, f(x;σ): %0.4f', x, sigma, pdf ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dists/halfnormal/pdf.h" +``` + +#### stdlib_base_dists_halfnormal_pdf( x, sigma ) + +Evaluates the [probability density function][pdf] (PDF) for a [half-normal][halfnormal-distribution] distribution with parameter `sigma` (scale parameter). + +```c +double y = stdlib_base_dists_halfnormal_pdf( 2.0, 1.0 ); +// returns ~0.108 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. +- **sigma**: `[in] double` scale parameter. + +```c +double stdlib_base_dists_halfnormal_pdf( const double x, const double sigma ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/dists/halfnormal/pdf.h" +#include "stdlib/constants/float64/eps.h" +#include +#include + +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); +} + +int main( void ) { + double sigma; + double x; + double y; + int i; + + for ( i = 0; i < 10; i++ ) { + x = random_uniform( 0.0, 10.0 ); + sigma = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 ); + y = stdlib_base_dists_halfnormal_pdf( x, sigma ); + printf( "x: %lf, σ: %lf, f(x;σ): %lf\n", x, sigma, y ); + } +} +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/benchmark.js new file mode 100644 index 000000000000..575780e5aa87 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/benchmark.js @@ -0,0 +1,90 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var pkg = require( './../package.json' ).name; +var pdf = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var sigma; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'float64' + }; + x = uniform( 100, 0.0, 10.0, opts ); + sigma = uniform( 100, EPS, 10.0, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = pdf( x[ i % x.length ], sigma[ i % sigma.length ] ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':factory', function benchmark( b ) { + var pdfFunc; + var sigma; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'float64' + }; + x = uniform( 100, 0.0, 10.0, opts ); + sigma = 2.0; + + pdfFunc = pdf.factory( sigma ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = pdfFunc( x[ i % x.length ] ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/benchmark.native.js new file mode 100644 index 000000000000..63a10263c7e7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/benchmark.native.js @@ -0,0 +1,68 @@ +/** +* @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 EPS = require( '@stdlib/constants/float64/eps' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var pdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( pdf instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var sigma; + var opts; + var x; + var y; + var i; + + opts = { + 'dtype': 'float64' + }; + x = uniform( 100, 0.0, 10.0, opts ); + sigma = uniform( 100, EPS, 10.0, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = pdf( x[ i % x.length ], sigma[ i % sigma.length ] ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/c/Makefile new file mode 100644 index 000000000000..979768abbcec --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/c/benchmark.c new file mode 100644 index 000000000000..6503edb5411e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/benchmark/c/benchmark.c @@ -0,0 +1,141 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/halfnormal/pdf.h" +#include "stdlib/constants/float64/eps.h" +#include +#include +#include +#include +#include + +#define NAME "halfnormal-pdf" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [min,max). +* +* @param min minimum value (inclusive) +* @param max maximum value (exclusive) +* @return random number +*/ +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + double sigma[ 100 ]; + double x[ 100 ]; + double y; + double t; + int i; + + for ( i = 0; i < 100; i++ ) { + x[ i ] = random_uniform( 0.0, 100.0 ); + sigma[ i ] = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_dists_halfnormal_pdf( x[ i%100 ], sigma[ i%100 ] ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/binding.gyp b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/binding.gyp new file mode 100644 index 000000000000..0d6508a12e99 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/repl.txt new file mode 100644 index 000000000000..94458c4ecf2c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/repl.txt @@ -0,0 +1,68 @@ + +{{alias}}( x, sigma ) + Evaluates the probability density function (PDF) for a half-normal + distribution with scale parameter `sigma` at a value `x`. + + If provided `NaN` as any argument, the function returns `NaN`. + + If provided `sigma <= 0`, the function returns `NaN`. + + If provided a negative value for `x`, the function returns `0`. + + Parameters + ---------- + x: number + Input value. + + sigma: number + Scale parameter. + + Returns + ------- + out: number + Evaluated PDF. + + Examples + -------- + > var y = {{alias}}( 0.0, 1.0 ) + ~0.798 + > y = {{alias}}( 2.0, 1.0 ) + ~0.108 + > y = {{alias}}( 0.5, 1.0 ) + ~0.704 + > y = {{alias}}( -1.0, 1.0 ) + 0.0 + > y = {{alias}}( NaN, 1.0 ) + NaN + > y = {{alias}}( 0.0, NaN ) + NaN + // Negative scale parameter: + > y = {{alias}}( 2.0, -1.0 ) + NaN + + +{{alias}}.factory( sigma ) + Returns a function for evaluating the probability density function (PDF) + of a half-normal distribution with scale parameter `sigma`. + + Parameters + ---------- + sigma: number + Scale parameter. + + Returns + ------- + pdf: Function + Probability density function (PDF). + + Examples + -------- + > var myPDF = {{alias}}.factory( 2.0 ); + > var y = myPDF( 0.0 ) + ~0.399 + > y = myPDF( 2.0 ) + ~0.242 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/types/index.d.ts new file mode 100644 index 000000000000..a000ddbf13af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/types/index.d.ts @@ -0,0 +1,115 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Evaluates the probability density function (PDF) for a half-normal distribution. +* +* @param x - input value +* @returns evaluated PDF +*/ +type Unary = ( x: number ) => number; + +/** +* Interface for the probability density function (PDF) of a half-normal distribution. +*/ +interface PDF { + /** + * Evaluates the probability density function (PDF) for a half-normal distribution with scale parameter `sigma` at a value `x`. + * + * ## Notes + * + * - If provided `sigma <= 0`, the function returns `NaN`. + * - If provided a negative value for `x`, the function returns `0`. + * + * @param x - input value + * @param sigma - scale parameter + * @returns evaluated PDF + * + * @example + * var y = pdf( 0.0, 1.0 ); + * // returns ~0.798 + * + * @example + * var y = pdf( 2.0, 1.0 ); + * // returns ~0.108 + * + * @example + * var y = pdf( 0.5, 1.0 ); + * // returns ~0.704 + * + * @example + * var y = pdf( -1.0, 1.0 ); + * // returns 0.0 + * + * @example + * var y = pdf( NaN, 1.0 ); + * // returns NaN + * + * @example + * var y = pdf( 0.0, NaN ); + * // returns NaN + * + * @example + * // Negative scale parameter: + * var y = pdf( 2.0, -1.0 ); + * // returns NaN + */ + ( x: number, sigma: number ): number; + + /** + * Returns a function for evaluating the probability density function (PDF) for a half-normal distribution with scale parameter `sigma`. + * + * @param sigma - scale parameter + * @returns PDF + * + * @example + * var myPDF = pdf.factory( 2.0 ); + * + * var y = myPDF( 0.0 ); + * // returns ~0.399 + * + * y = myPDF( 2.0 ); + * // returns ~0.242 + */ + factory( sigma: number ): Unary; +} + +/** +* Half-normal distribution probability density function (PDF). +* +* @param x - input value +* @param sigma - scale parameter +* @returns evaluated PDF +* +* @example +* var y = pdf( 2.0, 1.0 ); +* // returns ~0.108 +* +* @example +* var myPDF = pdf.factory( 2.0 ); +* var y = myPDF( 0.5 ); +* // returns ~0.387 +*/ +declare var pdf: PDF; + + +// EXPORTS // + +export = pdf; diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/types/test.ts new file mode 100644 index 000000000000..8566905c5829 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/docs/types/test.ts @@ -0,0 +1,99 @@ +/* +* @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. +*/ + +import pdf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + pdf( 2, 2 ); // $ExpectType number + pdf( 1, 8 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + pdf( true, 3 ); // $ExpectError + pdf( false, 2 ); // $ExpectError + pdf( '5', 1 ); // $ExpectError + pdf( [], 1 ); // $ExpectError + pdf( {}, 2 ); // $ExpectError + pdf( ( x: number ): number => x, 2 ); // $ExpectError + + pdf( 9, true ); // $ExpectError + pdf( 9, false ); // $ExpectError + pdf( 5, '5' ); // $ExpectError + pdf( 8, [] ); // $ExpectError + pdf( 9, {} ); // $ExpectError + pdf( 8, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + pdf(); // $ExpectError + pdf( 2 ); // $ExpectError + pdf( 2, 0, 4 ); // $ExpectError +} + +// Attached to main export is a `factory` method which returns a function... +{ + pdf.factory( 4 ); // $ExpectType Unary +} + +// The `factory` method returns a function which returns a number... +{ + const fcn = pdf.factory( 4 ); + fcn( 2 ); // $ExpectType number +} + +// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... +{ + const fcn = pdf.factory( 4 ); + fcn( true ); // $ExpectError + fcn( false ); // $ExpectError + fcn( '5' ); // $ExpectError + fcn( [] ); // $ExpectError + fcn( {} ); // $ExpectError + fcn( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... +{ + const fcn = pdf.factory( 4 ); + fcn(); // $ExpectError + fcn( 2, 0 ); // $ExpectError + fcn( 2, 0, 1 ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided values other than a number... +{ + pdf.factory( true ); // $ExpectError + pdf.factory( false ); // $ExpectError + pdf.factory( '5' ); // $ExpectError + pdf.factory( [] ); // $ExpectError + pdf.factory( {} ); // $ExpectError + pdf.factory( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided an unsupported number of arguments... +{ + pdf.factory(); // $ExpectError + pdf.factory( 2, 0 ); // $ExpectError + pdf.factory( 2, 0, 1 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/c/example.c new file mode 100644 index 000000000000..2b014e793952 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/c/example.c @@ -0,0 +1,40 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/halfnormal/pdf.h" +#include +#include + +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); +} + +int main( void ) { + double sigma; + double x; + double y; + int i; + + for ( i = 0; i < 25; i++ ) { + x = random_uniform( 0.0, 10.0 ); + sigma = random_uniform( 0.0, 10.0 ); + y = stdlib_base_dists_halfnormal_pdf( x, sigma ); + printf( "x: %lf, σ: %lf, f(x;σ): %lf\n", x, sigma, y ); + } +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/index.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/index.js new file mode 100644 index 000000000000..fc5ee6cf0ed8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/examples/index.js @@ -0,0 +1,31 @@ +/** +* @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'; + +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var pdf = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; +var x = uniform( 10, 0.0, 10.0, opts ); +var sigma = uniform( 10, 0.0, 10.0, opts ); + +logEachMap( 'x: %lf, σ: %lf, f(x;σ): %lf', x, sigma, pdf ); diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/include.gypi b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + ' + */ + function pdf( x ) { + var C; + if ( isnan( x ) ) { + return NaN; + } + if ( x < 0.0 ) { + return 0.0; + } + C = x / sigma; + return A * exp( -0.5 * ( C*C ) ); + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/index.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/index.js new file mode 100644 index 000000000000..60fad3c728b7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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'; + +/** +* Half-normal distribution probability density function (PDF). +* +* @module @stdlib/stats/base/dists/halfnormal/pdf +* +* @example +* var pdf = require( '@stdlib/stats/base/dists/halfnormal/pdf' ); +* +* var y = pdf( 2.0, 1.0 ); +* // returns ~0.108 +* +* y = pdf( 0.5, 1.0 ); +* // returns ~0.704 +* +* y = pdf( -1.0, 1.0 ); +* // returns 0.0 +* +* @example +* var pdfFactory = require( '@stdlib/stats/base/dists/halfnormal/pdf' ).factory; +* +* var pdf = pdfFactory( 1.0 ); +* var y = pdf( 2.0 ); +* // returns ~0.108 +* +* y = pdf( 1.0 ); +* // returns ~0.484 +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var factory = require( './factory.js' ); + + +// MAIN // + +setReadOnly( main, 'factory', factory ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/main.js new file mode 100644 index 000000000000..19a015661389 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/main.js @@ -0,0 +1,77 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var exp = require( '@stdlib/math/base/special/exp' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var PI = require( '@stdlib/constants/float64/pi' ); + + +// MAIN // + +/** +* Evaluates the probability density function (PDF) for a half-normal distribution. +* +* @param {number} x - input value +* @param {PositiveNumber} sigma - scale parameter +* @returns {number} evaluated PDF +* +* @example +* var y = pdf( 2.0, 1.0 ); +* // returns ~0.108 +* +* @example +* var y = pdf( 0.5, 1.0 ); +* // returns ~0.704 +* +* @example +* var y = pdf( -1.0, 1.0 ); +* // returns 0.0 +* +* @example +* var y = pdf( NaN, 1.0 ); +* // returns NaN +* +* @example +* var y = pdf( 0.0, NaN ); +* // returns NaN +* +* @example +* var y = pdf( 0.0, -1.0 ); +* // returns NaN +*/ +function pdf( x, sigma ) { + var C; + if ( isnan( x ) || isnan( sigma ) || sigma <= 0.0 ) { + return NaN; + } + if ( x < 0.0 ) { + return 0.0; + } + C = x / sigma; + return ( sqrt( 2.0 / PI ) * exp( -0.5 * ( C*C ) ) / sigma ); +} + + +// EXPORTS // + +module.exports = pdf; diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/native.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/native.js new file mode 100644 index 000000000000..14355d745477 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/lib/native.js @@ -0,0 +1,55 @@ +/** +* @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 addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Evaluates the probability density function (PDF) for a half-normal distribution. +* +* @private +* @param {number} x - input value +* @param {PositiveNumber} sigma - scale parameter +* @returns {number} evaluated PDF +* +* @example +* var y = pdf( 2.0, 1.0 ); +* // returns ~0.108 +* +* @example +* var y = pdf( 0.5, 1.0 ); +* // returns ~0.704 +* +* @example +* var y = pdf( -1.0, 1.0 ); +* // returns 0.0 +*/ +function pdf( x, sigma ) { + return addon( x, sigma ); +} + + +// EXPORTS // + +module.exports = pdf; diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/manifest.json new file mode 100644 index 000000000000..6a5815255b3a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/manifest.json @@ -0,0 +1,86 @@ +{ + "options": { + "task": "build", + "wasm": false + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "wasm": false, + "src": [ + "./src/pdf.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/binary", + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/exp", + "@stdlib/constants/float64/pi", + "@stdlib/math/base/assert/is-nan" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/pdf.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/constants/float64/eps", + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/exp", + "@stdlib/constants/float64/pi", + "@stdlib/math/base/assert/is-nan" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/pdf.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/exp", + "@stdlib/constants/float64/pi", + "@stdlib/math/base/assert/is-nan" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/package.json b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/package.json new file mode 100644 index 000000000000..ec8a756da706 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/base/dists/halfnormal/pdf", + "version": "0.0.0", + "description": "Half-normal distribution probability density function (PDF).", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "distribution", + "dist", + "continuous", + "probability", + "pdf", + "half-normal", + "univariate" + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/Makefile b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/addon.c b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/addon.c new file mode 100644 index 000000000000..27ab9192b45b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/addon.c @@ -0,0 +1,22 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/halfnormal/pdf.h" +#include "stdlib/math/base/napi/binary.h" + +STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_dists_halfnormal_pdf ) diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/pdf.c b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/pdf.c new file mode 100644 index 000000000000..6acc67b51eda --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/src/pdf.c @@ -0,0 +1,46 @@ +/** +* @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. +*/ + +#include "stdlib/stats/base/dists/halfnormal/pdf.h" +#include "stdlib/math/base/assert/is_nan.h" +#include "stdlib/math/base/special/exp.h" +#include "stdlib/math/base/special/sqrt.h" +#include "stdlib/constants/float64/pi.h" + +/** +* Evaluates the probability density function (PDF) for a half-normal distribution. +* +* @param x input value +* @param sigma scale parameter +* @return evaluated PDF +* +* @example +* double y = stdlib_base_dists_halfnormal_pdf( 2.0, 1.0 ); +* // returns ~0.108 +*/ +double stdlib_base_dists_halfnormal_pdf( const double x, const double sigma ) { + double C; + if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( sigma ) || sigma <= 0.0 ) { + return 0.0/0.0; // NaN + } + if ( x < 0.0 ) { + return 0.0; + } + C = x / sigma; + return ( stdlib_base_sqrt( 2.0 / STDLIB_CONSTANT_FLOAT64_PI ) * stdlib_base_exp( -0.5 * ( C*C ) ) / sigma ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/REQUIRE b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/REQUIRE new file mode 100644 index 000000000000..98be20b58ed3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/REQUIRE @@ -0,0 +1,3 @@ +Distributions 0.23.8 +julia 1.5 +JSON 0.21 diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/data.json b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/data.json new file mode 100644 index 000000000000..ef979174aa29 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/data.json @@ -0,0 +1 @@ +{"sigma":[0.0005027698928828538,8.33977450663294,1.739351630272375,0.013923919808057544,0.00017166006949722202,0.2250113561914013,0.0006836575750694594,3.851728262627637e-5,0.0003942374504325936,0.0013133910521928235],"expected":[540.8742605176662,0.07673827634595597,7.408574432093552e-17,42.17094618038342,0.00021875574190708885,0.11019944114042587,0.3904045154336773,1.6635539205950216e-9,0.009225932289574298,2.357160749519236e-19],"x":[0.0007376851835890518,5.538603976641584,14.832907764838476,0.010903846598781639,0.0009971590797320585,0.5928762313212765,0.0027351146104325525,0.00029911303439581356,0.001955235159985202,0.013041782749973967]} diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/runner.jl b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/runner.jl new file mode 100644 index 000000000000..38388dbb3665 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/fixtures/julia/runner.jl @@ -0,0 +1,93 @@ +#!/usr/bin/env julia +# +# @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. + +using Distributions +using JSON +using Random + +""" + halfnormal_pdf( x, sigma ) + +Evaluate the half-normal probability density function. + +The half-normal distribution is defined as + + X = |Z|, where Z ~ Normal(0, σ) + +which is equivalent to a truncated normal distribution + + Truncated( Normal(0, σ), 0, ∞ ). +""" +halfnormal_pdf( x, sigma ) = pdf( Truncated( Normal( 0.0, sigma ), 0.0, Inf ), x ) + +""" + logrand( a, b ) + +Generate a log-uniform random number in the interval [a, b]. +""" +logrand( a, b ) = exp( rand()*( log(b)-log(a) ) + log(a) ) + +""" + gen( n, name ) + +Generate randomized half-normal PDF fixture data and write to file. + +# Arguments + +* `n::Integer`: number of random samples +* `name::AbstractString`: output filename +""" +function gen( n, name ) + X = Float64[] + S = Float64[] + Z = Float64[] + + # Ensure reproducibility: + Random.seed!( 123456 ) + + for i in 1:n + # σ spans many orders of magnitude: + sigma = logrand( 1.0e-6, 10.0 ) + + # Sample x in units of σ to avoid trivial underflow: + x = rand() * 10.0 * sigma + + push!( X, x ) + push!( S, sigma ) + push!( Z, halfnormal_pdf( x, sigma ) ) + end + + # Store data to be written to file: + data = Dict([ + ( "x", X ), + ( "sigma", S ), + ( "expected", Z ) + ]) + + # Output path relative to this script: + filepath = joinpath( dirname(@__FILE__), name ) + + # Write JSON: + open( filepath, "w" ) do io + write( io, JSON.json( data ) ) + write( io, "\n" ) + end +end + +# Generate randomized fixtures: +gen( 10, "data.json" ) diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.factory.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.factory.js new file mode 100644 index 000000000000..48aa24efd0d6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.factory.js @@ -0,0 +1,163 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var factory = require( './../lib/factory.js' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var pdf = factory( 1.0 ); + t.strictEqual( typeof pdf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the created function returns `NaN`', function test( t ) { + var pdf; + var y; + + pdf = factory( 1.0 ); + y = pdf( NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + pdf = factory( NaN ); + y = pdf( 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + pdf = factory( NaN ); + y = pdf( NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a finite `sigma`, the function returns a function which returns `0` when provided `+infinity` for `x`', function test( t ) { + var pdf; + var y; + + pdf = factory( 1.0 ); + y = pdf( PINF ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a finite `sigma`, the function returns a function which returns `0` when provided `-infinity` for `x`', function test( t ) { + var pdf; + var y; + + pdf = factory( 1.0 ); + y = pdf( NINF ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a negative `sigma`, the created function always returns `NaN`', function test( t ) { + var pdf; + var y; + + pdf = factory( -1.0 ); + + y = pdf( 2.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + pdf = factory( NINF ); + y = pdf( 2.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if `sigma` equals `0`, the created function always returns `NaN`', function test( t ) { + var pdf; + var y; + + pdf = factory( 0.0 ); + + y = pdf( 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( -1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( PINF ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( NINF ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the created function evaluates the pdf for `x` given parameter `sigma`', function test( t ) { + var expected; + var delta; + var sigma; + var pdf; + var tol; + var x; + var y; + var i; + + expected = data.expected; + x = data.x; + sigma = data.sigma; + for ( i = 0; i < x.length; i++ ) { + pdf = factory( sigma[i] ); + y = pdf( x[i] ); + if ( y === expected[i] ) { + t.strictEqual( y, expected[i], 'x: '+x[i]+', sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i] ); + } else { + delta = abs( y - expected[ i ] ); + tol = 40.0 * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' ); + } + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.js new file mode 100644 index 000000000000..f21fbd456392 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.js @@ -0,0 +1,94 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var pdf = require( './../lib' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof pdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) { + var y = pdf( NaN, 1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 0.0, NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided `sigma <= 0`, the function returns `NaN`', function test( t ) { + var y; + + y = pdf( 2.0, -1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + y = pdf( 2.0, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided `x < 0`, the function returns `0`', function test( t ) { + var y = pdf( -1.0, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function evaluates the half-normal pdf', function test( t ) { + var expected; + var delta; + var sigma; + var tol; + var x; + var i; + var y; + + expected = data.expected; + x = data.x; + sigma = data.sigma; + for ( i = 0; i < expected.length; i++ ) { + y = pdf( x[i], sigma[i] ); + if ( y === expected[i] ) { + t.strictEqual( y, expected[i], 'x: '+x[i]+', sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i] ); + } else { + delta = abs( y - expected[ i ] ); + tol = 40.0 * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. x: ' + x[i] + ', sigma: ' + sigma[i] + '. y: ' + y + '. E: ' + expected[ i ] + '. Δ: ' + delta + '. tol: ' + tol + '.' ); + } + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.native.js new file mode 100644 index 000000000000..fb1a4f6d4f57 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.native.js @@ -0,0 +1,103 @@ +/** +* @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 tape = require( 'tape' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// VARIABLES // + +var pdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( pdf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof pdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) { + var y = pdf( NaN, 1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 0.0, NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided `sigma <= 0`, the function returns `NaN`', opts, function test( t ) { + var y; + + y = pdf( 2.0, -1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + y = pdf( 2.0, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided `x < 0`, the function returns `0`', opts, function test( t ) { + var y = pdf( -1.0, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function evaluates the half-normal pdf', opts, function test( t ) { + var expected; + var delta; + var sigma; + var tol; + var x; + var i; + var y; + + expected = data.expected; + x = data.x; + sigma = data.sigma; + for ( i = 0; i < expected.length; i++ ) { + y = pdf( x[i], sigma[i] ); + if ( y === expected[i] ) { + t.strictEqual( y, expected[i], 'x: '+x[i]+', sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i] ); + } else { + delta = abs( y - expected[ i ] ); + tol = 40.0 * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. x: ' + x[i] + ', sigma: ' + sigma[i] + '. y: ' + y + '. E: ' + expected[ i ] + '. Δ: ' + delta + '. tol: ' + tol + '.' ); + } + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.pdf.js b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.pdf.js new file mode 100644 index 000000000000..8e714499b96e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/halfnormal/pdf/test/test.pdf.js @@ -0,0 +1,134 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var pdf = require( './../lib' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof pdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) { + var y; + + y = pdf( NaN, 1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 0.0, NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( NaN, NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a negative value for `x`, the function returns `0`', function test( t ) { + var y = pdf( -1.0, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided `+infinity` for `x` and a finite `sigma`, the function returns `0`', function test( t ) { + var y = pdf( PINF, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a negative `sigma`, the function always returns `NaN`', function test( t ) { + var y; + + y = pdf( 2.0, -1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 0.0, -1.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 2.0, NINF ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided `sigma` equals `0`, the function always returns `NaN`', function test( t ) { + var y; + + y = pdf( 0.0, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( 1.0, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( -1.0, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( PINF, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( NINF, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + y = pdf( NaN, 0.0 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function evaluates the pdf for `x` given parameter `sigma`', function test( t ) { + var expected; + var delta; + var sigma; + var tol; + var x; + var y; + var i; + + expected = data.expected; + x = data.x; + sigma = data.sigma; + for ( i = 0; i < x.length; i++ ) { + y = pdf( x[i], sigma[i] ); + if ( y === expected[i] ) { + t.strictEqual( y, expected[i], 'x: '+x[i]+', sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i] ); + } else { + delta = abs( y - expected[ i ] ); + tol = 40 * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' ); + } + } + t.end(); +});