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 128 129 130 131 132 133 134 135 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @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 sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
var max = require( '@stdlib/math/base/special/max' );
var pow = require( '@stdlib/math/base/special/pow' );
// MAIN //
/**
* Calculates the fitted value `ys` for a value `xs` on the horizontal axis.
*
* ## Method
*
* - The smoothed value for the x-axis value at the current index is computed using a (robust) locally weighted regression of degree one. The tricube weight function is used with `h` equal to the maximum of `xs - x[ nleft ]` and `x[ nright ] - xs`.
*
* ## References
*
* - Cleveland, William S. 1979. "Robust Locally and Smoothing Weighted Regression Scatterplots." _Journal of the American Statistical Association_ 74 (368): 829–36. doi:[10.1080/01621459.1979.10481038](https://doi.org/10.1080/01621459.1979.10481038).
* - Cleveland, William S. 1981. "Lowess: A program for smoothing scatterplots by robust locally weighted regression." _American Statistician_ 35 (1): 54–55. doi:[10.2307/2683591](https://doi.org/10.2307/2683591).
*
* @private
* @param {NumericArray} x - ordered x-axis values (abscissa values)
* @param {NumericArray} y - corresponding y-axis values (ordinate values)
* @param {PositiveInteger} n - number of observations
* @param {NonNegativeInteger} i - current index
* @param {NonNegativeInteger} nleft - index of the first point used in computing the fitted value
* @param {NonNegativeInteger} nright - index of the last point used in computing the fitted value
* @param {ProbabilityArray} w - weights at indices from `nleft` to `nright` to be used in the calculation of the fitted value
* @param {boolean} userw - boolean indicating whether a robust fit is carried out using the weights in `rw`
* @param {ProbabilityArray} rw - robustness weights
* @returns {number} fitted value
*/
function lowest( x, y, n, i, nleft, nright, w, userw, rw ) {
var range;
var nrt;
var h1;
var h9;
var xs;
var ys;
var h;
var a;
var b;
var c;
var r;
var j;
xs = x[ i ];
range = x[ n - 1 ] - x[ 0 ];
h = max( xs - x[ nleft ], x[ nright ] - xs );
h9 = 0.999 * h;
h1 = 0.001 * h;
// Compute weights (pick up all ties on right):
a = 0.0; // sum of weights
for ( j = nleft; j < n; j++ ) {
w[ j ] = 0.0;
r = abs( x[ j ] - xs );
if ( r <= h9 ) { // small enough for non-zero weight
if ( r > h1 ) {
w[ j ] = pow( 1.0-pow( r/h, 3.0 ), 3.0 );
} else {
w[ j ] = 1.0;
}
if ( userw ) {
w[ j ] *= rw[ j ];
}
a += w[ j ];
}
else if ( x[ j ] > xs ) {
break; // get out at first zero weight on right
}
}
nrt = j - 1; // rightmost point (may be greater than `nright` because of ties)
if ( a <= 0.0 ) {
return y[ i ];
}
// Make sum of weights equal to one:
for ( j = nleft; j <= nrt; j++ ) {
w[ j ] /= a;
}
if ( h > 0.0 ) { // use linear fit
// Find weighted center of x values:
a = 0.0;
for ( j = nleft; j <= nrt; j++ ) {
a += w[ j ] * x[ j ];
}
b = xs - a;
c = 0.0;
for ( j = nleft; j <= nrt; j++ ) {
c += w[ j ] * pow( x[ j ] - a, 2.0 );
}
if ( sqrt( c ) > 0.001 * range ) {
// Points are spread out enough to compute slope:
b /= c;
for ( j = nleft; j <= nrt; j++ ) {
w[ j ] *= ( 1.0 + ( b*(x[j]-a) ) );
}
}
}
ys = 0.0;
for ( j = nleft; j <= nrt; j++ ) {
ys += w[ j ] * y[ j ];
}
return ys;
}
// EXPORTS //
module.exports = lowest;
|