diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/README.md b/lib/node_modules/@stdlib/stats/incr/mskewness/README.md new file mode 100644 index 000000000000..e841819d082e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/README.md @@ -0,0 +1,214 @@ + + +# incrmskewness + +> Compute moving [corrected sample skewness][sample-skewness] incrementally. + +
+ +The [skewness][sample-skewness] for a random variable `X` is defined as + + + +```math +\mathop{\mathrm{Skewness}}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr] +``` + + + + + +For a sample of `n` values, the [sample skewness][sample-skewness] is + + + +```math +b_1 = \frac{m_3}{s^3} = \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}} +``` + + + + + +where `m_3` is the sample third central moment and `s` is the sample standard deviation. + +An alternative definition for the [sample skewness][sample-skewness] which includes an adjustment factor (and is the implemented definition) is + + + +```math +G_1 = \frac{n^2}{(n-1)(n-2)} \frac{m_3}{s^3} = \frac{\sqrt{n(n-1)}}{n-2} \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrmskewness = require( '@stdlib/stats/incr/mskewness' ); +``` + +#### incrmskewness( W ) + +Returns an accumulator `function` which incrementally computes a moving [corrected sample skewness][sample-skewness] over a sliding window of size `W`. + +```javascript +var accumulator = incrmskewness( 5 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated moving [corrected sample skewness][sample-skewness]. If not provided an input value `x`, the accumulator function returns the current moving [corrected sample skewness][sample-skewness]. + +```javascript +var accumulator = incrmskewness( 5 ); + +var skewness = accumulator(); +// returns null + +skewness = accumulator( 2.0 ); +// returns null + +skewness = accumulator( -5.0 ); +// returns null + +skewness = accumulator( -10.0 ); +// returns ~0.492 + +skewness = accumulator(); +// returns ~0.492 +``` + +
+ + + +
+ +## Notes + +- 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 least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrmskewness = require( '@stdlib/stats/incr/mskewness' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrmskewness( 3 ); + +// For each simulated datum, update the corrected sample skewness... +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]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/mskewness/benchmark/benchmark.js new file mode 100644 index 000000000000..7a7e5edbae57 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/benchmark/benchmark.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var pkg = require( './../package.json' ).name; +var incrmskewness = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrmskewness( ( i % 5 ) + 1 ); + 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( pkg+'::accumulator', function benchmark( b ) { + var acc; + var len; + var v; + var i; + var x; + + len = 100; + x = new Float64Array( len ); + for ( i = 0; i < len; i++ ) { + x[ i ] = uniform( 0.0, 1.0 ); + } + + acc = incrmskewness( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( x[ i % len ] ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_adjusted_sample_skewness.svg b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_adjusted_sample_skewness.svg new file mode 100644 index 000000000000..3277353af222 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_adjusted_sample_skewness.svg @@ -0,0 +1,171 @@ + +upper G 1 equals StartFraction n squared Over left-parenthesis n minus 1 right-parenthesis left-parenthesis n minus 2 right-parenthesis EndFraction StartFraction m 3 Over s cubed EndFraction equals StartFraction StartRoot n left-parenthesis n minus 1 right-parenthesis EndRoot Over n minus 2 EndFraction StartStartFraction StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis cubed OverOver left-parenthesis StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared right-parenthesis Superscript 3 slash 2 Baseline EndEndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_sample_skewness.svg b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_sample_skewness.svg new file mode 100644 index 000000000000..147334d80f8b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_sample_skewness.svg @@ -0,0 +1,131 @@ + +b 1 equals StartFraction m 3 Over s cubed EndFraction equals StartStartFraction StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis cubed OverOver left-parenthesis StartFraction 1 Over n minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared right-parenthesis Superscript 3 slash 2 Baseline EndEndFraction + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_skewness.svg b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_skewness.svg new file mode 100644 index 000000000000..45650ae35b70 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_skewness.svg @@ -0,0 +1,59 @@ + +upper S k e w n e s s left-bracket upper X right-bracket equals normal upper E left-bracket left-parenthesis StartFraction upper X minus mu Over sigma EndFraction right-parenthesis cubed right-bracket + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/repl.txt new file mode 100644 index 000000000000..f63ae2903803 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/repl.txt @@ -0,0 +1,43 @@ +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving + corrected sample skewness. + + The `W` parameter defines the window size (i.e., the number of values + over which to compute the skewness). + + 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. + + As `W` values are needed to fill the window buffer, the first `W-1` + returned values are calculated from smaller sample sizes + (provided at least `3` values have been accumulated). + Until the window is full, each returned value is calculated + from all provided values. + + Parameters + ---------- + W: integer + Window size. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var v = accumulator( 2.0 ) + null + > v = accumulator( -5.0 ) + null + > v = accumulator( -10.0 ) + ~0.492 + > v = accumulator( 5.0 ) + ~0.935 + > v = accumulator() + ~0.935 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/types/index.d.ts new file mode 100644 index 000000000000..1110de9609e2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/types/index.d.ts @@ -0,0 +1,72 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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, the accumulator function returns an updated corrected sample skewness. If not provided a value, the accumulator function returns the current corrected sample skewness. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param x - value +* @returns corrected sample skewness or null +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving corrected sample skewness. +* +* ## Notes +* +* - 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. +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for at least `W` invocations. +* +* @param W - window size +* @returns accumulator function +* +* @example +* var accumulator = incrmskewness( 3 ); +* +* var skewness = accumulator(); +* // returns null +* +* skewness = accumulator( 2.0 ); +* // returns null +* +* skewness = accumulator( -5.0 ); +* // returns null +* +* skewness = accumulator( -10.0 ); +* // returns ~0.492 +* +* skewness = accumulator( 5.0 ); +* // returns ~0.935 +* +* skewness = accumulator(); +* // returns ~0.935 +*/ +declare function incrmskewness( W: number ): accumulator; + + +// EXPORTS // + +export = incrmskewness; diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/types/test.ts new file mode 100644 index 000000000000..c7be01d7640f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/docs/types/test.ts @@ -0,0 +1,67 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 incrmskewness = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrmskewness( 3 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided invalid arguments... +{ + incrmskewness( '5' ); // $ExpectError + incrmskewness( true ); // $ExpectError + incrmskewness( false ); // $ExpectError + incrmskewness( null ); // $ExpectError + incrmskewness( [] ); // $ExpectError + incrmskewness( {} ); // $ExpectError + incrmskewness( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrmskewness(); // $ExpectError + incrmskewness( 3, 2 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrmskewness( 3 ); + + acc(); // $ExpectType number | null + acc( 2 ); // $ExpectType number | null + acc( -5 ); // $ExpectType number | null + acc( 10 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrmskewness( 5 ); + + 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/mskewness/examples/index.js b/lib/node_modules/@stdlib/stats/incr/mskewness/examples/index.js new file mode 100644 index 000000000000..e67a4a0d50c1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 incrmskewness = require( './../lib' ); + +var accumulator; +var mskewness; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrmskewness( 5 ); + +// For each simulated datum, update the moving corrected sample skewness... +console.log( '\nValue\tSkewness\n' ); + +for ( i = 0; i < 100; i++ ) { + v = randu() * 100.0; + mskewness = accumulator( v ); + if ( i < 2 ) { + console.log( '%s\t%s', v.toFixed( 4 ), mskewness ); + } else { + console.log( '%s\t%s', v.toFixed( 4 ), mskewness.toFixed( 4 ) ); + } +} +console.log( '\nFinal moving skewness: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/lib/index.js b/lib/node_modules/@stdlib/stats/incr/mskewness/lib/index.js new file mode 100644 index 000000000000..e142c69c7787 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 moving corrected sample skewness incrementally. +* +* @module @stdlib/stats/incr/mskewness +* +* @example +* var incrmskewness = require( '@stdlib/stats/incr/mskewness' ); +* +* var accumulator = incrmskewness( 3 ); +* +* var mskewness = accumulator(); +* // returns null +* +* mskewness = accumulator( 2.0 ); +* // returns null +* +* mskewness = accumulator( -5.0 ); +* // returns null +* +* mskewness = accumulator( -10.0 ); +* // returns ~0.492 +* +* mskewness = accumulator( 5.0 ); +* // returns ~0.935 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/lib/main.js b/lib/node_modules/@stdlib/stats/incr/mskewness/lib/main.js new file mode 100644 index 000000000000..cabf345d3894 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/lib/main.js @@ -0,0 +1,226 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var Float64Array = require( '@stdlib/array/float64' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving corrected sample skewness. +* +* ## Method +* +* The algorithm computes the corrected sample skewness using the formula for `G_1` in [Joanes and Gill 1998][@joanes:1998]. Updates are performed using an extension of Welford's algorithm for sliding windows. +* +* ## 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 = incrmskewness( 3 ); +* +* var skewness = accumulator(); +* // returns null +* +* skewness = accumulator( 2.0 ); +* // returns null +* +* skewness = accumulator( -5.0 ); +* // returns null +* +* skewness = accumulator( -10.0 ); +* // returns ~0.492 +* +* skewness = accumulator( 5.0 ); +* // returns ~0.935 +* +* skewness = accumulator(); +* // returns ~0.935 +*/ +function incrmskewness( W ) { + var deltaN; + var term1; + var delta; + var mean; + var buf; + var tmp; + var g1; + var M2; + var M3; + var i; + var N; + + 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; + i = -1; + N = 0; + + return accumulator; + + /** + * 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. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} corrected sample skewness or null + */ + function accumulator( x ) { + var meanOld; + var M2Old; + var v; + var k; + + if ( arguments.length === 0 ) { + if ( N < 3 ) { + return ( isnan( M3 ) ) ? NaN : null; + } + // Calculate the population skewness: + g1 = sqrt( N ) * M3 / pow( M2, 1.5 ); + + // Return the corrected sample skewness: + return sqrt( N * ( N - 1 ) ) * g1 / ( N - 2 ); + } + + // Update the index for managing the circular buffer: + i = ( i + 1 ) % W; + + // Case: incoming value is NaN, so the accumulated statistics are automatically NaN... + if ( isnan( x ) ) { + M2 = NaN; + M3 = NaN; + mean = NaN; + N = W; // explicitly set to avoid `N < W` branch + } + // Case: initial window... + else if ( N < W ) { + N += 1; + delta = x - mean; + deltaN = delta / N; + term1 = delta * deltaN * ( N - 1 ); + + tmp = term1 * deltaN * ( N - 2 ); + tmp -= 3.0 * deltaN * M2; + M3 += tmp; + + M2 += term1; + mean += deltaN; + } + // Case: outgoing value is NaN or current stats are NaN, and, thus, we need to recompute the accumulated values... + else if ( isnan( buf[ i ] ) || isnan( M2 ) || isnan( M3 ) ) { + N = 0; + M2 = 0.0; + M3 = 0.0; + mean = 0.0; + + // Reconstruct statistics from the buffer (excluding the outgoing value at `i`, adding `x`): + for ( k = 0; k < W; k++ ) { + v = ( k === i ) ? x : buf[ k ]; + if ( isnan( v ) ) { + N = W; + M2 = NaN; + M3 = NaN; + mean = NaN; + break; + } + N += 1; + delta = v - mean; + deltaN = delta / N; + term1 = delta * deltaN * ( N - 1 ); + + tmp = term1 * deltaN * ( N - 2 ); + tmp -= 3.0 * deltaN * M2; + M3 += tmp; + + M2 += term1; + mean += deltaN; + } + } + // Case: neither the current stats nor the incoming/outgoing values are NaN, so we update the accumulated values (remove old, add new)... + else { + // Remove the outgoing value (buf[i]): + v = buf[ i ]; + + // To remove, we reverse the Welford update. We calculate the mean *before* v was added (which corresponds to N-1). N here is W. + meanOld = ( ( N * mean ) - v ) / ( N - 1 ); + + delta = v - meanOld; + deltaN = delta / N; // This is ( mean - meanOld ) + + term1 = delta * deltaN * ( N - 1 ); + + // Revert M2 first (order matters relative to add operation, but we need M2_old for M3): + M2Old = M2 - term1; + + tmp = term1 * deltaN * ( N - 2 ); + tmp -= 3.0 * deltaN * M2Old; + + M3 -= tmp; + M2 = M2Old; + mean = meanOld; + + // Now Add the new value (x) + delta = x - mean; + deltaN = delta / N; + term1 = delta * deltaN * ( N - 1 ); + + tmp = term1 * deltaN * ( N - 2 ); + tmp -= 3.0 * deltaN * M2; + M3 += tmp; + + M2 += term1; + mean += deltaN; + } + + buf[ i ] = x; + + if ( N < 3 ) { + return ( isnan( M3 ) ) ? NaN : null; + } + // Calculate the population skewness: + g1 = sqrt( N ) * M3 / pow( M2, 1.5 ); + + // Return the corrected sample skewness: + return sqrt( N * ( N - 1 ) ) * g1 / ( N - 2 ); + } +} + + +// EXPORTS // + +module.exports = incrmskewness; diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/package.json b/lib/node_modules/@stdlib/stats/incr/mskewness/package.json new file mode 100644 index 000000000000..0117e9c872c2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/stats/incr/mskewness", + "version": "0.0.0", + "description": "Compute moving corrected sample skewness 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": {}, + "devDependencies": {}, + "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", + "skewness", + "sample skewness", + "shape", + "corrected", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/mskewness/test/test.js b/lib/node_modules/@stdlib/stats/incr/mskewness/test/test.js new file mode 100644 index 000000000000..1d0260ebfc2e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/mskewness/test/test.js @@ -0,0 +1,196 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 EPS = require( '@stdlib/constants/float64/eps' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrmskewness = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrmskewness, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.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 an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrmskewness( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.strictEqual( typeof incrmskewness( 3 ), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving corrected sample skewness incrementally', function test( t ) { + var expected; + var actual; + var delta; + var data; + var acc; + var tol; + var i; + var N; + + data = [ 3.0, 2.0, 7.0, -1.0, 9.0, -2.0 ]; + N = data.length; + + expected = [ + null, + null, + 1.4578629673213048, + 0.4366620845757488, + 0.11718750000000001, + 0.329176993486849 + ]; + + acc = incrmskewness( 5 ); + + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ] ); + if ( expected[ i ] === null ) { + t.strictEqual( actual, expected[i], 'returns expected value for index '+i ); + } else { + delta = abs( actual - expected[ i ] ); + tol = 1.5 * EPS * abs( expected[ i ] ); + t.strictEqual( delta <= tol, true, 'within tolerance. Actual: '+actual+'. Expected: '+expected[i]+'. Delta: '+delta+'. Tol: '+tol+'.' ); + } + } + + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current skewness', function test( t ) { + var expected; + var data; + var acc; + var i; + + data = [ 3.0, 2.0, 7.0, -1.0, 9.0 ]; + expected = 0.11718750000000001; + acc = incrmskewness( 5 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + + t.strictEqual( acc(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrmskewness( 3 ); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the corrected sample skewness is `null` until at least 3 datums have been provided', function test( t ) { + var skewness; + var acc; + + acc = incrmskewness( 5 ); + + skewness = acc(); + t.strictEqual( skewness, null, 'returns expected value' ); + + skewness = acc( 2.0 ); + t.strictEqual( skewness, null, 'returns expected value' ); + + skewness = acc( 2.0 ); + t.strictEqual( skewness, null, 'returns expected value' ); + + skewness = acc( 3.0 ); + t.notEqual( skewness, null, 'does not return null' ); + + t.end(); +}); + +tape( 'if provided `NaN`, the accumulated value is `NaN` for at least `W` invocations', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + acc = incrmskewness( 3 ); + + data = [ + 1.0, // 1 + 2.0, // 1, 2 + 3.0, // 1, 2, 3 + NaN, // 2, 3, NaN + 5.0, // 3, NaN, 5 + 6.0, // NaN, 5, 6 + 7.0 // 5, 6, 7 + ]; + + expected = [ + null, + null, + 0.0, + NaN, + NaN, + NaN, + 0.0 + ]; + + for ( i = 0; i < data.length; i++ ) { + actual = acc( data[ i ] ); + if ( expected[ i ] === null ) { + t.strictEqual( actual, null, 'returns null for index '+i ); + } else if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( actual ), true, 'returns NaN for index '+i ); + } else { + t.strictEqual( actual, expected[ i ], 'returns expected value for window '+i ); + } + } + t.end(); +});