main.js (3350B)
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 25 26 // MAIN // 27 28 /** 29 * Returns an accumulator function which incrementally computes a corrected sample excess kurtosis. 30 * 31 * ## Method 32 * 33 * The algorithm computes the sample excess kurtosis using the formula for `G_2` in [Joanes and Gill 1998][@joanes:1998]. In contrast to alternatives for calculating a sample kurtosis, `G_2` is an unbiased estimator under normality. 34 * 35 * ## References 36 * 37 * - 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]. 38 * 39 * [@joanes:1998]: http://dx.doi.org/10.1111/1467-9884.00122 40 * 41 * @returns {Function} accumulator function 42 * 43 * @example 44 * var accumulator = incrkurtosis(); 45 * 46 * var kurtosis = accumulator(); 47 * // returns null 48 * 49 * kurtosis = accumulator( 2.0 ); 50 * // returns null 51 * 52 * kurtosis = accumulator( 2.0 ); 53 * // returns null 54 * 55 * kurtosis = accumulator( -4.0 ); 56 * // returns null 57 * 58 * kurtosis = accumulator( -4.0 ); 59 * // returns -6.0 60 */ 61 function incrkurtosis() { 62 var deltaN2; 63 var deltaN; 64 var delta; 65 var term1; 66 var mean; 67 var tmp; 68 var g2; 69 var M2; 70 var M3; 71 var M4; 72 var N; 73 74 deltaN2 = 0.0; 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 M4 = 0.0; 82 N = 0; 83 84 return accumulator; 85 86 /** 87 * If provided a value, the accumulator function returns an updated corrected sample excess kurtosis. If not provided a value, the accumulator function returns the current corrected sample excess kurtosis. 88 * 89 * @private 90 * @param {number} [x] - new value 91 * @returns {(number|null)} corrected sample excess kurtosis 92 */ 93 function accumulator( x ) { 94 if ( arguments.length === 0 ) { 95 if ( N < 4 ) { 96 return ( isnan( M4 ) ) ? NaN : null; 97 } 98 // Calculate the population excess kurtosis: 99 g2 = (( N*M4 ) / ( M2*M2 )) - 3.0; 100 101 // Return the corrected sample excess kurtosis: 102 return (N-1) / ( (N-2)*(N-3) ) * ( ((N+1)*g2) + 6.0 ); 103 } 104 N += 1; 105 delta = x - mean; 106 deltaN = delta / N; 107 deltaN2 = deltaN * deltaN; 108 109 term1 = delta * deltaN * (N-1); 110 111 tmp = term1 * deltaN2 * ((N*N) - (3*N) + 3); 112 tmp += 6.0 * deltaN2 * M2; 113 tmp -= 4.0 * deltaN * M3; 114 M4 += tmp; 115 116 tmp = term1 * deltaN * (N-2); 117 tmp -= 3.0 * deltaN * M2; 118 M3 += tmp; 119 120 M2 += term1; 121 mean += deltaN; 122 if ( N < 4 ) { 123 return ( isnan( M4 ) ) ? NaN : null; 124 } 125 // Calculate the population excess kurtosis: 126 g2 = (N*M4 / ( M2*M2 )) - 3.0; 127 128 // Return the corrected sample excess kurtosis: 129 return (N-1) / ( (N-2)*(N-3) ) * ( ((N+1)*g2) + 6.0 ); 130 } 131 } 132 133 134 // EXPORTS // 135 136 module.exports = incrkurtosis;