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 @@ -49,15 +49,13 @@ var C = 0.5 * ( LN2 - LNPI );
*/
function factory( sigma ) {
var lsigma;
var sigma2;
if (
isnan( sigma ) ||
sigma <= 0.0
) {
return constantFunction( NaN );
}
lsigma = ln( sigma );
sigma2 = sigma * sigma;
return logpdf;

/**
Expand All @@ -72,13 +70,15 @@ function factory( sigma ) {
* // returns <number>
*/
function logpdf( x ) {
var v;
if ( isnan( x ) ) {
return NaN;
}
if ( x < 0.0 ) {
return NINF;
}
return C - lsigma - ( (x*x) / ( 2.0 * sigma2 ) );
v = x / sigma;
return C - lsigma - ( (v*v) / 2.0 );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var C = 0.5 * ( LN2 - LNPI );
* // returns NaN
*/
function logpdf( x, sigma ) {
var v;
if (
isnan( x ) ||
isnan( sigma ) ||
Expand All @@ -86,7 +87,8 @@ function logpdf( x, sigma ) {
if ( x < 0.0 ) {
return NINF;
}
return C - ln( sigma ) - ( (x*x) / ( 2.0 * (sigma*sigma) ) );
v = x / sigma;
return C - ln( sigma ) - ( (v*v) / 2.0 );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static const double C = 0.5 * ( STDLIB_CONSTANT_FLOAT64_LN2 - STDLIB_CONSTANT_FL
* // returns ~-0.546
*/
double stdlib_base_dists_halfnormal_logpdf( const double x, const double sigma ) {
double v;
if (
stdlib_base_is_nan( x ) ||
stdlib_base_is_nan( sigma ) ||
Expand All @@ -48,5 +49,6 @@ double stdlib_base_dists_halfnormal_logpdf( const double x, const double sigma )
if ( x < 0.0 ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
return C - stdlib_base_ln( sigma ) - ( (x*x) / ( 2.0 * (sigma*sigma) ) );
v = x / sigma;
return C - stdlib_base_ln( sigma ) - ( (v*v) / 2.0 );
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

var constantFunction = require( '@stdlib/utils/constant-function' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var abs2 = require( '@stdlib/math/base/special/abs2' );
var ln = require( '@stdlib/math/base/special/ln' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PI = require( '@stdlib/constants/float64/pi' );
var LN_TWO_PI = require( '@stdlib/constants/float64/ln-two-pi' );


// MAIN //
Expand All @@ -46,19 +46,15 @@ var PI = require( '@stdlib/constants/float64/pi' );
* // returns ~-3.672
*/
function factory( mu, sigma ) {
var s2;
var A;
var B;
if (
isnan( mu ) ||
isnan( sigma ) ||
sigma <= 0.0
) {
return constantFunction( NaN );
}
s2 = pow( sigma, 2.0 );
A = -0.5 * ln( 2.0 * s2 * PI );
B = -1.0 / ( 2.0 * s2 );
A = ( -0.5 * LN_TWO_PI ) - ln( sigma );
return logpdf;

/**
Expand All @@ -73,13 +69,15 @@ function factory( mu, sigma ) {
* // returns <number>
*/
function logpdf( x ) {
var lnx;
if ( isnan( x ) ) {
return NaN;
}
if ( x <= 0.0 ) {
return NINF;
}
return A - ln( x ) + ( B * pow( ln(x) - mu, 2.0 ) );
lnx = ln( x );
return A - lnx - ( 0.5 * abs2( ( lnx - mu ) / sigma ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var abs2 = require( '@stdlib/math/base/special/abs2' );
var ln = require( '@stdlib/math/base/special/ln' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PI = require( '@stdlib/constants/float64/pi' );
var LN_TWO_PI = require( '@stdlib/constants/float64/ln-two-pi' );


// MAIN //
Expand Down Expand Up @@ -71,9 +71,8 @@ var PI = require( '@stdlib/constants/float64/pi' );
* // returns NaN
*/
function logpdf( x, mu, sigma ) {
var s2;
var lnx;
var A;
var B;
if (
isnan( x ) ||
isnan( mu ) ||
Expand All @@ -85,10 +84,9 @@ function logpdf( x, mu, sigma ) {
if ( x <= 0.0 ) {
return NINF;
}
s2 = pow( sigma, 2.0 );
A = -0.5 * ln( 2.0 * s2 * PI );
B = -1.0 / ( 2.0 * s2 );
return A - ln( x ) + ( B * pow( ln(x) - mu, 2.0 ) );
lnx = ln( x );
A = ( -0.5 * LN_TWO_PI ) - ln( sigma );
return A - lnx - ( 0.5 * abs2( ( lnx - mu ) / sigma ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"dependencies": [
"@stdlib/math/base/napi/ternary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/pow",
"@stdlib/math/base/special/abs2",
"@stdlib/math/base/special/ln",
"@stdlib/constants/float64/ninf",
"@stdlib/constants/float64/pi"
"@stdlib/constants/float64/ln-two-pi"
]
},
{
Expand All @@ -59,11 +59,11 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/pow",
"@stdlib/math/base/special/abs2",
"@stdlib/math/base/special/ln",
"@stdlib/constants/float64/ninf",
"@stdlib/constants/float64/eps",
"@stdlib/constants/float64/pi"
"@stdlib/constants/float64/ln-two-pi"
]
},
{
Expand All @@ -79,10 +79,10 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/pow",
"@stdlib/math/base/special/abs2",
"@stdlib/math/base/special/ln",
"@stdlib/constants/float64/ninf",
"@stdlib/constants/float64/pi"
"@stdlib/constants/float64/ln-two-pi"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include "stdlib/stats/base/dists/lognormal/logpdf.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/abs2.h"
#include "stdlib/math/base/special/ln.h"
#include "stdlib/constants/float64/ninf.h"
#include "stdlib/constants/float64/pi.h"
#include "stdlib/constants/float64/ln_two_pi.h"

/**
* Evaluates the natural logarithm of the probability density function (PDF) for a lognormal distribution with location parameter `mu` and scale parameter `sigma` at a value `x`.
Expand All @@ -37,9 +37,7 @@
*/
double stdlib_base_dists_lognormal_logpdf( const double x, const double mu, const double sigma ) {
double lnx;
double s2;
double A;
double B;

if (
stdlib_base_is_nan( x ) ||
Expand All @@ -52,9 +50,7 @@ double stdlib_base_dists_lognormal_logpdf( const double x, const double mu, cons
if ( x <= 0.0 ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
s2 = stdlib_base_pow( sigma, 2.0 );
lnx = stdlib_base_ln( x );
A = -0.5 * stdlib_base_ln( 2.0 * s2 * STDLIB_CONSTANT_FLOAT64_PI );
B = -1.0 / ( 2.0 * s2 );
return A - lnx + ( B * stdlib_base_pow( lnx - mu, 2.0 ) );
A = ( -0.5 * STDLIB_CONSTANT_FLOAT64_LN_TWO_PI ) - stdlib_base_ln( sigma );
return A - lnx - ( 0.5 * stdlib_base_abs2( ( lnx - mu ) / sigma ) );
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@

var constantFunction = require( '@stdlib/utils/constant-function' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var exp = require( '@stdlib/math/base/special/exp' );
var pow = require( '@stdlib/math/base/special/pow' );
var abs2 = require( '@stdlib/math/base/special/abs2' );
var ln = require( '@stdlib/math/base/special/ln' );
var PI = require( '@stdlib/constants/float64/pi' );
var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' );


// MAIN //
Expand All @@ -47,19 +46,15 @@ var PI = require( '@stdlib/constants/float64/pi' );
* // returns ~0.025
*/
function factory( mu, sigma ) {
var s2;
var A;
var B;
if (
isnan( mu ) ||
isnan( sigma ) ||
sigma <= 0.0
) {
return constantFunction( NaN );
}
s2 = pow( sigma, 2.0 );
A = 1.0 / ( sqrt( 2.0 * s2 * PI ) );
B = -1.0 / ( 2.0 * s2 );
A = 1.0 / ( sigma * SQRT_TWO_PI );
return pdf;

/**
Expand All @@ -80,7 +75,7 @@ function factory( mu, sigma ) {
if ( x <= 0.0 ) {
return 0.0;
}
return (1.0/x) * A * exp( B * pow( ln(x) - mu, 2.0 ) );
return (1.0/x) * A * exp( -0.5 * abs2( ( ln(x) - mu ) / sigma ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var exp = require( '@stdlib/math/base/special/exp' );
var pow = require( '@stdlib/math/base/special/pow' );
var abs2 = require( '@stdlib/math/base/special/abs2' );
var ln = require( '@stdlib/math/base/special/ln' );
var PI = require( '@stdlib/constants/float64/pi' );
var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' );


// MAIN //
Expand Down Expand Up @@ -72,9 +71,7 @@ var PI = require( '@stdlib/constants/float64/pi' );
* // returns NaN
*/
function pdf( x, mu, sigma ) {
var s2;
var A;
var B;
if (
isnan( x ) ||
isnan( mu ) ||
Expand All @@ -86,10 +83,8 @@ function pdf( x, mu, sigma ) {
if ( x <= 0.0 ) {
return 0.0;
}
s2 = pow( sigma, 2.0 );
A = 1.0 / ( sqrt( 2.0 * s2 * PI ) );
B = -1.0 / ( 2.0 * s2 );
return (1.0/x) * A * exp( B * pow( ln(x) - mu, 2.0 ) );
A = 1.0 / ( sigma * SQRT_TWO_PI );
return (1.0/x) * A * exp( -0.5 * abs2( ( ln(x) - mu ) / sigma ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
"@stdlib/math/base/napi/ternary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/pow",
"@stdlib/math/base/special/abs2",
"@stdlib/math/base/special/ln",
"@stdlib/math/base/special/sqrt",
"@stdlib/constants/float64/pi"
"@stdlib/constants/float64/sqrt-two-pi"
]
},
{
Expand All @@ -62,10 +61,9 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/pow",
"@stdlib/math/base/special/abs2",
"@stdlib/math/base/special/ln",
"@stdlib/math/base/special/sqrt",
"@stdlib/constants/float64/pi"
"@stdlib/constants/float64/sqrt-two-pi"
]
},
{
Expand All @@ -83,10 +81,9 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/pow",
"@stdlib/math/base/special/abs2",
"@stdlib/math/base/special/ln",
"@stdlib/math/base/special/sqrt",
"@stdlib/constants/float64/pi"
"@stdlib/constants/float64/sqrt-two-pi"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
#include "stdlib/math/base/special/ln.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/sqrt.h"
#include "stdlib/constants/float64/pi.h"
#include "stdlib/math/base/special/abs2.h"
#include "stdlib/constants/float64/sqrt_two_pi.h"

/**
* Evaluates the PDF for a lognormal distribution with location `mu` and scale `sigma`.
Expand All @@ -39,7 +38,6 @@
double stdlib_base_dists_lognormal_pdf( const double x, const double mu, const double sigma ) {
double exp_value;
double lnx_u;
double s2;
double z;
double y;
if (
Expand All @@ -53,11 +51,10 @@ double stdlib_base_dists_lognormal_pdf( const double x, const double mu, const d
if ( x <= 0.0 ) {
return 0.0;
}
s2 = stdlib_base_pow( sigma, 2.0 );
lnx_u = stdlib_base_pow( ( stdlib_base_ln( x ) - mu ), 2.0 );
z = -lnx_u / ( 2.0 * s2 );
lnx_u = stdlib_base_abs2( ( stdlib_base_ln( x ) - mu ) / sigma );
z = -0.5 * lnx_u;
exp_value = stdlib_base_exp( z );
y = 1.0 / ( x * sigma * stdlib_base_sqrt( 2.0 * STDLIB_CONSTANT_FLOAT64_PI ) );
y = 1.0 / ( x * sigma * STDLIB_CONSTANT_FLOAT64_SQRT_TWO_PI );

return y * exp_value;
}
Loading