Skip to content

feat(stats): add C implementation for stats/base/dists/negative-binomial/mgf #4771

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 18 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 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
Prev Previous commit
Next Next commit
feat: add the JS native and its benchmark and test files
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
  • Loading branch information
anandkaranubc committed Jan 5, 2025
commit 340c83607225d5ac23409e80735bbaa0e03a97c9
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @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 ceil = require( '@stdlib/math/base/special/ceil' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// MAIN //

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

len = 100;
t = new Float64Array( len );
r = new Float64Array( len );
p = new Float64Array( len );

for ( i = 0; i < len; i++ ) {
t[ i ] = randu() * -2.0; // t values must be negative
r[ i ] = ceil( randu() * 100.0 ); // r values must be positive integers
p[ i ] = ( randu() * (1.0 - EPS) ) + EPS; // p values in (0,1)
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mgf( t[ i % len ], r[ i % len ], p[ 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @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 addon = require( './../src/addon.node' );


// MAIN //

/**
* Evaluates the moment-generating function (MGF) for a negative binomial distribution.
*
* @private
* @param {number} t - input value
* @param {PositiveNumber} r - number of successes until experiment is stopped
* @param {Probability} p - success probability
* @returns {number} evaluated MGF
*
* @example
* var y = mgf( 0.05, 20.0, 0.8 );
* // returns ~267.839
*
* @example
* var y = mgf( 0.1, 20.0, 0.1 );
* // returns ~9.347
*
* @example
* var y = mgf( 0.5, 10.0, 0.4 );
* // returns ~42822.023
*
* @example
* var y = mgf( 0.1, 0.0, 0.5 );
* // returns NaN
*
* @example
* var y = mgf( NaN, 20.0, 0.5 );
* // returns NaN
*/
function mgf( t, r, p ) {
return addon( t, r, p );
}


// EXPORTS //

module.exports = mgf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @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 tape = require( 'tape' );
var resolve = require( 'path' ).resolve;
var tryRequire = require( '@stdlib/utils/try-require' );


// VARIABLES //

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


// TESTS //

tape( 'main export is a function', opts, function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof mgf, 'function', 'main export is a function' );
t.end();
});

tape( 'the function returns NaN if provided a NaN input for `t`', opts, function test( t ) {
var y = mgf( NaN, 2.0, 0.5 );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );
t.end();
});

tape( 'the function returns NaN if provided a NaN input for `r`', opts, function test( t ) {
var y = mgf( 0.1, NaN, 0.5 );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );
t.end();
});

tape( 'the function returns NaN if provided a NaN input for `p`', opts, function test( t ) {
var y = mgf( 0.1, 2.0, NaN );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );
t.end();
});

tape( 'the function evaluates the MGF for valid inputs', opts, function test( t ) {
var y = mgf( 0.0, 1.0, 0.9 );
t.strictEqual( y, 1.0, 'returns 1 when t is 0' );

y = mgf( 0.05, 20.0, 0.8 );
t.ok( Math.abs( y - 267.839 ) < 1e-3, 'returns ~267.839' );

y = mgf( 0.1, 20.0, 0.1 );
t.ok( Math.abs( y - 9.347 ) < 1e-3, 'returns ~9.347' );

y = mgf( 0.5, 10.0, 0.4 );
t.ok( Math.abs( y - 42822.023 ) < 1e-3, 'returns ~42822.023' );

t.end();
});

tape( 'the function returns NaN if `p` is outside the interval [0,1]', opts, function test( t ) {
var y = mgf( 0.1, 2.0, -0.5 );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );

y = mgf( 0.1, 2.0, 1.5 );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );

t.end();
});

tape( 'the function returns NaN if `r` is less than or equal to 0', opts, function test( t ) {
var y = mgf( 0.1, -2.0, 0.5 );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );

y = mgf( 0.1, 0.0, 0.5 );
t.strictEqual( typeof y, 'number', 'returns a number' );
t.ok( isNaN( y ), 'returns NaN' );

t.end();
});