Skip to content
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
@@ -0,0 +1,25 @@
'use strict';

/**
* Half-normal distribution expected value.
*
* @module @stdlib/stats/base/dists/halfnormal/mean
*
* @example
* var mean = require( '@stdlib/stats/base/dists/halfnormal/mean' );
*
* var v = mean( 1.0 );
* // returns ~0.798
*
* var v = mean( 2.0 );
* // returns ~1.596
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var PI = require( '@stdlib/constants/float64/pi' );


// VARIABLES //

var SQRT_TWO_OVER_PI = sqrt( 2.0 / PI );


// MAIN //

/**
* Returns the expected value of a half-normal distribution.
*
* @param {number} sigma - scale parameter
* @returns {number} expected value
*
* @example
* var v = mean( 1.0 );
* // returns ~0.798
*
* @example
* var v = mean( 2.0 );
* // returns ~1.596
*
* @example
* var v = mean( -1.0 );
* // returns NaN
*/
function mean( sigma ) {
if ( isnan( sigma ) || sigma <= 0.0 ) {
return NaN;
}
return sigma * SQRT_TWO_OVER_PI;
}


// EXPORTS //

module.exports = mean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "@stdlib/stats-base-dists-halfnormal-mean",
"version": "0.0.0",
"description": "Half-normal distribution expected value.",
"license": "Apache-2.0",
"author": {
"name": "The Stdlib Authors",
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
},
"contributors": [
{
"name": "The Stdlib Authors",
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
}
],
"main": "./lib",
"directories": {
"lib": "./lib",
"doc": "./docs",
"test": "./test",
"benchmark": "./benchmark"
},
"types": "./docs/types",
"scripts": {
"test": "make test",
"test-cov": "make test-cov",
"test-browsers": "make test-browsers"
},
"homepage": "https://stdlib.io",
"repository": {
"type": "git",
"url": "git://github.com/stdlib-js/stdlib.git"
},
"bugs": {
"url": "https://github.com/stdlib-js/stdlib/issues"
},
"dependencies": {
"@stdlib/constants/float64/pi": "^0.2.2",
"@stdlib/math/base/assert/is-nan": "^0.2.2",
"@stdlib/math/base/special/sqrt": "^0.2.2"
},
"devDependencies": {
"@stdlib/math/base/special/abs": "^0.2.2",
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
"istanbul": "^0.4.1",
"tap-min": "git+https://github.com/Planeshifter/tap-min.git",
"@stdlib/bench/harness": "^0.2.2"
},
"engines": {
"node": ">=0.10.0",
"npm": ">2.7.0"
},
"os": [
"aix",
"darwin",
"freebsd",
"linux",
"macos",
"openbsd",
"sunos",
"win32",
"windows"
],
"keywords": [
"stdlib",
"stdmath",
"statistics",
"stats",
"distribution",
"half-normal",
"halfnormal",
"mean",
"expected value",
"expectation"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

// MODULES //

var tape = require( 'tape' );

Check failure on line 5 in lib/node_modules/@stdlib/stats/base/dists/halfnormal/mean/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

"tape" is not published
var abs = require( '@stdlib/math/base/special/abs' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var PINF = require( '@stdlib/constants/float64/pinf' );

Check failure on line 8 in lib/node_modules/@stdlib/stats/base/dists/halfnormal/mean/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'PINF' is assigned a value but never used
var NINF = require( '@stdlib/constants/float64/ninf' );
var mean = require( './../lib' );

// TESTS //

Check failure on line 12 in lib/node_modules/@stdlib/stats/base/dists/halfnormal/mean/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Header comment must be preceded by one additional empty line

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof mean, 'function', 'main export is a function' );
t.end();
});

tape( 'if provided `NaN` for `sigma`, the function returns `NaN`', function test( t ) {
var v = mean( NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.end();
});

tape( 'if provided a value less than or equal to 0 for `sigma`, the function returns `NaN`', function test( t ) {
var v;

v = mean( 0.0 );
t.equal( isnan( v ), true, 'returns NaN' );

v = mean( -1.0 );
t.equal( isnan( v ), true, 'returns NaN' );

v = mean( NINF );
t.equal( isnan( v ), true, 'returns NaN' );

t.end();
});

tape( 'the function returns the expected value', function test( t ) {
var expected;
var sigma;
var v;
var i;

// Test cases (sigma, expected)
// Mean = sigma * sqrt(2/pi) -> sigma * ~0.79788456

Check failure on line 48 in lib/node_modules/@stdlib/stats/base/dists/halfnormal/mean/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Missing empty line before comment
sigma = [ 1.0, 2.0, 5.0, 0.5 ];
expected = [
0.79788456,
1.59576912,
3.98942280,
0.39894228
];

for ( i = 0; i < sigma.length; i++ ) {
v = mean( sigma[ i ] );
// We check if the difference is very small (epsilon)

Check failure on line 59 in lib/node_modules/@stdlib/stats/base/dists/halfnormal/mean/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Missing empty line before comment
if ( abs( v - expected[ i ] ) > 1e-7 ) {
t.fail( 'returns ' + v + ' when expected ' + expected[ i ] );
} else {
t.pass( 'returns ' + v + ' when expected ' + expected[ i ] );
}
}
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

/**
* Half-normal distribution variance.
*
* @module @stdlib/stats/base/dists/halfnormal/variance
*
* @example
* var variance = require( '@stdlib/stats/base/dists/halfnormal/variance' );
*
* var v = variance( 1.0 );
* // returns ~0.363
*
* var v = variance( 2.0 );
* // returns ~1.454
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var PI = require( '@stdlib/constants/float64/pi' );


// VARIABLES //

// 1 - 2/pi
var FACTOR = 1.0 - ( 2.0 / PI );


// MAIN //

/**
* Returns the variance of a half-normal distribution.
*
* @param {number} sigma - scale parameter
* @returns {number} variance
*
* @example
* var v = variance( 1.0 );
* // returns ~0.363
*
* @example
* var v = variance( 2.0 );
* // returns ~1.454
*
* @example
* var v = variance( -1.0 );
* // returns NaN
*/
function variance( sigma ) {
if ( isnan( sigma ) || sigma <= 0.0 ) {
return NaN;
}
return pow( sigma, 2.0 ) * FACTOR;
}


// EXPORTS //

module.exports = variance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "@stdlib/stats-base-dists-halfnormal-variance",
"version": "0.0.0",
"description": "Half-normal distribution variance.",
"license": "Apache-2.0",
"author": {
"name": "The Stdlib Authors",
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
},
"contributors": [
{
"name": "The Stdlib Authors",
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
}
],
"main": "./lib",
"directories": {
"doc": "./docs",
"lib": "./lib",
"test": "./test"
},
"types": "./docs/types",
"scripts": {
"test": "make test",
"test-cov": "make test-cov",
"test-browsers": "make test-browsers"
},
"homepage": "https://stdlib.io",
"repository": {
"type": "git",
"url": "git://github.com/stdlib-js/stdlib.git"
},
"bugs": {
"url": "https://github.com/stdlib-js/stdlib/issues"
},
"dependencies": {
"@stdlib/constants/float64/pi": "^0.2.2",
"@stdlib/math/base/assert/is-nan": "^0.2.2",
"@stdlib/math/base/special/pow": "^0.2.2"
},
"devDependencies": {
"@stdlib/math/base/special/abs": "^0.2.2",
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
"istanbul": "^0.4.1",
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
},
"engines": {
"node": ">=0.10.0",
"npm": ">2.7.0"
},
"os": [
"aix",
"darwin",
"freebsd",
"linux",
"macos",
"openbsd",
"sunos",
"win32",
"windows"
],
"keywords": [
"stdlib",
"stdmath",
"statistics",
"stats",
"distribution",
"half-normal",
"halfnormal",
"variance",
"var",
"dispersion"
]
}
Loading
Loading