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 @@ -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' );
Expand All @@ -31,6 +33,7 @@
// VARIABLES //

var normalCDF = normal( 0.0, 1.0 );
var SQRT_HALF = sqrt( 2.0 ) / 2.0;


// MAIN //
Expand Down Expand Up @@ -65,6 +68,9 @@
* // returns 0.0
*/
function factory( a, b, mu, sigma ) {
var EXPTA;
var TA;
var TB;
var A;
var C;

Expand All @@ -79,7 +85,19 @@
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;

/**
Expand All @@ -90,13 +108,26 @@
* @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 ) );

Check warning on line 129 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 86. Maximum allowed is 80
return ( A * exp( z2 ) ) / den;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -30,6 +32,7 @@
// VARIABLES //

var normalCDF = normal( 0.0, 1.0 );
var SQRT_HALF = sqrt( 2.0 ) / 2.0;


// MAIN //
Expand Down Expand Up @@ -81,10 +84,42 @@
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 );

Check warning on line 89 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 89. Maximum allowed is 80
}
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 );

Check warning on line 93 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 89. Maximum allowed is 80
}
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 ) ) );

Check warning on line 120 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 126. Maximum allowed is 80
}


// EXPORTS //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand All @@ -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"
]
Expand All @@ -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"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand All @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,60 @@
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 ] );

Check warning on line 146 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 108. Maximum allowed is 80
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 ] );

Check warning on line 170 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 108. Maximum allowed is 80
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,60 @@
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 ] );

Check warning on line 137 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 108. Maximum allowed is 80
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 ] );

Check warning on line 161 in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/test/test.pdf.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 108. Maximum allowed is 80
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;
Expand Down