Skip to content

Commit 2825e0a

Browse files
committed
feat : add C implementation for stats/base/dists/truncated-normal/pdf
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 318ba82 commit 2825e0a

File tree

20 files changed

+1733
-2
lines changed

20 files changed

+1733
-2
lines changed

lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf/README.md

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ f(x;\mu,\sigma,a,b) = \begin{cases} \frac{\frac{1}{\sigma}\phi(\frac{x - \mu}{\
4040

4141
<!-- </equation> -->
4242

43-
where `Phi` and `phi` denote the [cumulative distribution function][cdf] and [density function][pdf] of the [normal][normal-distribution] distribution, respectively, `mu` is the location and `sigma > 0` is the scale parameter of the distribution. `a` and `b` are the minimum and maximum support.
43+
where `Phi` and `phi` denote the [cumulative distribution function][pdf] and [density function][pdf] of the [normal][normal-distribution] distribution, respectively, `mu` is the location and `sigma > 0` is the scale parameter of the distribution. `a` and `b` are the minimum and maximum support.
4444

4545
</section>
4646

@@ -145,6 +145,111 @@ for ( i = 0; i < 25; i++ ) {
145145

146146
<!-- /.examples -->
147147

148+
<!-- C interface documentation. -->
149+
150+
* * *
151+
152+
<section class="c">
153+
154+
## C APIs
155+
156+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
157+
158+
<section class="intro">
159+
160+
</section>
161+
162+
<!-- /.intro -->
163+
164+
<!-- C usage documentation. -->
165+
166+
<section class="usage">
167+
168+
### Usage
169+
170+
```c
171+
#include "stdlib/stats/base/dists/truncated-normal/pdf.h"
172+
```
173+
174+
#### stdlib_base_dists_truncated_normal_pdf( x, a, b , mu , sigma )
175+
176+
Evaluates the r distribution function (pdf) for an truncated-normal distribution.
177+
178+
```c
179+
double out = stdlib_base_dists_truncated_normal_pdf( 0.9, 0.0, 1.0, 0.0, 1.0 );
180+
// returns ~0.7795
181+
```
182+
183+
The function accepts the following arguments:
184+
185+
- **x**: `[in] double` input value.
186+
- **a**: `[in] double` lower limit.
187+
- **b**: `[in] double` upper limit.
188+
- **mu**: `[in] double` location parameter.
189+
- **sigma**: `[in] double` scale parameter.
190+
191+
192+
```c
193+
double stdlib_base_dists_truncated-normal_pdf( const double x, const double a, const double b , const double mu , const double sigma );
194+
```
195+
</section>
196+
197+
<!-- /.usage -->
198+
199+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
200+
201+
<section class="notes">
202+
203+
</section>
204+
205+
<!-- /.notes -->
206+
207+
<!-- C API usage examples. -->
208+
209+
<section class="examples">
210+
211+
### Examples
212+
213+
```c
214+
#include "stdlib/stats/base/dists/truncated-normal/pdf.h"
215+
#include <stdlib.h>
216+
#include <stdio.h>
217+
#include <math.h>
218+
219+
static double random_uniform( const double min, const double max ) {
220+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
221+
return min + ( v * (max - min) );
222+
}
223+
224+
int main( void ) {
225+
double x;
226+
double a;
227+
double b;
228+
double mu;
229+
double sigma;
230+
double y;
231+
int i;
232+
233+
for ( i = 0; i < 25; i++ ) {
234+
x = random_uniform( -10.0, 10.0 );
235+
a = random_uniform( -20.0, 0.0 );
236+
b = random_uniform( a, a + 40.0 );
237+
mu = random_uniform( a, b );
238+
sigma = random_uniform( 0.1, 5.0 );
239+
y = stdlib_base_dists_truncated_normal_pdf( x, a, b, mu, sigma );
240+
printf( "x: %lf, a: %lf, b: %lf, mu: %lf, sigma: %lf, f(x;a,b,mu,sigma): %lf\n", x, a, b, mu, sigma, y );
241+
}
242+
}
243+
```
244+
245+
</section>
246+
247+
<!-- /.examples -->
248+
249+
</section>
250+
251+
<!-- /.c -->
252+
148253
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149254
150255
<section class="related">
@@ -157,7 +262,7 @@ for ( i = 0; i < 25; i++ ) {
157262
158263
<section class="links">
159264
160-
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
265+
[pdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
161266
162267
[pdf]: https://en.wikipedia.org/wiki/Probability_density_function
163268
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 Float64Array = require( '@stdlib/array/float64' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var pdf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var len;
35+
var x;
36+
var a;
37+
var bmin;
38+
var bmax;
39+
var mu;
40+
var sigma;
41+
var y;
42+
var i;
43+
44+
len = 100;
45+
x = new Float64Array( len );
46+
a = new Float64Array( len );
47+
bmin = new Float64Array( len );
48+
bmax = new Float64Array( len );
49+
mu = new Float64Array( len );
50+
sigma = new Float64Array( len );
51+
52+
for ( i = 0; i < len; i++ ) {
53+
x[ i ] = uniform( -10.0, 10.0 );
54+
a[ i ] = uniform( -20.0, 0.0 );
55+
bmin[ i ] = uniform( a[ i ], a[ i ] + 40.0 );
56+
bmax[ i ] = uniform( 0.1, 5.0 );
57+
mu[ i ] = uniform( -5.0, 5.0 );
58+
sigma[ i ] = uniform( 0.1, 5.0 );
59+
}
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
y = pdf( x[ i % len ], a[ i % len ], bmin[ i % len ], mu[ i % len ], sigma[ i % len ] );
64+
if ( isnan( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( y ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( pkg+':factory', function benchmark( b ) {
77+
var mypdf;
78+
var sigma;
79+
var bmin;
80+
var len;
81+
var mu;
82+
var a;
83+
var x;
84+
var y;
85+
var i;
86+
87+
sigma = 1.0;
88+
bmin = 1.0;
89+
len = 100;
90+
mu = 0.5;
91+
a = 0.0;
92+
x = new Float64Array( len );
93+
mypdf = pdf.factory( a, bmin, mu, sigma );
94+
for ( i = 0; i < len; i++ ) {
95+
x[ i ] = uniform( -2.0, 2.0 );
96+
}
97+
98+
b.tic();
99+
for ( i = 0; i < b.iterations; i++ ) {
100+
y = mypdf( x[ i % len ] );
101+
if ( isnan( y ) ) {
102+
b.fail( 'should not return NaN' );
103+
}
104+
}
105+
b.toc();
106+
if ( isnan( y ) ) {
107+
b.fail( 'should not return NaN' );
108+
}
109+
b.pass( 'benchmark finished' );
110+
b.end();
111+
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var pdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( pdf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var sigma;
44+
var bmin;
45+
var len;
46+
var mu;
47+
var x;
48+
var a;
49+
var y;
50+
var i;
51+
52+
len = 100;
53+
x = new Float64Array( len );
54+
a = new Float64Array( len );
55+
bmin = new Float64Array( len );
56+
mu = new Float64Array( len );
57+
sigma = new Float64Array( len );
58+
59+
for ( i = 0; i < len; i++ ) {
60+
x[ i ] = uniform( -10.0, 10.0 );
61+
a[ i ] = uniform( -20.0, 0.0 );
62+
bmin[ i ] = uniform( a[ i ], a[ i ] + 40.0 );
63+
mu[ i ] = uniform( -5.0, 5.0 );
64+
sigma[ i ] = uniform( 0.1, 5.0 );
65+
}
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
y = pdf( x[ i % len ], a[ i % len ], bmin[ i % len ], mu[ i % len ], sigma[ i % len ] );
70+
if ( isnan( y ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( y ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
});

0 commit comments

Comments
 (0)