From d3bf11582fa64d104f19b76c9d359e6c29d4dac3 Mon Sep 17 00:00:00 2001 From: Uday Gund Date: Fri, 4 Dec 2026 19:30:07 +0530 Subject: [PATCH 1/2] fix: improve accuracy in the tails for `stats/base/dists/truncated-normal/pdf` When both endpoints lie on the same side of `mu`, evaluates the normalizing constant relative to the density at the endpoint closest to `mu` using `erfcx`, avoiding the cancellation of `Phi(zb) - Phi(za)` and the underflow of `exp(-z^2/2)`. Resolves: https://github.com/stdlib-js/stdlib/issues/17149 Co-Authored-By: Claude Opus 5.5 --- 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_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../dists/truncated-normal/pdf/lib/factory.js | 39 ++++++++- .../dists/truncated-normal/pdf/lib/main.js | 27 ++++++ .../dists/truncated-normal/pdf/manifest.json | 15 +++- .../dists/truncated-normal/pdf/src/main.c | 27 ++++++ .../truncated-normal/pdf/test/test.native.js | 87 +++++++++++++++++++ .../truncated-normal/pdf/test/test.pdf.js | 87 +++++++++++++++++++ 6 files changed, 277 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js index 3fd133fa8007..a72a983efe3b 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js @@ -25,7 +25,10 @@ var exp = require( '@stdlib/math/base/special/exp' ); var abs2 = require( '@stdlib/math/base/special/abs2' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var normal = require( '@stdlib/stats/base/dists/normal/cdf' ).factory; +var erfcx = require( '@stdlib/math/base/special/erfcx' ); +var abs = require( '@stdlib/math/base/special/abs' ); var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' ); +var SQRT_TWO = require( '@stdlib/constants/float64/sqrt-two' ); // VARIABLES // @@ -65,8 +68,16 @@ var normalCDF = normal( 0.0, 1.0 ); * // returns 0.0 */ function factory( a, b, mu, sigma ) { + var tail; + var zc; + var zo; var A; var C; + var c; + var d; + var e; + var o; + var t; if ( isnan( a ) || @@ -78,8 +89,27 @@ function factory( a, b, mu, sigma ) { ) { return constantFunction( NaN ); } - A = 1.0 / ( sigma * SQRT_TWO_PI ); - C = normalCDF( (b-mu)/sigma ) - normalCDF( (a-mu)/sigma ); + + // If both endpoints lie on the same side of `mu`, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids both the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1` and the underflow of `exp(-z^2/2)`. Differences such as `zc - z` are computed from the inputs, as `(c-x)/sigma`, to avoid amplifying rounding errors: + tail = ( a >= mu || b <= mu ); + if ( tail ) { + if ( a >= mu ) { + c = a; + o = b; + } else { + c = b; + o = a; + } + zc = abs( c-mu ) / sigma; + zo = abs( o-mu ) / sigma; + e = ( (c-o)/sigma ) * ( ( (c-mu)+(o-mu) ) / sigma ); + t = erfcx( zo/SQRT_TWO ) * exp( 0.5*e ); + d = 0.5 * ( erfcx( zc/SQRT_TWO ) - t ); + A = 1.0 / ( sigma * SQRT_TWO_PI * d ); + } else { + A = 1.0 / ( sigma * SQRT_TWO_PI ); + C = normalCDF( (b-mu)/sigma ) - normalCDF( (a-mu)/sigma ); + } return pdf; /** @@ -90,12 +120,17 @@ function factory( a, b, mu, sigma ) { * @returns {number} evaluated PDF */ function pdf( x ) { + var e; if ( isnan( x ) ) { return NaN; } if ( x < a || x > b ) { return 0.0; } + if ( tail ) { + e = ( (c-x)/sigma ) * ( ( (c-mu)+(x-mu) ) / sigma ); + return A * exp( 0.5*e ); + } return A * exp( -0.5 * abs2( ( x - mu ) / sigma ) ) / C; } } diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js index ac390d8251cc..26d3992aef4a 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js @@ -24,7 +24,10 @@ var exp = require( '@stdlib/math/base/special/exp' ); var abs2 = require( '@stdlib/math/base/special/abs2' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var normal = require( '@stdlib/stats/base/dists/normal/cdf' ).factory; +var erfcx = require( '@stdlib/math/base/special/erfcx' ); +var abs = require( '@stdlib/math/base/special/abs' ); var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' ); +var SQRT_TWO = require( '@stdlib/constants/float64/sqrt-two' ); // VARIABLES // @@ -65,8 +68,15 @@ var normalCDF = normal( 0.0, 1.0 ); * // returns 0.0 */ function pdf( x, a, b, mu, sigma ) { + var zc; + var zo; var A; var C; + var c; + var d; + var e; + var o; + var t; if ( isnan( x ) || @@ -80,6 +90,23 @@ function pdf( x, a, b, mu, sigma ) { if ( x < a || x > b ) { return 0.0; } + // If both endpoints lie on the same side of `mu`, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids both the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1` and the underflow of `exp(-z^2/2)`. Differences such as `zc - z` are computed from the inputs, as `(c-x)/sigma`, to avoid amplifying rounding errors: + if ( a >= mu || b <= mu ) { + if ( a >= mu ) { + c = a; + o = b; + } else { + c = b; + o = a; + } + zc = abs( c-mu ) / sigma; + zo = abs( o-mu ) / sigma; + e = ( (c-o)/sigma ) * ( ( (c-mu)+(o-mu) ) / sigma ); + t = erfcx( zo/SQRT_TWO ) * exp( 0.5*e ); + d = 0.5 * ( erfcx( zc/SQRT_TWO ) - t ); + e = ( (c-x)/sigma ) * ( ( (c-mu)+(x-mu) ) / sigma ); + return exp( 0.5*e ) / ( sigma * SQRT_TWO_PI * d ); + } A = 1.0 / ( sigma * SQRT_TWO_PI ); C = normalCDF( (b-mu)/sigma ) - normalCDF( (a-mu)/sigma ); return A * exp( -0.5 * abs2( ( x - mu ) / sigma ) ) / C; diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/manifest.json index d1866c25fc43..eadac7cff867 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/manifest.json @@ -43,7 +43,10 @@ "@stdlib/math/base/special/exp", "@stdlib/math/base/special/abs2", "@stdlib/constants/float64/sqrt-two-pi", - "@stdlib/stats/base/dists/normal/cdf" + "@stdlib/stats/base/dists/normal/cdf", + "@stdlib/math/base/special/erfcx", + "@stdlib/math/base/special/abs", + "@stdlib/constants/float64/sqrt-two" ] }, { @@ -62,7 +65,10 @@ "@stdlib/math/base/special/exp", "@stdlib/math/base/special/abs2", "@stdlib/constants/float64/sqrt-two-pi", - "@stdlib/stats/base/dists/normal/cdf" + "@stdlib/stats/base/dists/normal/cdf", + "@stdlib/math/base/special/erfcx", + "@stdlib/math/base/special/abs", + "@stdlib/constants/float64/sqrt-two" ] }, { @@ -81,7 +87,10 @@ "@stdlib/math/base/special/exp", "@stdlib/math/base/special/abs2", "@stdlib/constants/float64/sqrt-two-pi", - "@stdlib/stats/base/dists/normal/cdf" + "@stdlib/stats/base/dists/normal/cdf", + "@stdlib/math/base/special/erfcx", + "@stdlib/math/base/special/abs", + "@stdlib/constants/float64/sqrt-two" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c index b6052cd49781..9cdc562b1750 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c @@ -22,6 +22,9 @@ #include "stdlib/math/base/special/abs2.h" #include "stdlib/stats/base/dists/normal/cdf.h" #include "stdlib/constants/float64/sqrt_two_pi.h" +#include "stdlib/math/base/special/erfcx.h" +#include "stdlib/math/base/special/abs.h" +#include "stdlib/constants/float64/sqrt_two.h" /** * Evaluates the probability density function (PDF) for a truncated normal distribution with endpoints `a` and `b`, location parameter `mu`, and scale parameter `sigma` at a value `x`. @@ -41,6 +44,13 @@ double stdlib_base_dists_truncated_normal_pdf( const double x, const double a, c double z; double A; double C; + double zc; + double zo; + double c; + double d; + double e; + double o; + double t; if ( stdlib_base_is_nan( x ) || @@ -54,6 +64,23 @@ double stdlib_base_dists_truncated_normal_pdf( const double x, const double a, c if ( x < a || x > b ) { return 0.0; } + // If both endpoints lie on the same side of `mu`, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids both the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1` and the underflow of `exp(-z^2/2)`. Differences such as `zc - z` are computed from the inputs, as `(c-x)/sigma`, to avoid amplifying rounding errors: + if ( a >= mu || b <= mu ) { + if ( a >= mu ) { + c = a; + o = b; + } else { + c = b; + o = a; + } + zc = stdlib_base_abs( c - mu ) / sigma; + zo = stdlib_base_abs( o - mu ) / sigma; + e = ( ( c - o ) / sigma ) * ( ( ( c - mu ) + ( o - mu ) ) / sigma ); + t = stdlib_base_erfcx( zo / STDLIB_CONSTANT_FLOAT64_SQRT2 ) * stdlib_base_exp( 0.5 * e ); + d = 0.5 * ( stdlib_base_erfcx( zc / STDLIB_CONSTANT_FLOAT64_SQRT2 ) - t ); + e = ( ( c - x ) / sigma ) * ( ( ( c - mu ) + ( x - mu ) ) / sigma ); + return stdlib_base_exp( 0.5 * e ) / ( sigma * STDLIB_CONSTANT_FLOAT64_SQRT_TWO_PI * d ); + } A = 1.0 / ( sigma * STDLIB_CONSTANT_FLOAT64_SQRT_TWO_PI ); C = stdlib_base_dists_normal_cdf( ( b - mu ) / sigma, 0.0, 1.0 ) - stdlib_base_dists_normal_cdf( ( a - mu ) / sigma, 0.0, 1.0 ); z = ( x - mu ) / sigma; diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js index 61d4f43f7279..07e9e412edf7 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js @@ -23,6 +23,8 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); var tryRequire = require( '@stdlib/utils/try-require' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); @@ -142,3 +144,88 @@ tape( 'the function evaluates the PDF', opts, function test( t ) { } t.end(); }); + +tape( 'the function evaluates the PDF when both endpoints lie on the same side of `mu` (e.g., far in the tails)', opts, function test( t ) { + var expected; + var sigma; + var mu; + var a; + var b; + var x; + var y; + var i; + + // Reference values computed with 130 digits of precision from `phi(z) / ( sigma * ( Q(za) - Q(zb) ) )`, where `Q(t) = erfc(t/sqrt(2))/2`, with `erfc` evaluated from its power series and continued fraction: + x = [ + 11.0, + 9.0, + 7.0, + 40.0, + -11.0, + -7.0, + 105.0, + 5.5, + 3.2 + ]; + a = [ + 10.0, + 8.0, + 6.0, + 40.0, + -12.0, + NINF, + 100.0, + 5.0, + 3.0 + ]; + b = [ + 12.0, + 10.0, + PINF, + 41.0, + -10.0, + -6.0, + 110.0, + 6.0, + 4.0 + ]; + mu = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 50.0, + 0.0, + 0.0 + ]; + sigma = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 5.0, + 1.0, + 1.0 + ]; + expected = [ + 0.000278065632930861, + 0.001652441544171957, + 0.00925890411694295, + 40.02496884720726, + 0.000278065632930861, + 0.00925890411694295, + 5.5613126586172206e-05, + 0.3770066559405626, + 1.8085569341463803 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = pdf( x[ i ], a[ i ], b[ i ], mu[ i ], sigma[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 5 ), true, 'returns expected value' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js index 5069c836dea2..83e340ba599e 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js @@ -23,6 +23,8 @@ var tape = require( 'tape' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); var pdf = require( './../lib' ); @@ -137,3 +139,88 @@ tape( 'the function evaluates the PDF', function test( t ) { } t.end(); }); + +tape( 'the function evaluates the PDF when both endpoints lie on the same side of `mu` (e.g., far in the tails)', function test( t ) { + var expected; + var sigma; + var mu; + var a; + var b; + var x; + var y; + var i; + + // Reference values computed with 130 digits of precision from `phi(z) / ( sigma * ( Q(za) - Q(zb) ) )`, where `Q(t) = erfc(t/sqrt(2))/2`, with `erfc` evaluated from its power series and continued fraction: + x = [ + 11.0, + 9.0, + 7.0, + 40.0, + -11.0, + -7.0, + 105.0, + 5.5, + 3.2 + ]; + a = [ + 10.0, + 8.0, + 6.0, + 40.0, + -12.0, + NINF, + 100.0, + 5.0, + 3.0 + ]; + b = [ + 12.0, + 10.0, + PINF, + 41.0, + -10.0, + -6.0, + 110.0, + 6.0, + 4.0 + ]; + mu = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 50.0, + 0.0, + 0.0 + ]; + sigma = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 5.0, + 1.0, + 1.0 + ]; + expected = [ + 0.000278065632930861, + 0.001652441544171957, + 0.00925890411694295, + 40.02496884720726, + 0.000278065632930861, + 0.00925890411694295, + 5.5613126586172206e-05, + 0.3770066559405626, + 1.8085569341463803 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = pdf( x[ i ], a[ i ], b[ i ], mu[ i ], sigma[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 5 ), true, 'returns expected value' ); + } + t.end(); +}); From 87a3d3cd0044f2349e215887cbc85667c50533be Mon Sep 17 00:00:00 2001 From: Uday Gund Date: Fri, 4 Dec 2026 20:33:05 +0530 Subject: [PATCH 2/2] fix: use the tail formula only when the interval is at least two standard deviations from `mu` For intervals closer to `mu`, the difference of the erfcx terms is less accurate than `Phi(zb) - Phi(za)` (e.g., up to 30 times larger errors for `[0, 1e-8]` and `[0.3, 0.4]`). The crossover is near `1.8` standard deviations. Also adds tests for intervals near `mu` and a test file for the `factory` method. Co-Authored-By: Claude Opus 5.5 --- 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_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../dists/truncated-normal/pdf/lib/factory.js | 4 +- .../dists/truncated-normal/pdf/lib/main.js | 5 +- .../dists/truncated-normal/pdf/src/main.c | 11 +- .../truncated-normal/pdf/test/test.factory.js | 235 ++++++++++++++++++ .../truncated-normal/pdf/test/test.native.js | 85 +++++++ .../truncated-normal/pdf/test/test.pdf.js | 85 +++++++ 6 files changed, 416 insertions(+), 9 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.factory.js diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js index a72a983efe3b..83d22a1d8213 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js @@ -90,8 +90,8 @@ function factory( a, b, mu, sigma ) { return constantFunction( NaN ); } - // If both endpoints lie on the same side of `mu`, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids both the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1` and the underflow of `exp(-z^2/2)`. Differences such as `zc - z` are computed from the inputs, as `(c-x)/sigma`, to avoid amplifying rounding errors: - tail = ( a >= mu || b <= mu ); + // If both endpoints lie on the same side of `mu`, at least two standard deviations away from it, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1`, as well as the underflow of `exp(-z^2/2)`. Differences of squares such as `zc^2 - z^2` are computed from the inputs, as `((c-x)/sigma) * ((c-mu)+(x-mu))/sigma`, to avoid amplifying rounding errors. Closer to `mu`, the direct formula is more accurate: + tail = ( a-mu >= 2.0*sigma || mu-b >= 2.0*sigma ); if ( tail ) { if ( a >= mu ) { c = a; diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js index 26d3992aef4a..4422e806f360 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js @@ -90,8 +90,9 @@ function pdf( x, a, b, mu, sigma ) { if ( x < a || x > b ) { return 0.0; } - // If both endpoints lie on the same side of `mu`, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids both the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1` and the underflow of `exp(-z^2/2)`. Differences such as `zc - z` are computed from the inputs, as `(c-x)/sigma`, to avoid amplifying rounding errors: - if ( a >= mu || b <= mu ) { + + // If both endpoints lie on the same side of `mu`, at least two standard deviations away from it, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1`, as well as the underflow of `exp(-z^2/2)`. Differences of squares such as `zc^2 - z^2` are computed from the inputs, as `((c-x)/sigma) * ((c-mu)+(x-mu))/sigma`, to avoid amplifying rounding errors. Closer to `mu`, the direct formula is more accurate: + if ( a-mu >= 2.0*sigma || mu-b >= 2.0*sigma ) { if ( a >= mu ) { c = a; o = b; diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c index 9cdc562b1750..3002bd8141cd 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/src/main.c @@ -41,16 +41,16 @@ * // returns ~0.7795 */ double stdlib_base_dists_truncated_normal_pdf( const double x, const double a, const double b, const double mu, const double sigma ) { - double z; - double A; - double C; double zc; double zo; + double A; + double C; double c; double d; double e; double o; double t; + double z; if ( stdlib_base_is_nan( x ) || @@ -64,8 +64,9 @@ double stdlib_base_dists_truncated_normal_pdf( const double x, const double a, c if ( x < a || x > b ) { return 0.0; } - // If both endpoints lie on the same side of `mu`, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids both the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1` and the underflow of `exp(-z^2/2)`. Differences such as `zc - z` are computed from the inputs, as `(c-x)/sigma`, to avoid amplifying rounding errors: - if ( a >= mu || b <= mu ) { + + // If both endpoints lie on the same side of `mu`, at least two standard deviations away from it, let `c` be the endpoint closest to `mu` and `o` the other one. Since `Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` (and, by symmetry, `Phi(-t) = Q(t)`), dividing both the numerator and the normalizing constant by `exp(-zc^2/2)` avoids the cancellation of `Phi(zb) - Phi(za)` when both values are close to `0` or `1`, as well as the underflow of `exp(-z^2/2)`. Differences of squares such as `zc^2 - z^2` are computed from the inputs, as `((c-x)/sigma) * ((c-mu)+(x-mu))/sigma`, to avoid amplifying rounding errors. Closer to `mu`, the direct formula is more accurate: + if ( a - mu >= 2.0 * sigma || mu - b >= 2.0 * sigma ) { if ( a >= mu ) { c = a; o = b; diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.factory.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.factory.js new file mode 100644 index 000000000000..7d9c81a10f3f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.factory.js @@ -0,0 +1,235 @@ +/** +* @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 isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var factory = require( './../lib/factory.js' ); + + +// 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( 'if provided `NaN` for any parameter, the created function returns `NaN`', function test( t ) { + var pdf; + var y; + + pdf = factory( 0.0, 1.0, 0.0, 1.0 ); + y = pdf( NaN ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + pdf = factory( NaN, 1.0, 0.0, 1.0 ); + y = pdf( 0.5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + pdf = factory( 0.0, 1.0, 0.0, NaN ); + y = pdf( 0.5 ); + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the created function returns `0` if provided `x` outside `[a, b]`', function test( t ) { + var pdf = factory( 3.0, 4.0, 0.0, 1.0 ); + t.strictEqual( pdf( 2.5 ), 0.0, 'returns expected value' ); + t.strictEqual( pdf( 4.5 ), 0.0, 'returns expected value' ); + t.strictEqual( pdf( PINF ), 0.0, 'returns expected value' ); + t.strictEqual( pdf( NINF ), 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the created function evaluates the PDF when both endpoints lie on the same side of `mu` (e.g., far in the tails)', function test( t ) { + var expected; + var sigma; + var mu; + var a; + var b; + var x; + var y; + var i; + + // Reference values computed with 130 digits of precision from `phi(z) / ( sigma * ( Q(za) - Q(zb) ) )`, where `Q(t) = erfc(t/sqrt(2))/2`, with `erfc` evaluated from its power series and continued fraction: + x = [ + 11.0, + 9.0, + 7.0, + 40.0, + -11.0, + -7.0, + 105.0, + 5.5, + 3.2 + ]; + a = [ + 10.0, + 8.0, + 6.0, + 40.0, + -12.0, + NINF, + 100.0, + 5.0, + 3.0 + ]; + b = [ + 12.0, + 10.0, + PINF, + 41.0, + -10.0, + -6.0, + 110.0, + 6.0, + 4.0 + ]; + mu = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 50.0, + 0.0, + 0.0 + ]; + sigma = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 5.0, + 1.0, + 1.0 + ]; + expected = [ + 0.000278065632930861, + 0.001652441544171957, + 0.00925890411694295, + 40.02496884720726, + 0.000278065632930861, + 0.00925890411694295, + 5.5613126586172206e-05, + 0.3770066559405626, + 1.8085569341463803 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = factory( a[ i ], b[ i ], mu[ i ], sigma[ i ] )( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 5 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the created function evaluates the PDF when the endpoints lie close to `mu`', function test( t ) { + var expected; + var sigma; + var mu; + var a; + var b; + var x; + var y; + var i; + + // Intervals near `mu`, including intervals ending at `mu` and intervals just below and above the two-standard-deviation threshold for the tail formula. Reference values computed as in the previous test. The narrow interval `[0.3, 0.4]` is ill-conditioned, as the normalizing constant `Phi(0.4) - Phi(0.3)` loses about one digit to cancellation: + x = [ + 0.5, + 0.35, + 1.5, + -0.5, + 2.5, + 1.9, + 2.05, + -2.5, + 0.1 + ]; + a = [ + 0.0, + 0.3, + 1.0, + -1.0, + 1.0, + 1.9, + 2.0, + -3.0, + -0.2 + ]; + b = [ + 1.0, + 0.4, + 2.0, + 0.0, + 4.0, + 3.0, + 2.1, + -1.95, + 0.25 + ]; + mu = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.1 + ]; + sigma = [ + 1.0, + 1.0, + 1.0, + 1.0, + 2.0, + 1.0, + 1.0, + 1.0, + 0.5 + ]; + expected = [ + 1.0314069011438771, + 10.003656399234615, + 0.9530001060727347, + 1.0314069011438771, + 0.3195541179514373, + 2.397655048017141, + 9.986676397906189, + 0.723169556436616, + 2.3217380476390383 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = factory( a[ i ], b[ i ], mu[ i ], sigma[ i ] )( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js index 07e9e412edf7..9ec6769bf2c8 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js @@ -229,3 +229,88 @@ tape( 'the function evaluates the PDF when both endpoints lie on the same side o } t.end(); }); + +tape( 'the function evaluates the PDF when the endpoints lie close to `mu`', opts, function test( t ) { + var expected; + var sigma; + var mu; + var a; + var b; + var x; + var y; + var i; + + // Intervals near `mu`, including intervals ending at `mu` and intervals just below and above the two-standard-deviation threshold for the tail formula. Reference values computed as in the previous test. The narrow interval `[0.3, 0.4]` is ill-conditioned, as the normalizing constant `Phi(0.4) - Phi(0.3)` loses about one digit to cancellation: + x = [ + 0.5, + 0.35, + 1.5, + -0.5, + 2.5, + 1.9, + 2.05, + -2.5, + 0.1 + ]; + a = [ + 0.0, + 0.3, + 1.0, + -1.0, + 1.0, + 1.9, + 2.0, + -3.0, + -0.2 + ]; + b = [ + 1.0, + 0.4, + 2.0, + 0.0, + 4.0, + 3.0, + 2.1, + -1.95, + 0.25 + ]; + mu = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.1 + ]; + sigma = [ + 1.0, + 1.0, + 1.0, + 1.0, + 2.0, + 1.0, + 1.0, + 1.0, + 0.5 + ]; + expected = [ + 1.0314069011438771, + 10.003656399234615, + 0.9530001060727347, + 1.0314069011438771, + 0.3195541179514373, + 2.397655048017141, + 9.986676397906189, + 0.723169556436616, + 2.3217380476390383 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = pdf( x[ i ], a[ i ], b[ i ], mu[ i ], sigma[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js index 83e340ba599e..a6a3751256f4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js +++ b/lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js @@ -224,3 +224,88 @@ tape( 'the function evaluates the PDF when both endpoints lie on the same side o } t.end(); }); + +tape( 'the function evaluates the PDF when the endpoints lie close to `mu`', function test( t ) { + var expected; + var sigma; + var mu; + var a; + var b; + var x; + var y; + var i; + + // Intervals near `mu`, including intervals ending at `mu` and intervals just below and above the two-standard-deviation threshold for the tail formula. Reference values computed as in the previous test. The narrow interval `[0.3, 0.4]` is ill-conditioned, as the normalizing constant `Phi(0.4) - Phi(0.3)` loses about one digit to cancellation: + x = [ + 0.5, + 0.35, + 1.5, + -0.5, + 2.5, + 1.9, + 2.05, + -2.5, + 0.1 + ]; + a = [ + 0.0, + 0.3, + 1.0, + -1.0, + 1.0, + 1.9, + 2.0, + -3.0, + -0.2 + ]; + b = [ + 1.0, + 0.4, + 2.0, + 0.0, + 4.0, + 3.0, + 2.1, + -1.95, + 0.25 + ]; + mu = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.1 + ]; + sigma = [ + 1.0, + 1.0, + 1.0, + 1.0, + 2.0, + 1.0, + 1.0, + 1.0, + 0.5 + ]; + expected = [ + 1.0314069011438771, + 10.003656399234615, + 0.9530001060727347, + 1.0314069011438771, + 0.3195541179514373, + 2.397655048017141, + 9.986676397906189, + 0.723169556436616, + 2.3217380476390383 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = pdf( x[ i ], a[ i ], b[ i ], mu[ i ], sigma[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' ); + } + t.end(); +});