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

Fix issue 5895 #5975

Closed
wants to merge 6 commits into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void print_summary( int total, int passing ) {
}

/**
* Prints benchmarks results.
* Prints benchmark results.
*
* @param elapsed elapsed time in seconds
*/
Expand Down Expand Up @@ -107,21 +107,18 @@ static double benchmark( void ) {
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = floor( 79.0*rand_double() );
x[ i ] = floor( 79.0 * rand_double() );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
y = stdlib_base_binet( x[ i%100 ] );
y = binet( x[ i % 100 ] );
if ( y < 0 ) {
printf( "should return a nonnegative number\n" );
break;
}
}
elapsed = tic() - t;
if ( y < 0 ) {
printf( "should return a nonnegative number\n" );
}
return elapsed;
}

Expand All @@ -143,4 +140,6 @@ int main( void ) {
printf( "ok %d benchmark finished\n", i+1 );
}
print_summary( REPEATS, REPEATS );

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' ); // Corrected import
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
Expand All @@ -32,31 +32,31 @@ var variance = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var len;
var mu;
var c;
var y;
var i;

len = 100;
mu = new Float64Array( len );
c = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
mu[ i ] = ( randu() * 100.0 ) - 50.0;
c[ i ] = ( randu() * 20.0 ) + EPS;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = variance( mu[ i % len ], c[ 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();
var len;
var mu;
var c;
var y;
var i;

len = 100;
mu = new Float64Array( len );
c = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
mu[ i ] = uniform( -50.0, 50.0 ); // Using uniform instead of randu
c[ i ] = uniform( EPS, 20.0 + EPS ); // Using uniform instead of randu
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = variance( mu[ i % len ], c[ 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
Expand Up @@ -24,15 +24,16 @@ var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var variance = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var levy = require( resolve( __dirname, '../../lib/index.js' ) );
var variance = levy.variance;
var opts = {
'skip': ( variance instanceof Error )
};
Expand All @@ -51,8 +52,8 @@ bench( pkg+'::native', opts, function benchmark( b ) {
mu = new Float64Array( len );
c = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
mu[ i ] = ( randu() * 100.0 ) - 50.0;
c[ i ] = ( randu() * 20.0 ) + EPS;
mu[ i ] = uniform( -50.0, 50.0 );
c[ i ] = uniform( EPS, 20.0 );
}

b.tic();
Expand All @@ -68,4 +69,4 @@ bench( pkg+'::native', opts, function benchmark( b ) {
}
b.pass( 'benchmark finished' );
b.end();
});
});
87 changes: 17 additions & 70 deletions lib/node_modules/@stdlib/stats/base/dists/normal/mgf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,18 @@ limitations under the License.

> [Normal][normal-distribution] distribution moment-generating function (MGF).

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

The [moment-generating function][mgf] for a [normal][normal-distribution] random variable is

<!-- <equation class="equation" label="eq:normal_mgf_function" align="center" raw="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \exp\{ \mu t + \frac{1}{2}\sigma^2t^2 \}" alt="Moment-generating function (MGF) for a normal distribution."> -->
The [moment-generating function][mgf] for a [normal][normal-distribution] random variable is:

```math
M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \exp\{ \mu t + \frac{1}{2}\sigma^2t^2 \}
```

<!-- <div class="equation" align="center" data-raw-text="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \exp\{ \mu t + \frac{1}{2}\sigma^2t^2 \}" data-equation="eq:normal_mgf_function">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/normal/mgf/docs/img/equation_normal_mgf_function.svg" alt="Moment-generating function (MGF) for a normal distribution.">
<br>
</div> -->

<!-- </equation> -->

where `mu` is the mean and `sigma > 0` is the standard deviation.

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage
Expand Down Expand Up @@ -95,61 +80,28 @@ y = mgf( 2.0, 0.0, -1.0 );
// returns NaN
```

#### mgf.factory( mu, sigma )

Returns a function for evaluating the [moment-generating function][mgf] (MGF) of a [normal][normal-distribution] distribution with parameters `mu` and `sigma`.

```javascript
var mymgf = mgf.factory( 4.0, 2.0 );

var y = mymgf( 1.0 );
// returns ~403.429

y = mymgf( 0.5 );
// returns ~12.182
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">
<section class="references">

## Examples
<!-- Moved content from lines 153–158 -->

<!-- eslint no-undef: "error" -->
<hr>

```javascript
var randu = require( '@stdlib/random/base/randu' );
var mgf = require( '@stdlib/stats/base/dists/normal/mgf' );

var sigma;
var mu;
var t;
var y;
var i;

for ( i = 0; i < 10; i++ ) {
t = randu();
mu = (randu() * 10.0) - 5.0;
sigma = randu() * 20.0;
y = mgf( t, mu, sigma );
console.log( 't: %d, µ: %d, σ: %d, M_X(t;µ,σ): %d', t.toFixed( 4 ), mu.toFixed( 4 ), sigma.toFixed( 4 ), y.toFixed( 4 ) );
}
```
<h2>References</h2>
<ul>
<li>
<a href="https://en.wikipedia.org/wiki/Normal_distribution">Normal Distribution</a>
</li>
<li>
<a href="https://en.wikipedia.org/wiki/Moment-generating_function">Moment-Generating Function</a>
</li>
</ul>

</section>

<!-- Closing section tag added -->

<!-- C interface documentation. -->

* * *
Expand Down Expand Up @@ -203,8 +155,7 @@ double stdlib_base_dists_normal_mgf( const double t, const double mu, const doub

<section class="notes">

</section>

</section>
<!-- /.notes -->

<!-- C API usage examples. -->
Expand Down Expand Up @@ -263,10 +214,6 @@ int main( void ) {

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[normal-distribution]: https://en.wikipedia.org/wiki/Normal_distribution
Expand All @@ -275,4 +222,4 @@ int main( void ) {

</section>

<!-- /.links -->

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/**
* @license Apache-2.0
*
Expand All @@ -7,7 +9,7 @@
* 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
* 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,
Expand All @@ -16,8 +18,6 @@
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
Expand All @@ -28,9 +28,20 @@ var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
var mgf = require( './../lib' );


// MAIN //

bench( pkg+':factory', function benchmark( b ) {
var mymgf;
var sigma;
var mu;
var t;
var y;
var i;

mu = 0.0;
sigma = 1.5;
mymgf = mgf.factory( mu, sigma );

bench( pkg, function benchmark( b ) {
var sigma;
var len;
Expand Down Expand Up @@ -64,18 +75,46 @@ bench( pkg, function benchmark( b ) {
b.end();
});

bench( pkg+':factory', function benchmark( b ) {
var mymgf;
var sigma;
var mu;
var t;
var y;
var i;
b.tic();
for ( i = 0; i < 100; i++ ) { // L63: Hardcoded 100
t = randu();
y = mymgf( t );
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();
});

mu = 0.0;
sigma = 1.5;
mymgf = mgf.factory( mu, sigma );
bench( pkg, function benchmark( b ) {
var sigma;
var mu;
var t;
var y;
var i;

b.tic();
for ( i = 0; i < 100; i++ ) { // L97: Hardcoded 100
t = randu();
mu = ( randu()*100.0 ) - 50.0;
sigma = ( randu()*20.0 ) + EPS;
y = mgf( t, mu, sigma );
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();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
t = uniform( 0.0, 1.0 );
Expand All @@ -91,3 +130,4 @@ bench( pkg+':factory', function benchmark( b ) {
b.pass( 'benchmark finished' );
b.end();
});