All files / stats/base/dists/laplace/ctor/lib main.js

100% Statements 443/443
100% Branches 35/35
100% Functions 19/19
100% Lines 443/443

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 4441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x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1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
* @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.
*/
 
/* eslint-disable no-restricted-syntax, no-invalid-this */
 
'use strict';
 
// MODULES //
 
var defineProperty = require( '@stdlib/utils/define-property' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isnan = require( '@stdlib/assert/is-nan' );
var entropy = require( '@stdlib/stats/base/dists/laplace/entropy' );
var kurtosis = require( '@stdlib/stats/base/dists/laplace/kurtosis' );
var mean = require( '@stdlib/stats/base/dists/laplace/mean' );
var median = require( '@stdlib/stats/base/dists/laplace/median' );
var mode = require( '@stdlib/stats/base/dists/laplace/mode' );
var skewness = require( '@stdlib/stats/base/dists/laplace/skewness' );
var stdev = require( '@stdlib/stats/base/dists/laplace/stdev' );
var variance = require( '@stdlib/stats/base/dists/laplace/variance' );
var cdf = require( '@stdlib/stats/base/dists/laplace/cdf' );
var logcdf = require( '@stdlib/stats/base/dists/laplace/logcdf' );
var logpdf = require( '@stdlib/stats/base/dists/laplace/logpdf' );
var mgf = require( '@stdlib/stats/base/dists/laplace/mgf' );
var pdf = require( '@stdlib/stats/base/dists/laplace/pdf' );
var quantile = require( '@stdlib/stats/base/dists/laplace/quantile' );
var format = require( '@stdlib/string/format' );
 
 
// FUNCTIONS //
 
/**
* Evaluates the cumulative distribution function (CDF).
*
* @private
* @param {number} x - input value
* @returns {Probability} evaluated CDF
*/
function laplaceCDF( x ) {
	return cdf( x, this.mu, this.b );
}
 
/**
* Evaluates the natural logarithm of the cumulative distribution function (logCDF).
*
* @private
* @param {number} x - input value
* @returns {number} evaluated logCDF
*/
function laplaceLogCDF( x ) {
	return logcdf( x, this.mu, this.b );
}
 
/**
* Evaluates the natural logarithm of the probability density function (logPDF).
*
* @private
* @param {number} x - input value
* @returns {number} evaluated logPDF
*/
function laplaceLogPDF( x ) {
	return logpdf( x, this.mu, this.b );
}
 
/**
* Evaluates the moment-generating function (MGF).
*
* @private
* @param {number} t - input value
* @returns {number} evaluated MGF
*/
function laplaceMGF( t ) {
	return mgf( t, this.mu, this.b );
}
 
/**
* Evaluates the probability density function (PDF).
*
* @private
* @param {number} x - input value
* @returns {number} evaluated PDF
*/
function laplacePDF( x ) {
	return pdf( x, this.mu, this.b );
}
 
/**
* Evaluates the quantile function.
*
* @private
* @param {Probability} p - input probability
* @returns {number} evaluated quantile function
*/
function laplaceQuantile( p ) {
	return quantile( p, this.mu, this.b );
}
 
 
// MAIN //
 
/**
* Laplace distribution constructor.
*
* @constructor
* @param {number} [mu=0.0] - location parameter
* @param {PositiveNumber} [b=1.0] - scale parameter
* @throws {TypeError} `b` must be a positive number
* @returns {Laplace} distribution instance
*
* @example
* var laplace = new Laplace( 1.0, 1.0 );
*
* var y = laplace.cdf( 1.5 );
* // returns ~0.697
*
* var mu = laplace.mean;
* // returns 1.0
*/
function Laplace() {
	var mu;
	var b;
	if ( !(this instanceof Laplace) ) {
		if ( arguments.length === 0 ) {
			return new Laplace();
		}
		return new Laplace( arguments[ 0 ], arguments[ 1 ] );
	}
	if ( arguments.length ) {
		mu = arguments[ 0 ];
		b = arguments[ 1 ];
		if ( !isNumber( mu ) || isnan( mu ) ) {
			throw new TypeError( format( 'invalid argument. Location parameter must be a number. Value: `%s`.', mu ) );
		}
		if ( !isPositive( b ) ) {
			throw new TypeError( format( 'invalid argument. Scale parameter must be a positive number. Value: `%s`.', b ) );
		}
	} else {
		mu = 0.0;
		b = 1.0;
	}
	defineProperty( this, 'mu', {
		'configurable': false,
		'enumerable': true,
		'get': function get() {
			return mu;
		},
		'set': function set( value ) {
			if ( !isNumber( value ) || isnan( value ) ) {
				throw new TypeError( format( 'invalid assignment. Must be a number. Value: `%s`.', value ) );
			}
			mu = value;
		}
	});
	defineProperty( this, 'b', {
		'configurable': false,
		'enumerable': true,
		'get': function get() {
			return b;
		},
		'set': function set( value ) {
			if ( !isPositive( value ) ) {
				throw new TypeError( format( 'invalid assignment. Must be a positive number. Value: `%s`.', value ) );
			}
			b = value;
		}
	});
	return this;
}
 
/**
* Laplace distribution differential entropy.
*
* @name entropy
* @memberof Laplace.prototype
* @type {number}
* @see [differential entropy]{@link https://en.wikipedia.org/wiki/Entropy_%28information_theory%29}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.entropy;
* // returns ~4.178
*/
setReadOnlyAccessor( Laplace.prototype, 'entropy', function get() {
	return entropy( this.mu, this.b );
});
 
/**
* Laplace distribution excess kurtosis.
*
* @name kurtosis
* @memberof Laplace.prototype
* @type {number}
* @see [kurtosis]{@link https://en.wikipedia.org/wiki/Kurtosis}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.kurtosis;
* // returns 3.0
*/
setReadOnlyAccessor( Laplace.prototype, 'kurtosis', function get() {
	return kurtosis( this.mu, this.b );
});
 
/**
* Laplace distribution expected value.
*
* @name mean
* @memberof Laplace.prototype
* @type {number}
* @see [expected value]{@link https://en.wikipedia.org/wiki/Expected_value}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.mean;
* // returns 4.0
*/
setReadOnlyAccessor( Laplace.prototype, 'mean', function get() {
	return mean( this.mu, this.b );
});
 
/**
* Laplace distribution median.
*
* @name median
* @memberof Laplace.prototype
* @type {number}
* @see [median]{@link https://en.wikipedia.org/wiki/Median}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.median;
* // returns 4.0
*/
setReadOnlyAccessor( Laplace.prototype, 'median', function get() {
	return median( this.mu, this.b );
});
 
/**
* Laplace distribution mode.
*
* @name mode
* @memberof Laplace.prototype
* @type {number}
* @see [mode]{@link https://en.wikipedia.org/wiki/Mode_%28statistics%29}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.mode;
* // returns 4.0
*/
setReadOnlyAccessor( Laplace.prototype, 'mode', function get() {
	return mode( this.mu, this.b );
});
 
/**
* Laplace distribution skewness.
*
* @name skewness
* @memberof Laplace.prototype
* @type {number}
* @see [skewness]{@link https://en.wikipedia.org/wiki/Skewness}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.skewness;
* // returns 0.0
*/
setReadOnlyAccessor( Laplace.prototype, 'skewness', function get() {
	return skewness( this.mu, this.b );
});
 
/**
* Laplace distribution standard deviation.
*
* @name stdev
* @memberof Laplace.prototype
* @type {PositiveNumber}
* @see [standard deviation]{@link https://en.wikipedia.org/wiki/Standard_deviation}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.stdev;
* // returns ~16.971
*/
setReadOnlyAccessor( Laplace.prototype, 'stdev', function get() {
	return stdev( this.mu, this.b );
});
 
/**
* Laplace distribution variance.
*
* @name variance
* @memberof Laplace.prototype
* @type {PositiveNumber}
* @see [variance]{@link https://en.wikipedia.org/wiki/Variance}
*
* @example
* var laplace = new Laplace( 4.0, 12.0 );
*
* var v = laplace.variance;
* // returns 288.0
*/
setReadOnlyAccessor( Laplace.prototype, 'variance', function get() {
	return variance( this.mu, this.b );
});
 
/**
* Evaluates the cumulative distribution function (CDF).
*
* @name cdf
* @memberof Laplace.prototype
* @type {Function}
* @param {number} x - input value
* @returns {number} evaluated CDF
* @see [cdf]{@link https://en.wikipedia.org/wiki/Cumulative_distribution_function}
*
* @example
* var laplace = new Laplace( 2.0, 4.0 );
*
* var v = laplace.cdf( 0.5 );
* // returns ~0.344
*/
setReadOnly( Laplace.prototype, 'cdf', laplaceCDF );
 
/**
* Evaluates the natural logarithm of the cumulative distribution function (logCDF).
*
* @name logcdf
* @memberof Laplace.prototype
* @type {Function}
* @param {number} x - input value
* @returns {number} evaluated logCDF
* @see [cdf]{@link https://en.wikipedia.org/wiki/Cumulative_distribution_function}
*
* @example
* var laplace = new Laplace( 2.0, 4.0 );
*
* var v = laplace.logcdf( 0.9 );
* // returns ~-0.968
*/
setReadOnly( Laplace.prototype, 'logcdf', laplaceLogCDF );
 
/**
* Evaluates the natural logarithm of the probability density function (logPDF).
*
* @name logpdf
* @memberof Laplace.prototype
* @type {Function}
* @param {number} x - input value
* @returns {number} evaluated logPDF
* @see [pdf]{@link https://en.wikipedia.org/wiki/Probability_density_function}
*
* @example
* var laplace = new Laplace( 2.0, 4.0 );
*
* var v = laplace.logpdf( 0.8 );
* // returns ~-2.379
*/
setReadOnly( Laplace.prototype, 'logpdf', laplaceLogPDF );
 
/**
* Evaluates the moment-generating function (MGF).
*
* @name mgf
* @memberof Laplace.prototype
* @type {Function}
* @param {number} t - input value
* @returns {number} evaluated MGF
* @see [mgf]{@link https://en.wikipedia.org/wiki/Moment-generating_function}
*
* @example
* var laplace = new Laplace( 2.0, 4.0 );
*
* var v = laplace.mgf( 0.01 );
* // returns ~1.022
*/
setReadOnly( Laplace.prototype, 'mgf', laplaceMGF );
 
/**
* Evaluates the probability density function (PDF).
*
* @name pdf
* @memberof Laplace.prototype
* @type {Function}
* @param {number} x - input value
* @returns {number} evaluated PDF
* @see [pdf]{@link https://en.wikipedia.org/wiki/Probability_density_function}
*
* @example
* var laplace = new Laplace( 2.0, 4.0 );
*
* var v = laplace.pdf( 0.8 );
* // returns ~0.093
*/
setReadOnly( Laplace.prototype, 'pdf', laplacePDF );
 
/**
* Evaluates the quantile function.
*
* @name quantile
* @memberof Laplace.prototype
* @type {Function}
* @param {Probability} p - input probability
* @returns {number} evaluated quantile function
* @see [quantile function]{@link https://en.wikipedia.org/wiki/Quantile_function}
*
* @example
* var laplace = new Laplace( 2.0, 4.0 );
*
* var v = laplace.quantile( 0.5 );
* // returns 2.0
*/
setReadOnly( Laplace.prototype, 'quantile', laplaceQuantile );
 
 
// EXPORTS //
 
module.exports = Laplace;