Skip to content

Latest commit

 

History

History
266 lines (168 loc) · 6.3 KB

File metadata and controls

266 lines (168 loc) · 6.3 KB

Logarithm of Probability Density Function

Laplace distribution logarithm of probability density function (PDF).

The probability density function (PDF) for a Laplace random variable is

$$f(x\mid\mu,b) = \frac{1}{2b} \exp \left( -\frac{|x-\mu|}{b} \right)$$

where mu is the location parameter and b > 0 is the scale parameter (also called diversity).

Usage

var logpdf = require( '@stdlib/stats/base/dists/laplace/logpdf' );

logpdf( x, mu, b )

Evaluates the logarithm of the probability density function (PDF) for a Laplace distribution with parameters mu (location parameter) and b > 0 (scale parameter).

var y = logpdf( 2.0, 0.0, 1.0 );
// returns ~-2.693

y = logpdf( -1.0, 2.0, 3.0 );
// returns ~-2.792

y = logpdf( 2.5, 2.0, 3.0 );
// returns ~-1.958

If provided NaN as any argument, the function returns NaN.

var y = logpdf( NaN, 0.0, 1.0 );
// returns NaN

y = logpdf( 0.0, NaN, 1.0 );
// returns NaN

y = logpdf( 0.0, 0.0, NaN );
// returns NaN

If provided b <= 0, the function returns NaN.

var y = logpdf( 2.0, 0.0, -1.0 );
// returns NaN

y = logpdf( 2.0, 8.0, 0.0 );
// returns NaN

logpdf.factory( mu, b )

Returns a function for evaluating the logarithm of the PDF for a Laplace distribution with parameters mu (location parameter) and b > 0 (scale parameter).

var mylogpdf = logpdf.factory( 10.0, 2.0 );

var y = mylogpdf( 10.0 );
// returns ~-1.386

y = mylogpdf( 5.0 );
// returns ~-3.886

y = mylogpdf( 12.0 );
// returns ~-2.386

Notes

  • In virtually all cases, using the logpdf or logcdf functions is preferable to manually computing the logarithm of the pdf or cdf, respectively, since the latter is prone to overflow and underflow.

Examples

var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var logpdf = require( '@stdlib/stats/base/dists/laplace/logpdf' );

var opts = {
    'dtype': 'float64'
};
var x = uniform( 100, 0.0, 10.0, opts );
var mu = uniform( 100, 0.0, 10.0, opts );
var b = uniform( 100, 0.0, 10.0, opts );

logEachMap( 'x: %0.4f, µ: %0.4f, b: %0.4f, ln(f(x;µ,b)): %0.4f', x, mu, b, logpdf );

C APIs

Usage

#include "stdlib/stats/base/dists/laplace/logpdf.h"

stdlib_base_dists_laplace_logpdf( x, mu, b )

Evaluates the logarithm of the probability density function (PDF) for a Laplace distribution with location parameter mu and scale parameter b at a value x.

double out = stdlib_base_dists_laplace_logpdf( 2.0, 0.0, 1.0 );
// returns ~-2.693

The function accepts the following arguments:

  • x: [in] double input value.
  • mu: [in] double location parameter.
  • b: [in] double rate parameter.
double stdlib_base_dists_laplace_logpdf( const double x, const double mu, const double b );

Examples

#include "stdlib/stats/base/dists/laplace/logpdf.h"
#include <stdlib.h>
#include <stdio.h>

static double random_uniform( const double min, const double max ) {
    double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
    return min + ( v*(max-min) );
}

int main( void ) {
    double mu;
    double b;
    double x;
    double y;
    int i;

    for ( i = 0; i < 25; i++ ) {
        mu = random_uniform( -5.0, 5.0 );
        b = random_uniform( 0.0, 20.0 );
        x = random_uniform( 0.0, 20.0 );
        y = stdlib_base_dists_laplace_logpdf( x, mu, b );
        printf( "x: %lf, µ: %lf, b: %lf, ln(f(x;µ,b)): %lf\n", x, mu, b, y );
    }
}