main.js (3074B)
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 sqrt = require( '@stdlib/math/base/special/sqrt' ); 25 var pow = require( '@stdlib/math/base/special/pow' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an accumulator function which incrementally computes a corrected sample skewness. 32 * 33 * ## Method 34 * 35 * The algorithm computes the corrected sample skewness using the formula for `G_1` in [Joanes and Gill 1998][@joanes:1998]. 36 * 37 * ## References 38 * 39 * - Joanes, D. N., and C. A. Gill. 1998. "Comparing measures of sample skewness and kurtosis." _Journal of the Royal Statistical Society: Series D (The Statistician)_ 47 (1). Blackwell Publishers Ltd: 183–89. doi:[10.1111/1467-9884.00122][@joanes:1998]. 40 * 41 * [@joanes:1998]: http://dx.doi.org/10.1111/1467-9884.00122 42 * 43 * 44 * @returns {Function} accumulator function 45 * 46 * @example 47 * var accumulator = incrskewness(); 48 * 49 * var skewness = accumulator(); 50 * // returns null 51 * 52 * skewness = accumulator( 2.0 ); 53 * // returns null 54 * 55 * skewness = accumulator( -5.0 ); 56 * // returns null 57 * 58 * skewness = accumulator( -10.0 ); 59 * // returns ~0.492 60 * 61 * skewness = accumulator(); 62 * // returns ~0.492 63 */ 64 function incrskewness() { 65 var deltaN; 66 var delta; 67 var term1; 68 var mean; 69 var tmp; 70 var g1; 71 var M2; 72 var M3; 73 var N; 74 75 deltaN = 0.0; 76 delta = 0.0; 77 term1 = 0.0; 78 mean = 0.0; 79 M2 = 0.0; 80 M3 = 0.0; 81 N = 0; 82 83 return accumulator; 84 85 /** 86 * If provided a value, the accumulator function returns an updated corrected sample skewness. If not provided a value, the accumulator function returns the current corrected sample skewness. 87 * 88 * @private 89 * @param {number} [x] - new value 90 * @returns {(number|null)} corrected sample skewness or null 91 */ 92 function accumulator( x ) { 93 if ( arguments.length === 0 ) { 94 if ( N < 3 ) { 95 return ( isnan( M3 ) ) ? NaN : null; 96 } 97 // Calculate the population skewness: 98 g1 = sqrt( N )*M3 / pow( M2, 1.5 ); 99 100 // Return the corrected sample skewness: 101 return sqrt( N*(N-1) )*g1 / (N-2); 102 } 103 N += 1; 104 delta = x - mean; 105 deltaN = delta / N; 106 term1 = delta * deltaN * (N-1); 107 108 tmp = term1 * deltaN * (N-2); 109 tmp -= 3.0 * deltaN * M2; 110 M3 += tmp; 111 112 M2 += term1; 113 mean += deltaN; 114 if ( N < 3 ) { 115 return ( isnan( M3 ) ) ? NaN : null; 116 } 117 // Calculate the population skewness: 118 g1 = sqrt( N )*M3 / pow( M2, 1.5 ); 119 120 // Return the corrected sample skewness: 121 return sqrt( N*(N-1) )*g1 / (N-2); 122 } 123 } 124 125 126 // EXPORTS // 127 128 module.exports = incrskewness;