All files main.js

98.71% Statements 308/312
93.54% Branches 29/31
100% Functions 3/3
98.71% Lines 308/312

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 3131x 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 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 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 12x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 67x 67x 67x 2x     2x 2x 1x 1x 1x 1x 1x 65x 65x 65x 65x 67x 41x 41x 41x 41x 2x 2x 41x 6x 6x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 41x 26x 26x 9x 9x 9x 24x 24x 24x 24x 24x 24x 67x 2x 2x 67x     24x 24x 24x 67x 2x 67x 2x 2x 22x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 22x 22x 67x 7x 7x 15x 15x 67x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 23x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var Float64Array = require( '@stdlib/array/float64' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Returns an accumulator function which incrementally computes a moving corrected sample excess kurtosis.
*
* ## Method
*
* The algorithm maintains running central moment accumulators `M2`, `M3`,
* and `M4` using Welford's online algorithm. When the window is full, each
* new datum triggers an O(1) downdate (removing the oldest value) followed
* by an O(1) update (adding the newest value).
*
* ### Welford update (adding value `x`, going from `n-1` to `n` values)
*
* ```text
* delta  = x - mean_prev
* deltaN = delta / n
* term1  = delta * deltaN * (n - 1)
* M4    += term1 * deltaN^2 * (n^2 - 3n + 3) + 6*deltaN^2*M2 - 4*deltaN*M3
* M3    += term1 * deltaN * (n - 2) - 3*deltaN*M2
* M2    += term1
* mean  += deltaN
* ```
*
* ### Welford downdate (removing value `xo`, going from `n` to `n-1` values)
*
* ```text
* mean_new = (n*mean - xo) / (n - 1)
* delta    = xo - mean_new
* deltaN   = delta / n
* term1    = delta * deltaN * (n - 1)
* M2_new   = M2 - term1
* M3_new   = M3 - term1*deltaN*(n-2) + 3*deltaN*M2_new
* M4_new   = M4 - term1*deltaN^2*(n^2-3n+3) - 6*deltaN^2*M2_new + 4*deltaN*M3_new
* ```
*
* The corrected sample excess kurtosis is
*
* ```text
* G_2 = (n-1)/((n-2)*(n-3)) * ((n+1)*g2 + 6)
* ```
*
* where `g2 = n*M4/M2^2 - 3`.
*
* ## Notes
*
* -   The kurtosis is only defined for a window containing at least 4 values.
*     For fewer values the accumulator returns `null`.
*
* -   Input values are **not** type-checked. If a `NaN` enters the window,
*     the accumulator returns `NaN` until the `NaN` leaves the window, at
*     which point the central moments are recomputed from the circular buffer.
*
* ## References
*
* -   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].
*
* [@joanes:1998]: http://dx.doi.org/10.1111/1467-9884.00122
*
* @param {PositiveInteger} W - window size
* @throws {TypeError} must provide a positive integer
* @returns {Function} accumulator function
*
* @example
* var accumulator = mkurtosis( 4 );
*
* var kurtosis = accumulator();
* // returns null
*
* kurtosis = accumulator( 2.0 );
* // returns null
*
* kurtosis = accumulator( 2.0 );
* // returns null
*
* kurtosis = accumulator( -4.0 );
* // returns null
*
* kurtosis = accumulator( -4.0 );
* // returns -6.0
*
* kurtosis = accumulator( 3.0 );
* // returns -5.652200677131425
*/
function mkurtosis( W ) {
	var deltaN2;
	var deltaN;
	var delta;
	var term1;
	var mean;
	var nans;
	var buf;
	var M2;
	var M3;
	var M4;
	var g2;
	var N;
	var n;
	var i;
 
	if ( !isPositiveInteger( W ) ) {
		throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
	}
 
	buf = new Float64Array( W );
	mean = 0.0;
	M2 = 0.0;
	M3 = 0.0;
	M4 = 0.0;
	N = 0;
	i = -1;
	nans = 0;
 
	return accumulator;
 
	/**
	* If provided a value, returns an updated moving corrected sample excess kurtosis; otherwise, returns the current moving corrected sample excess kurtosis.
	*
	* @private
	* @param {number} [x] - new value
	* @returns {(number|null)} corrected sample excess kurtosis or null
	*/
	function accumulator( x ) {
		var xo;
 
		if ( arguments.length === 0 ) {
			if ( nans > 0 ) {
				return NaN;
			}
			n = ( N < W ) ? N : W;
			if ( n < 4 ) {
				return null;
			}
			g2 = ( n * M4 / ( M2 * M2 ) ) - 3.0;
			return ( (n-1) / ( (n-2)*(n-3) ) ) * ( ( (n+1)*g2 ) + 6.0 );
		}
 
		// Advance the circular buffer index:
		i = ( i + 1 ) % W;
 
		if ( N < W ) {
			// Fill phase: window is not yet full.
			buf[ i ] = x;
			N += 1;
			if ( isnan( x ) ) {
				nans += 1;
			}
			if ( nans > 0 ) {
				return NaN;
			}
 
			// Welford update (add x, going from N-1 to N):
			n = N;
			delta = x - mean;
			deltaN = delta / n;
			deltaN2 = deltaN * deltaN;
			term1 = delta * deltaN * ( n - 1 );
 
			M4 += term1 * deltaN2 * ( ( n*n ) - ( 3*n ) + 3 );
			M4 += 6.0 * deltaN2 * M2;
			M4 -= 4.0 * deltaN * M3;
 
			M3 += term1 * deltaN * ( n - 2 );
			M3 -= 3.0 * deltaN * M2;
 
			M2 += term1;
			mean += deltaN;
 
			if ( n < 4 ) {
				return null;
			}
			g2 = ( n * M4 / ( M2 * M2 ) ) - 3.0;
			return ( (n-1) / ( (n-2)*(n-3) ) ) * ( ( (n+1)*g2 ) + 6.0 );
		}
 
		// Slide phase: window is full (N >= W).
		xo = buf[ i ];
		buf[ i ] = x;
 
		// Track NaN count changes:
		if ( isnan( xo ) ) {
			nans -= 1;
		}
		if ( isnan( x ) ) {
			nans += 1;
		}
 
		// If a NaN just left the window and none remain, recompute from scratch
		// because NaN corrupted the running moment state:
		if ( nans === 0 && isnan( xo ) ) {
			recompute();
		} else if ( nans > 0 ) {
			// NaN still present or just entered; skip moment update:
			return NaN;
		} else {
			// Normal case: downdate xo, then update x.
			n = W;
 
			// Downdate: remove xo from (n, mean, M2, M3, M4).
			mean = ( ( n * mean ) - xo ) / ( n - 1 );
 
			// Recover delta/deltaN as they were when xo was originally added:
			delta = xo - mean;
			deltaN = delta / n;
			deltaN2 = deltaN * deltaN;
			term1 = delta * deltaN * ( n - 1 );
 
			M2 -= term1;
			M3 -= term1 * deltaN * ( n - 2 );
			M3 += 3.0 * deltaN * M2;
			M4 -= term1 * deltaN2 * ( ( n*n ) - ( 3*n ) + 3 );
			M4 -= 6.0 * deltaN2 * M2;
			M4 += 4.0 * deltaN * M3;
 
			// Update: add x to (n-1) values to recover a full window of n:
			delta = x - mean;
			deltaN = delta / n;
			deltaN2 = deltaN * deltaN;
			term1 = delta * deltaN * ( n - 1 );
 
			M4 += term1 * deltaN2 * ( ( n*n ) - ( 3*n ) + 3 );
			M4 += 6.0 * deltaN2 * M2;
			M4 -= 4.0 * deltaN * M3;
 
			M3 += term1 * deltaN * ( n - 2 );
			M3 -= 3.0 * deltaN * M2;
 
			M2 += term1;
			mean += deltaN;
		}
 
		n = W;
		if ( n < 4 ) {
			return null;
		}
		g2 = ( n * M4 / ( M2 * M2 ) ) - 3.0;
		return ( (n-1) / ( (n-2)*(n-3) ) ) * ( ( (n+1)*g2 ) + 6.0 );
	}
 
	/**
	* Recomputes central moments M2, M3, M4 and the mean from the circular buffer.
	*
	* @private
	*/
	function recompute() {
		var v;
		var k;
 
		mean = 0.0;
		M2 = 0.0;
		M3 = 0.0;
		M4 = 0.0;
 
		// Iterate over buffer from oldest (index (i+1)%W) to newest (index i):
		for ( k = 1; k <= W; k++ ) {
			v = buf[ ( i + k ) % W ];
 
			// Welford update for position k (1-indexed):
			n = k;
			delta = v - mean;
			deltaN = delta / n;
			deltaN2 = deltaN * deltaN;
			term1 = delta * deltaN * ( n - 1 );
 
			M4 += term1 * deltaN2 * ( ( n*n ) - ( 3*n ) + 3 );
			M4 += 6.0 * deltaN2 * M2;
			M4 -= 4.0 * deltaN * M3;
 
			M3 += term1 * deltaN * ( n - 2 );
			M3 -= 3.0 * deltaN * M2;
 
			M2 += term1;
			mean += deltaN;
		}
	}
}
 
 
// EXPORTS //
 
module.exports = mkurtosis;