logcdf.js (1876B)
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 isnan = require( '@stdlib/math/base/assert/is-nan' ); 24 var atan2 = require( '@stdlib/math/base/special/atan2' ); 25 var ln = require( '@stdlib/math/base/special/ln' ); 26 27 28 // VARIABLES // 29 30 var ONE_OVER_PI = 0.3183098861837907; 31 32 33 // MAIN // 34 35 /** 36 * Evaluates the natural logarithm of the cumulative distribution function (logCDF) for a Cauchy distribution with location parameter `x0` and scale parameter `gamma` at a value `x`. 37 * 38 * @param {number} x - input value 39 * @param {number} x0 - location parameter 40 * @param {PositiveNumber} gamma - scale parameter 41 * @returns {number} evaluated logCDF 42 * 43 * @example 44 * var y = logcdf( 4.0, 0.0, 2.0 ); 45 * // returns ~-0.16 46 * 47 * @example 48 * var y = logcdf( 1.0, 0.0, 2.0 ); 49 * // returns ~-0.435 50 * 51 * @example 52 * var y = logcdf( 1.0, 3.0, 2.0 ); 53 * // returns ~-1.386 54 * 55 * @example 56 * var y = logcdf( NaN, 0.0, 2.0 ); 57 * // returns NaN 58 * 59 * @example 60 * var y = logcdf( 1.0, 2.0, NaN ); 61 * // returns NaN 62 * 63 * @example 64 * var y = logcdf( 1.0, NaN, 3.0 ); 65 * // returns NaN 66 */ 67 function logcdf( x, x0, gamma ) { 68 if ( 69 isnan( x ) || 70 isnan( gamma ) || 71 isnan( x0 ) || 72 gamma <= 0.0 73 ) { 74 return NaN; 75 } 76 return ln( ( ONE_OVER_PI * atan2( x-x0, gamma ) ) + 0.5 ); 77 } 78 79 80 // EXPORTS // 81 82 module.exports = logcdf;