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..e8f17908cac5 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 @@ -23,6 +23,8 @@ var constantFunction = require( '@stdlib/utils/constant-function' ); var exp = require( '@stdlib/math/base/special/exp' ); var abs2 = require( '@stdlib/math/base/special/abs2' ); +var erfcx = require( '@stdlib/math/base/special/erfcx' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var normal = require( '@stdlib/stats/base/dists/normal/cdf' ).factory; var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' ); @@ -31,6 +33,7 @@ var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' ); // VARIABLES // var normalCDF = normal( 0.0, 1.0 ); +var SQRT_HALF = sqrt( 2.0 ) / 2.0; // MAIN // @@ -65,6 +68,9 @@ var normalCDF = normal( 0.0, 1.0 ); * // returns 0.0 */ function factory( a, b, mu, sigma ) { + var EXPTA; + var TA; + var TB; var A; var C; @@ -79,7 +85,19 @@ 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 ( a >= mu ) { + // Both endpoints lie in the upper tail: + TA = ( a - mu ) / sigma; + TB = ( b - mu ) / sigma; + EXPTA = exp( 0.5 * ( ( TA*TA ) - ( TB*TB ) ) ); + } else if ( b <= mu ) { + // Both endpoints lie in the lower tail (mirror the upper tail): + TA = ( mu - b ) / sigma; + TB = ( mu - a ) / sigma; + EXPTA = exp( 0.5 * ( ( TA*TA ) - ( TB*TB ) ) ); + } else { + C = normalCDF( ( b-mu ) / sigma ) - normalCDF( ( a-mu ) / sigma ); + } return pdf; /** @@ -90,13 +108,26 @@ function factory( a, b, mu, sigma ) { * @returns {number} evaluated PDF */ function pdf( x ) { + var den; + var z2; + var z; + if ( isnan( x ) ) { return NaN; } if ( x < a || x > b ) { return 0.0; } - return A * exp( -0.5 * abs2( ( x - mu ) / sigma ) ) / C; + if ( a >= mu ) { + z = ( x - mu ) / sigma; + } else if ( b <= mu ) { + z = ( mu - x ) / sigma; + } else { + return A * exp( -0.5 * abs2( ( x - mu ) / sigma ) ) / C; + } + z2 = 0.5 * ( ( TA*TA ) - ( z*z ) ); + den = 0.5 * ( erfcx( SQRT_HALF * TA ) - ( erfcx( SQRT_HALF * TB ) * EXPTA ) ); + return ( A * exp( z2 ) ) / den; } } 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..ced103b87210 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 @@ -22,6 +22,8 @@ var exp = require( '@stdlib/math/base/special/exp' ); var abs2 = require( '@stdlib/math/base/special/abs2' ); +var erfcx = require( '@stdlib/math/base/special/erfcx' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var normal = require( '@stdlib/stats/base/dists/normal/cdf' ).factory; var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' ); @@ -30,6 +32,7 @@ var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' ); // VARIABLES // var normalCDF = normal( 0.0, 1.0 ); +var SQRT_HALF = sqrt( 2.0 ) / 2.0; // MAIN // @@ -81,10 +84,42 @@ function pdf( x, a, b, mu, sigma ) { return 0.0; } A = 1.0 / ( sigma * SQRT_TWO_PI ); + if ( a >= mu ) { + // Both endpoints lie in the upper tail: + return A * tailPDF( ( x - mu ) / sigma, ( a - mu ) / sigma, ( b - mu ) / sigma ); + } + if ( b <= mu ) { + // Both endpoints lie in the lower tail (mirror the upper tail): + return A * tailPDF( ( mu - x ) / sigma, ( mu - b ) / sigma, ( mu - a ) / sigma ); + } C = normalCDF( (b-mu)/sigma ) - normalCDF( (a-mu)/sigma ); return A * exp( -0.5 * abs2( ( x - mu ) / sigma ) ) / C; } +/** +* Evaluates the rescaled PDF for the case where both endpoints lie on the same side of `mu`. +* +* ## Notes +* +* - Let `ta` and `tb` (`ta <= tb`) be the standardized distances of the endpoints from `mu`, and `z` (`ta <= z <= tb`) the standardized distance of `x`. The normalizer is `C = Q(ta) - Q(tb)`, where `Q(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` is the survival function of the standard normal distribution. Dividing the numerator and the normalizer by `exp(-ta^2/2)` yields +* +* ```text +* tailPDF = exp( 0.5*(ta^2 - z^2) ) / ( 0.5 * ( erfcx(ta/sqrt(2)) - erfcx(tb/sqrt(2)) * exp( 0.5*(ta^2 - tb^2) ) ) ) +* ``` +* +* which avoids the cancellation in `Phi(tb) - Phi(ta)` when both endpoints lie in the upper tail. +* +* @private +* @param {number} z - standardized value of `x` +* @param {number} ta - smaller standardized endpoint +* @param {number} tb - larger standardized endpoint +* @returns {number} evaluated rescaled PDF +*/ +function tailPDF( z, ta, tb ) { + var expta = exp( 0.5 * ( ( ta*ta ) - ( tb*tb ) ) ); + return exp( 0.5 * ( ( ta*ta ) - ( z*z ) ) ) / ( 0.5 * ( erfcx( SQRT_HALF * ta ) - ( erfcx( SQRT_HALF * tb ) * expta ) ) ); +} + // EXPORTS // 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..93d3bd63541e 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 @@ -42,6 +42,7 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/exp", "@stdlib/math/base/special/abs2", + "@stdlib/math/base/special/erfcx", "@stdlib/constants/float64/sqrt-two-pi", "@stdlib/stats/base/dists/normal/cdf" ] @@ -61,6 +62,7 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/exp", "@stdlib/math/base/special/abs2", + "@stdlib/math/base/special/erfcx", "@stdlib/constants/float64/sqrt-two-pi", "@stdlib/stats/base/dists/normal/cdf" ] @@ -80,6 +82,7 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/exp", "@stdlib/math/base/special/abs2", + "@stdlib/math/base/special/erfcx", "@stdlib/constants/float64/sqrt-two-pi", "@stdlib/stats/base/dists/normal/cdf" ] 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..7f8280a81dc6 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 @@ -20,9 +20,13 @@ #include "stdlib/math/base/assert/is_nan.h" #include "stdlib/math/base/special/exp.h" #include "stdlib/math/base/special/abs2.h" +#include "stdlib/math/base/special/erfcx.h" #include "stdlib/stats/base/dists/normal/cdf.h" #include "stdlib/constants/float64/sqrt_two_pi.h" +// Constant for 1/sqrt(2): +static const double SQRT_HALF = 0.707106781186547524400844362104849039; + /** * 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`. * @@ -39,6 +43,9 @@ */ 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 ta; + double tb; + double expta; double A; double C; @@ -55,6 +62,22 @@ double stdlib_base_dists_truncated_normal_pdf( const double x, const double a, c return 0.0; } A = 1.0 / ( sigma * STDLIB_CONSTANT_FLOAT64_SQRT_TWO_PI ); + if ( a >= mu ) { + // Both endpoints lie in the upper tail: with `ta` and `tb` the standardized distances of the endpoints from `mu`, the normalizer is `C = Q(ta) - Q(tb)`, where `Q(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))`. Dividing the numerator and the normalizer by `exp(-ta^2/2)` avoids the cancellation in `Phi(tb) - Phi(ta)`: + ta = ( a - mu ) / sigma; + tb = ( b - mu ) / sigma; + z = ( x - mu ) / sigma; + expta = stdlib_base_exp( 0.5 * ( ta*ta - tb*tb ) ); + return A * stdlib_base_exp( 0.5 * ( ta*ta - z*z ) ) / ( 0.5 * ( stdlib_base_erfcx( SQRT_HALF * ta ) - stdlib_base_erfcx( SQRT_HALF * tb ) * expta ) ); + } + if ( b <= mu ) { + // Both endpoints lie in the lower tail (mirror the upper tail): + ta = ( mu - b ) / sigma; + tb = ( mu - a ) / sigma; + z = ( mu - x ) / sigma; + expta = stdlib_base_exp( 0.5 * ( ta*ta - tb*tb ) ); + return A * stdlib_base_exp( 0.5 * ( ta*ta - z*z ) ) / ( 0.5 * ( stdlib_base_erfcx( SQRT_HALF * ta ) - stdlib_base_erfcx( SQRT_HALF * tb ) * expta ) ); + } 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; return A * stdlib_base_exp( -0.5 * stdlib_base_abs2( z ) ) / C; 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..3211ed05813b 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 @@ -119,6 +119,60 @@ tape( 'the function does not overflow or underflow for extreme inputs', opts, fu t.end(); }); +tape( 'the function evaluates the PDF when both endpoints lie in the upper tail', function test( t ) { + var expected; + var values; + var y; + var i; + + values = [ + [ 11.0, 10.0, 12.0, 0.0, 1.0 ], + [ 40.0, 40.0, 41.0, 0.0, 1.0 ], + [ 105.0, 100.0, 110.0, 50.0, 5.0 ], + [ 9.0, 8.0, 10.0, 0.0, 1.0 ], + [ 7.0, 6.0, Infinity, 0.0, 1.0 ], + [ 5.5, 5.0, 6.0, 0.0, 1.0 ] + ]; + expected = [ + 2.78065632930861e-4, + 40.02496884720726, + 5.5613126586172206e-5, + 1.652441544171957e-3, + 9.25890411694295e-3, + 0.3770066559405626 + ]; + + for ( i = 0; i < values.length; i++ ) { + y = pdf( values[ i ][ 0 ], values[ i ][ 1 ], values[ i ][ 2 ], values[ i ][ 3 ], values[ i ][ 4 ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 30 ), true, 'returns expected value for x=' + values[ i ][ 0 ] ); + } + t.end(); +}); + +tape( 'the function evaluates the PDF when both endpoints lie in the lower tail', function test( t ) { + var expected; + var values; + var y; + var i; + + values = [ + [ -11.0, -12.0, -10.0, 0.0, 1.0 ], + [ -40.0, -41.0, -40.0, 0.0, 1.0 ], + [ -9.0, -10.0, -8.0, 0.0, 1.0 ] + ]; + expected = [ + 2.78065632930861e-4, + 40.02496884720726, + 1.652441544171957e-3 + ]; + + for ( i = 0; i < values.length; i++ ) { + y = pdf( values[ i ][ 0 ], values[ i ][ 1 ], values[ i ][ 2 ], values[ i ][ 3 ], values[ i ][ 4 ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 30 ), true, 'returns expected value for x=' + values[ i ][ 0 ] ); + } + t.end(); +}); + tape( 'the function evaluates the PDF', opts, function test( t ) { var expected; var sigma; 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..2efb9c095e6c 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 @@ -110,6 +110,60 @@ tape( 'the function does not overflow or underflow for extreme inputs', function t.end(); }); +tape( 'the function evaluates the PDF when both endpoints lie in the upper tail', function test( t ) { + var expected; + var values; + var y; + var i; + + values = [ + [ 11.0, 10.0, 12.0, 0.0, 1.0 ], + [ 40.0, 40.0, 41.0, 0.0, 1.0 ], + [ 105.0, 100.0, 110.0, 50.0, 5.0 ], + [ 9.0, 8.0, 10.0, 0.0, 1.0 ], + [ 7.0, 6.0, Infinity, 0.0, 1.0 ], + [ 5.5, 5.0, 6.0, 0.0, 1.0 ] + ]; + expected = [ + 2.78065632930861e-4, + 40.02496884720726, + 5.5613126586172206e-5, + 1.652441544171957e-3, + 9.25890411694295e-3, + 0.3770066559405626 + ]; + + for ( i = 0; i < values.length; i++ ) { + y = pdf( values[ i ][ 0 ], values[ i ][ 1 ], values[ i ][ 2 ], values[ i ][ 3 ], values[ i ][ 4 ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 30 ), true, 'returns expected value for x=' + values[ i ][ 0 ] ); + } + t.end(); +}); + +tape( 'the function evaluates the PDF when both endpoints lie in the lower tail', function test( t ) { + var expected; + var values; + var y; + var i; + + values = [ + [ -11.0, -12.0, -10.0, 0.0, 1.0 ], + [ -40.0, -41.0, -40.0, 0.0, 1.0 ], + [ -9.0, -10.0, -8.0, 0.0, 1.0 ] + ]; + expected = [ + 2.78065632930861e-4, + 40.02496884720726, + 1.652441544171957e-3 + ]; + + for ( i = 0; i < values.length; i++ ) { + y = pdf( values[ i ][ 0 ], values[ i ][ 1 ], values[ i ][ 2 ], values[ i ][ 3 ], values[ i ][ 4 ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 30 ), true, 'returns expected value for x=' + values[ i ][ 0 ] ); + } + t.end(); +}); + tape( 'the function evaluates the PDF', function test( t ) { var expected; var sigma;