main.js (10743B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /* eslint-disable no-restricted-syntax, no-invalid-this */ 20 21 'use strict'; 22 23 // MODULES // 24 25 var defineProperty = require( '@stdlib/utils/define-property' ); 26 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); 27 var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); 28 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 29 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 30 var entropy = require( './../../../../../base/dists/arcsine/entropy' ); 31 var kurtosis = require( './../../../../../base/dists/arcsine/kurtosis' ); 32 var mean = require( './../../../../../base/dists/arcsine/mean' ); 33 var median = require( './../../../../../base/dists/arcsine/median' ); 34 var mode = require( './../../../../../base/dists/arcsine/mode' ); 35 var skewness = require( './../../../../../base/dists/arcsine/skewness' ); 36 var stdev = require( './../../../../../base/dists/arcsine/stdev' ); 37 var variance = require( './../../../../../base/dists/arcsine/variance' ); 38 var cdf = require( './../../../../../base/dists/arcsine/cdf' ); 39 var logcdf = require( './../../../../../base/dists/arcsine/logcdf' ); 40 var logpdf = require( './../../../../../base/dists/arcsine/logpdf' ); 41 var pdf = require( './../../../../../base/dists/arcsine/pdf' ); 42 var quantile = require( './../../../../../base/dists/arcsine/quantile' ); 43 44 45 // FUNCTIONS // 46 47 /** 48 * Evaluates the cumulative distribution function (CDF). 49 * 50 * @private 51 * @param {number} x - input value 52 * @returns {Probability} evaluated CDF 53 */ 54 function arcsineCDF( x ) { 55 return cdf( x, this.a, this.b ); 56 } 57 58 /** 59 * Evaluates the natural logarithm of the cumulative distribution function (CDF). 60 * 61 * @private 62 * @param {number} x - input value 63 * @returns {number} evaluated logCDF 64 */ 65 function arcsineLogCDF( x ) { 66 return logcdf( x, this.a, this.b ); 67 } 68 69 /** 70 * Evaluates the natural logarithm of the probability density function (logPDF). 71 * 72 * @private 73 * @param {number} x - input value 74 * @returns {number} evaluated logPDF 75 */ 76 function arcsineLogPDF( x ) { 77 return logpdf( x, this.a, this.b ); 78 } 79 80 /** 81 * Evaluates the probability density function (PDF). 82 * 83 * @private 84 * @param {number} x - input value 85 * @returns {number} evaluated PDF 86 */ 87 function arcsinePDF( x ) { 88 return pdf( x, this.a, this.b ); 89 } 90 91 /** 92 * Evaluates the quantile function. 93 * 94 * @private 95 * @param {Probability} p - input probability 96 * @returns {number} evaluated quantile function 97 */ 98 function arcsineQuantile( p ) { 99 return quantile( p, this.a, this.b ); 100 } 101 102 103 // MAIN // 104 105 /** 106 * Arcsine distribution constructor. 107 * 108 * @constructor 109 * @param {number} [a=0.0] - minimum support 110 * @param {number} [b=1.0] - maximum support 111 * @throws {TypeError} `a` must be a number primitive 112 * @throws {TypeError} `b` must be a number primitive 113 * @throws {RangeError} `a` must be less than `b` 114 * @returns {Arcsine} distribution instance 115 * 116 * @example 117 * var arcsine = new Arcsine( 0.0, 1.0 ); 118 * 119 * var y = arcsine.cdf( 0.8 ); 120 * // returns ~0.705 121 * 122 * var mu = arcsine.mean; 123 * // returns 0.5 124 */ 125 function Arcsine() { 126 var a; 127 var b; 128 if ( !(this instanceof Arcsine) ) { 129 if ( arguments.length === 0 ) { 130 return new Arcsine(); 131 } 132 return new Arcsine( arguments[ 0 ], arguments[ 1 ] ); 133 } 134 if ( arguments.length ) { 135 a = arguments[ 0 ]; 136 b = arguments[ 1 ]; 137 if ( !isNumber( a ) || isnan( a ) ) { 138 throw new TypeError( 'invalid argument. Minimum support `a` must be a number primitive. Value: `' + a + '`' ); 139 } 140 if ( !isNumber( b ) || isnan( b ) ) { 141 throw new TypeError( 'invalid argument. Maximum support `b` must be a number primitive. Value: `' + b + '`' ); 142 } 143 if ( a >= b ) { 144 throw new RangeError( 'invalid arguments. Minimum support `a` must be less than maximum support `b`. Value: `' + a + ',' + b + '`' ); 145 } 146 } else { 147 a = 0.0; 148 b = 1.0; 149 } 150 defineProperty( this, 'a', { 151 'configurable': false, 152 'enumerable': true, 153 'get': function get() { 154 return a; 155 }, 156 'set': function set( value ) { 157 if ( !isNumber( value ) || isnan( value ) ) { 158 throw new TypeError( 'invalid value. Must be a number primitive. Value: `' + value + '`' ); 159 } 160 if ( value >= b ) { 161 throw new RangeError( 'invalid value. Must be less than `b`. Value: `'+ value + '`' ); 162 } 163 a = value; 164 } 165 }); 166 defineProperty( this, 'b', { 167 'configurable': false, 168 'enumerable': true, 169 'get': function get() { 170 return b; 171 }, 172 'set': function set( value ) { 173 if ( !isNumber( value ) || isnan( value ) ) { 174 throw new TypeError( 'invalid value. Must be a number primitive. Value: `' + value + '`' ); 175 } 176 if ( value <= a ) { 177 throw new RangeError( 'invalid value. Must be greater than `a`. Value: `'+ value + '`' ); 178 } 179 b = value; 180 } 181 }); 182 return this; 183 } 184 185 /** 186 * Arcsine distribution differential entropy. 187 * 188 * @name entropy 189 * @memberof Arcsine.prototype 190 * @type {number} 191 * @see [differential entropy]{@link https://en.wikipedia.org/wiki/Entropy_%28information_theory%29} 192 * 193 * @example 194 * var arcsine = new Arcsine( 4.0, 12.0 ); 195 * 196 * var v = arcsine.entropy; 197 * // returns ~1.838 198 */ 199 setReadOnlyAccessor( Arcsine.prototype, 'entropy', function get() { 200 return entropy( this.a, this.b ); 201 }); 202 203 /** 204 * Arcsine distribution excess kurtosis. 205 * 206 * @name kurtosis 207 * @memberof Arcsine.prototype 208 * @type {number} 209 * @see [kurtosis]{@link https://en.wikipedia.org/wiki/Kurtosis} 210 * 211 * @example 212 * var arcsine = new Arcsine( 4.0, 12.0 ); 213 * 214 * var v = arcsine.kurtosis; 215 * // returns -1.5 216 */ 217 setReadOnlyAccessor( Arcsine.prototype, 'kurtosis', function get() { 218 return kurtosis( this.a, this.b ); 219 }); 220 221 /** 222 * Arcsine distribution expected value. 223 * 224 * @name mean 225 * @memberof Arcsine.prototype 226 * @type {number} 227 * @see [expected value]{@link https://en.wikipedia.org/wiki/Expected_value} 228 * 229 * @example 230 * var arcsine = new Arcsine( 4.0, 12.0 ); 231 * 232 * var v = arcsine.mean; 233 * // returns 8.0 234 */ 235 setReadOnlyAccessor( Arcsine.prototype, 'mean', function get() { 236 return mean( this.a, this.b ); 237 }); 238 239 /** 240 * Arcsine distribution median. 241 * 242 * @name median 243 * @memberof Arcsine.prototype 244 * @type {number} 245 * @see [median]{@link https://en.wikipedia.org/wiki/Median} 246 * 247 * @example 248 * var arcsine = new Arcsine( 4.0, 12.0 ); 249 * 250 * var v = arcsine.median; 251 * // returns 8.0 252 */ 253 setReadOnlyAccessor( Arcsine.prototype, 'median', function get() { 254 return median( this.a, this.b ); 255 }); 256 257 /** 258 * Arcsine distribution mode. 259 * 260 * @name mode 261 * @memberof Arcsine.prototype 262 * @type {number} 263 * @see [mode]{@link https://en.wikipedia.org/wiki/Mode_%28statistics%29} 264 * 265 * @example 266 * var arcsine = new Arcsine( 4.0, 12.0 ); 267 * 268 * var v = arcsine.mode; 269 * // returns 4.0 270 */ 271 setReadOnlyAccessor( Arcsine.prototype, 'mode', function get() { 272 return mode( this.a, this.b ); 273 }); 274 275 /** 276 * Arcsine distribution skewness. 277 * 278 * @name skewness 279 * @memberof Arcsine.prototype 280 * @type {number} 281 * @see [skewness]{@link https://en.wikipedia.org/wiki/Skewness} 282 * 283 * @example 284 * var arcsine = new Arcsine( 4.0, 12.0 ); 285 * 286 * var v = arcsine.skewness; 287 * // returns 0.0 288 */ 289 setReadOnlyAccessor( Arcsine.prototype, 'skewness', function get() { 290 return skewness( this.a, this.b ); 291 }); 292 293 /** 294 * Arcsine distribution standard deviation. 295 * 296 * @name stdev 297 * @memberof Arcsine.prototype 298 * @type {number} 299 * @see [standard deviation]{@link https://en.wikipedia.org/wiki/Standard_deviation} 300 * 301 * @example 302 * var arcsine = new Arcsine( 4.0, 12.0 ); 303 * 304 * var v = arcsine.stdev; 305 * // returns ~2.828 306 */ 307 setReadOnlyAccessor( Arcsine.prototype, 'stdev', function get() { 308 return stdev( this.a, this.b ); 309 }); 310 311 /** 312 * Arcsine distribution variance. 313 * 314 * @name variance 315 * @memberof Arcsine.prototype 316 * @type {number} 317 * @see [variance]{@link https://en.wikipedia.org/wiki/Variance} 318 * 319 * @example 320 * var arcsine = new Arcsine( 4.0, 12.0 ); 321 * 322 * var v = arcsine.variance; 323 * // returns 8.0 324 */ 325 setReadOnlyAccessor( Arcsine.prototype, 'variance', function get() { 326 return variance( this.a, this.b ); 327 }); 328 329 /** 330 * Evaluates the cumulative distribution function (CDF). 331 * 332 * @name cdf 333 * @memberof Arcsine.prototype 334 * @type {Function} 335 * @param {number} x - input value 336 * @returns {number} evaluated CDF 337 * @see [cdf]{@link https://en.wikipedia.org/wiki/Cumulative_distribution_function} 338 * 339 * @example 340 * var arcsine = new Arcsine( 2.0, 4.0 ); 341 * 342 * var v = arcsine.cdf( 3.0 ); 343 * // returns ~0.5 344 */ 345 setReadOnly( Arcsine.prototype, 'cdf', arcsineCDF ); 346 347 /** 348 * Evaluates the natural logarithm of the cumulative distribution function (CDF). 349 * 350 * @name logcdf 351 * @memberof Arcsine.prototype 352 * @type {Function} 353 * @param {number} x - input value 354 * @returns {number} evaluated logCDF 355 * @see [cdf]{@link https://en.wikipedia.org/wiki/Cumulative_distribution_function} 356 * 357 * @example 358 * var arcsine = new Arcsine( 2.0, 4.0 ); 359 * 360 * var v = arcsine.logcdf( 3.0 ); 361 * // returns ~-0.693 362 */ 363 setReadOnly( Arcsine.prototype, 'logcdf', arcsineLogCDF ); 364 365 /** 366 * Evaluates the natural logarithm of the probability density function (logPDF). 367 * 368 * @name logpdf 369 * @memberof Arcsine.prototype 370 * @type {Function} 371 * @param {number} x - input value 372 * @returns {number} evaluated logPDF 373 * @see [pdf]{@link https://en.wikipedia.org/wiki/Probability_density_function} 374 * 375 * @example 376 * var arcsine = new Arcsine( 2.0, 4.0 ); 377 * 378 * var v = arcsine.logpdf( 2.5 ); 379 * // returns ~-1.0 380 */ 381 setReadOnly( Arcsine.prototype, 'logpdf', arcsineLogPDF ); 382 383 /** 384 * Evaluates the probability density function (PDF). 385 * 386 * @name pdf 387 * @memberof Arcsine.prototype 388 * @type {Function} 389 * @param {number} x - input value 390 * @returns {number} evaluated PDF 391 * @see [pdf]{@link https://en.wikipedia.org/wiki/Probability_density_function} 392 * 393 * @example 394 * var arcsine = new Arcsine( 2.0, 4.0 ); 395 * 396 * var v = arcsine.pdf( 2.4 ); 397 * // returns ~0.398 398 */ 399 setReadOnly( Arcsine.prototype, 'pdf', arcsinePDF ); 400 401 /** 402 * Evaluates the quantile function. 403 * 404 * @name quantile 405 * @memberof Arcsine.prototype 406 * @type {Function} 407 * @param {Probability} p - input probability 408 * @returns {number} evaluated quantile function 409 * @see [quantile function]{@link https://en.wikipedia.org/wiki/Quantile_function} 410 * 411 * @example 412 * var arcsine = new Arcsine( 2.0, 4.0 ); 413 * 414 * var v = arcsine.quantile( 0.5 ); 415 * // returns 3.0 416 */ 417 setReadOnly( Arcsine.prototype, 'quantile', arcsineQuantile ); 418 419 420 // EXPORTS // 421 422 module.exports = Arcsine;