Skip to content

Commit a07ba7f

Browse files
committed
nanskewness
1 parent a0bf2f0 commit a07ba7f

File tree

10 files changed

+806
-0
lines changed

10 files changed

+806
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
# incrnanskewness
22+
23+
> Compute a [corrected sample skewness][sample-skewness] incrementally, ignoring `NaN` values.
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/skewness/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/skewness/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/skewness/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 incrnanskewness = require( '@stdlib/stats/incr/nanskewness' );
84+
```
85+
86+
#### incrnanskewness()
87+
88+
Returns an accumulator `function` which incrementally computes a [corrected sample skewness][sample-skewness], ignoring `NaN` values.
89+
90+
```javascript
91+
var accumulator = incrnanskewness();
92+
```
93+
94+
#### accumulator( \[x] )
95+
96+
If provided an input value `x`, the accumulator function returns an updated [corrected sample skewness][sample-skewness]. If not provided an input value `x`, the accumulator function returns the current [corrected sample skewness][sample-skewness].
97+
98+
```javascript
99+
var accumulator = incrnanskewness();
100+
101+
var skewness = accumulator();
102+
// returns null
103+
104+
skewness = accumulator( 2.0 );
105+
// returns null
106+
107+
skewness = accumulator( NaN );
108+
// returns null
109+
110+
skewness = accumulator( -5.0 );
111+
// returns null
112+
113+
skewness = accumulator( -10.0 );
114+
// returns ~0.492
115+
116+
skewness = accumulator();
117+
// returns ~0.492
118+
```
119+
120+
</section>
121+
122+
<!-- /.usage -->
123+
124+
<section class="notes">
125+
126+
## Notes
127+
128+
- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
129+
130+
</section>
131+
132+
<!-- /.notes -->
133+
134+
<section class="examples">
135+
136+
## Examples
137+
138+
<!-- eslint no-undef: "error" -->
139+
140+
```javascript
141+
var randu = require( '@stdlib/random/base/randu' );
142+
var incrnanskewness = require( '@stdlib/stats/incr/nanskewness' );
143+
144+
var accumulator;
145+
var v;
146+
var i;
147+
148+
// Initialize an accumulator:
149+
accumulator = incrnanskewness();
150+
151+
// For each simulated datum, update the corrected sample skewness...
152+
for ( i = 0; i < 100; i++ ) {
153+
if ( randu() < 0.2 ) {
154+
v = NaN;
155+
} else {
156+
v = randu() * 100.0;
157+
}
158+
accumulator( v );
159+
}
160+
console.log( accumulator() );
161+
```
162+
163+
</section>
164+
165+
<!-- /.examples -->
166+
167+
* * *
168+
169+
<section class="references">
170+
171+
## References
172+
173+
- 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].
174+
175+
</section>
176+
177+
<!-- /.references -->
178+
179+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
180+
181+
<section class="related">
182+
183+
* * *
184+
185+
## See Also
186+
187+
- <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>
188+
- <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>
189+
- <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>
190+
- <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>
191+
- <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>
192+
193+
</section>
194+
195+
<!-- /.related -->
196+
197+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="links">
200+
201+
[sample-skewness]: https://en.wikipedia.org/wiki/Skewness
202+
203+
[@joanes:1998]: http://onlinelibrary.wiley.com/doi/10.1111/1467-9884.00122/
204+
205+
<!-- <related-links> -->
206+
207+
[@stdlib/stats/incr/kurtosis]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/kurtosis
208+
209+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
210+
211+
[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/stdev
212+
213+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
214+
215+
[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance
216+
217+
<!-- </related-links> -->
218+
219+
</section>
220+
221+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanskewness = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanskewness();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanskewness();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a corrected
4+
sample skewness, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated corrected
7+
sample skewness. If not provided a value, the accumulator function returns
8+
the current corrected sample skewness.
9+
10+
Returns
11+
-------
12+
acc: Function
13+
Accumulator function.
14+
15+
Examples
16+
--------
17+
> var accumulator = {{alias}}();
18+
> var v = accumulator( 2.0 )
19+
null
20+
> v = accumulator( -5.0 )
21+
null
22+
> v = accumulator( NaN )
23+
null
24+
> v = accumulator( -10.0 )
25+
~0.492
26+
> v = accumulator()
27+
~0.492
28+
29+
See Also
30+
--------
31+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* 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.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
29+
*
30+
* @param x - value
31+
* @returns corrected sample skewness or null
32+
*/
33+
type accumulator = ( x?: number ) => number | null;
34+
35+
/**
36+
* Returns an accumulator function which incrementally computes a corrected sample skewness, ignoring `NaN` values.
37+
*
38+
* ## Notes
39+
*
40+
* - 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.
41+
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
42+
*
43+
* @returns accumulator function
44+
*
45+
* @example
46+
* var accumulator = incrnanskewness();
47+
*
48+
* var skewness = accumulator();
49+
* // returns null
50+
*
51+
* skewness = accumulator( 2.0 );
52+
* // returns null
53+
*
54+
* skewness = accumulator( NaN );
55+
* // returns null
56+
*
57+
* skewness = accumulator( -5.0 );
58+
* // returns null
59+
*
60+
* skewness = accumulator( -10.0 );
61+
* // returns ~0.492
62+
*
63+
* skewness = accumulator();
64+
* // returns ~0.492
65+
*/
66+
declare function incrnanskewness(): accumulator;
67+
68+
69+
// EXPORTS //
70+
71+
export = incrnanskewness;

0 commit comments

Comments
 (0)