time-to-botec

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

print.js (4994B)


      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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' );
     24 var isObject = require( '@stdlib/assert/is-plain-object' );
     25 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     26 var roundn = require( '@stdlib/math/base/special/roundn' );
     27 var repeat = require( '@stdlib/string/repeat' );
     28 var max = require( '@stdlib/math/base/special/max' );
     29 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     30 
     31 
     32 // FUNCTIONS //
     33 
     34 /**
     35 * Returns n spaces.
     36 *
     37 * @private
     38 * @param {integer} n - number of spaces
     39 * @returns {string} n spaces
     40 */
     41 function spaces( n ) {
     42 	if ( n <= 0 ) {
     43 		return '';
     44 	}
     45 	return repeat( ' ', n );
     46 }
     47 
     48 
     49 // MAIN //
     50 
     51 /**
     52 * Returns a function to pretty print test results.
     53 *
     54 * @private
     55 * @param {Object} results - test results
     56 * @returns {Function} pretty print function
     57 */
     58 function prettyPrint( results ) {
     59 	return print;
     60 
     61 	/**
     62 	* Pretty-print output of ANOVA.
     63 	*
     64 	* @private
     65 	* @param {Object} [opts] - options object
     66 	* @param {PositiveInteger} [opts.digits=4] - number of digits after the decimal point
     67 	* @param {boolean} [opts.decision=true] - boolean indicating whether to print the test decision
     68 	* @throws {TypeError} options argument must be an object
     69 	* @throws {TypeError} must provide valid options
     70 	* @returns {string} formatted results
     71 	*/
     72 	function print( opts ) {
     73 		var statistic;
     74 		var decision;
     75 		var treatMS;
     76 		var treatDF;
     77 		var treatSS;
     78 		var extra1;
     79 		var extra2;
     80 		var extra3;
     81 		var errMS;
     82 		var errDF;
     83 		var errSS;
     84 		var ndgts;
     85 		var dgts;
     86 		var str;
     87 
     88 		dgts = 4;
     89 		decision = true;
     90 		if ( arguments.length > 0 ) {
     91 			if ( !isObject( opts ) ) {
     92 				throw new TypeError( 'invalid argument. First argument must be an options object. Value: `' + opts + '`.' );
     93 			}
     94 			if ( hasOwnProp( opts, 'digits' ) ) {
     95 				if ( !isPositiveInteger( opts.digits ) ) {
     96 					throw new TypeError( 'invalid option. `digits` option must be a positive integer. Option: `' + opts.digits + '`.' );
     97 				}
     98 				dgts = opts.digits;
     99 			}
    100 			if ( hasOwnProp( opts, 'decision' ) ) {
    101 				if ( !isBoolean( opts.decision ) ) {
    102 					throw new TypeError( 'invalid option. `decision` option must be a boolean primitive. Option: `' + opts.decision + '`.' );
    103 				}
    104 				decision = opts.decision;
    105 			}
    106 		}
    107 		ndgts = -dgts;
    108 
    109 		str = '';
    110 		str += results.method;
    111 		str += '\n\n';
    112 
    113 		// Hypothesis
    114 		str += 'Null Hypothesis: All Means Equal';
    115 		str += '\n';
    116 		str += 'Alternate Hypothesis: At Least one Mean not Equal';
    117 		str += '\n\n';
    118 
    119 		treatSS = roundn( results.treatment.ss, ndgts ).toString();
    120 		errSS = roundn( results.error.ss, ndgts ).toString();
    121 		treatMS = roundn( results.treatment.ms, ndgts ).toString();
    122 		errMS = roundn( results.error.ms, ndgts ).toString();
    123 		treatDF = results.treatment.df.toString();
    124 		errDF = results.error.df.toString();
    125 		statistic = roundn( results.statistic, ndgts ).toString();
    126 
    127 		extra1 = max( max( treatDF.length, errDF.length ), 2 );
    128 		extra2 = max( max( treatSS.length, errSS.length ), 2 );
    129 		extra3 = max( max( treatMS.length, errMS.length ), 3 );
    130 
    131 		// Formatted table
    132 		str += '              ';
    133 		str += 'df';
    134 		str += spaces( 1 + extra1 );
    135 		str += 'SS';
    136 		str += spaces( 2 + extra2 );
    137 		str += 'MS';
    138 		str += spaces( 1 + extra3 );
    139 		str += 'F Score';
    140 		str += spaces( max( 7, statistic.length ) - 7 + 2 );
    141 		str += 'P Value';
    142 		str += '\n';
    143 
    144 		// Now start adding in values
    145 		str += 'Treatment';
    146 		str += spaces( 5 );
    147 		str += results.treatment.df;
    148 		str += spaces( 3 + extra1 - treatDF.length );
    149 
    150 		str += treatSS;
    151 		str += spaces( 4 + extra2 - treatSS.length );
    152 		str += treatMS;
    153 		str += spaces( 3 + extra3 - treatMS.length );
    154 		str += statistic;
    155 		str += spaces( max( 7, statistic.length ) - statistic.length + 2 );
    156 		str += roundn( results.pValue, ndgts );
    157 		str += '\n';
    158 
    159 		// Next line
    160 		str += 'Errors';
    161 		str += '        ';
    162 		str += results.error.df;
    163 		str += spaces( 3 + extra1 - errDF.length );
    164 		str += errSS;
    165 		str += spaces( 4 + extra2 - errSS.length );
    166 		str += errMS;
    167 
    168 		if ( decision ) {
    169 			str += '\n\n';
    170 			if ( results.rejected ) {
    171 				str += 'Reject Null: ';
    172 				str += roundn( results.pValue, ndgts );
    173 				str += ' <= ';
    174 				str += results.alpha;
    175 			} else {
    176 				str += 'Fail to Reject Null: ';
    177 				str += roundn( results.pValue, ndgts );
    178 				str += ' >= ';
    179 				str += results.alpha;
    180 			}
    181 		}
    182 		return str;
    183 	}
    184 }
    185 
    186 
    187 // EXPORTS //
    188 
    189 module.exports = prettyPrint;