Skip to content

Commit 3a035a5

Browse files
committed
feat: add stats/incr/mskewness
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent e4bb414 commit 3a035a5

File tree

13 files changed

+1420
-0
lines changed

13 files changed

+1420
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# incrmskewness
22+
23+
> Compute moving [corrected sample skewness][sample-skewness] incrementally.
24+
25+
<section class="intro">
26+
27+
The [skewness][sample-skewness] for a random variable `X` is defined as
28+
29+
<!-- <equation class="equation" label="eq:skewness" align="center" raw="\operatorname{Skewness}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr]" alt="Equation for skewness."> -->
30+
31+
```math
32+
\mathop{\mathrm{Skewness}}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr]
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{Skewness}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr]" data-equation="eq:skewness">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_skewness.svg" alt="Equation for skewness.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
For a sample of `n` values, the [sample skewness][sample-skewness] is
43+
44+
<!-- <equation class="equation" label="eq:sample_skewness" align="center" raw="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}}" alt="Equation for the sample skewness."> -->
45+
46+
```math
47+
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}}
48+
```
49+
50+
<!-- <div class="equation" align="center" data-raw-text="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}}" data-equation="eq:sample_skewness">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_sample_skewness.svg" alt="Equation for the sample skewness.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
where `m_3` is the sample third central moment and `s` is the sample standard deviation.
58+
59+
An alternative definition for the [sample skewness][sample-skewness] which includes an adjustment factor (and is the implemented definition) is
60+
61+
<!-- <equation class="equation" label="eq:adjusted_sample_skewness" align="center" raw="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}}" alt="Equation for the adjusted sample skewness."> -->
62+
63+
```math
64+
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}}
65+
```
66+
67+
<!-- <div class="equation" align="center" data-raw-text="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}}" data-equation="eq:adjusted_sample_skewness">
68+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_adjusted_sample_skewness.svg" alt="Equation for the adjusted sample skewness.">
69+
<br>
70+
</div> -->
71+
72+
<!-- </equation> -->
73+
74+
</section>
75+
76+
<!-- /.intro -->
77+
78+
<section class="usage">
79+
80+
## Usage
81+
82+
```javascript
83+
var incrmskewness = require( '@stdlib/stats/incr/mskewness' );
84+
```
85+
86+
#### incrmskewness( W )
87+
88+
Returns an accumulator `function` which incrementally computes a moving [corrected sample skewness][sample-skewness] over a sliding window of size `W`.
89+
90+
```javascript
91+
var accumulator = incrmskewness( 5 );
92+
```
93+
94+
#### accumulator( \[x] )
95+
96+
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].
97+
98+
```javascript
99+
var accumulator = incrmskewness( 5 );
100+
101+
var skewness = accumulator();
102+
// returns null
103+
104+
skewness = accumulator( 2.0 );
105+
// returns null
106+
107+
skewness = accumulator( -5.0 );
108+
// returns null
109+
110+
skewness = accumulator( -10.0 );
111+
// returns ~0.492
112+
113+
skewness = accumulator();
114+
// returns ~0.492
115+
```
116+
117+
</section>
118+
119+
<!-- /.usage -->
120+
121+
<section class="notes">
122+
123+
## Notes
124+
125+
- 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.
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var randu = require( '@stdlib/random/base/randu' );
139+
var incrmskewness = require( '@stdlib/stats/incr/mskewness' );
140+
141+
var accumulator;
142+
var v;
143+
var i;
144+
145+
// Initialize an accumulator:
146+
accumulator = incrmskewness( 3 );
147+
148+
// For each simulated datum, update the corrected sample skewness...
149+
for ( i = 0; i < 100; i++ ) {
150+
v = randu() * 100.0;
151+
accumulator( v );
152+
}
153+
console.log( accumulator() );
154+
```
155+
156+
</section>
157+
158+
<!-- /.examples -->
159+
160+
* * *
161+
162+
<section class="references">
163+
164+
## References
165+
166+
- 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].
167+
168+
</section>
169+
170+
<!-- /.references -->
171+
172+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
173+
174+
<section class="related">
175+
176+
* * *
177+
178+
## See Also
179+
180+
- <span class="package-name">[`@stdlib/stats/incr/kurtosis`][@stdlib/stats/incr/kurtosis]</span><span class="delimiter">: </span><span class="description">compute a corrected sample excess kurtosis incrementally.</span>
181+
- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
182+
- <span class="package-name">[`@stdlib/stats/incr/stdev`][@stdlib/stats/incr/stdev]</span><span class="delimiter">: </span><span class="description">compute a corrected sample standard deviation incrementally.</span>
183+
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span>
184+
- <span class="package-name">[`@stdlib/stats/incr/variance`][@stdlib/stats/incr/variance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample variance incrementally.</span>
185+
186+
</section>
187+
188+
<!-- /.related -->
189+
190+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="links">
193+
194+
[sample-skewness]: https://en.wikipedia.org/wiki/Skewness
195+
196+
[@joanes:1998]: http://onlinelibrary.wiley.com/doi/10.1111/1467-9884.00122/
197+
198+
<!-- <related-links> -->
199+
200+
[@stdlib/stats/incr/kurtosis]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/kurtosis
201+
202+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
203+
204+
[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/stdev
205+
206+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
207+
208+
[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance
209+
210+
<!-- </related-links> -->
211+
212+
</section>
213+
214+
<!-- /.links -->
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
24+
var Float64Array = require( '@stdlib/array/float64' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var pkg = require( './../package.json' ).name;
27+
var incrmskewness = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var f;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
f = incrmskewness( ( i % 5 ) + 1 );
38+
if ( typeof f !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
if ( typeof f !== 'function' ) {
44+
b.fail( 'should return a function' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg+'::accumulator', function benchmark( b ) {
51+
var acc;
52+
var len;
53+
var v;
54+
var i;
55+
var x;
56+
57+
len = 100;
58+
x = new Float64Array( len );
59+
for ( i = 0; i < len; i++ ) {
60+
x[ i ] = uniform( 0.0, 1.0 );
61+
}
62+
63+
acc = incrmskewness( 5 );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = acc( x[ i % len ] );
68+
if ( v !== v ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( v !== v ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});

0 commit comments

Comments
 (0)