Skip to content

feat: add C implementation for stats/base/dists/discrete-uniform/quantile #4666

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,31 +126,136 @@ y = myquantile( 0.3 );
<!-- eslint no-undef: "error" -->

```javascript
var randint = require( '@stdlib/random/base/discrete-uniform' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var quantile = require( '@stdlib/stats/base/dists/discrete-uniform/quantile' );

var randa = randint.factory( 0, 5 );
var randb = randint.factory();
var p;
var a;
var b;
var p;
var v;
var i;

p = uniform( 10, 0.0, 1.0 );
a = discreteUniform( 10, 0, 5 );
b = discreteUniform( 10, 2, 8 );

for ( i = 0; i < 10; i++ ) {
p = randu();
a = randa();
b = randb( a, a+randa() );
v = quantile( p, a, b );
console.log( 'p: %d, a: %d, b: %d, Q(p;a,b): %d', p.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), v.toFixed( 4 ) );
v = quantile( p[ i ], a[ i ], b[ i ] );
console.log( 'p: %d, a: %d, b: %d, Q(p;a,b): %d', p[ i ].toFixed( 4 ), a[ i ], b[ i ], v );
}
```

</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

```c
#include "stdlib/stats/base/dists/discrete-uniform/quantile.h"
```

#### stdlib_base_dists_discrete_uniform_quantile( x, a, b )

Evaluates the [quantile function][quantile-function] for a [discrete uniform][discrete-uniform-distribution] distribution with parameters `a` (minimum support) and `b` (maximum support).

```c
double out = stdlib_base_dists_discrete_uniform_quantile( 0.8, 0, 1 );
// returns 1.0
```

The function accepts the following arguments:

- **p**: `[in] double` input probability.
- **a**: `[in] int32_t` minimum support.
- **b**: `[in] int32_t` maximum support.

```c
double stdlib_base_dists_discrete_uniform_quantile( const double p, const int32_t a, const int32_t 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/discrete-uniform/quantile.h"
#include "stdlib/math/base/special/round.h"
#include <stdint.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 ) {
int32_t a;
int32_t b;
double p;
double y;
int i;

for ( i = 0; i < 10; i++ ) {
p = random_uniform( 0.0, 1.0 );
a = stdlib_base_round( random_uniform( 0.0, 5.0 ) );
b = stdlib_base_round( random_uniform( a, a + 5.0 ) );
y = stdlib_base_dists_discrete_uniform_quantile( p, a, b );
printf( "p: %lf, a: %d, b: %d, Q(p;a,b): %lf\n", p, a, b, y );
}
}
```

</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">

</section>

<!-- /.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,9 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var quantile = require( './../lib' );
Expand All @@ -40,14 +39,9 @@ bench( pkg, function benchmark( b ) {
var i;

len = 100;
p = new Float64Array( len );
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
p[ i ] = uniform( 0.0, 1.0 );
min[ i ] = discreteUniform( -20, 0 );
max[ i ] = discreteUniform( min[ i ], min[ i ] + 40 );
}
p = uniform( len, 0.0, 1.0 );
min = discreteUniform( len, -10, 0 );
max = discreteUniform( len, 0, 10 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
Expand Down Expand Up @@ -77,10 +71,7 @@ bench( pkg+':factory', function benchmark( b ) {
max = 2;
myquantile = quantile.factory( min, max );
len = 100;
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
p[ i ] = uniform( 0.0, 1.0 );
}
p = uniform( len, 0.0, 1.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @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 uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var min;
var max;
var len;
var p;
var y;
var i;

len = 100;
p = uniform( len, 0.0, 1.0 );
min = discreteUniform( len, -10, 0 );
max = discreteUniform( len, 0, 10 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = quantile( p[ i % len ], min[ i % len ], max[ 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