time-to-botec

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

print.js (3209B)


      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 * Pretty-print output of test.
     34 *
     35 * @param {Object} [opts] - options object
     36 * @param {PositiveInteger} [opts.digits=4] - number of digits after the decimal point
     37 * @param {boolean} [opts.decision=true] - boolean indicating whether to print the test decision
     38 * @throws {TypeError} options argument must be an object
     39 * @throws {TypeError} must provide valid options
     40 * @returns {string} formatted output
     41 */
     42 function print( opts ) { // eslint-disable-line stdlib/no-redeclare
     43 	/* eslint-disable no-invalid-this */
     44 	var decision;
     45 	var dgts;
     46 	var str;
     47 
     48 	dgts = 4;
     49 	decision = true;
     50 	if ( arguments.length > 0 ) {
     51 		if ( !isObject( opts ) ) {
     52 			throw new TypeError( 'invalid argument. First argument must be an options object. Value: `' + opts + '`.' );
     53 		}
     54 		if ( hasOwnProp( opts, 'digits' ) ) {
     55 			if ( !isPositiveInteger( opts.digits ) ) {
     56 				throw new TypeError( 'invalid option. `digits` option must be a positive integer. Option: `' + opts.digits + '`.' );
     57 			}
     58 			dgts = opts.digits;
     59 		}
     60 		if ( hasOwnProp( opts, 'decision' ) ) {
     61 			if ( !isBoolean( opts.decision ) ) {
     62 				throw new TypeError( 'invalid option. `decision` option must be a boolean primitive. Option: `' + opts.decision + '`.' );
     63 			}
     64 			decision = opts.decision;
     65 		}
     66 	}
     67 	str = '';
     68 	str += this.method;
     69 	str += '\n\n';
     70 	str += 'Alternative hypothesis: ';
     71 	if ( this.method === 'Paired Wilcoxon signed rank test' ) {
     72 		str += 'Median of the difference `x - y` is ';
     73 	} else {
     74 		str += 'Median of `x` is ';
     75 	}
     76 	switch ( this.alternative ) {
     77 	case 'two-sided':
     78 	default:
     79 		str += 'not equal to ';
     80 		break;
     81 	case 'less':
     82 		str += 'less than ';
     83 		break;
     84 	case 'greater':
     85 		str += 'greater than ';
     86 		break;
     87 	}
     88 	str += this.nullValue;
     89 	str += '\n\n';
     90 	str += '    pValue: ' + roundn( this.pValue, -dgts ) + '\n';
     91 	str += '    statistic: ' + roundn( this.statistic, -dgts ) + '\n';
     92 	str += '\n';
     93 	if ( decision ) {
     94 		str += 'Test Decision: ';
     95 		if ( this.rejected ) {
     96 			str += 'Reject null in favor of alternative at ' + (this.alpha*100) + '% significance level';
     97 		} else {
     98 			str += 'Fail to reject null in favor of alternative at ' + (this.alpha*100) + '% significance level';
     99 		}
    100 		str += '\n';
    101 	}
    102 	return str;
    103 }
    104 
    105 
    106 // EXPORTS //
    107 
    108 module.exports = print;