Skip to content

Latest commit

 

History

History
263 lines (167 loc) · 6.35 KB

File metadata and controls

263 lines (167 loc) · 6.35 KB

Probability Density Function

Cauchy distribution probability density function (PDF).

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

$$f(x;\gamma,x_0)=\frac{1}{\pi\gamma\,\left[1 + \left(\frac{x-x_0}{\gamma}\right)^2\right]}\!$$

where x0 is the location parameter and gamma > 0 is the scale parameter.

Usage

var pdf = require( '@stdlib/stats/base/dists/cauchy/pdf' );

pdf( x, x0, gamma )

Evaluates the probability density function (PDF) for a Cauchy distribution with parameters x0 (location parameter) and gamma > 0 (scale parameter).

var y = pdf( 2.0, 1.0, 1.0 );
// returns ~0.159

y = pdf( 4.0, 3.0, 0.1 );
// returns ~0.0315

y = pdf( 4.0, 3.0, 3.0 );
// returns ~0.095

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

var y = pdf( NaN, 1.0, 1.0 );
// returns NaN

y = pdf( 2.0, NaN, 1.0 );
// returns NaN

y = pdf( 2.0, 1.0, NaN );
// returns NaN

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

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

y = pdf( 2.0, 0.0, 0.0 );
// returns NaN

pdf.factory( x0, gamma )

Returns a function for evaluating the PDF of a Cauchy distribution with location parameter x0 and scale parameter gamma.

var mypdf = pdf.factory( 10.0, 2.0 );

var y = mypdf( 10.0 );
// returns ~0.159

y = mypdf( 5.0 );
// returns ~0.022

Examples

var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pdf = require( '@stdlib/stats/base/dists/cauchy/pdf' );

var opts = {
    'dtype': 'float64'
};
var gamma = uniform( 10, EPS, 20.0, opts );
var x0 = uniform( 10, -5.0, 5.0, opts );
var x = uniform( 10, 0.0, 10.0, opts );

logEachMap( 'x: %0.4f, x0: %0.4f, γ: %0.4f, f(x;x0,γ): %0.4f', x, x0, gamma, pdf );

C APIs

Usage

#include "stdlib/stats/base/dists/cauchy/pdf.h"

stdlib_base_dists_cauchy_pdf( x, x0, gamma )

Evaluates the probability density function (PDF) for a Cauchy distribution with parameters x0 (location parameter) and gamma > 0 (scale parameter).

double out = stdlib_base_dists_cauchy_pdf( 0.5, 0.0, 2.0 );
// returns ~0.14979

The function accepts the following arguments:

  • x: [in] double input value.
  • x0: [in] double location parameter.
  • gamma: [in] double scale parameter.
double stdlib_base_dists_cauchy_pdf( const double x, const double x0, const double gamma );

Examples

#include "stdlib/stats/base/dists/cauchy/pdf.h"
#include "stdlib/constants/float64/eps.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 gamma;
    double x0;
    double x;
    double y;
    int i;

    for ( i = 0; i < 25; i++ ) {
        x = random_uniform( 0.0, 10.0 );
        x0 = random_uniform( -5.0, 5.0 );
        gamma = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 );
        y = stdlib_base_dists_cauchy_pdf( x, x0, gamma );
        printf( "x: %lf, k: %lf, γ: %lf, f(x;x0,γ): %lf\n", x, x0, gamma, y );
    }
}