logcdf.js (2335B)
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 expm1 = require( '@stdlib/math/base/special/expm1' ); 25 var log1p = require( '@stdlib/math/base/special/log1p' ); 26 var exp = require( '@stdlib/math/base/special/exp' ); 27 var pow = require( '@stdlib/math/base/special/pow' ); 28 var ln = require( '@stdlib/math/base/special/ln' ); 29 var LNHALF = require( '@stdlib/constants/float64/ln-half' ); 30 var NINF = require( '@stdlib/constants/float64/ninf' ); 31 32 33 // MAIN // 34 35 /** 36 * Evaluates the natural logarithm of the cumulative distribution function (CDF) for a Weibull distribution with scale parameter `k` and shape parameter `lambda` at a value `x`. 37 * 38 * @param {number} x - input value 39 * @param {PositiveNumber} k - scale parameter 40 * @param {PositiveNumber} lambda - shape parameter 41 * @returns {number} natural logarithm of CDF 42 * 43 * @example 44 * var y = logcdf( 2.0, 1.0, 1.0 ); 45 * // returns ~-0.145 46 * 47 * @example 48 * var y = logcdf( -1.0, 2.0, 2.0 ); 49 * // returns -Infinity 50 * 51 * @example 52 * var y = logcdf( +Infinity, 4.0, 2.0 ); 53 * // returns 0.0 54 * 55 * @example 56 * var y = logcdf( -Infinity, 4.0, 2.0 ); 57 * // returns -Infinity 58 * 59 * @example 60 * var y = logcdf( NaN, 0.0, 1.0 ); 61 * // returns NaN 62 * 63 * @example 64 * var y = logcdf( 0.0, NaN, 1.0 ); 65 * // returns NaN 66 * 67 * @example 68 * var y = logcdf( 0.0, 0.0, NaN ); 69 * // returns NaN 70 * 71 * @example 72 * var y = logcdf( 2.0, 0.0, -1.0 ); 73 * // returns NaN 74 */ 75 function logcdf( x, k, lambda ) { 76 var p; 77 if ( 78 isnan( k ) || 79 isnan( lambda ) || 80 k <= 0.0 || 81 lambda <= 0.0 82 ) { 83 return NaN; 84 } 85 if ( x < 0.0 ) { 86 return NINF; 87 } 88 p = -pow( x / lambda, k ); 89 return ( p < LNHALF ) ? log1p( -exp( p ) ) : ln( -expm1( p ) ); 90 } 91 92 93 // EXPORTS // 94 95 module.exports = logcdf;