smirnov.js (1661B)
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 binomcoefln = require( '@stdlib/math/base/special/binomcoefln' ); 24 var floor = require( '@stdlib/math/base/special/floor' ); 25 var exp = require( '@stdlib/math/base/special/exp' ); 26 var ln = require( '@stdlib/math/base/special/ln' ); 27 28 29 // MAIN // 30 31 /** 32 * Evaluates the CDF for the one-sided test statistics, i.e., the maximum by which the empirical CDF exceeds / is less than the hypothesized CDF. 33 * 34 * @private 35 * @param {number} d - the argument of the CDF of D_n^+ / D_n^- 36 * @param {PositiveInteger} n - number of variates 37 * @returns {number} evaluated CDF, i.e., P( D_n^+ < d ) 38 */ 39 function pKolmogorov1( d, n ) { 40 var len; 41 var out; 42 var tmp; 43 var i; 44 45 if ( d <= 0.0 ) { 46 return 0.0; 47 } 48 if ( d >= 1.0 ) { 49 return 1.0; 50 } 51 len = floor( n * (1.0-d) ) + 1; 52 out = 0.0; 53 for ( i = 0; i < len; i++ ) { 54 tmp = binomcoefln( n, i ); 55 tmp += ( n - i ) * ln( 1.0 - d - (i/n) ); 56 tmp += ( i - 1.0 ) * ln( d + (i/n) ); 57 out += exp( tmp ); 58 } 59 return 1.0 - (d * out); 60 } 61 62 63 // EXPORTS // 64 65 module.exports = pKolmogorov1;