Skip to content

Commit f4234cc

Browse files
committed
refactor: align main and factory.js with Julia-based triangular MGF implementation
--- 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: passed - 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 ---
1 parent 51e7260 commit f4234cc

File tree

3 files changed

+81
-44
lines changed

3 files changed

+81
-44
lines changed

lib/node_modules/@stdlib/stats/base/dists/triangular/mgf/lib/factory.js

+41-23
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,24 @@
2323
var constantFunction = require( '@stdlib/utils/constant-function' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var exp = require( '@stdlib/math/base/special/exp' );
26-
var pow = require( '@stdlib/math/base/special/pow' );
26+
var expm1 = require( '@stdlib/math/base/special/expm1' );
27+
28+
29+
// FUNCTIONS //
30+
31+
/**
32+
* Helper function for repeated computation in the MGF formula.
33+
*
34+
* @private
35+
* @param {number} x - input value
36+
* @returns {number} evaluated result
37+
*/
38+
function phi2(x) {
39+
if (x === 0) {
40+
return 1;
41+
}
42+
return (2 * (expm1(x) - x)) / (x * x);
43+
}
2744

2845

2946
// MAIN //
@@ -44,23 +61,16 @@ var pow = require( '@stdlib/math/base/special/pow' );
4461
* y = mgf( 2.0 );
4562
* // returns ~10.205
4663
*/
47-
function factory( a, b, c ) {
48-
var bmc;
49-
var bma;
50-
var cma;
51-
64+
function factory(a, b, c) {
5265
if (
53-
isnan( a ) ||
54-
isnan( b ) ||
55-
isnan( c ) ||
66+
isnan(a) ||
67+
isnan(b) ||
68+
isnan(c) ||
5669
a > c ||
5770
c > b
5871
) {
59-
return constantFunction( NaN );
72+
return constantFunction(NaN);
6073
}
61-
bmc = b - c;
62-
bma = b - a;
63-
cma = c - a;
6474
return mgf;
6575

6676
/**
@@ -74,20 +84,28 @@ function factory( a, b, c ) {
7484
* var y = mgf( 0.5 );
7585
* // returns <number>
7686
*/
77-
function mgf( t ) {
78-
var ret;
79-
80-
if ( isnan( t ) ) {
87+
function mgf(t) {
88+
if (isnan(t)) {
8189
return NaN;
8290
}
83-
if ( t === 0.0 ) {
91+
if (t === 0.0) {
8492
return 1.0;
8593
}
86-
ret = ( bmc * exp( a*t ) ) - ( bma * exp( c*t ) );
87-
ret += cma * exp( b*t );
88-
ret *= 2.0;
89-
ret /= bma * cma * bmc * pow( t, 2.0 );
90-
return ret;
94+
if (a < c) {
95+
if (c < b) {
96+
return (
97+
exp(c * t) * (
98+
((c - a) * phi2((a - c) * t)) +
99+
((b - c) * phi2((b - c) * t))
100+
) / (b - a)
101+
);
102+
}
103+
return exp(c * t) * phi2((a - c) * t);
104+
}
105+
if (c < b) {
106+
return exp(c * t) * phi2((b - c) * t);
107+
}
108+
return exp(c * t);
91109
}
92110
}
93111

lib/node_modules/@stdlib/stats/base/dists/triangular/mgf/lib/main.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@
2222

2323
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2424
var exp = require( '@stdlib/math/base/special/exp' );
25-
var pow = require( '@stdlib/math/base/special/pow' );
25+
var expm1 = require( '@stdlib/math/base/special/expm1' );
26+
27+
28+
// FUNCTIONS //
29+
30+
/**
31+
* Helper function for repeated computation in the MGF formula.
32+
*
33+
* @private
34+
* @param {number} x - input value
35+
* @returns {number} evaluated result
36+
*/
37+
function phi2(x) {
38+
if (x === 0) {
39+
return 1;
40+
}
41+
return (2 * (expm1(x) - x)) / (x * x);
42+
}
2643

2744

2845
// MAIN //
@@ -72,33 +89,35 @@ var pow = require( '@stdlib/math/base/special/pow' );
7289
* var y = mgf( 0.5, 1.0, 0.0, 1.5 );
7390
* // returns NaN
7491
*/
75-
function mgf( t, a, b, c ) {
76-
var bmc;
77-
var bma;
78-
var cma;
79-
var ret;
80-
92+
function mgf(t, a, b, c) {
8193
if (
82-
isnan( t ) ||
83-
isnan( a ) ||
84-
isnan( b ) ||
85-
isnan( c ) ||
94+
isnan(t) ||
95+
isnan(a) ||
96+
isnan(b) ||
97+
isnan(c) ||
8698
a > c ||
8799
c > b
88100
) {
89101
return NaN;
90102
}
91-
if ( t === 0.0 ) {
103+
if (t === 0.0) {
92104
return 1.0;
93105
}
94-
bmc = b - c;
95-
bma = b - a;
96-
cma = c - a;
97-
ret = ( bmc * exp( a*t ) ) - ( bma * exp( c*t ) );
98-
ret += cma * exp( b*t );
99-
ret *= 2.0;
100-
ret /= bma * cma * bmc * pow( t, 2.0 );
101-
return ret;
106+
if (a < c) {
107+
if (c < b) {
108+
return (
109+
exp(c * t) * (
110+
((c - a) * phi2((a - c) * t)) +
111+
((b - c) * phi2((b - c) * t))
112+
) / (b - a)
113+
);
114+
}
115+
return exp(c * t) * phi2((a - c) * t);
116+
}
117+
if (c < b) {
118+
return exp(c * t) * phi2((b - c) * t);
119+
}
120+
return exp(c * t);
102121
}
103122

104123

lib/node_modules/@stdlib/stats/base/dists/triangular/mgf/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@stdlib/math/base/napi/quaternary",
4242
"@stdlib/math/base/assert/is-nan",
4343
"@stdlib/math/base/special/exp",
44-
"@stdlib/math/base/special/pow"
44+
"@stdlib/math/base/special/expm1"
4545
]
4646
},
4747
{

0 commit comments

Comments
 (0)