Skip to content

Commit 91ad58e

Browse files
committed
fix: align Wald mode evaluation with Distributions.jl reference
Match the arithmetic ordering of `mode(InverseGaussian)` in Distributions.jl — `mu * (sqrt(1 + (3r/2)^2) - 3r/2)` with `r = mu/lambda` — so JS/C agree with the Julia fixture to 0 ULPs. Tighten the test tolerance from 500 to 2.
1 parent 505dc0d commit 91ad58e

4 files changed

Lines changed: 8 additions & 4 deletions

File tree

lib/node_modules/@stdlib/stats/base/dists/wald/mode/lib/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' );
5454
* // returns NaN
5555
*/
5656
function mode( mu, lambda ) {
57+
var r;
5758
if (
5859
isnan( mu ) ||
5960
isnan( lambda ) ||
@@ -62,7 +63,8 @@ function mode( mu, lambda ) {
6263
) {
6364
return NaN;
6465
}
65-
return ( 2.0*mu*lambda ) / ( sqrt( 4.0*lambda*lambda + 9.0*mu*mu ) + 3.0*mu );
66+
r = mu / lambda;
67+
return mu * ( sqrt( 1.0 + ( ( 3.0*r/2.0 ) * ( 3.0*r/2.0 ) ) ) - ( 3.0*r/2.0 ) );
6668
}
6769

6870

lib/node_modules/@stdlib/stats/base/dists/wald/mode/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* // returns ~0.325
3333
*/
3434
double stdlib_base_dists_wald_mode( const double mu, const double lambda ) {
35+
double r;
3536
if (
3637
stdlib_base_is_nan( mu ) ||
3738
stdlib_base_is_nan( lambda ) ||
@@ -40,5 +41,6 @@ double stdlib_base_dists_wald_mode( const double mu, const double lambda ) {
4041
) {
4142
return 0.0/0.0; // NaN
4243
}
43-
return ( 2.0*mu*lambda ) / ( stdlib_base_sqrt( 4.0*lambda*lambda + 9.0*mu*mu ) + 3.0*mu );
44+
r = mu / lambda;
45+
return mu * ( stdlib_base_sqrt( 1.0 + ( ( 3.0*r/2.0 ) * ( 3.0*r/2.0 ) ) ) - ( 3.0*r/2.0 ) );
4446
}

lib/node_modules/@stdlib/stats/base/dists/wald/mode/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ tape( 'the function returns the mode of a Wald distribution', function test( t )
117117
if ( y === expected[i] ) {
118118
t.strictEqual( y, expected[i], 'mu:'+mu[i]+', lambda: '+lambda[i]+', y: '+y+', expected: '+expected[i] );
119119
} else {
120-
t.ok( isAlmostSameValue( y, expected[i], 500 ), 'within tolerance. mu: '+mu[i]+'. lambda: '+lambda[i]+'. y: '+y+'. E: '+expected[ i ]+'.' );
120+
t.ok( isAlmostSameValue( y, expected[i], 2 ), 'within tolerance. mu: '+mu[i]+'. lambda: '+lambda[i]+'. y: '+y+'. E: '+expected[ i ]+'.' );
121121
}
122122
}
123123
t.end();

lib/node_modules/@stdlib/stats/base/dists/wald/mode/test/test.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ tape( 'the function returns the mode of a Wald distribution', opts, function tes
126126
if ( y === expected[i] ) {
127127
t.strictEqual( y, expected[i], 'mu:'+mu[i]+', lambda: '+lambda[i]+', y: '+y+', expected: '+expected[i] );
128128
} else {
129-
t.ok( isAlmostSameValue( y, expected[i], 500 ), 'within tolerance. mu: '+mu[i]+'. lambda: '+lambda[i]+'. y: '+y+'. E: '+expected[ i ]+'.' );
129+
t.ok( isAlmostSameValue( y, expected[i], 2 ), 'within tolerance. mu: '+mu[i]+'. lambda: '+lambda[i]+'. y: '+y+'. E: '+expected[ i ]+'.' );
130130
}
131131
}
132132
t.end();

0 commit comments

Comments
 (0)