Skip to content

Commit c55d944

Browse files
committed
feat: added complete implementation for stats/base/dists/halfnormal
--- 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: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed ---
1 parent 72317f7 commit c55d944

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4206
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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+
# Half-Normal
22+
23+
> Half-normal distribution.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var halfnormal = require( '@stdlib/stats/base/dists/halfnormal' );
31+
```
32+
33+
#### halfnormal
34+
35+
Half-normal distribution.
36+
37+
```javascript
38+
var dist = halfnormal;
39+
// returns {...}
40+
```
41+
42+
The namespace contains the following distribution functions:
43+
44+
<!-- <toc pattern="*+(cdf|pdf|mgf|quantile)*"> -->
45+
46+
<div class="namespace-toc">
47+
48+
- <span class="signature">[`cdf( x, sigma )`][@stdlib/stats/base/dists/halfnormal/cdf]</span><span class="delimiter">: </span><span class="description">half-normal distribution cumulative distribution function.</span>
49+
- <span class="signature">[`logcdf( x, sigma )`][@stdlib/stats/base/dists/halfnormal/logcdf]</span><span class="delimiter">: </span><span class="description">evaluate the natural logarithm of the cumulative distribution function (CDF) for a half-normal distribution.</span>
50+
- <span class="signature">[`logpdf( x, sigma )`][@stdlib/stats/base/dists/halfnormal/logpdf]</span><span class="delimiter">: </span><span class="description">evaluate the natural logarithm of the probability density function (PDF) for a half-normal distribution.</span>
51+
- <span class="signature">[`mgf( t, sigma )`][@stdlib/stats/base/dists/halfnormal/mgf]</span><span class="delimiter">: </span><span class="description">half-normal distribution moment-generating function (MGF).</span>
52+
- <span class="signature">[`pdf( x, sigma )`][@stdlib/stats/base/dists/halfnormal/pdf]</span><span class="delimiter">: </span><span class="description">half-normal distribution probability density function (PDF).</span>
53+
- <span class="signature">[`quantile( p, sigma )`][@stdlib/stats/base/dists/halfnormal/quantile]</span><span class="delimiter">: </span><span class="description">half-normal distribution quantile function.</span>
54+
55+
</div>
56+
57+
<!-- </toc> -->
58+
59+
The namespace contains the following functions for calculating distribution properties:
60+
61+
<!-- <toc pattern="*+(entropy|kurtosis|mean|median|mode|skewness|stdev|variance)*"> -->
62+
63+
<div class="namespace-toc">
64+
65+
- <span class="signature">[`entropy( sigma )`][@stdlib/stats/base/dists/halfnormal/entropy]</span><span class="delimiter">: </span><span class="description">half-normal distribution differential entropy.</span>
66+
- <span class="signature">[`kurtosis( sigma )`][@stdlib/stats/base/dists/halfnormal/kurtosis]</span><span class="delimiter">: </span><span class="description">half-normal distribution excess kurtosis.</span>
67+
- <span class="signature">[`mean( sigma )`][@stdlib/stats/base/dists/halfnormal/mean]</span><span class="delimiter">: </span><span class="description">half-normal distribution expected value.</span>
68+
- <span class="signature">[`median( sigma )`][@stdlib/stats/base/dists/halfnormal/median]</span><span class="delimiter">: </span><span class="description">half-normal distribution median.</span>
69+
- <span class="signature">[`mode( sigma )`][@stdlib/stats/base/dists/halfnormal/mode]</span><span class="delimiter">: </span><span class="description">half-normal distribution mode.</span>
70+
- <span class="signature">[`skewness( sigma )`][@stdlib/stats/base/dists/halfnormal/skewness]</span><span class="delimiter">: </span><span class="description">half-normal distribution skewness.</span>
71+
- <span class="signature">[`stdev( sigma )`][@stdlib/stats/base/dists/halfnormal/stdev]</span><span class="delimiter">: </span><span class="description">half-normal distribution standard deviation.</span>
72+
- <span class="signature">[`variance( sigma )`][@stdlib/stats/base/dists/halfnormal/variance]</span><span class="delimiter">: </span><span class="description">half-normal distribution variance.</span>
73+
74+
</div>
75+
76+
<!-- </toc> -->
77+
78+
The namespace contains a constructor function for creating a [half-normal][halfnormal-distribution] distribution object.
79+
80+
<!-- <toc pattern="*ctor*"> -->
81+
82+
<div class="namespace-toc">
83+
84+
- <span class="signature">[`HalfNormal( [sigma] )`][@stdlib/stats/base/dists/halfnormal/ctor]</span><span class="delimiter">: </span><span class="description">half-normal distribution constructor.</span>
85+
86+
</div>
87+
88+
<!-- </toc> -->
89+
90+
```javascript
91+
var HalfNormal = require( '@stdlib/stats/base/dists/halfnormal' ).HalfNormal;
92+
93+
var dist = new HalfNormal( 2.0 );
94+
95+
var y = dist.pdf( 1.0 );
96+
// returns 0.3520653267642996
97+
```
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="examples">
104+
105+
## Examples
106+
107+
<!-- eslint no-undef: "error" -->
108+
109+
```javascript
110+
var halfnormal = require( '@stdlib/stats/base/dists/halfnormal' );
111+
112+
/*
113+
A quality control engineer is analyzing the absolute deviations of product measurements from their target specification.
114+
115+
The absolute deviations follow a half-normal distribution with scale parameter σ (sigma) = 2.0 mm.
116+
117+
We can model these absolute deviations using a half-normal distribution with σ = 2.0.
118+
*/
119+
120+
var sigma = 2.0;
121+
122+
var halfnormalDist = new halfnormal.HalfNormal( sigma );
123+
124+
// Output the scale parameter:
125+
console.log( halfnormalDist.sigma );
126+
// => 2.0
127+
128+
// Mean absolute deviation:
129+
console.log( halfnormalDist.mean );
130+
// => ~1.596
131+
132+
// Median absolute deviation:
133+
console.log( halfnormalDist.median );
134+
// => ~1.349
135+
136+
// Mode (most common deviation):
137+
console.log( halfnormalDist.mode );
138+
// => 0.0
139+
140+
// Variance of absolute deviations:
141+
console.log( halfnormalDist.variance );
142+
// => 1.4535209105296745
143+
144+
// Probability density function at 1.0 mm deviation:
145+
console.log( halfnormal.pdf( 1.0, sigma ) );
146+
// => 0.3520653267642996
147+
148+
// Cumulative distribution function at 2.0 mm (portion of deviations ≤ 2.0 mm):
149+
console.log( halfnormal.cdf( 2.0, sigma ) );
150+
// => ~0.683
151+
152+
// 90th percentile of absolute deviations:
153+
console.log( halfnormal.quantile( 0.9, sigma ) );
154+
// => 3.289707253902946
155+
156+
// Moment-generating function value at 0.5:
157+
console.log( halfnormal.mgf( 0.5, sigma ) );
158+
// => 2.7742859576700094
159+
160+
// Entropy (measure of uncertainty):
161+
console.log( halfnormal.entropy( sigma ) );
162+
// => 1.4189385332046727
163+
164+
// Skewness (measure of asymmetry):
165+
console.log( halfnormal.skewness( sigma ) );
166+
// => ~0.995
167+
168+
// Excess kurtosis (measure of tail heaviness):
169+
console.log( halfnormal.kurtosis( sigma ) );
170+
// => ~0.869
171+
172+
var myquantile = halfnormal.quantile.factory( 2.0 );
173+
174+
// 25th percentile:
175+
console.log( myquantile( 0.25 ) );
176+
// => 0.6372787279287504
177+
178+
// 75th percentile:
179+
console.log( myquantile( 0.75 ) );
180+
// => 2.300698760752016
181+
182+
var mylogpdf = halfnormal.logpdf.factory( 2.0 );
183+
184+
// Logarithm of PDF at 1.0 mm:
185+
console.log( mylogpdf( 1.0 ) );
186+
// => -1.0439385332046727
187+
188+
// Logarithm of PDF at 3.0 mm:
189+
console.log( mylogpdf( 3.0 ) );
190+
// => -2.0439385332046727
191+
```
192+
193+
</section>
194+
195+
<!-- /.examples -->
196+
197+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
198+
199+
<section class="related">
200+
201+
</section>
202+
203+
<!-- /.related -->
204+
205+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
206+
207+
<section class="links">
208+
209+
[halfnormal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution
210+
211+
<!-- <toc-links> -->
212+
213+
[@stdlib/stats/base/dists/halfnormal/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/ctor
214+
215+
[@stdlib/stats/base/dists/halfnormal/entropy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/entropy
216+
217+
[@stdlib/stats/base/dists/halfnormal/kurtosis]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/kurtosis
218+
219+
[@stdlib/stats/base/dists/halfnormal/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/mean
220+
221+
[@stdlib/stats/base/dists/halfnormal/median]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/median
222+
223+
[@stdlib/stats/base/dists/halfnormal/mode]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/mode
224+
225+
[@stdlib/stats/base/dists/halfnormal/skewness]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/skewness
226+
227+
[@stdlib/stats/base/dists/halfnormal/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/stdev
228+
229+
[@stdlib/stats/base/dists/halfnormal/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/variance
230+
231+
[@stdlib/stats/base/dists/halfnormal/cdf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/cdf
232+
233+
[@stdlib/stats/base/dists/halfnormal/logcdf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/logcdf
234+
235+
[@stdlib/stats/base/dists/halfnormal/logpdf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/logpdf
236+
237+
[@stdlib/stats/base/dists/halfnormal/mgf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/mgf
238+
239+
[@stdlib/stats/base/dists/halfnormal/pdf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/pdf
240+
241+
[@stdlib/stats/base/dists/halfnormal/quantile]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/halfnormal/quantile
242+
243+
<!-- </toc-links> -->
244+
245+
</section>
246+
247+
<!-- /.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 constantFunction = require( '@stdlib/utils/constant-function' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
26+
var erf = require( '@stdlib/math/base/special/erf' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Returns a function for evaluating the cumulative distribution function (CDF) for a half-normal distribution.
33+
*
34+
* @param {PositiveNumber} sigma - scale parameter
35+
* @returns {Function} function to evaluate the cumulative distribution function
36+
*
37+
* @example
38+
* var cdf = factory( 2.0 );
39+
* var y = cdf( 1.0 );
40+
* // returns 0.3829249225480262
41+
*
42+
* y = cdf( 0.0 );
43+
* // returns 0.0
44+
*/
45+
function factory( sigma ) {
46+
var denom;
47+
if (
48+
isnan( sigma ) ||
49+
sigma < 0.0
50+
) {
51+
return constantFunction( NaN );
52+
}
53+
if ( sigma === 0.0 ) {
54+
return cdf;
55+
}
56+
denom = sigma * sqrt( 2.0 );
57+
return cdf;
58+
59+
/**
60+
* Evaluates the cumulative distribution function (CDF) for a half-normal distribution.
61+
*
62+
* @private
63+
* @param {number} x - input value
64+
* @returns {Probability} evaluated cumulative distribution function
65+
*
66+
* @example
67+
* var y = cdf( 2.0 );
68+
* // returns <number>
69+
*/
70+
function cdf( x ) {
71+
if ( isnan( x ) ) {
72+
return NaN;
73+
}
74+
if ( x < 0.0 ) {
75+
return 0.0;
76+
}
77+
if ( sigma === 0.0 ) {
78+
return ( x >= 0.0 ) ? 1.0 : 0.0;
79+
}
80+
return erf( x / denom );
81+
}
82+
}
83+
84+
85+
// EXPORTS //
86+
87+
module.exports = factory;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/**
22+
* Half-normal distribution cumulative distribution function (CDF).
23+
*
24+
* @module @stdlib/stats/base/dists/halfnormal/cdf
25+
*
26+
* @example
27+
* var cdf = require( '@stdlib/stats/base/dists/halfnormal/cdf' );
28+
*
29+
* var y = cdf( 2.0, 1.0 );
30+
* // returns 0.9544997361036416
31+
*
32+
* var myCDF = cdf.factory( 2.0 );
33+
* y = myCDF( 1.0 );
34+
* // returns 0.3829249225480262
35+
*/
36+
37+
// MODULES //
38+
39+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
40+
var main = require( './main.js' );
41+
var factory = require( './factory.js' );
42+
43+
44+
// MAIN //
45+
46+
setReadOnly( main, 'factory', factory );
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = main;

0 commit comments

Comments
 (0)