Skip to content
Open
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
@@ -0,0 +1,76 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var tryRequire = require( '@stdlib/utils/try-require' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var logpdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( logpdf instanceof Error )
};


// MAIN //

bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
var lambda;
var len;
var k;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
k = new Float64Array( len );
lambda = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( EPS, 50.0 );
k[ i ] = discreteUniform( 1, 10 );
lambda[ i ] = uniform( EPS, 20.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = logpdf( x[ i % len ], k[ i % len ], lambda[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef STDLIB_STATS_BASE_DISTS_ERLANG_LOGPDF_H
#define STDLIB_STATS_BASE_DISTS_ERLANG_LOGPDF_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* Evaluates the natural logarithm of the probability density function (PDF) for an Erlang distribution with shape parameter `k` and rate parameter `lambda`.
*/
double stdlib_base_dists_erlang_logpdf( const double x, const double k, const double lambda );

#ifdef __cplusplus
}
#endif

#endif // !STDLIB_STATS_BASE_DISTS_ERLANG_LOGPDF_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var addon = require( './../src/addon.node' );


// MAIN //

/**
* Evaluates the natural logarithm of the probability density function (PDF) for an Erlang distribution with shape parameter `k` and rate parameter `lambda` at a value `x`.
*
* @private
* @param {number} x - input value
* @param {NonNegativeInteger} k - shape parameter
* @param {PositiveNumber} lambda - rate parameter
* @returns {number} evaluated logPDF
*
* @example
* var y = logpdf( 0.1, 1, 1.0 );
* // returns ~-0.1
*/
function logpdf( x, k, lambda ) {
return addon( x, k, lambda );
}


// EXPORTS //

module.exports = logpdf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"options": {
"task": "build",
"wasm": false
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"wasm": false,
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/ternary",
"@stdlib/math/base/assert/is-nonnegative-integer",
"@stdlib/math/base/assert/is-nan",
"@stdlib/stats/base/dists/gamma/logpdf",
"@stdlib/constants/float64/ninf",
"@stdlib/constants/float64/pinf"
]
},
{
"task": "benchmark",
"wasm": false,
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nonnegative-integer",
"@stdlib/math/base/assert/is-nan",
"@stdlib/stats/base/dists/gamma/logpdf",
"@stdlib/constants/float64/ninf",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/eps"
]
},
{
"task": "examples",
"wasm": false,
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nonnegative-integer",
"@stdlib/math/base/assert/is-nan",
"@stdlib/stats/base/dists/gamma/logpdf",
"@stdlib/constants/float64/ninf",
"@stdlib/constants/float64/pinf"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"benchmark": "./benchmark",
"doc": "./docs",
"example": "./examples",
"include": "./include",
"lib": "./lib",
"src": "./src",
"test": "./test"
},
"types": "./docs/types",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_erlang_logpdf )
51 changes: 51 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/erlang/logpdf/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/stats/base/dists/erlang/logpdf.h"
#include "stdlib/stats/base/dists/gamma/logpdf.h"
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/constants/float64/ninf.h"
#include "stdlib/constants/float64/pinf.h"

/**
* Evaluates the natural logarithm of the probability density function (PDF) for an Erlang distribution with shape parameter `k` and rate parameter `lambda` at a value `x`.
*
* @param x input value
* @param k shape parameter (must be a nonnegative integer)
* @param lambda rate parameter (must be positive)
* @return evaluated logPDF
*
* @example
* double v = stdlib_base_dists_erlang_logpdf( 0.1, 1, 1.0 );
* // returns ~-0.1
*/
double stdlib_base_dists_erlang_logpdf( const double x, const double k, const double lambda ) {
if (
stdlib_base_is_nan( x ) ||
!stdlib_base_is_nonnegative_integer( k ) ||
stdlib_base_is_nan( lambda ) ||
lambda <= 0.0
) {
return 0.0 / 0.0; // NaN
}
if ( k == 0.0 ) {
return ( x == 0.0 ) ? STDLIB_CONSTANT_FLOAT64_PINF : STDLIB_CONSTANT_FLOAT64_NINF;
}
return stdlib_base_dists_gamma_logpdf( x, k, lambda );
}