Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1022x 1022x 1022x 1022x 1022x 1022x 1022x 1022x 1022x 1017x 1022x 7x 7x 1022x 463x 463x 552x 1022x 34x 34x 34x 1022x 12x 12x 12x 506x 506x 1022x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 46x 46x 46x 46x 2x 2x 2x 2x 2x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var exp = require( '@stdlib/math/base/special/exp' );
var abs2 = require( '@stdlib/math/base/special/abs2' );
var erfcx = require( '@stdlib/math/base/special/erfcx' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var normal = require( '@stdlib/stats/base/dists/normal/cdf' ).factory;
var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' );
// VARIABLES //
var normalCDF = normal( 0.0, 1.0 );
var SQRT_HALF = sqrt( 2.0 ) / 2.0;
// MAIN //
/**
* Evaluates the probability density function (PDF) for a truncated normal distribution with endpoints `a` and `b`, location parameter `mu` and scale parameter `sigma` at a value `x`.
*
* @param {number} x - input value
* @param {number} a - minimum support
* @param {number} b - maximum support
* @param {number} mu - location parameter
* @param {PositiveNumber} sigma - scale parameter
* @returns {number} evaluated PDF
*
* @example
* var y = pdf( 0.9, 0.0, 1.0, 0.0, 1.0 );
* // returns ~0.7795
*
* @example
* var y = pdf( 0.9, 0.0, 1.0, 0.5, 1.0 );
* // returns ~0.9617
*
* @example
* var y = pdf( 0.9, -1.0, 1.0, 0.5, 1.0 );
* // returns ~0.5896
*
* @example
* var y = pdf( 1.4, 0.0, 1.0, 0.0, 1.0 );
* // returns 0.0
*
* @example
* var y = pdf( -0.9, 0.0, 1.0, 0.0, 1.0 );
* // returns 0.0
*/
function pdf( x, a, b, mu, sigma ) {
var A;
var C;
if (
isnan( x ) ||
isnan( a ) ||
isnan( b ) ||
sigma <= 0.0 ||
a >= b
) {
return NaN;
}
if ( x < a || x > b ) {
return 0.0;
}
A = 1.0 / ( sigma * SQRT_TWO_PI );
if ( a >= mu ) {
// Both endpoints lie in the upper tail:
return A * tailPDF( ( x - mu ) / sigma, ( a - mu ) / sigma, ( b - mu ) / sigma );
}
if ( b <= mu ) {
// Both endpoints lie in the lower tail (mirror the upper tail):
return A * tailPDF( ( mu - x ) / sigma, ( mu - b ) / sigma, ( mu - a ) / sigma );
}
C = normalCDF( (b-mu)/sigma ) - normalCDF( (a-mu)/sigma );
return A * exp( -0.5 * abs2( ( x - mu ) / sigma ) ) / C;
}
/**
* Evaluates the rescaled PDF for the case where both endpoints lie on the same side of `mu`.
*
* ## Notes
*
* - Let `ta` and `tb` (`ta <= tb`) be the standardized distances of the endpoints from `mu`, and `z` (`ta <= z <= tb`) the standardized distance of `x`. The normalizer is `C = Q(ta) - Q(tb)`, where `Q(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))` is the survival function of the standard normal distribution. Dividing the numerator and the normalizer by `exp(-ta^2/2)` yields
*
* ```text
* tailPDF = exp( 0.5*(ta^2 - z^2) ) / ( 0.5 * ( erfcx(ta/sqrt(2)) - erfcx(tb/sqrt(2)) * exp( 0.5*(ta^2 - tb^2) ) ) )
* ```
*
* which avoids the cancellation in `Phi(tb) - Phi(ta)` when both endpoints lie in the upper tail.
*
* @private
* @param {number} z - standardized value of `x`
* @param {number} ta - smaller standardized endpoint
* @param {number} tb - larger standardized endpoint
* @returns {number} evaluated rescaled PDF
*/
function tailPDF( z, ta, tb ) {
var expta = exp( 0.5 * ( ( ta*ta ) - ( tb*tb ) ) );
return exp( 0.5 * ( ( ta*ta ) - ( z*z ) ) ) / ( 0.5 * ( erfcx( SQRT_HALF * ta ) - ( erfcx( SQRT_HALF * tb ) * expta ) ) );
}
// EXPORTS //
module.exports = pdf;
|