@stdlib/stats-base-dists-normal-pdf
v0.3.1
Published
Normal distribution probability density function (PDF).
Readme
Probability Density Function
Normal distribution probability density function (PDF).
The probability density function (PDF) for a normal random variable is
where µ is the mean and σ is the standard deviation.
Installation
npm install @stdlib/stats-base-dists-normal-pdfUsage
var pdf = require( '@stdlib/stats-base-dists-normal-pdf' );pdf( x, mu, sigma )
Evaluates the probability density function (PDF) for a normal distribution with parameters mu (mean) and sigma (standard deviation).
var y = pdf( 2.0, 0.0, 1.0 );
// returns ~0.054
y = pdf( -1.0, 4.0, 2.0 );
// returns ~0.009If provided NaN as any argument, the function returns NaN.
var y = pdf( NaN, 0.0, 1.0 );
// returns NaN
y = pdf( 0.0, NaN, 1.0 );
// returns NaN
y = pdf( 0.0, 0.0, NaN );
// returns NaNIf provided sigma < 0, the function returns NaN.
var y = pdf( 2.0, 0.0, -1.0 );
// returns NaNIf provided sigma = 0, the function evaluates the PDF of a degenerate distribution centered at mu.
var y = pdf( 2.0, 8.0, 0.0 );
// returns 0.0
y = pdf( 8.0, 8.0, 0.0 );
// returns Infinitypdf.factory( mu, sigma )
Partially applies mu and sigma to create a reusable function for evaluating the PDF.
var mypdf = pdf.factory( 10.0, 2.0 );
var y = mypdf( 10.0 );
// returns ~0.199
y = mypdf( 5.0 );
// returns ~0.009Examples
var uniform = require( '@stdlib/random-array-uniform' );
var logEachMap = require( '@stdlib/console-log-each-map' );
var pdf = require( '@stdlib/stats-base-dists-normal-pdf' );
var opts = {
'dtype': 'float64'
};
var sigma = uniform( 10, 0.0, 20.0, opts );
var mu = uniform( 10, -5.0, 5.0, opts );
var x = uniform( 10, 0.0, 10.0, opts );
logEachMap( 'x: %0.4f, µ: %0.4f, σ: %0.4f, f(x;µ,σ): %0.4f', x, mu, sigma, pdf );C APIs
Usage
#include "stdlib/stats/base/dists/normal/pdf.h"stdlib_base_dists_normal_pdf( x, mu, sigma )
Evaluates the probability density function (PDF) for a normal distribution with parameters mu (mean) and sigma (standard deviation).
double y = stdlib_base_dists_normal_pdf( 2.0, 0.0, 1.0 );
// returns ~0.054The function accepts the following arguments:
- x:
[in] doubleinput value. - mu:
[in] doublemean. - sigma:
[in] doublestandard deviation.
double stdlib_base_dists_normal_pdf( const double x, const double mu, const double sigma );Examples
#include "stdlib/stats/base/dists/normal/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 sigma;
double mu;
double x;
double y;
int i;
for ( i = 0; i < 10; i++ ) {
x = random_uniform( 0.0, 10.0 );
mu = random_uniform( -5.0, 5.0 );
sigma = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 );
y = stdlib_base_dists_normal_pdf( x, mu, sigma );
printf( "x: %lf, µ: %lf, σ: %lf, f(x;µ,σ): %lf\n", x, mu, sigma, y );
}
}Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
