time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

cdf.js (1764B)


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