Skip to content

Commit f4a0718

Browse files
feat: add implementation for stats/base/dists/halfnormal/logpdf
PR-URL: #9708 Ref: #9416 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 5e92b2e commit f4a0718

File tree

30 files changed

+2522
-0
lines changed

30 files changed

+2522
-0
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# Logarithm of Probability Density Function
22+
23+
> [Half-Normal][half-normal-distribution] distribution logarithm of [probability density function (PDF)][pdf].
24+
25+
<section class="intro">
26+
27+
The [probability density function][pdf] (PDF) for a [half-normal][half-normal-distribution] random variable is
28+
29+
<!-- <equation class="equation" label="eq:halfnormal_pdf" align="center" raw="f(x;\sigma) = \frac{\sqrt{2}}{\sigma\sqrt{\pi}} e^{-\frac{x^2}{2\sigma^2}}" alt="Probability density function (PDF) for a half-normal distribution."> -->
30+
31+
```math
32+
f(x;\sigma) = \frac{\sqrt{2}}{\sigma\sqrt{\pi}} e^{-\frac{x^2}{2\sigma^2}}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="f(x;\sigma) = \frac{\sqrt{2}}{\sigma\sqrt{\pi}} e^{-\frac{x^2}{2\sigma^2}}" data-equation="eq:halfnormal_pdf">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@main/lib/node_modules/@stdlib/stats/base/dists/halfnormal/logpdf/docs/img/equation_halfnormal_pdf.svg" alt="Probability density function (PDF) for a half-normal distribution.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
for `x >= 0`, where `sigma > 0` is the scale parameter. For `x < 0`, the PDF is `0`.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var logpdf = require( '@stdlib/stats/base/dists/halfnormal/logpdf' );
54+
```
55+
56+
#### logpdf( x, sigma )
57+
58+
Evaluates the logarithm of the [probability density function][pdf] (PDF) for a [half-normal][half-normal-distribution] distribution with parameter `sigma` (scale parameter).
59+
60+
```javascript
61+
var y = logpdf( 0.8, 1.0 );
62+
// returns ~-0.546
63+
64+
y = logpdf( 0.5, 1.0 );
65+
// returns ~-0.351
66+
67+
y = logpdf( 1.2, 2.0 );
68+
// returns ~-1.099
69+
```
70+
71+
If `x < 0`, the function returns `-Infinity`.
72+
73+
```javascript
74+
var y = logpdf( -0.2, 1.0 );
75+
// returns -Infinity
76+
```
77+
78+
If provided `NaN` as any argument, the function returns `NaN`.
79+
80+
```javascript
81+
var y = logpdf( NaN, 1.0 );
82+
// returns NaN
83+
84+
y = logpdf( 0.0, NaN );
85+
// returns NaN
86+
```
87+
88+
If provided `sigma <= 0`, the function returns `NaN`.
89+
90+
```javascript
91+
var y = logpdf( 2.0, -1.0 );
92+
// returns NaN
93+
94+
y = logpdf( 2.0, 0.0 );
95+
// returns NaN
96+
```
97+
98+
#### logpdf.factory( sigma )
99+
100+
Returns a `function` for evaluating the logarithm of the [PDF][pdf] for a [half-normal][half-normal-distribution] distribution with parameter `sigma` (scale parameter).
101+
102+
```javascript
103+
var mylogpdf = logpdf.factory( 1.0 );
104+
105+
var y = mylogpdf( 0.8 );
106+
// returns ~-0.546
107+
108+
y = mylogpdf( 1.2 );
109+
// returns ~-0.946
110+
```
111+
112+
</section>
113+
114+
<!-- /.usage -->
115+
116+
<section class="notes">
117+
118+
## Notes
119+
120+
- In virtually all cases, using the `logpdf` or `logcdf` functions is preferable to manually computing the logarithm of the `pdf` or `cdf`, respectively, since the latter is prone to overflow and underflow.
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var uniform = require( '@stdlib/random/array/uniform' );
134+
var logEachMap = require( '@stdlib/console/log-each-map' );
135+
var logpdf = require( '@stdlib/stats/base/dists/halfnormal/logpdf' );
136+
137+
var opts = {
138+
'dtype': 'float64'
139+
};
140+
var x = uniform( 25, 0.0, 3.0, opts );
141+
var sigma = uniform( 25, 0.0, 3.0, opts );
142+
143+
logEachMap( 'x: %0.4f, σ: %0.4f, ln(f(x;σ)): %0.4f', x, sigma, logpdf );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- C interface documentation. -->
151+
152+
* * *
153+
154+
<section class="c">
155+
156+
## C APIs
157+
158+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
159+
160+
<section class="intro">
161+
162+
</section>
163+
164+
<!-- /.intro -->
165+
166+
<!-- C usage documentation. -->
167+
168+
<section class="usage">
169+
170+
### Usage
171+
172+
```c
173+
#include "stdlib/stats/base/dists/halfnormal/logpdf.h"
174+
```
175+
176+
#### stdlib_base_dists_halfnormal_logpdf( x, sigma )
177+
178+
Evaluates the logarithm of the [probability density function][pdf] (PDF) for a [Half-Normal][half-normal-distribution] distribution with parameter `sigma` (scale parameter).
179+
180+
```c
181+
double out = stdlib_base_dists_halfnormal_logpdf( 0.8, 1.0 );
182+
// returns ~-0.546
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **x**: `[in] double` input value.
188+
- **sigma**: `[in] double` scale parameter.
189+
190+
```c
191+
double stdlib_base_dists_halfnormal_logpdf( const double x, const double sigma );
192+
```
193+
194+
</section>
195+
196+
<!-- /.usage -->
197+
198+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
199+
200+
<section class="notes">
201+
202+
</section>
203+
204+
<!-- /.notes -->
205+
206+
<!-- C API usage examples. -->
207+
208+
<section class="examples">
209+
210+
### Examples
211+
212+
```c
213+
#include "stdlib/stats/base/dists/halfnormal/logpdf.h"
214+
#include <stdlib.h>
215+
#include <stdio.h>
216+
217+
static double random_uniform( const double min, const double max ) {
218+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
219+
return min + ( v*(max-min) );
220+
}
221+
222+
int main( void ) {
223+
double sigma;
224+
double x;
225+
double y;
226+
int i;
227+
228+
for ( i = 0; i < 25; i++ ) {
229+
x = random_uniform( 0.0, 10.0 );
230+
sigma = random_uniform( 0.1, 10.0 );
231+
y = stdlib_base_dists_halfnormal_logpdf( x, sigma );
232+
printf( "x: %lf, σ: %lf, ln(f(x;σ)): %lf\n", x, sigma, y );
233+
}
234+
}
235+
```
236+
237+
</section>
238+
239+
<!-- /.examples -->
240+
241+
</section>
242+
243+
<!-- /.c -->
244+
245+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
246+
247+
<section class="related">
248+
249+
</section>
250+
251+
<!-- /.related -->
252+
253+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
254+
255+
<section class="links">
256+
257+
[half-normal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution
258+
259+
[pdf]: https://en.wikipedia.org/wiki/Probability_density_function
260+
261+
</section>
262+
263+
<!-- /.links -->
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 Float64Array = require( '@stdlib/array/float64' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var format = require( '@stdlib/string/format' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var EPS = require( '@stdlib/constants/float64/eps' );
29+
var pkg = require( './../package.json' ).name;
30+
var logpdf = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var sigma;
37+
var len;
38+
var x;
39+
var y;
40+
var i;
41+
42+
len = 100;
43+
x = new Float64Array( len );
44+
sigma = new Float64Array( len );
45+
for ( i = 0; i < len; i++ ) {
46+
x[ i ] = uniform( 0.0, 10.0 );
47+
sigma[ i ] = uniform( EPS, 10.0 );
48+
}
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = logpdf( x[ i % len ], sigma[ i % len ] );
53+
if ( isnan( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
bench( format( '%s:factory', pkg ), function benchmark( b ) {
66+
var mylogpdf;
67+
var sigma;
68+
var len;
69+
var x;
70+
var y;
71+
var i;
72+
73+
sigma = 4.0;
74+
mylogpdf = logpdf.factory( sigma );
75+
len = 100;
76+
x = new Float64Array( len );
77+
for ( i = 0; i < len; i++ ) {
78+
x[ i ] = uniform( 0.0, 20.0 );
79+
}
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
y = mylogpdf( x[ i % len ] );
84+
if ( isnan( y ) ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
}
88+
b.toc();
89+
if ( isnan( y ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});

0 commit comments

Comments
 (0)