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
187 changes: 187 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/erlang/logcdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<!--

@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.

-->

# Logarithm of Cumulative Distribution Function

> Evaluate the natural logarithm of the cumulative distribution function (CDF) for an [Erlang][erlang-distribution] distribution.

<section class="intro">

The [cumulative distribution function][cdf] for a [Erlang][erlang-distribution] random variable is

<!-- <equation class="equation" label="eq:erlang_cdf" align="center" raw="F(x; k,\lambda) = 1 - \sum_{n=0}^{k-1}\frac{1}{n!}e^{-\lambda x}(\lambda x)^n" alt="Cumulative distribution function for a Erlang distribution."> -->

```math
F(x; k,\lambda) = 1 - \sum_{n=0}^{k-1}\frac{1}{n!}e^{-\lambda x}(\lambda x)^n
```

<!-- <div class="equation" align="center" data-raw-text="F(x; k,\lambda) = 1 - \sum_{n=0}^{k-1}\frac{1}{n!}e^{-\lambda x}(\lambda x)^n" data-equation="eq:erlang_cdf">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/erlang/cdf/docs/img/equation_erlang_cdf.svg" alt="Cumulative distribution function for a Erlang distribution.">
<br>
</div> -->

<!-- </equation> -->

where `k` is the nonnegative integer shape parameter and `lambda > 0` is the rate parameter. The [Erlang][erlang-distribution] distribution is a special case of the gamma distribution, as `k` is constrained to the natural numbers.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var logcdf = require( '@stdlib/stats/base/dists/erlang/logcdf' );
```

#### logcdf( x, k, lambda )

Evaluates the natural logarithm of the [cumulative distribution function][cdf] (CDF) for an [Erlang][erlang-distribution] distribution with parameters `k` (shape parameter) and `lambda` (rate parameter).

```javascript
var y = logcdf( 2.0, 1, 1.0 );
// returns ~-0.145

y = logcdf( 2.0, 3, 1.0 );
// returns ~-1.129

y = logcdf( -1.0, 2, 2.0 );
// returns -Infinity

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

y = logcdf( +Infinity, 4, 2.0 );
// returns 0.0
```

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

```javascript
var y = logcdf( NaN, 1, 1.0 );
// returns NaN

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

y = logcdf( 0.0, 1, NaN );
// returns NaN
```

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

```javascript
var y = logcdf( 2.0, -2, 0.5 );
// returns NaN

y = logcdf( 2.0, 0.5, 0.5 );
// returns NaN
```

If provided `k = 0`, the function evaluates the logarithm of the [CDF][cdf] of a [degenerate distribution][degenerate-distribution] centered at `0`.

```javascript
var y = logcdf( 2.0, 0.0, 2.0 );
// returns -0.0

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

y = logcdf( 0.0, 0.0, 2.0 );
// returns -0.0
```

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

```javascript
var y = logcdf( 2.0, 1, 0.0 );
// returns NaN

y = logcdf( 2.0, 1, -5.0 );
// returns NaN
```

#### logcdf.factory( k, lambda )

Returns a `function` for evaluating the [cumulative distribution function][cdf] for an [Erlang][erlang-distribution] distribution with parameters `k` (shape parameter) and `lambda` (rate parameter).

```javascript
var mylogcdf = logcdf.factory( 2, 0.5 );

var y = mylogcdf( 6.0 );
// returns ~-0.222

y = mylogcdf( 2.0 );
// returns ~-1.331
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

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

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: %lf, k: %d, λ: %lf, F(x;k,λ): %lf', x, k, lambda, logcdf );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function

[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution

[erlang-distribution]: https://en.wikipedia.org/wiki/Erlang_distribution

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @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 bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var logcdf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var lambda;
var len;
var k;
var x;
var y;
var i;

len = 100;
x = uniform( len, 0.0, 100.0 );
k = discreteUniform( len, 0.0, 100.0 );
lambda = uniform( len, EPS, 20.0 );
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = logcdf( 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();
});

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

len = 100;
k = 2.0;
lambda = 1.5;
x = uniform( len, EPS, 50.0 );
mylogpdf = logcdf.factory( k, lambda );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mylogpdf( x[ 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,83 @@

{{alias}}( x, k, λ )
Evaluates the natural logarithm of cumulative distribution function
(CDF) for an Erlang distribution with shape parameter `k` and rate
parameter `λ` at a value `x`.

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

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

If provided a non-positive value for `λ`, the function returns `NaN`.

Parameters
----------
x: number
Input value.

k: number
Shape parameter.

λ: number
Rate parameter.

Returns
-------
out: number
Evaluated logCDF.

Examples
--------
> var y = {{alias}}( 2.0, 1, 1.0 )
~-0.145
> y = {{alias}}( 2.0, 3, 1.0 )
~-1.129
> y = {{alias}}( 2.0, 2.5, 1.0 )
NaN
> y = {{alias}}( -1.0, 2, 2.0 )
-Infinity
> y = {{alias}}( {{alias:@stdlib/constants/float64/pinf}}, 4, 2.0 )
0.0
> y = {{alias}}( {{alias:@stdlib/constants/float64/ninf}}, 4, 2.0 )
-Infinity
> y = {{alias}}( NaN, 0, 1.0 )
NaN
> y = {{alias}}( 0.0, NaN, 1.0 )
NaN
> y = {{alias}}( 0.0, 0, NaN )
NaN
> y = {{alias}}( 2.0, -1, 1.0 )
NaN
> y = {{alias}}( 2.0, 1, -1.0 )
NaN


{{alias}}.factory( k, λ )
Returns a function for evaluating the natural logarithm of cumulative
distribution function (CDF) of an Erlang distribution with shape parameter
`k` and rate parameter `λ`.

Parameters
----------
k: number
Shape parameter.

λ: number
Rate parameter.

Returns
-------
logcdf: Function
Natural logarithm of cumulative distribution function (CDF).

Examples
--------
> var mycdf = {{alias}}.factory( 2, 0.5 );
> var y = mycdf( 6.0 )
~-0.222
> y = mycdf( 2.0 )
~-1.331

See Also
--------

Loading