Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add C implementation for lognormal distribution logpdf #4444

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/lognormal/logpdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,112 @@ for ( i = 0; i < 10; i++ ) {
}
```

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/stats/base/dists/lognormal/logpdf.h"
```

#### stdlib_base_dists_lognormal_logpdf( x, mu, sigma )

Evaluates the logarithm of the probability density function (PDF) for a lognormal distribution.

```c
double out = stdlib_base_dists_lognormal_logpdf( 2.0, 0.0, 1.0 );
// returns ~-1.852
```

The function accepts the following arguments:

- **x**: `[in] double` input value.
- **mu**: `[in] double` location parameter.
- **sigma**: `[in] double` scale parameter.

```c
double stdlib_base_dists_lognormal_logpdf( const double x, const double mu, const double sigma );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/stats/base/dists/lognormal/logpdf.h"
#include <stdlib.h>
#include <stdio.h>

static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
}

int main( void ) {
double x;
double mu;
double sigma;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
x = random_uniform( 0.0, 20.0 );
mu = random_uniform( -5.0, 5.0 );
sigma = random_uniform( 0.0, 20.0 );
y = stdlib_base_dists_lognormal_logpdf( x, mu, sigma );
printf( "x: %lf, mu: %lf, sigma: %lf, ln(f(x;mu,sigma)): %lf\n", x, mu, sigma, y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

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

<!-- /.links -->








```c

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

#include "stdlib/stats/base/dists/arcsine/logpdf.h"

#### stdlib_base_dists_arcsine_logpdf( x, a, b )

Evaluates the logarithm of the probability density function (PDF) for an arcsine distribution.

double out = stdlib_base_dists_arcsine_logpdf( 2.0, 0.0, 4.0 );
// returns ~-1.838

The function accepts the following arguments:

- **x**: `[in] double` input value.
- **a**: `[in] double` minimum support.
- **b**: `[in] double` maximum support.

double stdlib_base_dists_arcsine_logpdf( const double x, const double a, const double b );

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

#include "stdlib/stats/base/dists/arcsine/logpdf.h"
#include <stdlib.h>
#include <stdio.h>

static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
}

int main( void ) {
double x;
double a;
double b;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
x = random_uniform( -10.0, 10.0 );
a = random_uniform( -20.0, 0.0 );
b = random_uniform( a, a+40.0 );
y = stdlib_base_dists_arcsine_logpdf( x, a, b );
printf( "x: %lf, a: %lf, b: %lf, ln(f(x;a,b)): %lf\n", x, a, b, y );
}
}

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var logpdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( logpdf instanceof Error )
};


// MAIN //

bench( pkg, opts, function benchmark( b ) {
var sigma;
var len;
var mu;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
mu = new Float64Array( len );
sigma = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 );
mu[ i ] = ( randu() * 10.0 ) - 5.0;
sigma[ i ] = randu() * 20.0;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = logpdf( x[ i % len ], mu[ i % len ], sigma[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading