pick_bandwidth.js (1890B)
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 pow = require( '@stdlib/math/base/special/pow' ); 24 var min = require( '@stdlib/math/base/special/min' ); 25 var EPSILON = require( '@stdlib/constants/float64/eps' ); 26 var stdev = require( './stdev.js' ); 27 var iqr = require( './iqr.js' ); 28 29 30 // MAIN // 31 32 /** 33 * Computes the rule-of-thumb bandwidth for the values in a column of `x`. 34 * 35 * @private 36 * @param {ndarrayLike} arr - input ndarray 37 * @param {number} j - index of the column from which to obtain the values 38 * @returns {NumericArray} array with bandwidth values 39 * 40 * @example 41 * var ndarrayLike = require( './ndarray_like.js' ); 42 * 43 * var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571, 1.7881, 2.019, 2.25, 2.481, 2.7119 ]; 44 * var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454, 4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ]; 45 * var arr = ndarrayLike( x, y ); 46 * var out = pickBandwidth( arr, 1 ); 47 * // returns ~1.717 48 */ 49 function pickBandwidth( arr, j ) { 50 var minElement; 51 var sigmaHat; 52 var powTerm; 53 var iqrVal; 54 var out; 55 56 iqrVal = iqr( arr, j ) / 1.34; 57 sigmaHat = stdev( arr, j ); 58 minElement = min( iqrVal, sigmaHat ); 59 powTerm = pow( arr.shape[ 0 ], -1/5 ); 60 out = 1.06 * powTerm * minElement; 61 if ( out === 0 ) { 62 out = EPSILON; 63 } 64 return out; 65 } 66 67 68 // EXPORTS // 69 70 module.exports = pickBandwidth;