Skip to content

Commit 655c367

Browse files
committed
feat: initial implementation of @stdlib/stats/base/dists/halfnormal/cdf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent db05364 commit 655c367

15 files changed

Lines changed: 1367 additions & 0 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Cumulative Distribution Function
22+
23+
> [Half-normal][half-normal-distribution] distribution [cumulative distribution function][cdf].
24+
25+
<section class="intro">
26+
27+
The [cumulative distribution function][cdf] for a [half-normal][half-normal-distribution] random variable is
28+
29+
```math
30+
F(x;\mu,\sigma) = \mathop{\mathrm{erf}}\left( \frac{x-\mu}{\sigma\sqrt{2}} \right)
31+
```
32+
33+
where `µ` is the location parameter and `σ` is the scale parameter.
34+
35+
</section>
36+
37+
<!-- /.intro -->
38+
39+
<section class="usage">
40+
41+
## Usage
42+
43+
```javascript
44+
var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );
45+
```
46+
47+
#### cdf( x, mu, sigma )
48+
49+
Evaluates the [cumulative distribution function][cdf] (CDF) for a [half-normal][half-normal-distribution] distribution with parameters `mu` (location) and `sigma` (scale).
50+
51+
```javascript
52+
var y = cdf( 2.0, 0.0, 1.0 );
53+
// returns 0.9544997361036416
54+
55+
y = cdf( -1.0, 0.0, 1.0 );
56+
// returns 0.0
57+
58+
y = cdf( -1.0, 2.0, 2.0 );
59+
// returns 0.0
60+
```
61+
62+
If provided `NaN` as any argument, the function returns `NaN`.
63+
64+
```javascript
65+
var y = cdf( NaN, 0.0, 1.0 );
66+
// returns NaN
67+
68+
y = cdf( 0.0, NaN, 1.0 );
69+
// returns NaN
70+
71+
y = cdf( 0.0, 0.0, NaN );
72+
// returns NaN
73+
```
74+
75+
If provided `sigma < 0`, the function returns `NaN`.
76+
77+
```javascript
78+
var y = cdf( 2.0, 0.0, -1.0 );
79+
// returns NaN
80+
```
81+
82+
If provided `sigma = 0`, the function evaluates the [CDF][cdf] of a [degenerate distribution][degenerate-distribution] centered at `mu`.
83+
84+
```javascript
85+
var y = cdf( 2.0, 8.0, 0.0 );
86+
// returns 0.0
87+
88+
y = cdf( 8.0, 8.0, 0.0 );
89+
// returns 1.0
90+
91+
y = cdf( 10.0, 8.0, 0.0 );
92+
// returns 1.0
93+
```
94+
95+
#### cdf.factory( mu, sigma )
96+
97+
Returns a function for evaluating the [cumulative distribution function][cdf] of a [half-normal][half-normal-distribution] distribution with parameters `mu` and `sigma`.
98+
99+
```javascript
100+
var mycdf = cdf.factory( 0.0, 1.0 );
101+
102+
var y = mycdf( 2.0 );
103+
// returns 0.9544997361036416
104+
105+
y = mycdf( -1.0 );
106+
// returns 0.0
107+
```
108+
109+
</section>
110+
111+
<!-- /.usage -->
112+
113+
<section class="examples">
114+
115+
## Examples
116+
117+
<!-- eslint no-undef: "error" -->
118+
119+
```javascript
120+
var randu = require( '@stdlib/random/base/randu' );
121+
var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );
122+
123+
var sigma;
124+
var mu;
125+
var x;
126+
var y;
127+
var i;
128+
129+
for ( i = 0; i < 10; i++ ) {
130+
x = randu() * 10.0;
131+
mu = (randu() * 10.0) - 5.0;
132+
sigma = randu() * 20.0;
133+
y = cdf( x, mu, sigma );
134+
console.log( 'x: %d, µ: %d, σ: %d, F(x;µ,σ): %d', x, mu, sigma, y );
135+
}
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
</section>
147+
148+
<!-- /.related -->
149+
150+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
151+
152+
<section class="links">
153+
154+
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
155+
156+
[half-normal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution
157+
158+
[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution
159+
160+
</section>
161+
162+
<!-- /.links -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var EPS = require( '@stdlib/constants/float64/eps' );
27+
var pkg = require( './../package.json' ).name;
28+
var cdf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var sigma;
35+
var len;
36+
var mu;
37+
var x;
38+
var y;
39+
var i;
40+
41+
len = 100;
42+
x = uniform( len, 0.0, 100.0 );
43+
mu = uniform( len, 0.0, 50.0 );
44+
sigma = uniform( len, EPS, 20.0 + EPS );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
y = cdf( x[ i % len ], mu[ i % len ], sigma[ i % len ]);
49+
if ( isnan( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
}
53+
b.toc();
54+
if ( isnan( y ) ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( pkg+':factory', function benchmark( b ) {
62+
var mycdf;
63+
var sigma;
64+
var mu;
65+
var x;
66+
var y;
67+
var i;
68+
69+
mu = 0.0;
70+
sigma = 1.5;
71+
mycdf = cdf.factory( mu, sigma );
72+
x = uniform( 100, 0.0, 10.0 );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
y = mycdf( x[ i % x.length ] );
77+
if ( isnan( y ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( y ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
{{alias}}( x, μ, σ )
3+
Evaluates the cumulative distribution function (CDF) for a half-normal
4+
distribution with location `μ` and scale `σ` at a value `x`.
5+
6+
If provided `NaN` as any argument, the function returns `NaN`.
7+
8+
If provided `σ < 0`, the function returns `NaN`.
9+
10+
Parameters
11+
----------
12+
x: number
13+
Input value.
14+
15+
μ: number
16+
Location parameter.
17+
18+
σ: number
19+
Scale parameter.
20+
21+
Returns
22+
-------
23+
out: number
24+
Evaluated CDF.
25+
26+
Examples
27+
--------
28+
> var y = {{alias}}( 2.0, 0.0, 1.0 )
29+
0.9544997361036416
30+
> y = {{alias}}( -1.0, 0.0, 1.0 )
31+
0.0
32+
> y = {{alias}}( -1.0, 2.0, 2.0 )
33+
0.0
34+
> y = {{alias}}( NaN, 0.0, 1.0 )
35+
NaN
36+
> y = {{alias}}( 0.0, NaN, 1.0 )
37+
NaN
38+
> y = {{alias}}( 0.0, 0.0, NaN )
39+
NaN
40+
41+
// Negative scale parameter:
42+
> y = {{alias}}( 2.0, 0.0, -1.0 )
43+
NaN
44+
45+
// Degenerate distribution centered at `μ` when `σ = 0.0`:
46+
> y = {{alias}}( 2.0, 8.0, 0.0 )
47+
0.0
48+
> y = {{alias}}( 8.0, 8.0, 0.0 )
49+
1.0
50+
> y = {{alias}}( 10.0, 8.0, 0.0 )
51+
1.0
52+
53+
54+
{{alias}}.factory( μ, σ )
55+
Returns a function for evaluating the cumulative distribution function (CDF)
56+
of a half-normal distribution with location `μ` and scale `σ`.
57+
58+
Parameters
59+
----------
60+
μ: number
61+
Location parameter.
62+
63+
σ: number
64+
Scale parameter.
65+
66+
Returns
67+
-------
68+
cdf: Function
69+
Cumulative distribution function (CDF).
70+
71+
Examples
72+
--------
73+
> var myCDF = {{alias}}.factory( 0.0, 1.0 );
74+
> var y = myCDF( 2.0 )
75+
0.9544997361036416
76+
77+
See Also
78+
--------
79+

0 commit comments

Comments
 (0)