Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
162 changes: 162 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/halfnormal/cdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# Cumulative Distribution Function

> [Half-normal][half-normal-distribution] distribution [cumulative distribution function][cdf].

<section class="intro">

The [cumulative distribution function][cdf] for a [half-normal][half-normal-distribution] random variable is

```math
F(x;\mu,\sigma) = \mathop{\mathrm{erf}}\left( \frac{x-\mu}{\sigma\sqrt{2}} \right)
```

where `µ` is the location parameter and `σ` is the scale parameter.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );
```

#### cdf( x, mu, sigma )

Evaluates the [cumulative distribution function][cdf] (CDF) for a [half-normal][half-normal-distribution] distribution with parameters `mu` (location) and `sigma` (scale).

```javascript
var y = cdf( 2.0, 0.0, 1.0 );
// returns 0.9544997361036416
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// returns 0.9544997361036416
// returns ~0.9545


y = cdf( -1.0, 0.0, 1.0 );
// returns 0.0

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

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

```javascript
var y = cdf( NaN, 0.0, 1.0 );
// returns NaN

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

y = cdf( 0.0, 0.0, NaN );
// returns NaN
```

If provided `sigma < 0`, the function returns `NaN`.

```javascript
var y = cdf( 2.0, 0.0, -1.0 );
// returns NaN
```

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

```javascript
var y = cdf( 2.0, 8.0, 0.0 );
// returns 0.0

y = cdf( 8.0, 8.0, 0.0 );
// returns 1.0

y = cdf( 10.0, 8.0, 0.0 );
// returns 1.0
```

#### cdf.factory( mu, sigma )

Returns a function for evaluating the [cumulative distribution function][cdf] of a [half-normal][half-normal-distribution] distribution with parameters `mu` and `sigma`.

```javascript
var mycdf = cdf.factory( 0.0, 1.0 );

var y = mycdf( 2.0 );
// returns 0.9544997361036416

y = mycdf( -1.0 );
// returns 0.0
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```javascript
var randu = require( '@stdlib/random/base/randu' );
var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );

var sigma;
var mu;
var x;
var y;
var i;

for ( i = 0; i < 10; i++ ) {
x = randu() * 10.0;
mu = (randu() * 10.0) - 5.0;
sigma = randu() * 20.0;
y = cdf( x, mu, sigma );
console.log( 'x: %d, µ: %d, σ: %d, F(x;µ,σ): %d', x, mu, sigma, y );
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Examples section uses the older randu() + manual loop pattern. To match the current stdlib style (see reference packages like normal/cdf and rayleigh/cdf), this should use uniform() + logEachMap():

Suggested change
var randu = require( '@stdlib/random/base/randu' );
var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );
var sigma;
var mu;
var x;
var y;
var i;
for ( i = 0; i < 10; i++ ) {
x = randu() * 10.0;
mu = (randu() * 10.0) - 5.0;
sigma = randu() * 20.0;
y = cdf( x, mu, sigma );
console.log( 'x: %d, µ: %d, σ: %d, F(x;µ,σ): %d', x, mu, sigma, y );
}
var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );
var opts = {
'dtype': 'float64'
};
var x = uniform( 10, 0.0, 10.0, opts );
var mu = uniform( 10, 0.0, 5.0, opts );
var sigma = uniform( 10, 0.0, 10.0, opts );
logEachMap( 'x: %0.4f, µ: %0.4f, σ: %0.4f, F(x;µ,σ): %0.4f', x, mu, sigma, cdf );

```

</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

[half-normal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var sigma;
var len;
var mu;
var x;
var y;
var i;

len = 100;
x = uniform( len, 0.0, 100.0 );
mu = uniform( len, 0.0, 50.0 );
sigma = uniform( len, EPS, 20.0 + EPS );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cdf( x[ i % len ], mu[ i % len ], sigma[ i % len ]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space before closing parenthesis. stdlib style requires spaces inside function call parentheses: foo( bar ) not foo(bar).

Suggested change
y = cdf( x[ i % len ], mu[ i % len ], sigma[ i % len ]);
y = cdf( x[ i % len ], mu[ i % len ], sigma[ 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( pkg+':factory', function benchmark( b ) {

Check warning on line 61 in lib/node_modules/@stdlib/stats/base/dists/halfnormal/cdf/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var mycdf;
var sigma;
var mu;
var x;
var y;
var i;

mu = 0.0;
sigma = 1.5;
mycdf = cdf.factory( mu, sigma );
x = uniform( 100, 0.0, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mycdf( x[ i % x.length ] );
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,79 @@

{{alias}}( x, μ, σ )
Evaluates the cumulative distribution function (CDF) for a half-normal
distribution with location `μ` and scale `σ` at a value `x`.

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

If provided `σ < 0`, the function returns `NaN`.

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

μ: number
Location parameter.

σ: number
Scale parameter.

Returns
-------
out: number
Evaluated CDF.

Examples
--------
> var y = {{alias}}( 2.0, 0.0, 1.0 )
0.9544997361036416
> y = {{alias}}( -1.0, 0.0, 1.0 )
0.0
> y = {{alias}}( -1.0, 2.0, 2.0 )
0.0
> y = {{alias}}( NaN, 0.0, 1.0 )
NaN
> y = {{alias}}( 0.0, NaN, 1.0 )
NaN
> y = {{alias}}( 0.0, 0.0, NaN )
NaN

// Negative scale parameter:
> y = {{alias}}( 2.0, 0.0, -1.0 )
NaN

// Degenerate distribution centered at `μ` when `σ = 0.0`:
> y = {{alias}}( 2.0, 8.0, 0.0 )
0.0
> y = {{alias}}( 8.0, 8.0, 0.0 )
1.0
> y = {{alias}}( 10.0, 8.0, 0.0 )
1.0


{{alias}}.factory( μ, σ )
Returns a function for evaluating the cumulative distribution function (CDF)
of a half-normal distribution with location `μ` and scale `σ`.

Parameters
----------
μ: number
Location parameter.

σ: number
Scale parameter.

Returns
-------
cdf: Function
Cumulative distribution function (CDF).

Examples
--------
> var myCDF = {{alias}}.factory( 0.0, 1.0 );
> var y = myCDF( 2.0 )
0.9544997361036416

See Also
--------

Loading
Loading