main.js (5695B)
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 isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); 24 var isNumber = require( '@stdlib/assert/is-number' ); 25 var isNumberArray = require( '@stdlib/assert/is-number-array' ).primitives; 26 var setReadOnly = require( '@stdlib/utils/define-read-only-property' ); 27 var isFunction = require( '@stdlib/assert/is-function' ); 28 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 29 var isnan = require( '@stdlib/assert/is-nan' ); 30 var max = require( './../../base/max' ); 31 var pKolmogorov1 = require( './smirnov.js' ); 32 var pKolmogorov = require( './marsaglia.js' ); 33 var ascending = require( './ascending.js' ); 34 var subtract = require( './subtract.js' ); 35 var validate = require( './validate.js' ); 36 var getCDF = require( './get_cdf.js' ); 37 var print = require( './print.js' ); // eslint-disable-line stdlib/no-redeclare 38 39 40 // FUNCTIONS // 41 42 var slice = Array.prototype.slice; 43 44 45 // MAIN // 46 47 /** 48 * Computes a Kolmogorov-Smirnov goodness-of-fit test. 49 * 50 * @param {NumericArray} x - input array holding numeric values 51 * @param {(Function|string)} y - either a CDF function or a string denoting the name of a distribution 52 * @param {...number} [params] - distribution parameters passed to reference CDF 53 * @param {Options} [options] - function options 54 * @param {number} [options.alpha=0.05] - significance level 55 * @param {boolean} [options.sorted=false] - boolean indicating if the input array is already in sorted order 56 * @param {string} [options.alternative="two-sided"] - string indicating whether to conduct two-sided or one-sided hypothesis test (other options: `less`, `greater`) 57 * @throws {TypeError} argument x has to be a typed array or array of numbers 58 * @throws {TypeError} argument y has to be a CDF function or string 59 * @throws {TypeError} options has to be simple object 60 * @throws {TypeError} alpha option has to be a number primitive 61 * @throws {RangeError} alpha option has to be a number in the interval `[0,1]` 62 * @throws {TypeError} alternative option has to be a string primitive 63 * @throws {Error} alternative option must be `two-sided`, `less` or `greater` 64 * @throws {TypeError} sorted option has to be a boolean primitive 65 * @returns {Object} test result object 66 * 67 * @example 68 * var out = kstest( [ 2.0, 1.0, 5.0, -5.0, 3.0, 0.5, 6.0 ], 'normal', 0.0, 1.0 ); 69 * // returns { 'pValue': ~0.015, 'statistic': ~0.556, ... } 70 */ 71 function kstest() { 72 var nDistParams; 73 var distParams; 74 var distArgs; 75 var options; 76 var alpha; 77 var opts; 78 var pval; 79 var stat; 80 var yVal; 81 var alt; 82 var err; 83 var idx; 84 var out; 85 var val; 86 var i; 87 var n; 88 var x; 89 var y; 90 91 x = arguments[ 0 ]; 92 y = arguments[ 1 ]; 93 if ( !isNumberArray( x ) && !isTypedArrayLike( x ) ) { 94 throw new TypeError( 'invalid argument. First argument must be a typed array or number array. Value: `' + x + '`.' ); 95 } 96 if ( !isFunction( y ) && !isString( y ) ) { 97 throw new TypeError( 'invalid argument. Second argument must be either a CDF function or a string primitive. Value: `' + y + '`' ); 98 } 99 if ( isString( y ) ) { 100 y = getCDF( y ); 101 } 102 nDistParams = y.length - 1.0; 103 n = x.length; 104 105 distParams = new Array( nDistParams ); 106 for ( i = 0; i < nDistParams; i++ ) { 107 idx = i + 2; 108 val = arguments[ idx ]; 109 if ( !isNumber( val ) || isnan( val ) ) { 110 throw new TypeError( 'invalid argument. Distribution parameter must be a number primitive. Value: `' + val + '`.' ); 111 } 112 distParams[ i ] = arguments[ idx ]; 113 } 114 opts = {}; 115 if ( arguments.length > 2 + nDistParams ) { 116 options = arguments[ 2 + nDistParams ]; 117 err = validate( opts, options ); 118 if ( err ) { 119 throw err; 120 } 121 } 122 // Make a copy to prevent mutation of x: 123 x = slice.call( x ); 124 125 if ( opts.alpha === void 0 ) { 126 alpha = 0.05; 127 } else { 128 alpha = opts.alpha; 129 } 130 if ( alpha < 0.0 || alpha > 1.0 ) { 131 throw new RangeError( 'invalid argument. Option `alpha` must be a number in the range 0 to 1. Value: `' + alpha + '`.' ); 132 } 133 134 // Input data has to be sorted: 135 if ( opts.sorted !== true ) { 136 x.sort( ascending ); 137 } 138 distArgs = [ null ].concat( distParams ); 139 for ( i = 0; i < n; i++ ) { 140 distArgs[ 0 ] = x[ i ]; 141 yVal = y.apply( null, distArgs ); 142 x[ i ] = yVal - ( i / n ); 143 } 144 145 alt = opts.alternative || 'two-sided'; 146 switch ( alt ) { 147 case 'two-sided': 148 stat = max( n, [ max( n, x, 1 ), max( n, subtract( 1/n, x ), 1 ) ], 1 ); 149 break; 150 case 'greater': 151 stat = max( n, subtract( 1/n, x ), 1 ); 152 break; 153 case 'less': 154 stat = max( n, x, 1 ); 155 break; 156 default: 157 throw new Error( 'Invalid option. `alternative` must be either `two-sided`, `less` or `greater`. Value: `' + alt + '`' ); 158 } 159 if ( alt === 'two-sided' ) { 160 pval = 1.0 - pKolmogorov( stat, n ); 161 } else { 162 pval = 1.0 - pKolmogorov1( stat, n ); 163 } 164 165 out = {}; 166 setReadOnly( out, 'rejected', pval <= alpha ); 167 setReadOnly( out, 'alpha', alpha ); 168 setReadOnly( out, 'pValue', pval ); 169 setReadOnly( out, 'statistic', stat ); 170 setReadOnly( out, 'method', 'Kolmogorov-Smirnov goodness-of-fit test' ); 171 setReadOnly( out, 'print', print ); 172 setReadOnly( out, 'alternative', alt ); 173 return out; 174 } 175 176 177 // EXPORTS // 178 179 module.exports = kstest;