Skip to content

Commit 6a5962d

Browse files
committed
feat: add the JS native files
1 parent dd99004 commit 6a5962d

File tree

3 files changed

+308
-0
lines changed

3 files changed

+308
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 Float64Array = require( '@stdlib/array/float64' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var round = require( '@stdlib/math/base/special/round' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var kurtosis = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( kurtosis instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var len;
45+
var N;
46+
var K;
47+
var n;
48+
var y;
49+
var i;
50+
51+
len = 100;
52+
N = new Float64Array( len );
53+
K = new Float64Array( len );
54+
n = new Float64Array( len );
55+
for ( i = 0; i < len; i++ ) {
56+
N[ i ] = round( ( randu() * 90.0 ) + 10.0 );
57+
K[ i ] = round( randu() * N[ i ] );
58+
n[ i ] = round( randu() * N[ i ] );
59+
}
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
y = kurtosis( N[ i % len ], K[ i % len ], n[ i % len ] );
64+
if ( isnan( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( y ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 addon = require( './../src/addon.node' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the kurtosis of a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
30+
*
31+
* @private
32+
* @param {number} N - population size
33+
* @param {number} K - subpopulation size
34+
* @param {number} n - number of draws
35+
* @returns {number} kurtosis
36+
*
37+
* @example
38+
* var k = kurtosis( 16, 11, 4 );
39+
* // returns ~-0.326
40+
*
41+
* @example
42+
* var k = kurtosis( 4, 2, 2 );
43+
* // returns 0.0
44+
*
45+
* @example
46+
* var k = kurtosis( 10, 5, 12 );
47+
* // returns NaN
48+
*
49+
* @example
50+
* var k = kurtosis( 10.3, 10, 4 );
51+
* // returns NaN
52+
*
53+
* @example
54+
* var k = kurtosis( 10, 5.5, 4 );
55+
* // returns NaN
56+
*
57+
* @example
58+
* var k = kurtosis( 10, 5, 4.5 );
59+
* // returns NaN
60+
*
61+
* @example
62+
* var k = kurtosis( NaN, 10, 4 );
63+
* // returns NaN
64+
*
65+
* @example
66+
* var k = kurtosis( 20, NaN, 4 );
67+
* // returns NaN
68+
*
69+
* @example
70+
* var k = kurtosis( 20, 10, NaN );
71+
* // returns NaN
72+
*/
73+
function kurtosis( N, K, n ) {
74+
return addon( N, K, n );
75+
}
76+
77+
78+
// EXPORTS //
79+
80+
module.exports = kurtosis;
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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 tape = require( 'tape' );
25+
var tryRequire = require( '@stdlib/utils/try-require' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var abs = require( '@stdlib/math/base/special/abs' );
28+
var PINF = require( '@stdlib/constants/float64/pinf' );
29+
var EPS = require( '@stdlib/constants/float64/eps' );
30+
31+
32+
// VARIABLES //
33+
34+
var kurtosis = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( kurtosis instanceof Error )
37+
};
38+
39+
40+
// FIXTURES //
41+
42+
var data = require( './fixtures/julia/data.json' );
43+
44+
45+
// TESTS //
46+
47+
tape( 'main export is a function', opts, function test( t ) {
48+
t.ok( true, __filename );
49+
t.strictEqual( typeof kurtosis, 'function', 'main export is a function' );
50+
t.end();
51+
});
52+
53+
tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) {
54+
var v = kurtosis( NaN, 10, 4 );
55+
t.equal( isnan( v ), true, 'returns NaN' );
56+
57+
v = kurtosis( 20, NaN, 4 );
58+
t.equal( isnan( v ), true, 'returns NaN' );
59+
60+
v = kurtosis( 20, 10, NaN );
61+
t.equal( isnan( v ), true, 'returns NaN' );
62+
63+
t.end();
64+
});
65+
66+
tape( 'if provided an `N` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) {
67+
var v;
68+
69+
v = kurtosis( 10.5, 4, 2 );
70+
t.equal( isnan( v ), true, 'returns NaN' );
71+
72+
v = kurtosis( -2, 4, 2 );
73+
t.equal( isnan( v ), true, 'returns NaN' );
74+
75+
v = kurtosis( -1, 4, 2 );
76+
t.equal( isnan( v ), true, 'returns NaN' );
77+
78+
v = kurtosis( 20.5, 10, 5 );
79+
t.equal( isnan( v ), true, 'returns NaN' );
80+
81+
v = kurtosis( PINF, 10, 5 );
82+
t.equal( isnan( v ), true, 'returns NaN' );
83+
84+
t.end();
85+
});
86+
87+
tape( 'if provided an `K` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) {
88+
var y;
89+
90+
y = kurtosis( 20, 3.5, 10 );
91+
t.equal( isnan( y ), true, 'returns NaN' );
92+
93+
y = kurtosis( 20, -2, 10 );
94+
t.equal( isnan( y ), true, 'returns NaN' );
95+
96+
y = kurtosis( 20, -1, 10 );
97+
t.equal( isnan( y ), true, 'returns NaN' );
98+
99+
y = kurtosis( 20, 2.5, 10 );
100+
t.equal( isnan( y ), true, 'returns NaN' );
101+
102+
y = kurtosis( 20, PINF, 10 );
103+
t.equal( isnan( y ), true, 'returns NaN' );
104+
105+
t.end();
106+
});
107+
108+
tape( 'if provided an `n` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) {
109+
var y;
110+
111+
y = kurtosis( 40, 20, 3.5 );
112+
t.equal( isnan( y ), true, 'returns NaN' );
113+
114+
y = kurtosis( 40, 20, -2 );
115+
t.equal( isnan( y ), true, 'returns NaN' );
116+
117+
y = kurtosis( 40, 20, -1 );
118+
t.equal( isnan( y ), true, 'returns NaN' );
119+
120+
y = kurtosis( 40, 20, 2.5 );
121+
t.equal( isnan( y ), true, 'returns NaN' );
122+
123+
y = kurtosis( 40, 20, PINF );
124+
t.equal( isnan( y ), true, 'returns NaN' );
125+
126+
t.end();
127+
});
128+
129+
tape( 'the function returns the excess kurtosis of a hypergeometric distribution', opts, function test( t ) {
130+
var expected;
131+
var delta;
132+
var tol;
133+
var N;
134+
var K;
135+
var n;
136+
var y;
137+
var i;
138+
139+
expected = data.expected;
140+
N = data.N;
141+
K = data.K;
142+
n = data.n;
143+
for ( i = 0; i < n.length; i++ ) {
144+
y = kurtosis( N[i], K[i], n[i] );
145+
if ( y === expected[i] ) {
146+
t.equal( y, expected[i], 'N: '+N[i]+', K: '+K[i]+', n: '+n[i]+', y: '+y+', expected: '+expected[i] );
147+
} else {
148+
delta = abs( y - expected[ i ] );
149+
tol = 1.0 * EPS * abs( expected[ i ] );
150+
t.ok( delta <= tol, 'within tolerance. N: '+N[i]+'. K: '+K[i]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
151+
}
152+
}
153+
t.end();
154+
});

0 commit comments

Comments
 (0)