Skip to content

Commit d1d64cb

Browse files
feat: add C implementation for lognormal distribution logpdf
--- 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: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 584df38 commit d1d64cb

File tree

16 files changed

+1534
-11
lines changed

16 files changed

+1534
-11
lines changed

lib/node_modules/@stdlib/stats/base/dists/lognormal/logpdf/README.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,112 @@ for ( i = 0; i < 10; i++ ) {
134134
}
135135
```
136136

137+
<!-- /.examples -->
138+
139+
<!-- C interface documentation. -->
140+
141+
* * *
142+
143+
<section class="c">
144+
145+
## C APIs
146+
147+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
148+
149+
<section class="intro">
150+
151+
</section>
152+
153+
<!-- /.intro -->
154+
155+
<!-- C usage documentation. -->
156+
157+
<section class="usage">
158+
159+
### Usage
160+
161+
```c
162+
#include "stdlib/stats/base/dists/lognormal/logpdf.h"
163+
```
164+
165+
#### stdlib_base_dists_lognormal_logpdf( x, mu, sigma )
166+
167+
Evaluates the logarithm of the probability density function (PDF) for a lognormal distribution.
168+
169+
```c
170+
double out = stdlib_base_dists_lognormal_logpdf( 2.0, 0.0, 1.0 );
171+
// returns ~-1.852
172+
```
173+
174+
The function accepts the following arguments:
175+
176+
- **x**: `[in] double` input value.
177+
- **mu**: `[in] double` location parameter.
178+
- **sigma**: `[in] double` scale parameter.
179+
180+
```c
181+
double stdlib_base_dists_lognormal_logpdf( const double x, const double mu, const double sigma );
182+
```
183+
184+
</section>
185+
186+
<!-- /.usage -->
187+
188+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
189+
190+
<section class="notes">
191+
192+
</section>
193+
194+
<!-- /.notes -->
195+
196+
<!-- C API usage examples. -->
197+
198+
<section class="examples">
199+
200+
### Examples
201+
202+
```c
203+
#include "stdlib/stats/base/dists/lognormal/logpdf.h"
204+
#include <stdlib.h>
205+
#include <stdio.h>
206+
207+
static double random_uniform( const double min, const double max ) {
208+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
209+
return min + ( v*(max-min) );
210+
}
211+
212+
int main( void ) {
213+
double x;
214+
double mu;
215+
double sigma;
216+
double y;
217+
int i;
218+
219+
for ( i = 0; i < 25; i++ ) {
220+
x = random_uniform( 0.0, 20.0 );
221+
mu = random_uniform( -5.0, 5.0 );
222+
sigma = random_uniform( 0.0, 20.0 );
223+
y = stdlib_base_dists_lognormal_logpdf( x, mu, sigma );
224+
printf( "x: %lf, mu: %lf, sigma: %lf, ln(f(x;mu,sigma)): %lf\n", x, mu, sigma, y );
225+
}
226+
}
227+
```
228+
137229
</section>
138230

139231
<!-- /.examples -->
140232

233+
</section>
234+
235+
<!-- /.c -->
236+
237+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
238+
239+
<section class="related">
240+
241+
</section>
242+
141243
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142244

143245
<section class="related">
@@ -159,3 +261,114 @@ for ( i = 0; i < 10; i++ ) {
159261
</section>
160262

161263
<!-- /.links -->
264+
265+
266+
267+
268+
269+
270+
271+
272+
```c
273+
274+
</section>
275+
276+
<!-- /.examples -->
277+
278+
<!-- C interface documentation. -->
279+
280+
* * *
281+
282+
<section class="c">
283+
284+
## C APIs
285+
286+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
287+
288+
<section class="intro">
289+
290+
</section>
291+
292+
<!-- /.intro -->
293+
294+
<!-- C usage documentation. -->
295+
296+
<section class="usage">
297+
298+
### Usage
299+
300+
#include "stdlib/stats/base/dists/arcsine/logpdf.h"
301+
302+
#### stdlib_base_dists_arcsine_logpdf( x, a, b )
303+
304+
Evaluates the logarithm of the probability density function (PDF) for an arcsine distribution.
305+
306+
double out = stdlib_base_dists_arcsine_logpdf( 2.0, 0.0, 4.0 );
307+
// returns ~-1.838
308+
309+
The function accepts the following arguments:
310+
311+
- **x**: `[in] double` input value.
312+
- **a**: `[in] double` minimum support.
313+
- **b**: `[in] double` maximum support.
314+
315+
double stdlib_base_dists_arcsine_logpdf( const double x, const double a, const double b );
316+
317+
</section>
318+
319+
<!-- /.usage -->
320+
321+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
322+
323+
<section class="notes">
324+
325+
</section>
326+
327+
<!-- /.notes -->
328+
329+
<!-- C API usage examples. -->
330+
331+
<section class="examples">
332+
333+
### Examples
334+
335+
#include "stdlib/stats/base/dists/arcsine/logpdf.h"
336+
#include <stdlib.h>
337+
#include <stdio.h>
338+
339+
static double random_uniform( const double min, const double max ) {
340+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
341+
return min + ( v*(max-min) );
342+
}
343+
344+
int main( void ) {
345+
double x;
346+
double a;
347+
double b;
348+
double y;
349+
int i;
350+
351+
for ( i = 0; i < 25; i++ ) {
352+
x = random_uniform( -10.0, 10.0 );
353+
a = random_uniform( -20.0, 0.0 );
354+
b = random_uniform( a, a+40.0 );
355+
y = stdlib_base_dists_arcsine_logpdf( x, a, b );
356+
printf( "x: %lf, a: %lf, b: %lf, ln(f(x;a,b)): %lf\n", x, a, b, y );
357+
}
358+
}
359+
360+
</section>
361+
362+
<!-- /.examples -->
363+
364+
</section>
365+
366+
<!-- /.c -->
367+
368+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
369+
370+
<section class="related">
371+
372+
</section>
373+
374+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 randu = require( '@stdlib/random/base/randu' );
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 logpdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( logpdf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg, opts, function benchmark( b ) {
43+
var sigma;
44+
var len;
45+
var mu;
46+
var x;
47+
var y;
48+
var i;
49+
50+
len = 100;
51+
x = new Float64Array( len );
52+
mu = new Float64Array( len );
53+
sigma = new Float64Array( len );
54+
for ( i = 0; i < len; i++ ) {
55+
x[ i ] = ( randu() * 20.0 );
56+
mu[ i ] = ( randu() * 10.0 ) - 5.0;
57+
sigma[ i ] = randu() * 20.0;
58+
}
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
y = logpdf( x[ i % len ], mu[ i % len ], sigma[ i % len ] );
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( y ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});

0 commit comments

Comments
 (0)