Skip to content

feat(stats): add C implementation for stats/base/dists/degenerate/pdf #4585

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 12 commits into from
Jan 18, 2025
Prev Previous commit
Next Next commit
feat: add the JS native files
  • Loading branch information
anandkaranubc committed Jan 5, 2025
commit 9ef70b2e36aa4ed173f456adcd60b04074ef5eff
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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 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 pdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( pdf instanceof Error )
};


// MAIN //

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

len = 100;
x = new Float64Array( len );
mu = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
mu[ i ] = ( randu() * 40.0 ) - 20.0;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = pdf( x[ i % len ], mu[ 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,59 @@
/**
* @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 addon = require( './../src/addon.node' );


// MAIN //

/**
* Evaluates the probability density function (PDF) for a degenerate distribution centered at `mu`.
*
* @private
* @param {number} x - input value
* @param {number} mu - constant value of the distribution
* @returns {number} evaluated probability density function
*
* @example
* var y = pdf( 3.0, 3.0 );
* // returns Infinity
*
* @example
* var y = pdf( 2.0, 3.0 );
* // returns 0.0
*
* @example
* var y = pdf( NaN, 2.0 );
* // returns NaN
*
* @example
* var y = pdf( 3.0, NaN );
* // returns NaN
*/
function pdf( x, mu ) {
return addon( x, mu );
}


// EXPORTS //

module.exports = pdf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 tape = require( 'tape' );
var tryRequire = require( '@stdlib/utils/try-require' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var PINF = require( '@stdlib/constants/float64/pinf' );


// VARIABLES //

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


// TESTS //

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

tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) {
var y;

y = pdf( NaN, 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );

y = pdf( 0.0, NaN );
t.equal( isnan( y ), true, 'returns NaN' );

y = pdf( NaN, NaN );
t.equal( isnan( y ), true, 'returns NaN' );

t.end();
});

tape( 'the function returns `+Infinity` if provided `x` equal to `mu`', opts, function test( t ) {
var y;

y = pdf( 2.0, 2.0 );
t.equal( y, PINF, 'returns +Infinity' );

y = pdf( 0.0, 0.0 );
t.equal( y, PINF, 'returns +Infinity' );

y = pdf( -3.0, -3.0 );
t.equal( y, PINF, 'returns +Infinity' );

t.end();
});

tape( 'the function returns `0.0` if provided `x` not equal to `mu`', opts, function test( t ) {
var y;

y = pdf( 2.0, 3.0 );
t.equal( y, 0.0, 'returns 0.0' );

y = pdf( 4.0, 3.0 );
t.equal( y, 0.0, 'returns 0.0' );

t.end();
});