Skip to content

feat: add C implementation for stats/base/dists/kumaraswamy/median #4020

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

Merged
merged 9 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 38 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/kumaraswamy/median.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/

#ifndef STDLIB_STATS_BASE_DISTS_KUMARASWAMY_MEDIAN_H
#define STDLIB_STATS_BASE_DISTS_KUMARASWAMY_MEDIAN_H

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
#ifdef __cplusplus
extern "C" {
#endif

/**
* Returns the median of a Kumaraswamy distribution.
*/
double stdlib_base_dists_kumaraswamy_median( const double a, const double b );

#ifdef __cplusplus
}
#endif

#endif // !STDLIB_STATS_BASE_DISTS_KUMARASWAMY_MEDIAN_H
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.
Copyright (c) 2014 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.
Expand Down Expand Up @@ -143,6 +143,114 @@ 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/kumaraswamy/median.h"
```

#### stdlib_base_dists_kumaraswamy_median( a, b )

Evaluates the [median][median] of a [Kumaraswamy][kumaraswamy-distribution] distribution with shape parameters a and b.

```c
double out = stdlib_base_dists_kumaraswamy_median( 2.0, 3.0 );
// returns ~1.817
```

The function accepts the following arguments:

- **a**: `[in] double` shape parameter.
- **b**: `[in] double` shape parameter.


```c
double stdlib_base_dists_kumaraswamy_median( 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

```c
#include "stdlib/stats/base/dists/kumaraswamy/median.h"
#include "stdlib/constants/float64/eps.h"
#include <stdlib.h>
#include <stdio.h>

double stdlib_base_dists_kumaraswamy_median( const double a, const double b ) {
return pow( 1.0 - pow( 0.5, 1.0 / b ), 1.0 / a );
}

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 a;
double b;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
a = random_uniform( 0.1, 10.0 );
b = random_uniform( 0.1, 10.0 );
y = stdlib_base_dists_kumaraswamy_median( a, b );
printf( "a: %lf, b: %lf, Median(a, b): %lf\n", a, b, y );
}

return 0;
}

```

</section>

<!-- /.examples -->

</section>


<!-- /.c -->


<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">
Expand All @@ -151,6 +259,7 @@ for ( i = 0; i < 10; i++ ) {

<!-- /.references -->


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

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

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 EPS = require( '@stdlib/constants/float64/eps' );
Expand All @@ -31,16 +32,23 @@ var median = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var shape1;
var shape2;
var alpha;
var beta;
var len;
var y;
var i;

len = 100;
alpha = new Float64Array( len );
beta = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
alpha[ i ] = ( randu() * 10.0 ) + EPS;
beta[ i ] = ( randu() * 10.0 ) + EPS;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
shape1 = ( randu()*10.0 ) + EPS;
shape2 = ( randu()*10.0 ) + EPS;
y = median( shape1, shape2 );
y = median( alpha[ i % len ], beta[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 EPS = require( '@stdlib/constants/float64/eps' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var alpha;
var beta;
var len;
var y;
var i;

len = 100;
alpha = new Float64Array( len );
beta = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
alpha[ i ] = ( randu() * 10.0 ) + EPS;
beta[ i ] = ( randu() * 10.0 ) + EPS;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = pdf( alpha[ i % len ], beta[ 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