Skip to content
Merged
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 @@ -42,8 +42,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/abs2",
"@stdlib/constants/float64/pi",
"@stdlib/math/base/special/sqrt",
"@stdlib/constants/float64/sqrt-two-pi",
"@stdlib/stats/base/dists/normal/cdf"
]
},
Expand All @@ -62,8 +61,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/abs2",
"@stdlib/constants/float64/pi",
"@stdlib/math/base/special/sqrt",
"@stdlib/constants/float64/sqrt-two-pi",
"@stdlib/stats/base/dists/normal/cdf"
]
},
Expand All @@ -82,8 +80,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/abs2",
"@stdlib/constants/float64/pi",
"@stdlib/math/base/special/sqrt",
"@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,8 @@
#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/sqrt.h"
#include "stdlib/stats/base/dists/normal/cdf.h"
#include "stdlib/constants/float64/pi.h"
#include "stdlib/constants/float64/sqrt_two_pi.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 @@ -39,9 +38,8 @@
* // 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 s2x2;
double z;
double A;
double B;
double C;

if (
Expand All @@ -56,10 +54,8 @@ double stdlib_base_dists_truncated_normal_pdf( const double x, const double a, c
if ( x < a || x > b ) {
return 0.0;
}
s2x2 = 2.0 * stdlib_base_abs2( sigma );
A = 1.0 / ( stdlib_base_sqrt( s2x2 * STDLIB_CONSTANT_FLOAT64_PI ) );
B = -1.0 / s2x2;
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 );

return A * stdlib_base_exp( B * stdlib_base_abs2( x - mu ) ) / C;
z = ( x - mu ) / sigma;
return A * stdlib_base_exp( -0.5 * stdlib_base_abs2( z ) ) / C;
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ tape( 'the function returns `0` when `x` is outside `[a,b]`', opts, function tes
t.end();
});

tape( 'the function does not overflow or underflow for extreme inputs', opts, function test( t ) {
var y;

// Verified against SciPy:
y = pdf( 0.0, -1e-160, 1e-160, 0.0, 1e-160 );
t.strictEqual( isAlmostSameValue( y, 5.843685672568167e+159, 1.0 ), true, 'returns expected value' );

// Verified against SciPy:
y = pdf( 1e200, -1e201, 1e201, 0.0, 1e200 );
t.strictEqual( isAlmostSameValue( y, 2.4197072451914336e-201, 1.0 ), true, 'returns expected value' );

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 @@ -96,6 +96,20 @@ tape( 'the function returns `0` if provided `x` outside `[a, b]`', function test
t.end();
});

tape( 'the function does not overflow or underflow for extreme inputs', function test( t ) {
var y;

// Verified against SciPy:
y = pdf( 0.0, -1e-160, 1e-160, 0.0, 1e-160 );
t.strictEqual( isAlmostSameValue( y, 5.843685672568167e+159, 1.0 ), true, 'returns expected value' );

// Verified against SciPy:
y = pdf( 1e200, -1e201, 1e201, 0.0, 1e200 );
t.strictEqual( isAlmostSameValue( y, 2.4197072451914336e-201, 1.0 ), true, 'returns expected value' );

t.end();
});

tape( 'the function evaluates the PDF', function test( t ) {
var expected;
var sigma;
Expand Down
Loading