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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -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 ) ||
Expand All @@ -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`, 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;
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;

/**
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -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 ) ||
Expand All @@ -80,6 +90,24 @@ 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`, 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;
} 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -38,9 +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 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 ) ||
Expand All @@ -54,6 +64,24 @@ 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`, 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;
} 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;
Expand Down
Loading