time-to-botec

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

print.js (3116B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' );
     24 var isObject = require( '@stdlib/assert/is-plain-object' );
     25 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     26 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     27 var roundn = require( '@stdlib/math/base/special/roundn' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns a function to pretty print test results.
     34 *
     35 * @private
     36 * @param {Object} results - test results
     37 * @returns {Function} pretty print function
     38 */
     39 function prettyPrint( results ) {
     40 	return print;
     41 
     42 	/**
     43 	* Pretty-print test results.
     44 	*
     45 	* @private
     46 	* @param {Options} [opts] - options object
     47 	* @param {PositiveInteger} [opts.digits=4] - number of digits after the decimal point
     48 	* @param {boolean} [opts.decision=true] - boolean indicating whether to print the test decision
     49 	* @throws {TypeError} options argument must be an object
     50 	* @throws {TypeError} must provide valid options
     51 	* @returns {string} formatted results
     52 	*/
     53 	function print( opts ) {
     54 		var decision;
     55 		var dgts;
     56 		var str;
     57 
     58 		dgts = 4;
     59 		decision = true;
     60 		if ( arguments.length > 0 ) {
     61 			if ( !isObject( opts ) ) {
     62 				throw new TypeError( 'invalid argument. Must provide an object. Value: `' + opts + '`.' );
     63 			}
     64 			if ( hasOwnProp( opts, 'digits' ) ) {
     65 				if ( !isPositiveInteger( opts.digits ) ) {
     66 					throw new TypeError( 'invalid option. `digits` option must be a positive integer. Option: `' + opts.digits + '`.' );
     67 				}
     68 				dgts = opts.digits;
     69 			}
     70 			if ( hasOwnProp( opts, 'decision' ) ) {
     71 				if ( !isBoolean( opts.decision ) ) {
     72 					throw new TypeError( 'invalid option. `decision` option must be a boolean primitive. Option: `' + opts.decision + '`.' );
     73 				}
     74 				decision = opts.decision;
     75 			}
     76 		}
     77 		str = '';
     78 		str += results.method;
     79 		str += '\n\n';
     80 		str += 'Null hypothesis: the two variables are independent';
     81 		str += '\n\n';
     82 		str += '    pValue: ' + roundn( results.pValue, -dgts ) + '\n';
     83 		str += '    statistic: ' + roundn( results.statistic, -dgts ) + '\n';
     84 		str += '    degrees of freedom: ' + results.df + '\n';
     85 		str += '\n';
     86 		if ( decision ) {
     87 			str += 'Test Decision: ';
     88 			if ( results.rejected ) {
     89 				str += 'Reject null in favor of alternative at ' + (results.alpha*100) + '% significance level';
     90 			} else {
     91 				str += 'Fail to reject null in favor of alternative at ' + (results.alpha*100) + '% significance level';
     92 			}
     93 			str += '\n';
     94 		}
     95 		return str;
     96 	}
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = prettyPrint;