Skip to content

Commit 22812ea

Browse files
feat: add C implementation for stats/base/dists/discrete-uniform/quantile
PR-URL: #4666 Closes: #3558 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: JoyBoy <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent de5c162 commit 22812ea

File tree

18 files changed

+1390
-35
lines changed

18 files changed

+1390
-35
lines changed

lib/node_modules/@stdlib/stats/base/dists/discrete-uniform/quantile/README.md

Lines changed: 115 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,136 @@ y = myquantile( 0.3 );
126126
<!-- eslint no-undef: "error" -->
127127

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

133-
var randa = randint.factory( 0, 5 );
134-
var randb = randint.factory();
133+
var p;
135134
var a;
136135
var b;
137-
var p;
138136
var v;
139137
var i;
140138

139+
p = uniform( 10, 0.0, 1.0 );
140+
a = discreteUniform( 10, 0, 5 );
141+
b = discreteUniform( 10, 2, 8 );
142+
141143
for ( i = 0; i < 10; i++ ) {
142-
p = randu();
143-
a = randa();
144-
b = randb( a, a+randa() );
145-
v = quantile( p, a, b );
146-
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 ) );
144+
v = quantile( p[ i ], a[ i ], b[ i ] );
145+
console.log( 'p: %d, a: %d, b: %d, Q(p;a,b): %d', p[ i ].toFixed( 4 ), a[ i ], b[ i ], v );
147146
}
148147
```
149148

150149
</section>
151150

152151
<!-- /.examples -->
153152

153+
<!-- C interface documentation. -->
154+
155+
<section class="c">
156+
157+
## C APIs
158+
159+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
160+
161+
<section class="intro">
162+
163+
</section>
164+
165+
<!-- /.intro -->
166+
167+
<!-- C usage documentation. -->
168+
169+
<section class="usage">
170+
171+
### Usage
172+
173+
```c
174+
#include "stdlib/stats/base/dists/discrete-uniform/quantile.h"
175+
```
176+
177+
#### stdlib_base_dists_discrete_uniform_quantile( x, a, b )
178+
179+
Evaluates the [quantile function][quantile-function] for a [discrete uniform][discrete-uniform-distribution] distribution with parameters `a` (minimum support) and `b` (maximum support).
180+
181+
```c
182+
double out = stdlib_base_dists_discrete_uniform_quantile( 0.8, 0, 1 );
183+
// returns 1.0
184+
```
185+
186+
The function accepts the following arguments:
187+
188+
- **p**: `[in] double` input probability.
189+
- **a**: `[in] int32_t` minimum support.
190+
- **b**: `[in] int32_t` maximum support.
191+
192+
```c
193+
double stdlib_base_dists_discrete_uniform_quantile( const double p, const int32_t a, const int32_t b );
194+
```
195+
196+
</section>
197+
198+
<!-- /.usage -->
199+
200+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
201+
202+
<section class="notes">
203+
204+
</section>
205+
206+
<!-- /.notes -->
207+
208+
<!-- C API usage examples. -->
209+
210+
<section class="examples">
211+
212+
### Examples
213+
214+
```c
215+
#include "stdlib/stats/base/dists/discrete-uniform/quantile.h"
216+
#include "stdlib/math/base/special/round.h"
217+
#include <stdint.h>
218+
#include <stdlib.h>
219+
#include <stdio.h>
220+
221+
static double random_uniform( const double min, const double max ) {
222+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
223+
return min + ( v*(max-min) );
224+
}
225+
226+
int main( void ) {
227+
int32_t a;
228+
int32_t b;
229+
double p;
230+
double y;
231+
int i;
232+
233+
for ( i = 0; i < 10; i++ ) {
234+
p = random_uniform( 0.0, 1.0 );
235+
a = stdlib_base_round( random_uniform( 0.0, 5.0 ) );
236+
b = stdlib_base_round( random_uniform( a, a + 5.0 ) );
237+
y = stdlib_base_dists_discrete_uniform_quantile( p, a, b );
238+
printf( "p: %lf, a: %d, b: %d, Q(p;a,b): %lf\n", p, a, b, y );
239+
}
240+
}
241+
```
242+
243+
</section>
244+
245+
<!-- /.examples -->
246+
247+
</section>
248+
249+
<!-- /.c -->
250+
251+
<!-- 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. -->
252+
253+
<section class="references">
254+
255+
</section>
256+
257+
<!-- /.references -->
258+
154259
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
155260

156261
<section class="related">

lib/node_modules/@stdlib/stats/base/dists/discrete-uniform/quantile/benchmark/benchmark.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var Float64Array = require( '@stdlib/array/float64' );
25-
var uniform = require( '@stdlib/random/base/uniform' );
26-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pkg = require( './../package.json' ).name;
2928
var quantile = require( './../lib' );
@@ -40,14 +39,9 @@ bench( pkg, function benchmark( b ) {
4039
var i;
4140

4241
len = 100;
43-
p = new Float64Array( len );
44-
min = new Float64Array( len );
45-
max = new Float64Array( len );
46-
for ( i = 0; i < len; i++ ) {
47-
p[ i ] = uniform( 0.0, 1.0 );
48-
min[ i ] = discreteUniform( -20, 0 );
49-
max[ i ] = discreteUniform( min[ i ], min[ i ] + 40 );
50-
}
42+
p = uniform( len, 0.0, 1.0 );
43+
min = discreteUniform( len, -10, 0 );
44+
max = discreteUniform( len, 0, 10 );
5145

5246
b.tic();
5347
for ( i = 0; i < b.iterations; i++ ) {
@@ -77,10 +71,7 @@ bench( pkg+':factory', function benchmark( b ) {
7771
max = 2;
7872
myquantile = quantile.factory( min, max );
7973
len = 100;
80-
p = new Float64Array( len );
81-
for ( i = 0; i < len; i++ ) {
82-
p[ i ] = uniform( 0.0, 1.0 );
83-
}
74+
p = uniform( len, 0.0, 1.0 );
8475

8576
b.tic();
8677
for ( i = 0; i < b.iterations; i++ ) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 uniform = require( '@stdlib/random/array/uniform' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-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 quantile = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( quantile instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var min;
44+
var max;
45+
var len;
46+
var p;
47+
var y;
48+
var i;
49+
50+
len = 100;
51+
p = uniform( len, 0.0, 1.0 );
52+
min = discreteUniform( len, -10, 0 );
53+
max = discreteUniform( len, 0, 10 );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
y = quantile( p[ i % len ], min[ i % len ], max[ i % len ] );
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});

0 commit comments

Comments
 (0)