time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

factory.js (7477B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
     25 var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' );
     26 var isObject = require( '@stdlib/assert/is-plain-object' );
     27 var isFunction = require( '@stdlib/assert/is-function' );
     28 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     29 var constantFunction = require( '@stdlib/utils/constant-function' );
     30 var noop = require( '@stdlib/utils/noop' );
     31 var chisquare = require( './../../../base/chisquare' ).factory;
     32 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     33 var typedarray2json = require( '@stdlib/array/to-json' );
     34 var validate = require( './validate.js' );
     35 var f0 = require( './f.js' );
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Returns a pseudorandom number generator for generating F distributed random numbers.
     42 *
     43 * @param {PositiveNumber} [d1] - degrees of freedom
     44 * @param {PositiveNumber} [d2] - degrees of freedom
     45 * @param {Options} [options] - function options
     46 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
     47 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
     48 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
     49 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
     50 * @throws {TypeError} `d1` must be a positive number
     51 * @throws {TypeError} `d2` must be a positive number
     52 * @throws {TypeError} options argument must be an object
     53 * @throws {TypeError} must provide valid options
     54 * @throws {Error} must provide a valid state
     55 * @returns {PRNG} pseudorandom number generator
     56 *
     57 * @example
     58 * var f = factory( 1.0, 2.0 );
     59 * var v = f();
     60 * // returns <number>
     61 *
     62 * @example
     63 * var f = factory( 0.5, 3.0, {
     64 *     'seed': 297
     65 * });
     66 * var v = f();
     67 * // returns <number>
     68 */
     69 function factory() {
     70 	var rchisq;
     71 	var opts;
     72 	var rand;
     73 	var prng;
     74 	var err;
     75 	var d1;
     76 	var d2;
     77 
     78 	if ( arguments.length === 0 ) {
     79 		rchisq = chisquare();
     80 	} else if ( arguments.length === 1 ) {
     81 		opts = arguments[ 0 ];
     82 		if ( !isObject( opts ) ) {
     83 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
     84 		}
     85 		if ( hasOwnProp( opts, 'prng' ) ) {
     86 			if ( !isFunction( opts.prng ) ) {
     87 				throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' );
     88 			}
     89 			rchisq = chisquare({
     90 				'prng': opts.prng
     91 			});
     92 		} else {
     93 			rchisq = chisquare( opts );
     94 		}
     95 	} else {
     96 		d1 = arguments[ 0 ];
     97 		d2 = arguments[ 1 ];
     98 		err = validate( d1, d2 );
     99 		if ( err ) {
    100 			throw err;
    101 		}
    102 		if ( arguments.length > 2 ) {
    103 			opts = arguments[ 2 ];
    104 			if ( !isObject( opts ) ) {
    105 				throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
    106 			}
    107 			if ( hasOwnProp( opts, 'prng' ) ) {
    108 				if ( !isFunction( opts.prng ) ) {
    109 					throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' );
    110 				}
    111 				rchisq = chisquare({
    112 					'prng': opts.prng
    113 				});
    114 			} else {
    115 				rchisq = chisquare( opts );
    116 			}
    117 		} else {
    118 			rchisq = chisquare();
    119 		}
    120 	}
    121 	if ( d1 === void 0 ) {
    122 		prng = f2;
    123 	} else {
    124 		prng = f1;
    125 	}
    126 	rand = rchisq.PRNG;
    127 
    128 	setReadOnly( prng, 'NAME', 'f' );
    129 
    130 	// If we are provided an "external" PRNG, we don't support getting or setting PRNG state, as we'd need to check for compatible state value types, etc, entailing considerable complexity.
    131 	if ( opts && opts.prng ) {
    132 		setReadOnly( prng, 'seed', null );
    133 		setReadOnly( prng, 'seedLength', null );
    134 		setReadWriteAccessor( prng, 'state', constantFunction( null ), noop );
    135 		setReadOnly( prng, 'stateLength', null );
    136 		setReadOnly( prng, 'byteLength', null );
    137 		setReadOnly( prng, 'toJSON', constantFunction( null ) );
    138 	} else {
    139 		setReadOnlyAccessor( prng, 'seed', getSeed );
    140 		setReadOnlyAccessor( prng, 'seedLength', getSeedLength );
    141 		setReadWriteAccessor( prng, 'state', getState, setState );
    142 		setReadOnlyAccessor( prng, 'stateLength', getStateLength );
    143 		setReadOnlyAccessor( prng, 'byteLength', getStateSize );
    144 		setReadOnly( prng, 'toJSON', toJSON );
    145 	}
    146 	setReadOnly( prng, 'PRNG', rand );
    147 	return prng;
    148 
    149 	/**
    150 	* Returns the PRNG seed.
    151 	*
    152 	* @private
    153 	* @returns {PRNGSeedMT19937} seed
    154 	*/
    155 	function getSeed() {
    156 		return rand.seed;
    157 	}
    158 
    159 	/**
    160 	* Returns the PRNG seed length.
    161 	*
    162 	* @private
    163 	* @returns {PositiveInteger} seed length
    164 	*/
    165 	function getSeedLength() {
    166 		return rand.seedLength;
    167 	}
    168 
    169 	/**
    170 	* Returns the PRNG state length.
    171 	*
    172 	* @private
    173 	* @returns {PositiveInteger} state length
    174 	*/
    175 	function getStateLength() {
    176 		return rand.stateLength;
    177 	}
    178 
    179 	/**
    180 	* Returns the PRNG state size (in bytes).
    181 	*
    182 	* @private
    183 	* @returns {PositiveInteger} state size (in bytes)
    184 	*/
    185 	function getStateSize() {
    186 		return rand.byteLength;
    187 	}
    188 
    189 	/**
    190 	* Returns the current pseudorandom number generator state.
    191 	*
    192 	* @private
    193 	* @returns {PRNGStateMT19937} current state
    194 	*/
    195 	function getState() {
    196 		return rand.state;
    197 	}
    198 
    199 	/**
    200 	* Sets the pseudorandom number generator state.
    201 	*
    202 	* @private
    203 	* @param {PRNGStateMT19937} s - generator state
    204 	* @throws {Error} must provide a valid state
    205 	*/
    206 	function setState( s ) {
    207 		rand.state = s;
    208 	}
    209 
    210 	/**
    211 	* Serializes the pseudorandom number generator as a JSON object.
    212 	*
    213 	* ## Notes
    214 	*
    215 	* -   `JSON.stringify()` implicitly calls this method when stringifying a PRNG.
    216 	*
    217 	* @private
    218 	* @returns {Object} JSON representation
    219 	*/
    220 	function toJSON() {
    221 		var out = {};
    222 		out.type = 'PRNG';
    223 		out.name = prng.NAME;
    224 		out.state = typedarray2json( rand.state );
    225 		if ( d1 === void 0 ) {
    226 			out.params = [];
    227 		} else {
    228 			out.params = [ d1, d2 ];
    229 		}
    230 		return out;
    231 	}
    232 
    233 	/**
    234 	* Returns a pseudorandom number from an F distribution with bound parameters.
    235 	*
    236 	* @private
    237 	* @returns {NonNegativeNumber} pseudorandom number
    238 	*
    239 	* @example
    240 	* var v = f1();
    241 	* // returns <number>
    242 	*/
    243 	function f1() {
    244 		return f0( rchisq, d1, d2 );
    245 	}
    246 
    247 	/**
    248 	* Returns a pseudorandom number from an F distribution with parameters `d1` and `d2`.
    249 	*
    250 	* @private
    251 	* @param {PositiveNumber} d1 - degrees of freedom
    252 	* @param {PositiveNumber} d2 - degrees of freedom
    253 	* @returns {NonNegativeNumber} pseudorandom number
    254 	*
    255 	* @example
    256 	* var v = f2( 3.0, 2.0 );
    257 	* // returns <number>
    258 	*
    259 	* @example
    260 	* var v = f2( 3.0, 0.0 );
    261 	* // returns NaN
    262 	*
    263 	* @example
    264 	* var v = f2( 0.0, 2.0 );
    265 	* // returns NaN
    266 	*
    267 	* @example
    268 	* var v = f2( NaN, NaN );
    269 	* // returns NaN
    270 	*/
    271 	function f2( d1, d2 ) {
    272 		if (
    273 			isnan( d1 ) ||
    274 			isnan( d2 ) ||
    275 			d1 <= 0.0 ||
    276 			d2 <= 0.0
    277 		) {
    278 			return NaN;
    279 		}
    280 		return f0( rchisq, d1, d2 );
    281 	}
    282 }
    283 
    284 
    285 // EXPORTS //
    286 
    287 module.exports = factory;