print.js (3088B)
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 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 68 str = ''; 69 str += this.method; 70 str += '\n\n'; 71 str += 'Null hypothesis: the CDF of `x` is '; 72 switch ( this.alternative ) { 73 case 'two-sided': 74 default: 75 str += 'equal to '; 76 break; 77 case 'less': 78 str += 'greater than or equal to '; 79 break; 80 case 'greater': 81 str += 'less than or equal to '; 82 break; 83 } 84 str += 'the reference CDF'; 85 str += '\n\n'; 86 str += ' pValue: ' + roundn( this.pValue, -dgts ) + '\n'; 87 str += ' statistic: ' + roundn( this.statistic, -dgts ); 88 str += '\n\n'; 89 if ( decision ) { 90 str += 'Test Decision: '; 91 if ( this.rejected ) { 92 str += 'Reject null in favor of alternative at ' + (this.alpha*100) + '% significance level'; 93 } else { 94 str += 'Fail to reject null in favor of alternative at ' + (this.alpha*100) + '% significance level'; 95 } 96 str += '\n'; 97 } 98 return str; 99 } 100 101 102 // EXPORTS // 103 104 module.exports = print;