main.js (3071B)
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 log1p = require( './../../../../base/special/log1p' ); 24 var abs = require( './../../../../base/special/abs' ); 25 var expm1 = require( './../../../../base/special/expm1' ); 26 var isnan = require( './../../../../base/assert/is-nan' ); 27 var NINF = require( '@stdlib/constants/float64/ninf' ); 28 29 30 // MAIN // 31 32 /** 33 * Computes a one-parameter Box-Cox transformation of `1+x`. 34 * 35 * ## Method 36 * 37 * When computing a one-parameter Box-Cox transformation 38 * 39 * - If \\( \lambda << 1 \\) and \\( \ln( x ) < 1.0 \\), then the product \\( \lambda \cdot \ln(x) \\) can lose precision, and, furthermore, \\( \operatorname{expm1}(x) = x \\) for \\( x < \epsilon \\). 40 * - For double-precision floating-point numbers, the range of the natural log is \\( \[-744.44, 709.78\] and \\( \epsilon \\) is the smallest value produced. 41 * - The value range means that we will have \\( |\lambda \cdot \ln(x)| < \epsilon \\) whenever \\( |\lambda| \leq \frac{\epsilon}{-\ln(d) \\), where \\( d \\) is the minimum double-precision floating-point number, thus corresponding to the value \\( \approx 2.98 \times 10^{-19} \\). 42 * 43 * For small `x` values, the same method described above applies with the modification that the smallest value returned by \\( \operatorname{log1p}(x) \\) is the minimum representable value, not \\( \epsilon \\). Furthermore, we need to guard against underflow when \\( \operatorname{log1p}(x) < \epsilon \\). 44 * 45 * @param {number} x - input value 46 * @param {number} lambda - power parameter 47 * @returns {number} Box-Cox transformation of `1+x` 48 * 49 * @example 50 * var v = boxcox1p( 1.0, 2.5 ); 51 * // returns ~1.8627 52 * 53 * @example 54 * var v = boxcox1p( 4.0, 2.5 ); 55 * // returns ~21.9607 56 * 57 * @example 58 * var v = boxcox1p( 10.0, 2.5 ); 59 * // returns ~160.1246 60 * 61 * @example 62 * var v = boxcox1p( 2.0, 0.0 ); 63 * // returns ~1.0986 64 * 65 * @example 66 * var v = boxcox1p( -1.0, 2.5 ); 67 * // returns -0.4 68 * 69 * @example 70 * var v = boxcox1p( 0.0, -1.0 ); 71 * // returns 0.0 72 * 73 * @example 74 * var v = boxcox1p( -1.0, -1.0 ); 75 * // returns -Infinity 76 */ 77 function boxcox1p( x, lambda ) { 78 var lgx; 79 if ( isnan( x ) || isnan( lambda ) || x < -1.0 ) { 80 return NaN; 81 } 82 if ( x === -1.0 && lambda < 0.0 ) { 83 return NINF; 84 } 85 lgx = log1p( x ); 86 if ( 87 abs( lambda ) < 1.0e-19 || 88 89 // Guard against underflow... 90 ( 91 abs( lgx ) < 1.0e-289 && 92 abs( lambda ) < 1.0e273 93 ) 94 ) { 95 return lgx; 96 } 97 return expm1( lambda*lgx ) / lambda; 98 } 99 100 101 // EXPORTS // 102 103 module.exports = boxcox1p;