Skip to content

Latest commit

 

History

History
264 lines (168 loc) · 6.45 KB

File metadata and controls

264 lines (168 loc) · 6.45 KB

Logarithm of Probability Density Function

Evaluate the natural logarithm of the probability density function (PDF) for an Erlang distribution.

The probability density function (PDF) for an Erlang random variable is

$$f(x; k,\lambda)={\lambda^k x^{k-1} e^{-\lambda x} \over (k-1)!} 1(x \ge 0)$$

where k is the shape parameter and lambda is the rate parameter.

Usage

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

logpdf( x, k, lambda )

Evaluates the natural logarithm of the probability density function (PDF) for an Erlang distribution with parameters k (shape parameter) and lambda (rate parameter).

var y = logpdf( 0.1, 1, 1.0 );
// returns ~-0.1

y = logpdf( 0.5, 2, 2.5 );
// returns ~-0.111

y = logpdf( -1.0, 4, 2.0 );
// returns -Infinity

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

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

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

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

If not provided a nonnegative integer for k, the function returns NaN.

var y = logpdf( 2.0, -2, 0.5 );
// returns NaN

y = logpdf( 2.0, 0.5, 0.5 );
// returns NaN

If provided k = 0, the function evaluates the logarithm of the PDF of a degenerate distribution centered at 0.

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

y = logpdf( 0.0, 0.0, 2.0 );
// returns Infinity

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

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

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

logpdf.factory( k, lambda )

Returns a function for evaluating the PDF for an Erlang distribution with parameters k (shape parameter) and lambda (rate parameter).

var mylogpdf = logpdf.factory( 3, 1.5 );

var y = mylogpdf( 1.0 );
// returns ~-0.977

y = mylogpdf( 4.0 );
// returns ~-2.704

C APIs

Usage

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

stdlib_base_dists_erlang_logpdf( x, k, lambda )

Evaluates the natural logarithm of the probability density function (PDF) for an Erlang distribution with shape parameter k and rate parameter lambda.

double y = stdlib_base_dists_erlang_logpdf( 1.0, 3.0, 1.5 );
// returns ~-0.977

The function accepts the following arguments:

  • x: [in] double input value.
  • k: [in] double shape parameter.
  • lambda: [in] double rate parameter.
double stdlib_base_dists_erlang_logpdf( const double x, const double k, const double lambda );

Examples

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

int main( void ) {
    const double x[] = { 0.0, 0.5, 1.0, 1.5, 2.0 };
    const double k[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
    const double lambda[] = { 0.5, 1.0, 1.5, 2.0, 2.5 };

    double y;
    int i;
    for ( i = 0; i < 5; i++ ) {
        y = stdlib_base_dists_erlang_logpdf( x[ i ], k[ i ], lambda[ i ] );
        printf( "x: %lf, k: %lf, lambda: %lf, ln(f(x; k, lambda)): %lf\n", x[ i ], k[ i ], lambda[ i ], y );
    }
}

Examples

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

var opts = {
    'dtype': 'float64'
};
var x = uniform( 20, 0.0, 10.0, opts );
var k = discreteUniform( 20, 0, 10, opts );
var lambda = uniform( 20, 0.0, 5.0, opts );

logEachMap( 'x: %0.4f, k: %d, λ: %0.4f, ln(f(x;k,λ)): %0.4f', x, k, lambda, logpdf );