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

math: Updated atanh implementation based on FreeBSD for improved accuracy and efficiency #2746

Closed
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
86 changes: 86 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/atanh/lib/atanh.js
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 isnan = require( '@stdlib/math-base-assert-is-nan' );

Check failure on line 23 in lib/node_modules/@stdlib/math/base/special/atanh/lib/atanh.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

cannot resolve module: "@stdlib/math-base-assert-is-nan"
var log1p = require( '@stdlib/math-base-special-log1p' );

Check failure on line 24 in lib/node_modules/@stdlib/math/base/special/atanh/lib/atanh.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

cannot resolve module: "@stdlib/math-base-special-log1p"
var PINF = require( '@stdlib/constants-float64-pinf' );

Check failure on line 25 in lib/node_modules/@stdlib/math/base/special/atanh/lib/atanh.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

cannot resolve module: "@stdlib/constants-float64-pinf"
var NINF = require( '@stdlib/constants-float64-ninf' );

Check failure on line 26 in lib/node_modules/@stdlib/math/base/special/atanh/lib/atanh.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

cannot resolve module: "@stdlib/constants-float64-ninf"


// MAIN //

/**
* Computes the hyperbolic arctangent of a number.
*
* @param {number} x - input value
* @returns {number} hyperbolic arctangent
*
* @example
* var v = atanh( 0.5 );
* // returns ~0.549
*
* @example
* var v = atanh( 0.0 );
* // returns 0.0
*
* @example
* var v = atanh( -0.0 );
* // returns -0.0
*
* @example
* var v = atanh( 1.0 );
* // returns Infinity
*
* @example
* var v = atanh( -1.0 );
* // returns -Infinity
*
* @example
* var v = atanh( 2.0 );
* // returns NaN
*
* @example
* var v = atanh( NaN );
* // returns NaN
*/
function atanh( x ) {
var ax;
if ( isnan( x ) || x < -1.0 || x > 1.0 ) {
return NaN;
}
if ( x === 1.0 ) {
return PINF;
}
if ( x === -1.0 ) {
return NINF;
}
ax = ( x < 0.0 ) ? -x : x;
if ( ax < 0.5 ) {
return x + ( x * x * x / 3.0 );
}
return 0.5 * log1p( ( 2.0 * x ) / ( 1.0 - x ) );
}


// EXPORTS //

module.exports = atanh;

Check failure on line 86 in lib/node_modules/@stdlib/math/base/special/atanh/lib/atanh.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Newline required at end of file but not found
73 changes: 73 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/hypergeometric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,80 @@ var y = dist.cdf( 0.5 );
var objectKeys = require( '@stdlib/utils/keys' );
var hypergeometric = require( '@stdlib/stats/base/dists/hypergeometric' );


console.log( objectKeys( hypergeometric ) );

// Example 1: Basic distribution properties
var N1 = 50; // population size
var K1 = 20; // number of successes in population
var n1 = 10; // number of draws

console.log( '\nExample 1: Basic distribution properties' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N1, K1, n1 );
console.log( 'Mean: %d', hypergeometric.mean( N1, K1, n1 ) );
console.log( 'Variance: %d', hypergeometric.variance( N1, K1, n1 ) );
console.log( 'Skewness: %d', hypergeometric.skewness( N1, K1, n1 ) );
console.log( 'Excess Kurtosis: %d', hypergeometric.kurtosis( N1, K1, n1 ) );
console.log( 'Standard Deviation: %d', hypergeometric.stdev( N1, K1, n1 ) );

// Example 2: PMF and CDF calculations
var N2 = 100;
var K2 = 40;
var n2 = 25;

console.log( '\nExample 2: PMF and CDF calculations' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N2, K2, n2 );
for ( var x = 0; x <= 10; x += 2 ) {
console.log( 'P(X = %d): %d', x, hypergeometric.pmf( x, N2, K2, n2 ) );
console.log( 'P(X <= %d): %d', x, hypergeometric.cdf( x, N2, K2, n2 ) );
}

// Example 3: Quantile function
var N3 = 80;
var K3 = 30;
var n3 = 20;

console.log( '\nExample 3: Quantile function' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N3, K3, n3 );
var probabilities = [ 0.1, 0.25, 0.5, 0.75, 0.9 ];
for ( var i = 0; i < probabilities.length; i++ ) {
var p = probabilities[i];
console.log( 'Q(%d): %d', p, hypergeometric.quantile( p, N3, K3, n3 ) );
}

// Example 4: Random number generation
var N4 = 60;
var K4 = 25;
var n4 = 15;

console.log( '\nExample 4: Random number generation' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N4, K4, n4 );
var Hypergeometric = hypergeometric.Hypergeometric;
var dist = new Hypergeometric( N4, K4, n4 );
for ( var j = 0; j < 10; j++ ) {
console.log( 'Random variate %d: %d', j+1, dist.random() );
}

// Example 5: Log PMF calculations
var N5 = 200;
var K5 = 80;
var n5 = 50;

console.log( '\nExample 5: Log PMF calculations' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N5, K5, n5 );
for ( var y = 10; y <= 30; y += 5 ) {
console.log( 'log(P(X = %d)): %d', y, hypergeometric.logpmf( y, N5, K5, n5 ) );
}

// Example 6: Mode calculation
var N6 = 75;
var K6 = 35;
var n6 = 20;

console.log( '\nExample 6: Mode calculation' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N6, K6, n6 );
console.log( 'Mode: %d', hypergeometric.mode( N6, K6, n6 ) );

```

</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,77 @@
var objectKeys = require( '@stdlib/utils/keys' );
var hypergeometric = require( './../lib' );

// Print namespace contents
console.log( 'Namespace contents:' );
console.log( objectKeys( hypergeometric ) );

// Example 1: Basic distribution properties
var N1 = 50; // population size
var K1 = 20; // number of successes in population
var n1 = 10; // number of draws

console.log( '\nExample 1: Basic distribution properties' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N1, K1, n1 );
console.log( 'Mean: %d', hypergeometric.mean( N1, K1, n1 ) );
console.log( 'Variance: %d', hypergeometric.variance( N1, K1, n1 ) );
console.log( 'Skewness: %d', hypergeometric.skewness( N1, K1, n1 ) );
console.log( 'Excess Kurtosis: %d', hypergeometric.kurtosis( N1, K1, n1 ) );
console.log( 'Standard Deviation: %d', hypergeometric.stdev( N1, K1, n1 ) );

// Example 2: PMF and CDF calculations
var N2 = 100;
var K2 = 40;
var n2 = 25;

console.log( '\nExample 2: PMF and CDF calculations' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N2, K2, n2 );
for ( var x = 0; x <= 10; x += 2 ) {
console.log( 'P(X = %d): %d', x, hypergeometric.pmf( x, N2, K2, n2 ) );
console.log( 'P(X <= %d): %d', x, hypergeometric.cdf( x, N2, K2, n2 ) );
}

// Example 3: Quantile function
var N3 = 80;
var K3 = 30;
var n3 = 20;

console.log( '\nExample 3: Quantile function' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N3, K3, n3 );
var probabilities = [ 0.1, 0.25, 0.5, 0.75, 0.9 ];
for ( var i = 0; i < probabilities.length; i++ ) {
var p = probabilities[i];
console.log( 'Q(%d): %d', p, hypergeometric.quantile( p, N3, K3, n3 ) );
}

// Example 4: Random number generation
var N4 = 60;
var K4 = 25;
var n4 = 15;

console.log( '\nExample 4: Random number generation' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N4, K4, n4 );
var Hypergeometric = hypergeometric.Hypergeometric;
var dist = new Hypergeometric( N4, K4, n4 );
for ( var j = 0; j < 10; j++ ) {
console.log( 'Random variate %d: %d', j+1, dist.random() );
}

// Example 5: Log PMF calculations
var N5 = 200;
var K5 = 80;
var n5 = 50;

console.log( '\nExample 5: Log PMF calculations' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N5, K5, n5 );
for ( var y = 10; y <= 30; y += 5 ) {
console.log( 'log(P(X = %d)): %d', y, hypergeometric.logpmf( y, N5, K5, n5 ) );
}

// Example 6: Mode calculation
var N6 = 75;
var K6 = 35;
var n6 = 20;

console.log( '\nExample 6: Mode calculation' );
console.log( 'Parameters: N=%d, K=%d, n=%d', N6, K6, n6 );
console.log( 'Mode: %d', hypergeometric.mode( N6, K6, n6 ) );
Loading