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 stats/base/dists/t/cdf #4253

Closed
wants to merge 2 commits into from
Closed
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
95 changes: 95 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/t/cdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,101 @@ 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/t/cdf.h"
```

#### stdlib_base_dists_t_cdf( x, v )

Evaluates the cumulative distribution function (CDF) for a Student's t distribution.

```c
double out = stdlib_base_dists_t_cdf( 2.0, 0.1 );
// returns ~0.611
```

The function accepts the following arguments:

- **x**: `[in] double` input value.
- **v**: `[in] double` degrees of freedom.

```c
double stdlib_base_dists_t_cdf( const double x, const double v );
```

</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/t/cdf.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 v;
double y;
int i;

for ( i = 0; i < 25; i++ ) {
x = random_uniform( -10.0, 10.0 );
v = random_uniform( 1.0, 30.0 );
y = stdlib_base_dists_t_cdf( x, v );
printf( "x: %lf, v: %lf, F(x;v): %lf\n", x, v, y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- 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
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
* 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.
Expand All @@ -21,8 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var ceil = require( '@stdlib/math/base/special/ceil' );
var randu = require( '@stdlib/random/base/randu' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -36,11 +36,12 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

x = uniform( 100, -50.0, 50.0 );
v = discreteUniform( 100, 1.0, 100.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 100;
v = ceil( randu()*100.0 );
y = cdf( x, v );
y = cdf( x[ i % x.length ], v[ i % v.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -55,18 +56,16 @@ bench( pkg, function benchmark( b ) {

bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var v;
var x;
var y;
var i;

v = 3.0;
mycdf = cdf.factory( v );
x = uniform( 100, -50.0, 50.0 );
mycdf = cdf.factory( 3.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 50.0;
y = mycdf( x );
y = mycdf( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var cdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); // Native implementation for t/cdf
var opts = {
'skip': ( cdf instanceof Error )
};


// MAIN //

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

len = 100;
x = new Float64Array( len );
v = new Float64Array( len );

for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
v[ i ] = ( randu() * 10.0 ) + 1.0;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cdf( x[ i % len ], v[ 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();
});

bench( pkg+'::native:factory', opts, function benchmark( b ) {
var mycdf;
var v;
var x;
var y;
var i;

v = 5.0;
mycdf = cdf.factory( v );
x = new Float64Array( 100 );

for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mycdf( x[ i % x.length ] );
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