factory.js (2166B)
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/math/base/assert/is-positive-integer' ); 24 var constantFunction = require( '@stdlib/utils/constant-function' ); 25 var isfinite = require( '@stdlib/math/base/assert/is-finite' ); 26 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 27 var exp = require( '@stdlib/math/base/special/exp' ); 28 var ln = require( '@stdlib/math/base/special/ln' ); 29 var LN2 = require( '@stdlib/constants/float64/ln-two' ); 30 var weights = require( './weights.js' ); 31 32 33 // MAIN // 34 35 /** 36 * Returns a function for evaluating the probability density function (PDF) for the distribution of the Wilcoxon signed rank test statistic with `n` observations. 37 * 38 * @param {PositiveInteger} n - number of observations 39 * @returns {Function} PDF 40 * 41 * @example 42 * var pdf = factory( 8 ); 43 * var y = pdf( 4.0 ); 44 * // returns ~0.008 45 * 46 * y = pdf( 17.0 ); 47 * // returns ~0.051 48 */ 49 function factory( n ) { 50 var mlim; 51 if ( !isPositiveInteger( n ) || !isfinite( n ) ) { 52 return constantFunction( NaN ); 53 } 54 mlim = n * ( n + 1 ) / 2; 55 return pdf; 56 57 /** 58 * Evaluates the probability density function (PDF) for the distribution of the Wilcoxon signed rank test statistic. 59 * 60 * @private 61 * @param {number} x - input value 62 * @returns {Probability} evaluated PDF 63 * 64 * @example 65 * var y = pdf( 2 ); 66 * // returns <number> 67 */ 68 function pdf( x ) { 69 if ( isnan( x ) ) { 70 return NaN; 71 } 72 if ( x < 0.0 || x > mlim ) { 73 return 0.0; 74 } 75 return exp( ln( weights( x, n ) ) - ( n * LN2 ) ); 76 } 77 } 78 79 80 // EXPORTS // 81 82 module.exports = factory;