diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/README.md b/lib/node_modules/@stdlib/stats/incr/mkurtosis/README.md new file mode 100644 index 000000000000..21158ccf1f5f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/README.md @@ -0,0 +1,228 @@ + + +# mkurtosis + +> Compute a moving [corrected sample excess kurtosis][sample-excess-kurtosis] incrementally. + +
+ +The [kurtosis][sample-excess-kurtosis] for a random variable `X` is defined as + + + +```math +\mathop{\mathrm{Kurtosis}}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^4 \biggr] +``` + + + +Using a univariate normal distribution as the standard of comparison, the [excess kurtosis][sample-excess-kurtosis] is the kurtosis minus `3`. + +For a window of `W` values, the [sample excess kurtosis][sample-excess-kurtosis] is + + + +```math +g_2 = \frac{m_4}{m_2^2} - 3 = \frac{\frac{1}{W} \displaystyle\sum_{i=0}^{W-1} (x_i - \bar{x})^4}{\biggl(\frac{1}{W} \displaystyle\sum_{i=0}^{W-1} (x_i - \bar{x})^2\biggr)^2} +``` + + + +The corrected (unbiased under normality) sample excess kurtosis is + + + +```math +G_2 = \frac{(n+1)n}{(n-1)(n-2)(n-3)} \frac{\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})^4}{\biggl(\displaystyle\sum_{i=0}^{n-1} (x_i - \bar{x})^2\biggr)^2} - 3 \frac{(n-1)^2}{(n-2)(n-3)} +``` + + + +where `n` is the number of values currently in the window (grows from 1 to `W` during the fill phase, then stays at `W`). + +
+ + + +
+ +## Usage + +```javascript +var mkurtosis = require( '@stdlib/stats/incr/mkurtosis' ); +``` + +#### mkurtosis( W ) + +Returns an accumulator `function` which incrementally computes a moving [corrected sample excess kurtosis][sample-excess-kurtosis] over a sliding window of size `W`. + +```javascript +var accumulator = mkurtosis( 4 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated moving [corrected sample excess kurtosis][sample-excess-kurtosis]. If not provided an input value `x`, the accumulator function returns the current moving [corrected sample excess kurtosis][sample-excess-kurtosis]. + +```javascript +var accumulator = mkurtosis( 4 ); + +var 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 + +kurtosis = accumulator(); +// returns -5.652200677131425 +``` + +
+ + + +
+ +## Notes + +- The `W` parameter defines the **window size**: only the `W` most recently provided values are used when computing the kurtosis. During the initial fill phase (fewer than `W` values have been provided), the kurtosis is computed over however many values are available. + +- The corrected sample excess kurtosis is only defined for **4 or more** values. If fewer than 4 values have been provided, the accumulator returns `null`. + +- **Algorithm** — The implementation avoids a full O(W) recomputation at each step by maintaining running central moment accumulators `M2`, `M3`, and `M4` (à la Welford). When the window is full, adding a new value `x` and discarding the oldest value `xo` is handled in two O(1) operations: + + 1. **Downdate** — algebraically reverse the Welford update that originally incorporated `xo`, recovering the moments as though `xo` were never in the window. + 2. **Update** — apply the standard Welford update for `x`. + + The downdate formulas, derived by inverting the Welford recurrence, are: + + ```text + mean_new = (W·mean - xo) / (W - 1) + delta = xo - mean_new + deltaN = delta / W + term1 = delta · deltaN · (W - 1) + M2_new = M2 - term1 + M3_new = M3 - term1·deltaN·(W-2) + 3·deltaN·M2_new + M4_new = M4 - term1·deltaN²·(W²-3W+3) - 6·deltaN²·M2_new + 4·deltaN·M3_new + ``` + +- **NaN handling** — Input values are **not** type-checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for, at most, `W` future invocations. Once the `NaN` value leaves the sliding window, the moments are **recomputed from scratch** from the circular buffer and the accumulator recovers. This differs from `@stdlib/stats/incr/kurtosis`, where a `NaN` permanently corrupts all future results. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var mkurtosis = require( '@stdlib/stats/incr/mkurtosis' ); + +var accumulator; +var v; +var i; + +// Initialize a moving kurtosis accumulator with a window size of 20: +accumulator = mkurtosis( 20 ); + +// For each simulated datum, update the moving corrected sample excess kurtosis: +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + +* * * + +
+ +## 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]. +- Pébay, Philippe. 2008. "Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments." _Technical Report_ SAND2008-6212. Sandia National Laboratories. . +- Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3): 419–20. doi:[10.1080/00401706.1962.10490022][@welford:1962]. + +[@joanes:1998]: http://dx.doi.org/10.1111/1467-9884.00122 +[@welford:1962]: https://doi.org/10.1080/00401706.1962.10490022 + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/mkurtosis/benchmark/benchmark.js new file mode 100644 index 000000000000..e27e181f42a7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/benchmark/benchmark.js @@ -0,0 +1,92 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var mkurtosis = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = mkurtosis( 20 ); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::accumulator,window=20', pkg ), function benchmark( b ) { + var acc; + var v; + var i; + + acc = mkurtosis( 20 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + if ( v !== v && v !== null ) { + b.fail( 'unexpected NaN' ); + } + } + b.toc(); + if ( v !== v && v !== null ) { + b.fail( 'unexpected NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::accumulator,window=100', pkg ), function benchmark( b ) { + var acc; + var v; + var i; + + acc = mkurtosis( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + if ( v !== v && v !== null ) { + b.fail( 'unexpected NaN' ); + } + } + b.toc(); + if ( v !== v && v !== null ) { + b.fail( 'unexpected NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/repl.txt new file mode 100644 index 000000000000..67e88ed04fec --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/repl.txt @@ -0,0 +1,50 @@ + +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving + corrected sample excess kurtosis. + + The `W` parameter defines the number of values over which to compute the + moving corrected sample excess kurtosis. + + If provided a value, the accumulator function returns an updated moving + corrected sample excess kurtosis. If not provided a value, the accumulator + function returns the current moving corrected sample excess kurtosis. + + As `W` values are needed to fill the window buffer, the first `W-1` + returned values are calculated from smaller sample sizes. Until the window + is full, each returned value is the kurtosis of all values provided thus + far. + + For fewer than 4 values, the accumulator returns `null`. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, the accumulated value is `NaN` for, at most, `W` future invocations. + Once the `NaN` value leaves the sliding window, the accumulator recovers. + + Parameters + ---------- + W: integer + Window size. Must be a positive integer. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 4 ); + > var v = accumulator( 2.0 ) + null + > v = accumulator( 2.0 ) + null + > v = accumulator( -4.0 ) + null + > v = accumulator( -4.0 ) + -6.0 + > v = accumulator( 3.0 ) + -5.652200677131425 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/types/index.d.ts new file mode 100644 index 000000000000..b56a6ba2aeaa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/types/index.d.ts @@ -0,0 +1,74 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, returns an updated moving corrected sample excess +* kurtosis; otherwise, returns the current moving corrected sample excess +* kurtosis. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results +* in `NaN`, the accumulated value is `NaN` for, at most, `W` future +* invocations. Once the `NaN` value leaves the window, the accumulator +* recovers. +* +* @param x - value +* @returns moving corrected sample excess kurtosis or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving +* corrected sample excess kurtosis. +* +* @param W - window size +* @throws must provide a positive integer +* @returns 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 +*/ +declare function mkurtosis( W: number ): accumulator; + + +// EXPORTS // + +export = mkurtosis; diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/types/test.ts new file mode 100644 index 000000000000..4b163c97ed58 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/docs/types/test.ts @@ -0,0 +1,65 @@ +/* +* @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. +*/ + +import mkurtosis = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + mkurtosis( 4 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided a non-number argument... +{ + mkurtosis( '5' ); // $ExpectError + mkurtosis( true ); // $ExpectError + mkurtosis( false ); // $ExpectError + mkurtosis( null ); // $ExpectError + mkurtosis( undefined ); // $ExpectError + mkurtosis( [] ); // $ExpectError + mkurtosis( {} ); // $ExpectError + mkurtosis( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is not provided any arguments... +{ + mkurtosis(); // $ExpectError +} + +// The function returns an accumulator which returns a number or null... +{ + const acc = mkurtosis( 4 ); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null +} + +// The compiler throws an error if the accumulator is provided invalid arguments... +{ + const acc = mkurtosis( 4 ); + + acc( '5' ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/examples/index.js b/lib/node_modules/@stdlib/stats/incr/mkurtosis/examples/index.js new file mode 100644 index 000000000000..6e14f1effa97 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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'; + +var randu = require( '@stdlib/random/base/randu' ); +var mkurtosis = require( './../lib' ); + +var accumulator; +var kurtosis; +var v; +var i; + +// Initialize a moving kurtosis accumulator with a window of 20: +accumulator = mkurtosis( 20 ); + +// For each simulated datum, update the moving corrected sample excess kurtosis: +console.log( '\nValue\tKurtosis\n' ); +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + kurtosis = accumulator( v ); + if ( kurtosis === null ) { + console.log( '%d\t%s', v.toFixed( 4 ), kurtosis ); + } else { + console.log( '%d\t%d', v.toFixed( 4 ), kurtosis.toFixed( 4 ) ); + } +} +console.log( '\nFinal moving kurtosis: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/lib/index.js b/lib/node_modules/@stdlib/stats/incr/mkurtosis/lib/index.js new file mode 100644 index 000000000000..c4b689385e93 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/lib/index.js @@ -0,0 +1,54 @@ +/** +* @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'; + +/** +* Compute a moving corrected sample excess kurtosis incrementally. +* +* @module @stdlib/stats/incr/mkurtosis +* +* @example +* var mkurtosis = require( '@stdlib/stats/incr/mkurtosis' ); +* +* var accumulator = mkurtosis( 4 ); +* +* var 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 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/lib/main.js b/lib/node_modules/@stdlib/stats/incr/mkurtosis/lib/main.js new file mode 100644 index 000000000000..de7670a4a10e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/lib/main.js @@ -0,0 +1,312 @@ +/** +* @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; diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/package.json b/lib/node_modules/@stdlib/stats/incr/mkurtosis/package.json new file mode 100644 index 000000000000..175032dd67ef --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/stats/incr/mkurtosis", + "version": "0.0.0", + "description": "Compute a moving corrected sample excess kurtosis incrementally.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": { + "@stdlib/array/float64": "^0.0.x", + "@stdlib/assert/is-positive-integer": "^0.0.x", + "@stdlib/math/base/assert/is-nan": "^0.0.x", + "@stdlib/string/format": "^0.0.x" + }, + "devDependencies": { + "@stdlib/constants/float64/eps": "^0.0.x", + "@stdlib/math/base/special/abs": "^0.0.x", + "@stdlib/math/base/special/max": "^0.0.x", + "@stdlib/random/base/randu": "^0.0.x" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "kurtosis", + "sample kurtosis", + "moving", + "window", + "sliding", + "shape", + "kurt", + "corrected", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/mkurtosis/test/test.js b/lib/node_modules/@stdlib/stats/incr/mkurtosis/test/test.js new file mode 100644 index 000000000000..022c3ba981e2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mkurtosis/test/test.js @@ -0,0 +1,350 @@ +/** +* @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 tape = require( 'tape' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var max = require( '@stdlib/math/base/special/max' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var mkurtosis = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Computes the corrected sample excess kurtosis for an array. +* +* @private +* @param {Array} arr - array of numbers +* @returns {number|null} kurtosis or null +*/ +function naiveKurtosis( arr ) { + var deltaN2; + var deltaN; + var delta; + var term1; + var mean; + var n1; + var M2; + var M3; + var M4; + var g2; + var n; + var i; + + n = arr.length; + if ( n < 4 ) { + return null; + } + mean = 0.0; + M2 = 0.0; + M3 = 0.0; + M4 = 0.0; + for ( i = 0; i < n; i++ ) { + n1 = i + 1; + delta = arr[ i ] - mean; + deltaN = delta / n1; + deltaN2 = deltaN * deltaN; + term1 = delta * deltaN * i; + M4 += term1 * deltaN2 * ( ( n1*n1 ) - ( 3*n1 ) + 3 ); + M4 += 6.0 * deltaN2 * M2; + M4 -= 4.0 * deltaN * M3; + M3 += term1 * deltaN * ( n1 - 2 ); + M3 -= 3.0 * deltaN * M2; + M2 += term1; + mean += deltaN; + } + g2 = ( n * M4 / ( M2 * M2 ) ) - 3.0; + return ( (n-1) / ( (n-2)*(n-3) ) ) * ( ( (n+1)*g2 ) + 6.0 ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mkurtosis, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws if not provided a positive integer for the window size', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + 0, + 3.14, + true, + false, + null, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws a type error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mkurtosis( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.strictEqual( typeof mkurtosis( 4 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current moving corrected sample excess kurtosis', function test( t ) { + var data; + var acc; + var i; + + data = [ -10.0, -10.0, 10.0, 10.0 ]; + acc = mkurtosis( 4 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.strictEqual( acc(), -6.0, 'returns current kurtosis without advancing state' ); + t.end(); +}); + +tape( 'the accumulator returns `null` until at least 4 datums have been provided', function test( t ) { + var acc; + var out; + + acc = mkurtosis( 6 ); + + out = acc(); + t.strictEqual( out, null, 'returns null (0 values)' ); + + out = acc( 2.0 ); + t.strictEqual( out, null, 'returns null (1 value)' ); + + out = acc( 8.0 ); + t.strictEqual( out, null, 'returns null (2 values)' ); + + out = acc( -4.0 ); + t.strictEqual( out, null, 'returns null (3 values)' ); + + out = acc( 3.0 ); + t.notEqual( out, null, 'does not return null (4 values)' ); + + t.end(); +}); + +tape( 'the accumulator returns `null` for all invocations when window size is less than 4', function test( t ) { + var acc; + var out; + var i; + + // W = 3: kurtosis is always undefined. + acc = mkurtosis( 3 ); + for ( i = 0; i < 10; i++ ) { + out = acc( i * 1.0 ); + t.strictEqual( out, null, 'returns null for W=3 (iteration '+i+')' ); + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes a moving corrected sample excess kurtosis (window: 4, matches incrkurtosis for first window)', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + // Using window=4: the first 4 results should match incrkurtosis exactly. + data = [ 2.0, 2.0, -4.0, -4.0 ]; + expected = [ + null, + null, + null, + -6.0 + ]; + + acc = mkurtosis( 4 ); + for ( i = 0; i < data.length; i++ ) { + actual = acc( data[ i ] ); + if ( expected[ i ] === null ) { + t.strictEqual( actual, null, 'returns null' ); + } else { + delta = abs( actual - expected[ i ] ); + tol = 2.0 * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol ); + } + } + t.end(); +}); + +tape( 'the accumulator correctly slides the window (W=4)', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + data = [ 2.0, 2.0, -4.0, -4.0, 1.5, -10.0, 5.0, 3.5, -1.0 ]; + acc = mkurtosis( 4 ); + + for ( i = 0; i < data.length; i++ ) { + actual = acc( data[ i ] ); + + if ( i < 3 ) { + t.strictEqual( actual, null, 'returns null for i='+i ); + } else { + // Expected: kurtosis of the last 4 values. + expected = naiveKurtosis( data.slice( max( 0, i-3 ), i+1 ) ); + delta = abs( actual - expected ); + tol = ( 2.0 * EPS * abs( expected ) ) + 1.0e-10; + t.ok( delta <= tol, 'i='+i+' actual: '+actual+' expected: '+expected+' Δ: '+delta+' tol: '+tol ); + } + } + t.end(); +}); + +tape( 'the accumulator correctly slides the window (W=6)', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + data = [ 3.0, -1.0, 7.0, 2.5, -4.0, 6.0, 1.0, -2.0, 8.0, 5.0 ]; + acc = mkurtosis( 6 ); + + for ( i = 0; i < data.length; i++ ) { + actual = acc( data[ i ] ); + if ( i < 3 ) { + t.strictEqual( actual, null, 'returns null for i='+i ); + } else if ( i < 6 ) { + // Window not full yet; compute over first i+1 values: + expected = naiveKurtosis( data.slice( 0, i+1 ) ); + if ( expected === null ) { + t.strictEqual( actual, null, 'returns null for i='+i ); + } else { + delta = abs( actual - expected ); + tol = ( 2.0 * EPS * abs( expected ) ) + 1.0e-10; + t.ok( delta <= tol, 'i='+i+' actual: '+actual+' expected: '+expected+' Δ: '+delta+' tol: '+tol ); + } + } else { + // Window full — last 6 values: + expected = naiveKurtosis( data.slice( i-5, i+1 ) ); + delta = abs( actual - expected ); + tol = ( 2.0 * EPS * abs( expected ) ) + 1.0e-10; + t.ok( delta <= tol, 'i='+i+' actual: '+actual+' expected: '+expected+' Δ: '+delta+' tol: '+tol ); + } + } + t.end(); +}); + +tape( 'the accumulator returns `NaN` when a NaN is in the window', function test( t ) { + var data; + var acc; + var v; + var i; + + // NaN at position 2 (0-indexed), window=4. + data = [ 1.0, 2.0, NaN, -2.0, -3.0, 4.0, 5.0 ]; + acc = mkurtosis( 4 ); + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if ( i >= 3 && i <= 5 ) { + // NaN in the window: positions 2-5. + t.ok( isnan( v ), 'returns NaN while NaN is in the window (i='+i+')' ); + } + } + t.end(); +}); + +tape( 'the accumulator recovers after a NaN exits the window', function test( t ) { + var data; + var acc; + var v; + var i; + + // W=4: NaN at index 0 leaves after 4 slides. + data = [ NaN, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; + acc = mkurtosis( 4 ); + + // Consume NaN and 3 good values: + for ( i = 0; i < 4; i++ ) { + v = acc( data[ i ] ); + + // Returns NaN or null while NaN is present or data is insufficient: + t.ok( isnan( v ) || v === null, 'returns NaN or null (i='+i+')' ); + } + + // Fifth value pushes NaN out of the window: + v = acc( data[ 4 ] ); + t.ok( !isnan( v ), 'recovered from NaN (i=4), got: ' + v ); + t.notEqual( v, null, 'does not return null after recovery' ); + + t.end(); +}); + +tape( 'the accumulator handles a window entirely filled with identical values (kurtosis undefined: M2=0)', function test( t ) { + var acc; + var v; + var i; + + acc = mkurtosis( 4 ); + for ( i = 0; i < 8; i++ ) { + v = acc( 5.0 ); + if ( i < 3 ) { + t.strictEqual( v, null, 'returns null for i='+i ); + } else { + // M2=0 leads to division by zero; null, NaN, or Infinity are all acceptable. + t.ok( v === null || isnan( v ) || !isFinite( v ), 'returns null, NaN, or Infinity for degenerate window (i='+i+')' ); + } + } + t.end(); +}); + +tape( 'the accumulator works with window size W=4 and exactly 4 values', function test( t ) { + var acc; + var v; + + acc = mkurtosis( 4 ); + + acc( -10.0 ); + acc( -10.0 ); + acc( 10.0 ); + v = acc( 10.0 ); + + t.strictEqual( v, -6.0, 'returns -6.0 for symmetric data' ); + t.end(); +});